gpt4 book ai didi

charts - 使用自定义线扩展折线图

转载 作者:行者123 更新时间:2023-12-04 21:29:41 25 4
gpt4 key购买 nike

我正在尝试使用以下代码扩展 Chart.js 中的折线图:

var shotsData = {
labels: ["Shot 1", "Shot 2", "Shot 3", "Shot 4", "Shot 5"],
datasets: [{ data: [5, 7, 1, 4, 9] }]
};

var ctx = document.getElementById("shots").getContext("2d");

Chart.types.Line.extend({
name: "LineAlt",
initialize: function () {
this.chart.ctx.beginPath();
this.chart.ctx.moveTo(0, 15);
this.chart.ctx.lineTo(159, 274);
this.chart.ctx.stroke();

Chart.types.Line.prototype.initialize.apply(this, arguments);
}
});

new Chart(ctx).LineAlt(shotsData);

这绘制了我的图表,但我还想在图表中添加一条自定义线,它写在 initialize 内功能,但这似乎不起作用。当我删除行 Chart.types.Line.prototype.initialize.apply(this, arguments);显示自定义行。

这是一把 fiddle

http://jsfiddle.net/92ANv/5/

最佳答案

您可以覆盖绘图功能以将您的绘图附加到 Canvas 上

draw: function () {
//call the super draw
Chart.types.Line.prototype.draw.apply(this, arguments);

//now your custom line
this.chart.ctx.beginPath();
this.chart.ctx.moveTo(0, 5);
this.chart.ctx.lineTo(100, 100);
this.chart.ctx.stroke();
}

fiddle http://jsfiddle.net/leighking2/5Lgzvu3r

关于charts - 使用自定义线扩展折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25135965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com