Chartjs-Chart.js: Some sectors not showing if difference is too big

0👍

You can use a logarithmic scale but only for lines. A donut is not a good choice for your use case

https://www.chartjs.org/docs/latest/samples/scales/log.html

Config:

  const config = {
  type: 'line',
  data: data,
  options: {
    responsive: true,
    plugins: {
      title: {
        display: true,
        text: 'Chart.js Line Chart - Logarithmic'
      }
    },
    scales: {
      x: {
        display: true,
      },
      y: {
        display: true,
        type: 'logarithmic',
      }
    }
  },
};

Setup:

const DATA_COUNT = 7;
const NUMBER_CFG = {count: DATA_COUNT, min: 0, max: 100};

const labels = Utils.months({count: 7});
const data = {
  labels: labels,
  datasets: [
    {
      label: 'Dataset 1',
      data: logNumbers(DATA_COUNT),
      borderColor: Utils.CHART_COLORS.red,
      backgroundColor: Utils.CHART_COLORS.red,
      fill: false,
    },
  ]
};

Action

const logNumbers = (num) => {
  const data = [];

  for (let i = 0; i < num; ++i) {
    data.push(Math.ceil(Math.random() * 10.0) * Math.pow(10, Math.ceil(Math.random() * 5)));
  }

  return data;
};

const actions = [
  {
    name: 'Randomize',
    handler(chart) {
      chart.data.datasets.forEach(dataset => {
        dataset.data = logNumbers(chart.data.labels.length);
      });
      chart.update();
    }
  },
];

0👍

One alternative is using 3 datasets, one for each data.

labels: ['a', 'b', 'c'],   
datasets: [
  {
    label: "My First Dataset",
    data: [878,0,0],
    backgroundColor: [
      "rgb(255, 205, 86)",
      "rgb(255, 99, 132)",
      "rgb(54, 162, 235)",          
    ],        
    offset:0,
    hoverOffset: 0,               
  },
  {
    label: "My First Dataset2",
    data: [0,19020,0],
    backgroundColor: [
      "rgb(255, 205, 86)",
      "rgb(255, 99, 132)",
      "rgb(54, 162, 235)",          
    ],        
    offset:0,
    hoverOffset: 0,       

  },
  {
    label: "My First Dataset2",
    data: [0,0,100412286],
    backgroundColor: [
      "rgb(255, 205, 86)",
      "rgb(255, 99, 132)", 
      "rgb(54, 162, 235)",         
    ],        
    offset:0,
    hoverOffset: 0,       

  },
],

enter image description here

I hope this help.

Leave a comment