Chart.js 3.x not able to display data on chart

454 Views Asked by At

I have the below chart using chart.js 3.x.
https://jsfiddle.net/7oxmesnj/1/

I have below chart configuration:

var options = {
  type: 'scatter',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [
        {
          label: '# of Votes',
          data: [12, 19, 3, 5, 2, 3],
        borderWidth: 0,
        showLine: true,
        },  
            {
                label: '# of Points',
                data: [7, 11, 5, 8, 3, 7],
                borderWidth: 0,
        showLine: true,
            }
        ]
  },
  options: {
    scales: {
        y:{
        type: 'linear',
        min: 0,
        max: 500,
        ticks: {
          stepSize: 100,
                    reverse: false
        }
      },
      x:{
        type: 'linear',
        min: 0,
        max: 500,
        ticks: {
          stepSize: 100,
                    reverse: false
        }
      }

    }
  }
}

I am not able to plot the data on the scatter chart.
I have followed the migration guild, but still no data on chart.
https://www.chartjs.org/docs/master/getting-started/v3-migration

2

There are 2 best solutions below

0
On

You should change datasets to have pairs of numbers (x, y). See documentation.

var options = {
  type: 'scatter',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [{x:12,y:7}, {x:19,y:11}, {x:3,y:5}, {x:5,y:8}, {x:2,y:3}, {x:3,y:7}],
      borderWidth: 1,
      showLine: true,
      pointBackgroundColor: 'red',
      borderColor: 'blue',
      backgroundColor: 'blue',
    }]
  },
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
canvas {
  background-color: #eee;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.0.0-beta/chart.min.js"></script>

<canvas id="chartJSContainer" width="600" height="400"></canvas>

0
On

The main problem in your code is the definition of the x-axis. The x-axis represents the labels, which are strings. Therefore, you cannot define numeric min, max and ticks.stepSize options.

In order to obtain a scatter chart with given data, you could change its type to 'line' and define showLine: false on each dataset.

Please take a look at your amended code below. I changed y

var options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        fill: false,
        showLine: false
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        fill: false,
        showLine: false
      }
    ]
  },
  options: {
    scales: {
      y: {
        min: 0,
        max: 50,
        ticks: {
          stepSize: 10
        }
      }
    }
  }
};

new Chart('chartJSContainer', options);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.0.0-beta/chart.min.js"></script>
<canvas id="chartJSContainer"></canvas>