How to make gap in chart.js graph?

Member

by mac , in category: Javascript , 5 months ago

How to make gap in chart.js graph?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by daisha , 5 months ago

@mac 

To create a gap in a Chart.js graph, you can use the null or undefined values for the data points where you want the gap to appear. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
var chartData = {
  labels: ["January", "February", "March", "April", "May", "June"],
  datasets: [{
    data: [10, 20, null, 30, undefined, 40],
    label: "Data",
    fill: false,
    borderColor: "rgb(75, 192, 192)",
    tension: 0.1
  }]
};

var chartOptions = {
  scales: {
    y: {
      beginAtZero: true
    }
  }
};

var lineChart = new Chart(document.getElementById("myChart"), {
  type: "line",
  data: chartData,
  options: chartOptions
});


In the data array, you can place null or undefined values at the desired positions. This will create gaps in the graph where those values are present.


Make sure to set fill: false in the dataset options to disable the line fill between the gaps.


The chartOptions object is used to configure the y-axis scale, but you can modify it according to your specific requirements.


Note that this example assumes you have a Chart.js library loaded and an HTML canvas element with the id "myChart" where the chart should be rendered. Make sure to adjust these parts as necessary for your scenario.