chart.js load totally new data(chart.js 加载全新数据)
问题描述
chart.js
的 API 允许编辑加载到其中的数据集,例如:
The API for chart.js
allows one to edit points of the datasets loaded into it, for example:
在 Chart 实例上调用 update() 将重新渲染图表任何更新的值,允许您编辑多个值现有点,然后在一个动画渲染循环中渲染它们.
.update( )
Calling update() on your Chart instance will re-render the chart with any updated values, allowing you to edit the value of multiple existing points, then render those in one animated render loop.
在您的 Chart 实例上调用 addData(valuesArray, label) 传递一个每个数据集的值数组,以及这些点的标签.
Calling addData(valuesArray, label) on your Chart instance passing an array of values for each dataset, along with a label for those points.
在您的 Chart 实例上调用 removeData() 将删除第一个图表上所有数据集的值.
Calling removeData() on your Chart instance will remove the first value for all datasets on the chart.
所有这些都很棒,但我不知道如何加载一个全新的数据集,消除旧的.文档似乎没有涵盖这一点.
All of these are great, but I cannot figure out how to load an entirely new dataset in, wiping out the old. The documentation does not seem to cover this.
推荐答案
我遇到了很大的问题
首先我尝试了 .clear()
然后我尝试了 .destroy()
并尝试将我的图表引用设置为 null
First I tried .clear()
then I tried .destroy()
and I tried setting my chart reference to null
最终解决了我的问题:删除 <canvas>
元素,然后将新的 <canvas>
重新附加到父容器
What finally fixed the issue for me: deleting the <canvas>
element and then reappending a new <canvas>
to the parent container
有一百万种方法可以做到这一点:
There's a million ways to do this:
var resetCanvas = function () {
$('#results-graph').remove(); // this is my <canvas> element
$('#graph-container').append('<canvas id="results-graph"><canvas>');
canvas = document.querySelector('#results-graph'); // why use jQuery?
ctx = canvas.getContext('2d');
ctx.canvas.width = $('#graph').width(); // resize to parent width
ctx.canvas.height = $('#graph').height(); // resize to parent height
var x = canvas.width/2;
var y = canvas.height/2;
ctx.font = '10pt Verdana';
ctx.textAlign = 'center';
ctx.fillText('This text is centered on the canvas', x, y);
};
这篇关于chart.js 加载全新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:chart.js 加载全新数据


基础教程推荐
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01