Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

empty donut chart chart js

let ctx = document.getElementById('chartContainer').getContext('2d');

let data = [[0, 0, 0], [1,2,3]];
let labels = ["A", "B", "C"];
let bgColors = ['yellow', 'orange', 'aquamarine'];

let options = {
  borderWidth: 1,
  borderColor: 'black',
  legend: {
    labels: {
    	// Prevent items with undefined labels from appearing in the legend
      filter: (item) => item.text !== undefined
    }
  },
  tooltips: {
    // Prevent items with undefined labels from showing tooltips
  	filter: (item, chart) => chart.labels[item.index] !== undefined
  }
}

let chartConfig = {
  type: 'pie',
  data: {
    labels: labels,
    datasets: [{
      data: data[0],
      backgroundColor: bgColors,
      label: "data",
      borderColor: 'black',
      borderWidth: 2
    }, {
      data: data[1],
      backgroundColor: bgColors,
      label: "data",
      borderColor: 'black',
      borderWidth: 2
    }]
  },
  options: options
}

// Check if data is all 0s; if it is, add dummy data to end with empty label
chartConfig.data.datasets.forEach(dataset => {
  if (dataset.data.every(el => el === 0)) {
    dataset.backgroundColor.push('rgba(255,255,255,0)');
    dataset.data.push(1);
  }
})


let pieChart = new Chart(ctx, chartConfig);
Comment

PREVIOUS NEXT
Code Example
Javascript :: update instance in sequelize 
Javascript :: js sol 
Javascript :: jquery excel export 
Javascript :: javascript how to multiply numbers 
Javascript :: javascript make variable 
Javascript :: Day of The Year 
Javascript :: assignment of struct in solidity 
Javascript :: jquery database add dropdown in datababe grid 
Javascript :: setEventListeners form inputs 
Javascript :: javascript centuries 
Javascript :: Conditionally add members to an object 
Javascript :: createSearchParams 
Javascript :: circle rect collision 
Javascript :: Array.find Shorthand javascript 
Javascript :: react controllers 
Javascript :: rxjs: from usage 
Javascript :: change to a constant variable 
Javascript :: apostrophe issue in javascript 
Javascript :: make price comma jquery 
Javascript :: can not found jstl core xml file 
Javascript :: replace text with hyperlink 
Javascript :: convert typescript to js online 
Javascript :: ES2022 - Top-level await modules 
Javascript :: javascript intersection recursion 
Javascript :: twitter user profile regex javascript 
Javascript :: alertify.js styled success messae 
Javascript :: data structures with javascript 
Javascript :: Default function arguments in ES6 
Javascript :: Subtracting Numbers in Array 
Javascript :: discord.js const 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =