因此,在查看Chart.js的一些代码示例时,我遇到了以下JSFiddle(http://jsfiddle.net/dbyze2ga/14/
)。
当我把它处理到我的IDE(bracket.io)时,它才起作用,直到我意识到这个jsfiddle使用chart.js 1.x.x.
使用v2.0扩展图表的当前语法是什么?我试过检查文档;但它真的让我更加困惑。没有帮助,至少在Brackets的情况下,它不会显示图表的任何真正有用的点完成。
有问题的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
});
答案 0 :(得分:1)
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>