score:4

drawing a dotted line

you don't need to extend the chart, but it would be cleaner to do it that way.


preview

enter image description here

script

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

        var ctx = this.chart.ctx;
        var originalbeziercurveto = ctx.beziercurveto;
        ctx.beziercurveto = function () {
            ctx.setlinedash([10, 10]);
            originalbeziercurveto.apply(this, arguments)
        }
    }
});

...

new chart(ctx).linealt(chartdata);

fiddle - https://jsfiddle.net/ahj6u14e/


note - the alternative would be to just override beziercurveto using the chart object.

this works because beziercurveto is only used to draw the line. if you wanted to do this for straight lines it wouldn't work because lineto is used for other stuff (axis, grid lines...)

chart.js 2.0 had a borderdash option when i last checked (see https://stackoverflow.com/a/31428640/360067)

score:6

for dotted lines use borderdash and bordercapstyle. the following example creates a dotted line (3px diameter):

data: {
  datasets: [
    {
        data           : data,
        borderwidth    : 3, // set diameter of dots here
        bordercolor    : '#ccc',
        fill           : false,
        pointradius    : 0,
        borderdash     : [0,6], // set 'length' of dash/dots to zero and
                                // space between dots (center to center)
                                // recommendation: 2x the borderwidth
        bordercapstyle : 'round' // this is where the magic happens
    }
  ]
}

output

output

output (better contrast for demonstration) enter image description here

score:7

in chart.js 2.1+, use the borderdash option within your dataset. it takes an array of two numbers. see this codepen


Related Query

More Query from same tag