Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending Line Chart with custom line

I'm trying to extend the Line chart in Chart.js with this code:

var shotsData = {
  labels: ["Shot 1", "Shot 2", "Shot 3", "Shot 4", "Shot 5"],
  datasets: [{ data: [5, 7, 1, 4, 9] }]
};

var ctx = document.getElementById("shots").getContext("2d");

Chart.types.Line.extend({
    name: "LineAlt",
    initialize: function () {
        this.chart.ctx.beginPath();
        this.chart.ctx.moveTo(0, 15);
        this.chart.ctx.lineTo(159, 274);
        this.chart.ctx.stroke();

        Chart.types.Line.prototype.initialize.apply(this, arguments);
    }
});

new Chart(ctx).LineAlt(shotsData);

This draws my chart, but I also want a custom line within the chart, which is written inside the initialize function, but this seems to not work. When I remove the line Chart.types.Line.prototype.initialize.apply(this, arguments); the custom line is displayed.

Here's a fiddle

http://jsfiddle.net/92ANv/5/

like image 972
Lars Avatar asked Feb 13 '23 08:02

Lars


1 Answers

You could override the draw function to append your drawing on to the canvas

draw: function () {
    //call the super draw
    Chart.types.Line.prototype.draw.apply(this, arguments);

    //now your custom line
    this.chart.ctx.beginPath();
    this.chart.ctx.moveTo(0, 5);
    this.chart.ctx.lineTo(100, 100);
    this.chart.ctx.stroke();
}

fiddle http://jsfiddle.net/leighking2/5Lgzvu3r

like image 135
Quince Avatar answered Feb 19 '23 06:02

Quince