根据 Highcharts 中悬停的系列突出显示工具提示共享项目
Highlight tooltip shared item depending on hovered serie in Highcharts
我有以下图表:http://jsfiddle.net/5oso60oq/
JS :
$(function () {
$('#container').highcharts({
chart: {
type: 'bar'
},
title: {
text: 'Stacked bar chart'
},
xAxis: {
categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
},
yAxis: {
min: 0,
title: {
text: 'Total fruit consumption'
}
},
legend: {
reversed: true
},
tooltip: {
shared: true
},
plotOptions: {
series: {
stacking: 'normal'
}
},
series: [{
name: 'John',
data: [5, 3, 4, 7, 2]
}, {
name: 'Jane',
data: [2, 2, 3, 2, 1]
}, {
name: 'Joe',
data: [3, 4, 4, 2, 5]
}]
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="min-width: 310px; max-width: 800px; height: 400px; margin: 0 auto"></div>
我想做的是突出显示(或更改样式)工具提示的系列元素,具体取决于图表上悬停的系列。
例如,如果我将鼠标悬停在 'Apples' 图表系列的绿色区域,我想在工具提示中突出显示 'Joe',依此类推...
我发现了一些关于悬停事件的提示,但渲染效果不佳。
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
}
试试这个
您应该禁用共享参数并使用格式化程序,它允许迭代所有系列和点。添加检查当前 x 值和打印点的条件。
tooltip: {
shared: false,
formatter: function() {
var series = this.series.chart.series,
each = Highcharts.each,
x = this.point.x,
txt = '<span style="font-size: 10px">' + this.key + '</span><br/>';
each(series, function(serie,i) {
each(serie.data, function(data, j){
if(data.x === x) {
txt += '<span style="color:' + data.color + '">\u25CF</span> ' + serie.name + ': <b>' + data.y + '</b><br/>';
}
});
});
return txt;
}
},
我一直在寻找同样的东西,并找到了我认为更好的解决方案
formatter(tooltip) {
// sort the values
// eslint-disable-next-line no-nested-ternary
this.points.sort((a, b) => ((a.y < b.y) ? -1 : (a.y > b.y) ? 1 : 0))
.reverse()
tooltip.chart.series.forEach((item) => {
if (!item.state) item.setState('inactive')
})
return tooltip.defaultFormatter.call(this, tooltip)
},
我有以下图表:http://jsfiddle.net/5oso60oq/
JS :
$(function () {
$('#container').highcharts({
chart: {
type: 'bar'
},
title: {
text: 'Stacked bar chart'
},
xAxis: {
categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
},
yAxis: {
min: 0,
title: {
text: 'Total fruit consumption'
}
},
legend: {
reversed: true
},
tooltip: {
shared: true
},
plotOptions: {
series: {
stacking: 'normal'
}
},
series: [{
name: 'John',
data: [5, 3, 4, 7, 2]
}, {
name: 'Jane',
data: [2, 2, 3, 2, 1]
}, {
name: 'Joe',
data: [3, 4, 4, 2, 5]
}]
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="min-width: 310px; max-width: 800px; height: 400px; margin: 0 auto"></div>
我想做的是突出显示(或更改样式)工具提示的系列元素,具体取决于图表上悬停的系列。
例如,如果我将鼠标悬停在 'Apples' 图表系列的绿色区域,我想在工具提示中突出显示 'Joe',依此类推...
我发现了一些关于悬停事件的提示,但渲染效果不佳。
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
}
试试这个
您应该禁用共享参数并使用格式化程序,它允许迭代所有系列和点。添加检查当前 x 值和打印点的条件。
tooltip: {
shared: false,
formatter: function() {
var series = this.series.chart.series,
each = Highcharts.each,
x = this.point.x,
txt = '<span style="font-size: 10px">' + this.key + '</span><br/>';
each(series, function(serie,i) {
each(serie.data, function(data, j){
if(data.x === x) {
txt += '<span style="color:' + data.color + '">\u25CF</span> ' + serie.name + ': <b>' + data.y + '</b><br/>';
}
});
});
return txt;
}
},
我一直在寻找同样的东西,并找到了我认为更好的解决方案
formatter(tooltip) {
// sort the values
// eslint-disable-next-line no-nested-ternary
this.points.sort((a, b) => ((a.y < b.y) ? -1 : (a.y > b.y) ? 1 : 0))
.reverse()
tooltip.chart.series.forEach((item) => {
if (!item.state) item.setState('inactive')
})
return tooltip.defaultFormatter.call(this, tooltip)
},