[Chartjs]-How can I set ChartJS x-axis ticks at specified/fixed intervals?

7👍

You could use a scatter chart that accepts the data as individual points, objects with properties x and y. To turn a scatter chart back to a line chart, simply define showLine: true on each dataset.

To product data as individual points, you can defined labels outside of the chart and convert both value arrays using Array.map() as follows.

valueArray.map((v, i) => ({ x: labels[i], y: v }))

The last thing to do is defining xAxis.ticks as follows:

ticks: {
  stepSize: 1,
  autoSkip: false,
  callback: value => value % 20 == 0 ? value : null,
  min: labels[0],
  max: labels[labels.length - 1],
  maxRotation: 0
},

Please have a look at your amended code below:

const labels = [7, 14, 21, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98, 105, 119, 126];
var myChart = new Chart('myChart', {
  type: 'scatter',
  data: {    
    datasets: [{
        label: 'IC',
        data: [9432.13209267, 1899.132847, 851.2122668, 3964.48968367, 9433, 8087.04121533, 1268.34642367, 825.29800317, 4271.00722867, 1157.57453933, 4928.214994, 783.88204033, 1846.623016, 4001.84214867, 709.70201067, 3917.61792167, 688.1571182].map((v, i) => ({ x: labels[i], y: v })),
        backgroundColor: [
          'rgba(255, 99, 132, 0.2)',
          'rgba(54, 162, 235, 0.2)',
          'rgba(255, 206, 86, 0.2)',
          'rgba(75, 192, 192, 0.2)',
          'rgba(153, 102, 255, 0.2)',
          'rgba(255, 159, 64, 0.2)'
        ],
        borderColor: [
          'rgba(255,99,132,1)',
          'rgba(54, 162, 235, 1)',
          'rgba(255, 206, 86, 1)',
          'rgba(75, 192, 192, 1)',
          'rgba(153, 102, 255, 1)',
          'rgba(255, 159, 64, 1)'
        ],
        showLine: true,
        borderWidth: 1
      },
      {
        label: 'Mtb',
        data: [14241.41334667, 48348.08833, 5055.28134533, 7614.80689233, 14240, 24536.66203, 1083.99264, 144.72451603, 15968.94539333, 160.150183, 5127.77524033, 953.9963646, 0, 2989.36556, 21.32920023, 27.03159138, 60310.22952333].map((v, i) => ({ x: labels[i], y: v })),
        backgroundColor: [
          'rgba(255, 99, 132, 0.2)',
          'rgba(54, 162, 235, 0.2)',
          'rgba(255, 206, 86, 0.2)',
          'rgba(75, 192, 192, 0.2)',
          'rgba(153, 102, 255, 0.2)',
          'rgba(255, 159, 64, 0.2)'
        ],
        borderColor: [
          'rgba(255,99,132,1)',
          'rgba(54, 162, 235, 1)',
          'rgba(255, 206, 86, 1)',
          'rgba(75, 192, 192, 1)',
          'rgba(153, 102, 255, 1)',
          'rgba(255, 159, 64, 1)'
        ],
        showLine: true,
        borderWidth: 1
      }
    ]
  },
  options: {
    scales: {
      xAxes: [{
        display: true,
        position: 'bottom',
        ticks: {
          stepSize: 1,
          autoSkip: false,
          callback: value => value % 20 == 0 ? value : null,
          min: labels[0],
          max: labels[labels.length - 1],
          maxRotation: 0
        },
        // afterBuildTicks: (axis, ticks) => [0, 20, 40, 60, 80, 100, 120],
        scaleLabel: {
          display: true,
          labelString: 'Days from initial test',
          fontSize: 16
        }
      }],
      yAxes: [{
          display: true,
          position: 'left',
          ticks: {
            beginAtZero: true,
            stepSize: 10000,
            min: 0,
            max: 70000
          },
          scaleLabel: {
            display: true,
            fontSize: 16
          }
        },
        {
          display: true,
          position: 'right',
          ticks: {
            beginAtZero: true,
            stepSize: 10000,
            min: 0,
            max: 70000
          },
          scaleLabel: {
            display: true,
            fontSize: 16
          }
        },
      ]
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="myChart" height="100"></canvas>

Leave a comment