gpt4 book ai didi

javascript - Chart.js 的新扩展语法是什么?

转载 作者:行者123 更新时间:2023-12-01 03:23:07 27 4
gpt4 key购买 nike

因此,在查看 Chart.js 的一些代码示例时,我遇到了以下 JSFiddle ( http://jsfiddle.net/dbyze2ga/14/ )。当我将其复制到我的 IDE (brackets.io) 时,它不起作用,直到我意识到这个 jsfiddle 使用 Chart.js 1.x.x。

使用 v2.0 扩展图表的当前语法是什么?我尝试检查文档;但老实说,这让我更加困惑。没有帮助,至少在括号的情况下,它不会显示图表的任何真正有用的点完成。

有问题的 JS 代码是:

var data = {
labels: ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"],
datasets: [{
data: [12, 3, 2, 1, 8, 8, 2, 2, 3, 5, 7, 1]
}]
};

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

Chart.types.Line.extend({
name: "LineWithLine",
draw: function () {
Chart.types.Line.prototype.draw.apply(this, arguments);

var point = this.datasets[0].points[this.options.lineAtIndex]
var scale = this.scale

// draw line
this.chart.ctx.beginPath();
this.chart.ctx.moveTo(point.x, scale.startPoint + 24);
this.chart.ctx.strokeStyle = '#ff0000';
this.chart.ctx.lineTo(point.x, scale.endPoint);
this.chart.ctx.stroke();

// write TODAY
this.chart.ctx.textAlign = 'center';
this.chart.ctx.fillText("TODAY", point.x, scale.startPoint + 12);
}
});

new Chart(ctx).LineWithLine(data, {
datasetFill : false,
lineAtIndex: 2
});

最佳答案

2.0 的变化在 documentation 中有解释。 :

var myLineExtend = Chart.controllers.line.prototype.draw;

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

var config = {
type: 'line',
data: {
labels: ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"],
datasets: [{
data: [12, 3, 2, 1, 8, 8, 2, 2, 3, 5, 7, 1]
}],
datasetFill : false,
lineAtIndex: 2
}
};

Chart.helpers.extend(Chart.controllers.line.prototype, {
draw: function () {

myLineExtend.apply(this, arguments);

var chart = this.chart;
var ctx = chart.chart.ctx;

var index = chart.config.data.lineAtIndex;
var xaxis = chart.scales['x-axis-0'];
var yaxis = chart.scales['y-axis-0'];

ctx.save();
ctx.beginPath();
ctx.moveTo(xaxis.getPixelForValue(undefined, index), yaxis.top + 24);
ctx.strokeStyle = '#ff0000';
ctx.lineTo(xaxis.getPixelForValue(undefined, index), yaxis.bottom);
ctx.stroke();
ctx.restore();

ctx.textAlign = 'center';
ctx.fillText("TODAY", xaxis.getPixelForValue(undefined, index), yaxis.top + 12);

}
});

new Chart(ctx, config);
<script src="https://github.com/chartjs/Chart.js/releases/download/v2.6.0/Chart.bundle.min.js"></script>
<canvas id="LineWithLine"></canvas>

关于javascript - Chart.js 的新扩展语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45023773/

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