gpt4 book ai didi

javascript - r- 如何一直显示 highcharter 对象上的标签

转载 作者:行者123 更新时间:2023-11-30 08:30:27 24 4
gpt4 key购买 nike

我使用了一个包含饼图和条形图的 Highcharts 对象,标签(百分比)仅显示鼠标在其上移动。见下面的代码:

library(highcharter)
data("favorite_bars")
data("favorite_pies")

highchart() %>%
hc_title(text = "This is a bar graph describing my favorite pies
including a pie chart describing my favorite bars") %>%
hc_subtitle(text = "In percentage of tastiness and awesomeness") %>%
hc_add_series_labels_values(favorite_pies$pie, favorite_pies$percent, name = "Pie",
colorByPoint = TRUE, type = "column") %>%
hc_add_series_labels_values(favorite_bars$bar, favorite_bars$percent,
colors = substr(terrain.colors(5), 0 , 7), type = "pie",
name = "Bar", colorByPoint = TRUE, center = c('35%', '10%'),
size = 100, dataLabels = list(enabled = FALSE)) %>%
hc_yAxis(title = list(text = "percentage of tastiness"),
labels = list(format = "{value}%"), max = 100) %>%
hc_xAxis(categories = favorite_pies$pie) %>%
hc_legend(enabled = FALSE) %>%
hc_tooltip(pointFormat = "{point.y}%") %>%
hc_credits(enabled = TRUE, text = "Source: HIMYM",
href = "https://www.youtube.com/watch?v=f_J8QU1m0Ng",
style = list(fontSize = "12px"))

而且我希望一直显示百分比而不是鼠标在上面移动。查看我从 highcharts 官方网站获得的图片: enter image description here enter image description here

最佳答案

可以在highcharts website上看到用于创建您通过单击查看选项粘贴的饼图的选项。

您将看到以下代码:

plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
}
}
}
},

这意味着您必须启用 dataLabels 选项并提供格式化程序。在您提供的代码中,您实际上可以在第二个 hc_add_series_labels_values 调用中看到 dataLabels enabled 选项设置为 FALSE:

hc_add_series_labels_values(favorite_bars$bar, favorite_bars$percent,
colors = substr(terrain.colors(5), 0 , 7), type = "pie",
name = "Bar", colorByPoint = TRUE, center = c('35%', '10%'),
size = 100, dataLabels = list(enabled = FALSE))

如果将其更改为 TRUE,您将看到标签。如果您还希望显示数字,则必须将函数传递给列表中的 format 元素,如下所示:

hc_add_series_labels_values(favorite_bars$bar, favorite_bars$percent,
colors = substr(terrain.colors(5), 0 , 7), type = "pie",
name = "Bar", colorByPoint = TRUE, center = c('35%', '10%'),
size = 100, dataLabels = list(enabled = TRUE,
format = '{point.name}: {point.percentage:.1f} %'))

然后您将不得不使用 dataLabels 的不同选项来根据需要显示它们,请参阅 Highcharts API以条形图类型为例。

关于javascript - r- 如何一直显示 highcharter 对象上的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38865890/

24 4 0