gpt4 book ai didi

Chart.js 2.0 - 垂直线

转载 作者:行者123 更新时间:2023-12-03 22:52:13 28 4
gpt4 key购买 nike

谁能告诉我如何扩展 Chart.js v2.0。我需要折线图中的垂直线,我想实现类似于 http://jsfiddle.net/dbyze2ga/ 的东西.

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
});

最佳答案

更新 : 见 https://stackoverflow.com/a/45092928/360067使用 Chart Annotations 插件获得更简单、更强大的解决方案。

您可以扩展 line键入以添加对绘制线条的支持

预览

enter image description here

脚本

var originalLineDraw = Chart.controllers.line.prototype.draw;
Chart.helpers.extend(Chart.controllers.line.prototype, {
draw: function() {
originalLineDraw.apply(this, arguments);

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

var index = chart.config.data.lineAtIndex;
if (index) {
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);
ctx.strokeStyle = '#ff0000';
ctx.lineTo(xaxis.getPixelForValue(undefined, index), yaxis.bottom);
ctx.stroke();
ctx.restore();
}
}
});

进而
var config = {
type: 'line',
data: {
labels: ...
datasets: [
...
],
lineAtIndex: 2
}
};

fiddle - http://jsfiddle.net/mn8x6fso/

关于Chart.js 2.0 - 垂直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36329630/

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