Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show Y axis ticks for every point in the graph

I'm using chartjs to display some data but I'm in trouble with the Y axis visualization. I want at every point in the graph a value in the Y axis. Right now the Y axis display value in steps of 10.

This is my actual yAxis configuration:

yAxes: [{
            position: "left",
            ticks: {
                source:"data",
            } 
        }]

This is the actual chart. I would like to display in the Y axis only the actual value of the points displayed. In the X axis this configuration works, the time is aligned with the points. actual

Expected:

expected

like image 310
Mattia Corradi Avatar asked Sep 06 '19 15:09

Mattia Corradi


1 Answers

You have to use the Axis Callbacks (docs), especially afterBuildTicks.

In my linked example (jsbin) I push 0 to my data because you can't use beginAtZero: true and remove all the duplicates in the array.

data.push(0); //if 'beginAtZero' is wanted
let uniqueData = data.filter((v, i, a) => a.indexOf(v) === i);

In the options you define the min and max values and the ticks in afterBuildTicks.

options: {
  scales: {
    yAxes: [{
      ticks: {
        autoSkip: false,
        min: Math.min(...uniqueData),
        max: Math.max(...uniqueData)
      },
      afterBuildTicks: function(scale) {
        scale.ticks = uniqueData;
      }
    }]
  }
}

You will have problems with overlapping ticks but I don't know a general way to prevent that and I don't know your specific use case.

like image 72
HeadhunterKev Avatar answered Oct 02 '22 22:10

HeadhunterKev