score:17

Accepted answer

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)'
        }
    ],
});

http://jsfiddle.net/vsh6tcfd/3/

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.


Related Query

More Query from same tag