Javascript Chart.js-绘制水平线
我想在图表中画一条水平线。但我做不到 我读过这个问题--但我无法转换绘制水平线而不是垂直线的代码Javascript Chart.js-绘制水平线,javascript,chart.js,Javascript,Chart.js,我想在图表中画一条水平线。但我做不到 我读过这个问题--但我无法转换绘制水平线而不是垂直线的代码 我希望您能帮助我(特别是potatopeelings:)。这里是绘制水平线的JavaScript代码 var data = { labels: ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"], datasets: [{ data: [12, 3,
我希望您能帮助我(特别是potatopeelings:)。这里是绘制水平线的JavaScript代码
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",
initialize: function () {
Chart.types.Line.prototype.initialize.apply(this, arguments);
},
draw: function () {
Chart.types.Line.prototype.draw.apply(this, arguments);
var point = this.datasets[0].points[this.options.lineAtIndex]
var scale = this.scale
console.log(this);
// draw line
this.chart.ctx.beginPath();
this.chart.ctx.moveTo(scale.startPoint+12, point.y);
this.chart.ctx.strokeStyle = '#ff0000';
this.chart.ctx.lineTo(this.chart.width, point.y);
this.chart.ctx.stroke();
// write TODAY
this.chart.ctx.textAlign = 'center';
this.chart.ctx.fillText("TODAY", scale.startPoint + 35, point.y+10);
}
});
new Chart(ctx).LineWithLine(data, {
datasetFill : false,
lineAtIndex: 2
});
这是基于,它可能并不完美,但您应该能够调整它以满足您的需要。如果您需要许多具有特定Y值的行,请尝试此代码
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 lines = this.options.limitLines;
for (var i = lines.length; --i >= 0;) {
var xStart = Math.round(this.scale.xScalePaddingLeft);
var linePositionY = this.scale.calculateY(lines[i].value);
this.chart.ctx.fillStyle = lines[i].color ? lines[i].color : this.scale.textColor;
this.chart.ctx.font = this.scale.font;
this.chart.ctx.textAlign = "left";
this.chart.ctx.textBaseline = "top";
if (this.scale.showLabels && lines[i].label) {
this.chart.ctx.fillText(lines[i].label, xStart + 5, linePositionY);
}
this.chart.ctx.lineWidth = this.scale.gridLineWidth;
this.chart.ctx.strokeStyle = lines[i].color ? lines[i].color : this.scale.gridLineColor;
if (this.scale.showHorizontalLines) {
this.chart.ctx.beginPath();
this.chart.ctx.moveTo(xStart, linePositionY);
this.chart.ctx.lineTo(this.scale.width, linePositionY);
this.chart.ctx.stroke();
this.chart.ctx.closePath();
}
this.chart.ctx.lineWidth = this.lineWidth;
this.chart.ctx.strokeStyle = this.lineColor;
this.chart.ctx.beginPath();
this.chart.ctx.moveTo(xStart - 5, linePositionY);
this.chart.ctx.lineTo(xStart, linePositionY);
this.chart.ctx.stroke();
this.chart.ctx.closePath();
}
}
});
new Chart(ctx).LineWithLine(data, {
datasetFill : false,
limitLines: [
{
label: 'max',
value: 17,
color: 'rgba(255, 0, 0, .8)'
},
{
label: 'min',
value: 1
},
{
value: 7,
color: 'rgba(0, 255, 255, .8)'
}
],
});
在没有任何附加代码的情况下,我通过添加一个新条目作为数据集,并使用以下选项来画一条线: 只需替换为数据集的大小以及该行表示的值
{
data: Array.apply(null, new Array(<length>)).map(Number.prototype.valueOf, <value>),
fill: false,
radius: 0,
backgroundColor: "rgba(0,0,0,0.1)"
}
{
数据:Array.apply(null,new Array()).map(Number.prototype.valueOf,),
填充:假,
半径:0,
背景颜色:“rgba(0,0,0,0.1)”
}
半径为0将以某种方式隐藏点,而fill:false将使其显示为一条线。根据dFelinger消息,我制作了一种新的字符类型,它在折线图上绘制了一条平均线。代码如下,只需复制并调用一个名为“lineWithAverage”的新类型的新图表,它仅适用于ChartJS2
Chart.controllers.lineWithAverage = Chart.controllers.line.extend({
initialize: function () {
Chart.controllers.line.prototype.initialize.apply(this, arguments);
},
draw: function () {
Chart.controllers.line.prototype.draw.apply(this, arguments);
var scale = this.scale
var sum = 0;
this.getDataset().data.forEach(function(value) {
sum = sum + value;
});
var average = sum / this.getDataset().data.length;
var averageCoord = this.calculatePointY(average);
// draw line
this.chart.chart.canvas.ctx = this.chart.chart.canvas.getContext('2d');
this.chart.chart.canvas.ctx.beginPath();
this.chart.chart.canvas.ctx.moveTo(0, averageCoord);
this.chart.chart.canvas.ctx.strokeStyle = '#979797';
this.chart.chart.canvas.ctx.lineTo(this.chart.chart.width, averageCoord);
this.chart.chart.canvas.ctx.stroke();
}
});
谢谢一切正常。但是,您知道如何将代码更改为设置值,在该值处显示线,而不仅仅是点吗?当我使用您的
数组时,我在非对象上调用了未捕获的TypeError:Object.defineProperty?他们有错误的版本或可怕的记录。真不敢相信它如此复杂,你将在这里找到你的代码和JS提琴