You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Highcharter人口金字塔:如何将Tooltip值改为绝对值

问题:在Highcharter中如何将Tooltip值改为绝对值?

我用Highcharter制作人口金字塔,通过将男性数值设为负值实现金字塔展示效果,目前已成功将x轴标签改为绝对值,但Tooltip仍显示负值,求解决办法。

我的代码:

PyramidPlot2 <- highchart() %>%
  hc_add_series(PopEDBinded, type = "bar", hcaes(x = c(PyramidLevels,PyramidLevels), y = value, group = Gender), color = c("#2f7ed8","#FF0000"), 
                showInLegend = F) %>% 
  hc_plotOptions(bar = list(stacking = "normal")) %>% 
  # format the labels on the x-axis (y-axis per HC)
  hc_yAxis(labels = list(formatter = htmlwidgets::JS( 
    "function() {
        return Math.abs(this.value); /* all labels to absolute values */
    }"
  ))) %>% 
  hc_xAxis(categories = PyramidLevels, reversed = F)%>%hc_title(text = "Males", align = "center", x = -115, y = 20, margin = 0,
           style = list(fontSize = "12px", color = "#FF0000")) %>%
  hc_subtitle(text = "Females", align = "center", y = 20, margin = 0,
              x = 250, style = list(fontSize = "12px", color = "#2f7ed8"))

人口金字塔效果:
人口金字塔效果

相关数据结构:

structure(list(Sex = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = c("Males", 
"Females", "Both Sexes"), class = "factor"), Age = structure(c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 16L, 17L, 18L), levels = c("Age 0-4", "Age 5-9", 
"Age 10-14", "Age 15-19", "Age 20-24", "Age 25-29", "Age 30-34", 
"Age 35-39", "Age 40-44", "Age 45-49", "Age 50-54", "Age 55-59", 
"Age 60-64", "Age 65-69", "Age 70-74", "Age 75-79", "Age 80-84", 
"Age 85 and over"), class = "factor"), CSO.Electoral.Divisions.2022 = c("Doonloughan, Galway County", 
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County", 
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County", 
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County", 
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County", 
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County", 
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County", 
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County", 
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County", 
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County", 
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County", 
"Doonloughan, Galway County", "Doonloughan, Galway County", "Doonloughan, Galway County", 
"Doonloughan, Galway County", "Doonloughan, Galway County"), 
    Census.Year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = "2022", class = "factor"), 
    Statistic = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = "Population", class = "factor"), 
    value = c(1, 4, 2, 4, 4, 2, 2, 4, 2, 4, 3, 2, 3, 4, 3, 2, 
    1, 1, -3, -3, -3, -3, -1, -1, -4, -5, -2, -2, -2, -1, -6, 
    -6, 0, -4, -2, -1), Percentage = c(2.1, 8.3, 4.2, 8.3, 8.3, 
    4.2, 4.2, 8.3, 4.2, 8.3, 6.2, 4.2, 6.2, 8.3, 6.2, 4.2, 2.1, 
    2.1, 6.1, 6.1, 6.1, 6.1, 2, 2, 8.2, 10.2, 4.1, 4.1, 4.1, 
    2, 12.2, 12.2, 0, 8.2, 4.1, 2), Gender = c("Female", "Female", 
    "Female", "Female", "Female", "Female", "Female", "Female", 
    "Female", "Female", "Female", "Female", "Female", "Female", 
    "Female", "Female", "Female", "Female", "Male", "Male", "Male", 
    "Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male", 
    "Male", "Male", "Male", "Male", "Male", "Male", "Male")), row.names = c(NA, 
-36L), class = "data.frame")

解决方案

给图表添加hc_tooltip配置,通过JavaScript格式化函数将Tooltip中的y值转为绝对值:

修改后的完整代码:

PyramidPlot2 <- highchart() %>%
  hc_add_series(PopEDBinded, type = "bar", hcaes(x = c(PyramidLevels,PyramidLevels), y = value, group = Gender), color = c("#2f7ed8","#FF0000"), 
                showInLegend = F) %>% 
  hc_plotOptions(bar = list(stacking = "normal")) %>% 
  # 格式化y轴标签为绝对值
  hc_yAxis(labels = list(formatter = htmlwidgets::JS( 
    "function() {
        return Math.abs(this.value);
    }"
  ))) %>% 
  # 格式化Tooltip显示绝对值
  hc_tooltip(formatter = htmlwidgets::JS(
    "function() {
        return '<b>' + this.series.name + '</b><br/>' +
               this.x + ': ' + Math.abs(this.y);
    }"
  )) %>%
  hc_xAxis(categories = PyramidLevels, reversed = F) %>%
  hc_title(text = "Males", align = "center", x = -115, y = 20, margin = 0,
           style = list(fontSize = "12px", color = "#FF0000")) %>%
  hc_subtitle(text = "Females", align = "center", y = 20, margin = 0,
              x = 250, style = list(fontSize = "12px", color = "#2f7ed8"))

说明

  • hc_tooltip中的formatter函数自定义了Tooltip的内容,用Math.abs(this.y)把原始的负值转为绝对值展示
  • 保留了性别和年龄段的信息,同时修正了数值显示问题,和轴标签的展示逻辑保持一致

内容的提问来源于stack exchange,提问作者osckt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 15:49:52