简体   繁体   English

使用工具提示的 Highcharter 的 hctreemap2 百分比?

[英]Percentage in hctreemap2 of Highcharter using tooltip?

I made a treemap with Highcharter.我用 Highcharter 制作了一个树状图。 As the map is interactive I want it to show both the total quantity and the percentage that each group represents of the total.由于 map 是交互式的,我希望它同时显示总数量和每组代表总数的百分比。 I have the following 2 codes that I made based on other questions here but both ways it shows the percentage at 0.00%.我根据此处的其他问题制作了以下 2 个代码,但两种方式都显示百分比为 0.00%。 How can I add percentage information to the highcarter treemap tooltip?如何将百分比信息添加到 highcarter 树图工具提示?

#Way 1
hctreemap2(data =Objeto,
           group_vars = c("objeto_contratar"),
           size_var = "total",
           color_var = "total",
           layoutAlgorithm = "squarified",
           levelIsConstant = FALSE) %>% 
  hc_colorAxis(minColor = brewer.pal(7, "Blues")[2],
               maxColor = brewer.pal(7, "Blues")[7]) %>%
               hc_tooltip(pointFormat = "Objeto: {point.name} <br>
                            Contratos: {point.value:,.0f} <br>
                            Porcentaje: {point.percentage:.2f} %")  %>% 
  hc_exporting(enabled = TRUE)

#Way 2
hctreemap2(data =Objeto,
           group_vars = c("objeto_contratar"),
           size_var = "total",
           color_var = "total",
           layoutAlgorithm = "squarified",
           levelIsConstant = FALSE) %>% 
  hc_colorAxis(minColor = brewer.pal(7, "Blues")[2],
               maxColor = brewer.pal(7, "Blues")[7]) %>%
  hc_tooltip(formatter = JS("function(){
                                return  '</b>'  + this.point.name + ': <br> Contratos: ' +this.point.value+' <br> Porcentage: '+Highcharts.numberFormat(this.point.percentage)+'%'
  }"),useHTML = FALSE)  %>% 
  hc_exporting(enabled = TRUE)

Could someone tell me how do I get it to display both data (the amount and percentage it represents) on the same graph when I hover over it?

Someone helped me.有人帮助了我。 This is what he did and it works.这就是他所做的并且有效。 I hope this helps someone else.我希望这对其他人有帮助。

hctreemap2(data =Objeto,
               group_vars = c("objeto_contratar"),
               size_var = "total",
               color_var = "total",
               layoutAlgorithm = "squarified",
               levelIsConstant = FALSE) %>% 
      hc_colorAxis(minColor = pal_azul(7)[1],
                   maxColor = pal_azul(7)[7]) %>% 
      hc_tooltip(formatter = JS('function () {
                            var total = 0;
                            for (var i = 0; i < this.series.data.length; i++)
                            {
                            if (this.series.data[i].node.children.length == 0)
                            total+=this.series.data[i].node.val;
                            }
                            var value;
                            if (this.point.node.children.length == 0)
                            {
                            value = this.point.options.value;
                            }
                            else
                            {
                            value = this.point.node.childrenTotal;
                            }
                            var porcentage = (value / total) *100;
                            return  "</b>"  + this.point.name + ": <br> Contratos: " +this.point.value+" <br> Porcentage: "+Highcharts.numberFormat(porcentage)+"%"
                            }'),useHTML = FALSE)  %>% 
      hc_exporting(enabled = TRUE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM