react-highcharts 中图表的共享图例?
Shared legend of charts in react-highcharts?
如何使用 react-highcharts 拥有两个具有共享图例(控制两个图表)的图表?
此 jsFiddle 中提供了使用 jQuery 的解决方案,但我无法弄清楚如何在 React 中执行此操作(并且让组件的选项引用其他组件)。
Jquery:
$(function () {
$('#container1').highcharts({
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
legend: {
enabled: false
},
series: [{
id: 'someId',
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]
}]
});
$('#container2').highcharts({
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
plotOptions: {
series: {
events: {
legendItemClick: function (event) {
var XYZ = $('#container1').highcharts(),
series = XYZ.get(this.options.id); //get corresponding series
if (series) {
if (this.visible) {
series.hide();
} else {
series.show();
}
}
}
}
}
},
series: [{
id: 'someId',
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]
}]
});
});
(有效)
感谢指点
您可以在 React 中以类似的方式实现想要的结果。您可以在下面找到一个使用 highcharts-react-official
包装器的工作示例:
plotOptions: {
series: {
events: {
legendItemClick: (function(component) {
return function() {
const chart = component.chart1.current.chart;
const series = chart.get(this.options.id);
if (series) {
if (this.visible) {
series.hide();
} else {
series.show();
}
}
}
})(this)
}
}
}
现场演示:https://codesandbox.io/s/oo0q4832jy
文档:https://github.com/highcharts/highcharts-react/blob/master/README.md
如何使用 react-highcharts 拥有两个具有共享图例(控制两个图表)的图表?
此 jsFiddle 中提供了使用 jQuery 的解决方案,但我无法弄清楚如何在 React 中执行此操作(并且让组件的选项引用其他组件)。
Jquery:
$(function () {
$('#container1').highcharts({
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
legend: {
enabled: false
},
series: [{
id: 'someId',
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]
}]
});
$('#container2').highcharts({
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
plotOptions: {
series: {
events: {
legendItemClick: function (event) {
var XYZ = $('#container1').highcharts(),
series = XYZ.get(this.options.id); //get corresponding series
if (series) {
if (this.visible) {
series.hide();
} else {
series.show();
}
}
}
}
}
},
series: [{
id: 'someId',
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]
}]
});
});
(有效)
感谢指点
您可以在 React 中以类似的方式实现想要的结果。您可以在下面找到一个使用 highcharts-react-official
包装器的工作示例:
plotOptions: {
series: {
events: {
legendItemClick: (function(component) {
return function() {
const chart = component.chart1.current.chart;
const series = chart.get(this.options.id);
if (series) {
if (this.visible) {
series.hide();
} else {
series.show();
}
}
}
})(this)
}
}
}
现场演示:https://codesandbox.io/s/oo0q4832jy
文档:https://github.com/highcharts/highcharts-react/blob/master/README.md