如何动态更改Chart.js中的注释值
我正在我的chart.js项目中使用chartjs插件注释和chartjs插件可拖动。当我更改了一条绘制垂直线的注释的值并尝试更新图表时,该线的位置保持不变。但是我可以看到图表对象中的值正在更新。如果有人仍然通过谷歌搜索登陆这里:这是一个bug。作为一种解决方法,您可以从注释中删除ID,但这可能会破坏鼠标事件。更多信息。调用图表插件注释,通过传递要更改的参数,通过函数更新值。以下是对我有效的方法。我使用一个单独的函数来设置新的值和文本标签,然后调用chart.update()方法。对我来说很好。 请参见更新图表及其选项的说明如何动态更改Chart.js中的注释值,chart.js,Chart.js,我正在我的chart.js项目中使用chartjs插件注释和chartjs插件可拖动。当我更改了一条绘制垂直线的注释的值并尝试更新图表时,该线的位置保持不变。但是我可以看到图表对象中的值正在更新。如果有人仍然通过谷歌搜索登陆这里:这是一个bug。作为一种解决方法,您可以从注释中删除ID,但这可能会破坏鼠标事件。更多信息。调用图表插件注释,通过传递要更改的参数,通过函数更新值。以下是对我有效的方法。我使用一个单独的函数来设置新的值和文本标签,然后调用chart.update()方法。对我来说很好。
options: { // chart.js options object
...
annotation: {
annotations: [
{
type: 'line',
mode: 'horizontal',
scaleID: 'y-percent',
value: 0,
borderColor: 'rgba(238, 68, 226,0.2)',
borderWidth: 2,
borderDash: [5,5],
label: {
enabled: true,
content: 'Your text label ',
position: 'end',
drawTime: 'afterDatasetsDraw',
backgroundColor: 'rgba(238, 68, 226,0.4)'
}
},
]
}, // annotation
} // options
setAnnotationValue(val){
chart.options.annotation.annotations[0].value = val; // set the Value
chart.options.annotation.annotations[0].label.content += " "+ val + "%"; // append the value to the label (if applicable)
chart.update(); // and finally update the chart.
},
找到解决方案..需要销毁图表并创建新图表