score:17
Here is the JavaScript code to draw a horizontal line.
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
});
http://jsfiddle.net/7a4hhzge/455/
This is based off of the code used to draw an arbitrary vertical line, it may not be perfect but you should be able to adjust it to fit your needs.
score:1
The simplest way I've found to create a horizontal line was by just creating a new dataset with every entry set to some constant value (y = 75 for example). Then I set pointRadius
and pointHitRadius
to zero to hide the tooltips and points on the graph.
datasets: [ { data: your_data, ... }, { data: your_data.map(r => 75), pointRadius: 0, pointHitRadius: 0, }, ]
score:3
Based on dFelinger message I made a new char type that draws an average line on the line chart. Here's the code, just copy and call a new chart with the new type called 'lineWithAverage' WORKS ONLY FOR CHARTJS 2
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();
}
});
score:5
If you need many lines with certain Y value, try this code
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)'
}
],
});
score:16
Without any additional code, I managed to draw a line by adding a new entry as a dataset with these options :
Just replace with the size of your dataset, and with the value that the line represents.
{
data: Array.apply(null, new Array(<length>)).map(Number.prototype.valueOf, <value>),
fill: false,
radius: 0,
backgroundColor: "rgba(0,0,0,0.1)"
}
The radius of 0 will somehow hide the dots and the fill: false will make it appear as a line.
Source: stackoverflow.com
Related Query
- Draw horizontal line on chart in chart.js on v2
- How do I draw a vertical line on a horizontal bar chart with ChartJS?
- Draw a horizontal and vertical line on mouse hover in chart js
- How to draw Horizontal line on Bar Chart Chartjs
- Chart.js - draw horizontal line in Bar Chart (type bar)
- Can't Draw Horizontal Line on Graph Using ChartJS Annotation Plugin and Primevue Chart
- How to draw multiple color bars in a bar chart along a Horizontal Line using chart.js
- Draw stacked horizontal bar chart with Average line using ChartJS
- How can I create a horizontal scrolling Chart.js line chart with a locked y axis?
- Chart.js - draw horizontal line
- ChartJS: Draw vertical line at data point on chart on mouseover
- Can we draw a Line Chart with both solid and dotted line in it?
- Chart Js V2 draw Horizontal Bar(Average) Over Vertical Bar
- Chart.js - draw horizontal line based on a certain y-value
- In Stacked horizontal bar chart how to remove the vertical line in Chart.js?
- Draw a horizontal bar chart from right to left
- Angular-chart / line chart with multiple horizontal lines (margins)
- Chart.js horizontal line chart or modified horizontal bar chart
- change space between horizontal (grid) lines for line chart
- Chart.js - Horizontal line on Bar chart interferes with tooltip
- Do not draw line on condition in ChartJS line chart
- ChartJS 2.9.4 can't overlay line data on Horizontal Bar chart
- Remove top horizontal line in a horizontal bar chart (Chart.js 2)
- Change horizontal line chart to vertical line
- Chartjs draw line chart where line go back and forth (by chronological order)
- I want to draw a horizontal line
- Is there any way to remove extra space and Horizontal line from Bar chart of Chart.js?
- Draw line between starting point and Ending point in semi doughnut chart in chart js
- dynamically update Chart.js draw line chart dataset data
- chart js draw line chart by points instead of by lines
More Query from same tag
- Chart.JS Global Options
- How to set max/min on x axis to display time range in Chart.js?
- MongoDB based data is not populating in Chart.js
- JsPDF and chart.js: Increase chart resolution
- Using chartJs in a Vue Component and a Webpack cli setup
- Chartjs not showing all data points
- Adding additional properties to a Chart JS dataset
- Chart.js tooltipTemplate not working
- Data Labels on top of points in line charts
- Ticks callback not displaying strings containing decimal
- How to group and count missing values using linq
- Chartjs draw line chart where line go back and forth (by chronological order)
- chart js when hover shows old values
- How do i use chartjs in react project properly?
- ng2-charts: Datalabels values are not shown in my grapghs
- How to incorporate chart.js in jsp file
- Price history with chart js
- JSON data in Chart.js is splitting values
- How do we can use chart.js in angular JS?
- Chartjs - Line between two dots on the Y axis
- wrong rendered bar chart Chart.js
- Chart js pie chart, darker shade for higher value and lighter shade for lower value. I'm displaying values monthly
- How to create a chart with chartjs.org with data from an array?
- How to trigger ChartJS legend onClick with out interrupting its normal working
- chartjs plot datetime value with time offset on the grid
- Php/ajax return JSON in same form as javascript array
- Implement ng2-charts in an Angular-Seed
- Using chart.js inside node.js
- ChartJS - adding scroll to horizontal legend in Line chart
- Chart.js - Pie chart calculate sum of visible values after legend click