javascript - 带有分割工具提示的 1 个图表中的多个饼图 Highcharts.js

标签 javascript charts highcharts tooltip pie-chart

目前我正在尝试创建一个包含 3 个饼图的 Highchart 图表。 (所有图表包含完全相同数量的数据点)。当我将鼠标悬停在饼图的一 block 上时,我希望在所有 3 个饼图的同一给定点上显示 3 个工具提示。

我尝试使用

{
    tooltip: { split: true} 
} 

但抛出 JavaScript 错误并且似乎不适用于饼图。我似乎无法让它正常工作。我还尝试重新定义 Highcharts.Tooltip.prototype.renderSplit 但也无法让它工作。

我有以下内容: https://jsfiddle.net/Visualq/4o1uyazr/13/

最佳答案

您可以在图表加载事件上创建多个工具提示,并在鼠标悬停事件上管理它们。

  1. 为每个系列创建一个工具提示

    Highcharts.chart('container', {
      chart: {
        type: 'pie',
        events: {
          load() {
            this.tooltips = this.series.slice(1).map(
            series => new Highcharts.Tooltip(
              this,
              Highcharts.merge(this.options.tooltip)
            )
          )
        }
      }
    },
    
  2. 鼠标悬停时调用 tooltip.refresh(point),其中 point 是应悬停的点。我要求具有相同名称的点。

    plotOptions: {
      series: {
        point: {
          events: {
            mouseOver(e) {
              const otherSeries = this.series.chart.series.filter(
                series => series !== this.series
              )
    
              const otherPoints = otherSeries.map(
                series => series.points.find(
                  point => point.name === this.name
                )
              )
    
              this.series.chart.tooltips.forEach((tooltip, i) => {
                tooltip.refresh(otherPoints[i])
              })
            }
          }
        }
      }
    },
    
  3. 封装工具提示的隐藏方法,以便所有工具提示将同时隐藏。

    Highcharts.wrap(Highcharts.Tooltip.prototype, 'hide', function(p, delay) {
      if (this === this.chart.tooltip) {
        p.call(this, delay)
    
        this.chart.tooltips.forEach(tooltip => {
          p.call(tooltip, delay)
        })
      }
    })
    

实例:https://jsfiddle.net/8w30m3o8/

如果您不希望工具提示在系列之间交换,则应将工具提示分配给特定系列,例如主工具提示应仅使用第一个系列中的点刷新,第二个工具提示应使用第二个系列中的点刷新,依此类推。

<小时/>

可能更简单的解决方案是使用 3 个图表并同步工具提示,与本示例中的操作类似:https://www.highcharts.com/demo/synchronized-charts

关于javascript - 带有分割工具提示的 1 个图表中的多个饼图 Highcharts.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50623174/

相关文章:

excel - Excel 图表图例中的新行?

具有可变线宽的 Highcharts 散点图

javascript - HighCharts 单击时将 div 添加到系列

javascript - 在Highchart中,为什么饼图的钻取数据颜色不同?

屏幕锁定时javascript setInterval暂停(IOS)

javascript - 如何将隐藏信息发送到打印机进行打印

javascript - Highcharts 两个日期比较

javascript - Vuex 状态不会被通用函数改变,而专用函数会改变它

android - TeeChart(JavaAndroid 2012)教程示例导致UnsupportedOperationException

java - 如何结合散点图和折线图来显示回归线? JavaFX