图表原型方法
使用此方法来销毁我们创建的任何图表实例。这将会清除 Chart.js 中对 chart 对象的所有引用,以及由 Chart.js 附加的任何关联的事件监听。该方法必须在 canvas 重新用于新图表之前调用。
// Destroys a specific chart instance
myLineChart.destroy();
.update(config)
触发图表的更新。这可以在更新数据对象之后安全地调用。这将更新所有的比例,图例,然后重新渲染图表。
// duration is the time for the animation of the redraw in milliseconds
// lazy is a boolean. if true, the animation can be interrupted by other animations
myLineChart.update(); // Calling update now animates the position of March from 90 to 50.
可以为config
对象提供更新过程的附加配置。 当在事件处理程序中手动调用update
并且需要一些不同的动画时将非常有用。
支持以下属性:
- duration (number): 动画重绘的时间,以毫秒为单位
- lazy (boolean): 如果为true,则动画可以被其他动画中断
- easing (string): 动画的easing函数。 相关值请参阅。
例子:
myChart.update({
duration: 800,
easing: 'easeOutBounce'
})
参见更新 Charts 获取更多详细信息
.reset()
将图表重置为初始动画之前的状态。然后可以使用update
来触发新的动画。
myLineChart.reset();
.render(config)
See for more details on the config object.
使用它来停止任何当前的动画循环。 这将在当前的动画帧中暂停图表。 调用.render()
重新启用动画。
// Stops the charts animation loop at its current frame
myLineChart.stop();
// => returns 'this' for chainability
.resize()
使用该方法来手动调整画布元素的大小。 每次调整 canvas 容器的大小时都会执行此操作,但如果要更改 canvas 节点容器元素的大小,则可以手动调用此方法。
// Resizes & redraws to fill its container element
myLineChart.resize();
// => returns 'this' for chainability
.clear()
将清除图表 canvas。 在动画帧之间广泛使用。
// Will clear the canvas that myLineChart is drawn on
myLineChart.clear();
.toBase64Image()
返回当前状态的图表的基本 64 位编码字符串
myLineChart.toBase64Image();
// => returns png data url of the image on the canvas
返回该图表的从legendCallback
选项中生成的图例 HTML 字符串。
.getElementAtEvent(e)
myLineChart.getElementAtEvent(e);
To get an item that was clicked on, getElementAtEvent
can be used.
function clickHandler(evt) {
var firstPoint = myChart.getElementAtEvent(evt)[0];
if (firstPoint) {
var label = myChart.data.labels[firstPoint._index];
var value = myChart.data.datasets[firstPoint._datasetIndex].data[firstPoint._index];
}
}
.getElementsAtEvent(e)
查找事件点下的元素,然后返回相同数据索引处的所有元素。 在内部用于“标签”模式突出显示。在 Chart 实例上调用getElementsAtEvent(event )
传递一个事件的参数或 jQuery 事件,将返回那个事件的相同位置上的点元素。
canvas.onclick = function(evt) {
var activePoints = myLineChart.getElementsAtEvent(evt);
// => activePoints is an array of points on the canvas that are at the same position as the click event.
};
这个功能对于实现基于 DOM 的工具提示,或者在应用程序中触发自定义行为是有用的。
.getDatasetAtEvent(e)
查找事件点下的元素,然后返回该数据集中的所有元素。 在内部用于'dataset'模式突出显示。
myLineChart.getDatasetAtEvent(e);
查找与当前索引相匹配的数据集并返回该元数据。 此返回的数据具有用于构建图表的所有元数据。
元数据的属性将包含关于每个点,矩形等的信息,具体取决于图表类型。中提供了大量的用法示例。