highcharts r-如何始终显示highcharter对象上的标签

mwkjh3gx  于 2022-11-10  发布在  Highcharts
关注(0)|答案(2)|浏览(145)

我使用了一个包括饼图和条形图的highcharter对象,其标签(百分比)只显示鼠标在其上移动的情况.见下面的代码:

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官方网站:第一次

yvfmudvl

yvfmudvl1#

您可以在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。

2skhul33

2skhul332#

我想我有点晚了,但一个简单的方法是添加到代码与管道

hc_plotOptions(pie = list(dataLabels = list(enabled = TRUE, format = "{point.name}: {point.percentage:.1f} %")))

相关问题