Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I draw dotted line using chartjs?

Tags:

chart.js

I would like to draw dotted line using chartjs. I did not see any options in creating dotted lines. I feel we need to extend the chartjs to support this. Can some one help me in this?

like image 395
Srinivas Raju Avatar asked Dec 12 '15 13:12

Srinivas Raju


People also ask

How do I add a padding in ChartJS?

Lets say you wanted to add 50px of padding to the left side of the chart canvas, you would do: let chart = new Chart(ctx, { type: 'line', data: data, options: { layout: { padding: { left: 50 } } } }); Copied!


2 Answers

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

like image 183
Alexander Uhl Avatar answered Oct 25 '22 17:10

Alexander Uhl


In Chart.js 2.1+, use the borderDash option within your dataset. It takes an array of two numbers. See this codepen

like image 43
BeetleJuice Avatar answered Oct 25 '22 19:10

BeetleJuice