简体   繁体   English

1张图中的多个饼图,带有拆分工具提示Highcharts.js

[英]Multiple pie charts in 1 graph with split tooltip Highcharts.js

Currently I'm trying to create a single Highchart graph with 3 pie charts. 目前,我正在尝试使用3个饼图创建一个Highchart图形。 (All charts contain exactly the same amount of datapoints). (所有图表都包含完全相同数量的数据点)。 When I hover over a piece of the pie I want 3 tooltips appear on all 3 pie charts at the same given point. 当我将鼠标悬停在一块饼上时,我希望3个工具提示出现在同一给定点的所有3个饼图上。

I tried using 我尝试使用

{
    tooltip: { split: true} 
} 

but throws a JavaScript error and doesn't seem to work on pie charts. 但会引发JavaScript错误,并且似乎无法在饼图上使用。 I can't seem to be able to get this to work properly. 我似乎无法使它正常工作。 I also tried redefining Highcharts.Tooltip.prototype.renderSplit but couldn't get it to work either. 我也尝试过重新定义Highcharts.Tooltip.prototype.renderSplit但也无法使其正常工作。

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

You can create multiple tooltips on chart load event and manage them on point mouse over event. 您可以在图表加载事件上创建多个工具提示,并在鼠标悬停事件上管理它们。

  1. Create for each series one tooltip 为每个系列创建一个工具提示

     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. On mouse over call tooltip.refresh(point) where point is the point which should be hovered. 在鼠标悬停时,调用tooltip.refresh(point) ,其中point是应悬停的点。 I call for the points with the same names. 我要求使用相同的名称。

     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. Wrap tooltip's hide method so all tooltips will be hidden simultaneously. 包装工具提示的hide方法,以便同时隐藏所有工具提示。

     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) }) } }) 

live example: https://jsfiddle.net/8w30m3o8/ 实时示例: https//jsfiddle.net/8w30m3o8/

If you do not want tooltips to swap between series, you should assign a tooltip to a specific series, eg the main tooltip should be refreshed only with the points from the first series, the second tooltip with the points from the second series, and so on. 如果您不希望工具提示在系列之间交换,则应将工具提示分配给特定的系列,例如,主工具提示仅应使用第一个系列的点刷新,第二个工具提示用第二个系列的点刷新,依此类推上。


Possibly easier solution would be to use 3 charts and synchronise tooltips similarly as it is done in this example: https://www.highcharts.com/demo/synchronized-charts 可能更简单的解决方案是使用3个图表并与本示例中的操作类似地同步工具提示: https : //www.highcharts.com/demo/synchronized-charts

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

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