我正在HighCharts中的堆叠列图。我需要一个单击传说时,传说默认情况下会灰色,但系列/堆栈不应隐藏并保持颜色相同。
我尝试了此操作,并通过使用此操作来停止隐藏:
events: {
legendItemClick: function () {
return false; // <== returning false will cancel the default action
}
}
,但我无法灰色或禁用点击传奇。
这是小提琴链接
您需要超过legenditemclick。只需根据您的要求添加颜色和切换即可。
请参阅此处的JSFIDDLE
一个带有切换功能的一个在这里
$(function () {
var selected = null;
$('#container').highcharts({
chart: {
type: 'column'
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
plotOptions: {
series: {
events: {
legendItemClick: function(event) {
event.preventDefault()
var series = $('#container').highcharts().series[0];
series.color = "#fbfbfbf"; $('#container').highcharts().legend.colorizeItem(series, series.visible);
$.each(series.data, function(i, point) {
point.graphic.attr({
fill: '#fbfbfb'
});
});
series.redraw();
this.update({ color: '#999' }, true, false);
selected = this;
},
mouseOver: function() {
if(this != selected)
this.update({ color: '#999' }, true,false);
}
}
}
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});
});
覆盖Highcharts.Legend.prototype.setItemEvents
核心功能似乎是一个优雅的解决方案。
我删除了负责处理mouseover
和mouseout
事件的代码。然后,我为onclick
事件实施了自定义行为:
(function(H) {
Highcharts.Legend.prototype.setItemEvents = function(item, legendItem, useHTML) {
// Set the events on the item group, or in case of useHTML, the item itself (#1249)
(useHTML ? legendItem : item.legendGroup).on('click', function(event) {
if (item.clicked) {
legendItem.css({
fill: 'black'
});
} else {
legendItem.css({
fill: 'gray'
});
}
item.clicked = !item.clicked;
});
};
})(Highcharts);
实时演示:http://jsfiddle.net/kkulig/kje28s4v/
docs关于覆盖核心函数的参考: https://www.highcharts.com/docs/extending-highcharts/externding-highcharts/extding-highcharts