[英]Chart.js - tooltips issue with multiple charts
我使用Chart.js(在最新版本2.7中),并且如果我的页面上有多个图表,则工具提示有问题。
在这个小提琴上: https : //jsfiddle.net/b4up8kw2/ ,我有2个图表,但是如果您看一下,第一个图表的工具提示(左侧)显示了第二个图表的值。
我使用以下代码来更新图表的数据集:
var configs = [];
var charts = [];
configs['chart1'] = config;
configs['chart2'] = config;
// update chart - randomScalingFactor() return random integer
function updateChart(chart) {
// set new values
configs[chart].data.datasets.forEach(function(dataset) {
dataset.data = [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
});
charts[chart].update();
}
$(function() {
charts['chart1'] = new Chart($('#canvas-1')[0].getContext('2d'), configs['chart1']);
charts['chart2'] = new Chart($('#canvas-2')[0].getContext('2d'), configs['chart2']);
for (var key in charts) {
updateChart(key);
}
});
我不明白,因为我使用2个单独的配置变量。 是我的代码还是插件的错误?
我已经通过clone
每个图表的配置解决了您的问题。 当前,您使用共享的一个对象config
渲染图表,并且在渲染第二个图表时更改配置。 因此,您应该为每个图表克隆配置。
这是我的解决方案(从您的jsfiddle分叉并编辑) https://jsfiddle.net/huynhsamha/bdvw1e98/
const clone = (o) => JSON.parse(JSON.stringify(o))
var configs = {
chart1: clone(config),
chart2: clone(config)
};
<script async src="//jsfiddle.net/huynhsamha/bdvw1e98/embed/js,html,css,result/dark/"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.