Nicnem
Nicnem

Reputation: 51

Chart.js: A value is empty, but the line must not be broken

This is a line graph what I want to draw.

The label is

[2019, 2020, 2021, 2022]

but the dataset is

[{
 data: [100, '', 200, 100]
 ...
}]

The second one in the dataset is null or '', but I want the line not to be broken.

How can I draw such graph?

Upvotes: 2

Views: 698

Answers (1)

LeeLenalee
LeeLenalee

Reputation: 31341

You can use the object notation to achieve this by telling to which x label chart.js has to link the value so you can skip some labels.

Example:

var options = {
  type: 'line',
  data: {
    labels: [2019, 2020, 2021, 2022],
    datasets: [{
      label: '# of Votes',
      data: [{
        x: 2019,
        y: 100
      }, {
        x: 2021,
        y: 200
      }, {
        x: 2022,
        y: 100
      }],
      borderColor: 'pink',
      backgroundColor: 'red',
      radius: 5
    }]
  },
  options: {
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.0/chart.js"></script>
</body>

Upvotes: 3

Related Questions