NULL pointer
NULL pointer

Reputation: 1377

Where does chartjs document what is required in the myChartConfig to instantiate using myChart = new Chart(ctx, myChartConfig)

I have deduced by trial and error that the myChartConfig in var myChart = new Chart(ctx, myChartConfig); must contain a valid type key.

Even if I try to specify a global default thus: Chart.defaults.global.type = 'line';, unless I include

myChartConfig = {
  type: 'bar'
}

I get an error '"undefined" is not a chart type.'

This appears to be the only required key in the myChartConfig object. I looked in the documentation but could not find where the constructor is documented beyond https://www.chartjs.org/docs/latest/developers/charts.html and this does not specify what is required for the chart config.

Upvotes: 0

Views: 193

Answers (1)

uminder
uminder

Reputation: 26150

type is not an attribute of options but it belongs to the higher level configuration object.

All attributes inside options are optional. I didn't find this information in chart.js documentation but in the TypeScript type definitions repository that contains the specification of the interface ChartOptions in the file types/chart.js/index.d.ts.

Below code snippet from https://www.chartjs.org/docs/latest/getting-started/ illustrates that it's fine to provide an empty options object or no options at all.

var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
        datasets: [{
            label: 'My First dataset',
            backgroundColor: 'rgb(255, 99, 132)',
            borderColor: 'rgb(255, 99, 132)',
            data: [0, 10, 5, 2, 20, 30, 45]
        }]
    },
    options: {} // may be omitted
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="myChart" height="80"> </canvas>

Upvotes: 1

Related Questions