Multi Series Pie

Multi Series Pie - 图1

config setup

  1. const config = {
  2. type: 'pie',
  3. data: data,
  4. options: {
  5. responsive: true,
  6. plugins: {
  7. legend: {
  8. labels: {
  9. generateLabels: function(chart) {
  10. // Get the default label list
  11. const original = Chart.overrides.pie.plugins.legend.labels.generateLabels;
  12. const labelsOriginal = original.call(this, chart);
  13. // Build an array of colors used in the datasets of the chart
  14. var datasetColors = chart.data.datasets.map(function(e) {
  15. return e.backgroundColor;
  16. });
  17. datasetColors = datasetColors.flat();
  18. // Modify the color and hide state of each label
  19. labelsOriginal.forEach(label => {
  20. // There are twice as many labels as there are datasets. This converts the label index into the corresponding dataset index
  21. label.datasetIndex = (label.index - label.index % 2) / 2;
  22. // The hidden state must match the dataset's hidden state
  23. label.hidden = !chart.isDatasetVisible(label.datasetIndex);
  24. // Change the color to match the dataset
  25. label.fillStyle = datasetColors[label.index];
  26. });
  27. return labelsOriginal;
  28. }
  29. },
  30. onClick: function(mouseEvent, legendItem, legend) {
  31. // toggle the visibility of the dataset from what it currently is
  32. legend.chart.getDatasetMeta(
  33. legendItem.datasetIndex
  34. ).hidden = legend.chart.isDatasetVisible(legendItem.datasetIndex);
  35. legend.chart.update();
  36. }
  37. },
  38. tooltip: {
  39. callbacks: {
  40. label: function(context) {
  41. const labelIndex = (context.datasetIndex * 2) + context.dataIndex;
  42. return context.chart.data.labels[labelIndex] + ': ' + context.formattedValue;
  43. }
  44. }
  45. }
  46. }
  47. },
  48. };
  1. const DATA_COUNT = 5;
  2. const NUMBER_CFG = {count: DATA_COUNT, min: 0, max: 100};
  3. const labels = Utils.months({count: 7});
  4. const data = {
  5. labels: ['Overall Yay', 'Overall Nay', 'Group A Yay', 'Group A Nay', 'Group B Yay', 'Group B Nay', 'Group C Yay', 'Group C Nay'],
  6. datasets: [
  7. {
  8. backgroundColor: ['#AAA', '#777'],
  9. data: [21, 79]
  10. },
  11. {
  12. backgroundColor: ['hsl(0, 100%, 60%)', 'hsl(0, 100%, 35%)'],
  13. data: [33, 67]
  14. },
  15. {
  16. backgroundColor: ['hsl(100, 100%, 60%)', 'hsl(100, 100%, 35%)'],
  17. data: [20, 80]
  18. },
  19. {
  20. backgroundColor: ['hsl(180, 100%, 60%)', 'hsl(180, 100%, 35%)'],
  21. data: [10, 90]
  22. }
  23. ]
  24. };