Chart.js堆积和分组条形图

时间:2016-05-28 13:29:56

标签: javascript charts

有没有办法用Chart.js库做堆叠和分组的条形图?

它应该看起来像这样http://www.highcharts.com/demo/column-stacked-and-grouped

3 个答案:

答案 0 :(得分:4)

好的,我找到了解决方案。它在GitHub issue中描述,解决方案在this JSFiddle

Chart.defaults.groupableBar = Chart.helpers.clone(Chart.defaults.bar);

var helpers = Chart.helpers;
Chart.controllers.groupableBar = Chart.controllers.bar.extend({
  calculateBarX: function (index, datasetIndex) {
    // position the bars based on the stack index
    var stackIndex = this.getMeta().stackIndex;
    return Chart.controllers.bar.prototype.calculateBarX.apply(this, [index, stackIndex]);
  },

  hideOtherStacks: function (datasetIndex) {
    var meta = this.getMeta();
    var stackIndex = meta.stackIndex;

    this.hiddens = [];
    for (var i = 0; i < datasetIndex; i++) {
      var dsMeta = this.chart.getDatasetMeta(i);
      if (dsMeta.stackIndex !== stackIndex) {
        this.hiddens.push(dsMeta.hidden);
        dsMeta.hidden = true;
      }
    }
  },

  unhideOtherStacks: function (datasetIndex) {
    var meta = this.getMeta();
    var stackIndex = meta.stackIndex;

    for (var i = 0; i < datasetIndex; i++) {
      var dsMeta = this.chart.getDatasetMeta(i);
      if (dsMeta.stackIndex !== stackIndex) {
        dsMeta.hidden = this.hiddens.unshift();
      }
    }
  },

  calculateBarY: function (index, datasetIndex) {
    this.hideOtherStacks(datasetIndex);
    var barY = Chart.controllers.bar.prototype.calculateBarY.apply(this, [index, datasetIndex]);
    this.unhideOtherStacks(datasetIndex);
    return barY;
  },

  calculateBarBase: function (datasetIndex, index) {
    this.hideOtherStacks(datasetIndex);
    var barBase = Chart.controllers.bar.prototype.calculateBarBase.apply(this, [datasetIndex, index]);
    this.unhideOtherStacks(datasetIndex);
    return barBase;
  },

  getBarCount: function () {
    var stacks = [];

    // put the stack index in the dataset meta
    Chart.helpers.each(this.chart.data.datasets, function (dataset, datasetIndex) {
      var meta = this.chart.getDatasetMeta(datasetIndex);
      if (meta.bar && this.chart.isDatasetVisible(datasetIndex)) {
        var stackIndex = stacks.indexOf(dataset.stack);
        if (stackIndex === -1) {
          stackIndex = stacks.length;
          stacks.push(dataset.stack);
        }
        meta.stackIndex = stackIndex;
      }
    }, this);

    this.getMeta().stacks = stacks;
    return stacks.length;
  },
});

var data = {
  labels: ["January", "February", "March"],
  datasets: [
    {
      label: "Apples",
      backgroundColor: "rgba(99,255,132,0.2)",
      data: [20, 10, 30],
      stack: 1
    },
    {
      label: "Bananas",
      backgroundColor: "rgba(99,132,255,0.2)",
      data: [40, 50, 20],
      stack: 1
    },
    {
      label: "Cookies",
      backgroundColor: "rgba(255,99,132,0.2)",
      data: [60, 20, 20],
      stack: 1
    },
    {
      label: "Apples",
      backgroundColor: "rgba(99,255,132,0.2)",
      data: [20, 10, 30],
      stack: 2
    },
    {
      label: "Bananas",
      backgroundColor: "rgba(99,132,255,0.2)",
      data: [40, 50, 20],
      stack: 2
    },
    {
      label: "Cookies",
      backgroundColor: "rgba(255,99,132,0.2)",
      data: [60, 20, 20],
      stack: 2
    },
    {
      label: "Apples",
      backgroundColor: "rgba(99,255,132,0.2)",
      data: [20, 10, 30],
      stack: 3
    },
    {
      label: "Bananas",
      backgroundColor: "rgba(99,132,255,0.2)",
      data: [40, 50, 20],
      stack: 3
    },
    {
      label: "Cookies",
      backgroundColor: "rgba(255,99,132,0.2)",
      data: [60, 20, 20],
      stack: 3
    },
  ]
};

var ctx = document.getElementById("myChart").getContext("2d");
new Chart(ctx, {
  type: 'groupableBar',
  data: data,
  options: {
    legend: {
      labels: {
        generateLabels: function(chart) {
          return Chart.defaults.global.legend.labels.generateLabels.apply(this, [chart]).filter(function(item, i){
                return i <= 2;
          });
        }
      }
    },
    scales: {
      yAxes: [{
        ticks: {
          max: 160,
        },
        stacked: true,
      }]
    }
  }
});

答案 1 :(得分:4)

您应该为每个数据集使用数据集对象的stack属性。

正如您在Chart.js Documentation中所看到的,堆栈定义为:

&#34;此数据集所属的组的ID(堆叠时,每个组将是一个单独的堆栈)&#34;

我相信这个功能最近已经推出,2016年Chart.js由于this post

而没有这个功能

答案 2 :(得分:-1)

您可以使用以下数据获得堆积的条形图

this.dataStackedBarChart = {
      type: 'horizontalBar',
      labels: this.stackedBarChartLabel,
      datasets: [
        {
          label: 'Success Count',
          stack: 'Stack 0',
          data: this.successCount,
          backgroundColor: 'green'
        },
        {
          label: 'FailureCount',
          stack: 'Stack 0',
          data: this.failureCount,
          backgroundColor: 'red'
        },
        {
          label: 'AvgDuration',
          stack: 'Stack 1',
          data: this.avgDuration,
          backgroundColor: 'black'
      },
      {
        label: 'MaxDuration',
        stack: 'Stack 2',
        data: this.maxDuration,
        backgroundColor: 'orange'
    },
    {
        label: 'MinDuration',
        stack: 'Stack 3',
        data: this.minDuration,
        backgroundColor: 'pink'
    }
      ],
      borderWidth: 2
    }
    this.optionsStackedBarChart = {
      tooltips: {
        callbacks: {
          label: function (tooltipItem, data) {
            var label = this.uniqueApiPath[tooltipItem.index];
            return label;
          }
        }
      },
      scales: {
        xAxes: [
          {
            stacked: true,
            display: true,
            ticks: {
              beginAtZero: true,
              min: 0,
              suggestedMin: 0
            }
          }
        ],
        yAxes: [
          {
            stacked: true,
            display: true,
            ticks: {
              beginAtZero: true,
              min: 0,
              suggestedMin: 0
            }
          }
        ]
      }