[Chartjs]-ChartJS set default options only to specific Canvas, not globally

1👍

Purpose of Chart.defaults means to have same values for multiple charts so it means you can not have two separate Chart.defaults settings.

Solution:

After understanding your requirement more clearly, here is what you can do with common properties.
Take all the not common datasets in a data var and push common properties in data like below and pass that to datasets in chart.

var data = [{
  label: 'India',
  data: [50, 55, 45, 43, 39],
  borderColor: '#666699'
}, {
  label: 'UAE',
  data: [15, 22, 19, 17, 19],
  borderColor: '#ff9933'
}, {
  label: 'US',
  data: [75, 62, 77, 81, 77],
  borderColor: '#33cccc'
}, {
  label: 'Oman',
  data: [15, 18, 23, 19, 21],
  borderColor: '#333300'
}, {
  label: 'Qatar',
  data: [26, 28, 27, 25, 26],
  borderColor: '#00cc99'
}, {
  label: 'Canada',
  data: [75, 77, 72, 79, 71],
  borderColor: '#cc33ff'
}, {
  label: 'Australia',
  data: [22, 28, 25, 23, 26],
  borderColor: '#3399ff'
}, {
  label: 'China',
  data: [51, 35, 41, 43, 39],
  borderColor: '#ff0000'
}];


data.forEach(function(obj) {
  obj.pointBackgroundColor = '#fff',
    obj.pointBorderWidth = 2,
    obj.borderWidth = 2,
    obj.lineTension = 0,
    obj.fill = false
});

console.log(data)// pass data var to datasets in chart

Complete working fiddle here.

I hope it will help you.

Leave a comment