score:9

Accepted answer

chart.js doesn't support this directly.

  1. you have to disable the default segment drawing and
  2. write your own instead

for 1., setting the stroke width to 0 does not work because canvas ignores 0 (and nan, undefined...), so you set it to a very small value to make the line invisible (there is a datasetstroke option, but there is no code that acts on it yet)

it would be logical to also disable the fill. however, with dataset fill turned off, the points get filled with black color (chart.js bug?), so make the points solid by reducing the radius and increasing the strokewidth.

var mylinechart = new chart(ctx).linealt(data, {
    datasetstrokewidth: 0.01,
    datasetfill: false,
    pointdotradius : 2,
    pointdotstrokewidth: 3
});

notice that the type is linealt - which is how you take care of the 2. - by extending the line chart type

chart.types.line.extend({
    name: "linealt",
    draw: function () {
        chart.types.line.prototype.draw.apply(this, arguments);

        // now draw the segments
        var ctx = this.chart.ctx
        this.datasets.foreach(function (dataset) {
            ctx.strokestyle = dataset.strokecolor

            var previouspoint = {
                value: null
            };
            dataset.points.foreach(function (point) {
                if (previouspoint.value !== null && point.value !== null) {
                    ctx.beginpath();
                    ctx.moveto(previouspoint.x, previouspoint.y);
                    ctx.lineto(point.x, point.y);
                    ctx.stroke();
                }
                previouspoint = point;
            })
        })
    }
});

fiddle - http://jsfiddle.net/slgefm04/66/

score:5

maybe this wasn't available back in 2015 but now the line graph has a styling option spangaps which if true, lines will be drawn between points with no or null data. if false, points with nan data will create a break in the line.


Related Query

More Query from same tag