【问题标题】:Multiple pie charts in 1 graph with split tooltip Highcharts.js1 个图表中的多个饼图,带有拆分工具提示 Highcharts.js
【发布时间】:2018-05-31 11:24:11
【问题描述】:

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

我尝试过使用

{
    tooltip: { split: true} 
} 

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

我有以下几点: https://jsfiddle.net/Visualq/4o1uyazr/13/

【问题讨论】:

  • 此答案告诉您如何同步多个图表的工具提示:stackoverflow.com/questions/31166038/… 取自 highcharts 页面上的此资源:highcharts.com/blog/snippets/synchronisation-of-multiple-charts
  • @ewolden 感谢您的评论,但是饼图无法按照问题中描述的方式同步,因为searchPoint 不适用于饼图(我找不到这个说法,但我试过这个)。拆分应该完全符合我的要求,但由于某种原因它会破坏工具提示。
  • 我的错,我做了一些调查,发现它失败的地方,似乎是一个错误,其中拆分工具提示需要一个带有 x 和 y 值的系列。而馅饼并不真正属于这一类。这里我在错误上设置了一个断点:i.imgur.com/4xXzG6K.png 设置anchorX 失败,因为f 未定义。

标签: javascript charts highcharts tooltip pie-chart


【解决方案1】:

您可以在图表加载事件中创建多个工具提示,并在鼠标悬停事件时对其进行管理。

  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

【讨论】:

  • 这个答案对我的情况来说太棒了!感谢您的回答。不过,我确实想补充一点,此方法不处理悬停状态。如果您也想添加它,您可以在执行 tooltip.refresh 的循环中执行 otherPoints[i].setState('hover');
猜你喜欢
  • 2016-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-26
相关资源
最近更新 更多