Stacked Bar Chart

Stacked Bar Chart - 图1

Randomize

config setup actions

  1. const config = {
  2. type: 'bar',
  3. data: data,
  4. options: {
  5. plugins: {
  6. title: {
  7. display: true,
  8. text: 'Chart.js Bar Chart - Stacked'
  9. },
  10. },
  11. responsive: true,
  12. scales: {
  13. x: {
  14. stacked: true,
  15. },
  16. y: {
  17. stacked: true
  18. }
  19. }
  20. }
  21. };
  1. const DATA_COUNT = 7;
  2. const NUMBER_CFG = {count: DATA_COUNT, min: -100, max: 100};
  3. const labels = Utils.months({count: 7});
  4. const data = {
  5. labels: labels,
  6. datasets: [
  7. {
  8. label: 'Dataset 1',
  9. data: Utils.numbers(NUMBER_CFG),
  10. backgroundColor: Utils.CHART_COLORS.red,
  11. },
  12. {
  13. label: 'Dataset 2',
  14. data: Utils.numbers(NUMBER_CFG),
  15. backgroundColor: Utils.CHART_COLORS.blue,
  16. },
  17. {
  18. label: 'Dataset 3',
  19. data: Utils.numbers(NUMBER_CFG),
  20. backgroundColor: Utils.CHART_COLORS.green,
  21. },
  22. ]
  23. };
  1. const actions = [
  2. {
  3. name: 'Randomize',
  4. handler(chart) {
  5. chart.data.datasets.forEach(dataset => {
  6. dataset.data = Utils.numbers({count: chart.data.labels.length, min: -100, max: 100});
  7. });
  8. chart.update();
  9. }
  10. },
  11. ];

Docs