javascript - 有两列的谷歌柱形图

标签 javascript jquery charts google-api google-visualization

如何处理 column chart当有月份和年份时。

我的数据是这种格式

['Group','Count','Month','Year'],
['A',10,'February',2015],
['B',8,'February',2015],
['C',15,'February',2016]

目的是创建一个柱形图,其中X 轴为月份Y 轴为计数。现在 X 轴应该有按月分组的年份计数。 像这样的 - enter image description here

我试图简单地传递上面的数据,看看我能得到什么,但我得到了错误。

有什么方法可以根据按月分组的年份为 Axis 分配值?

JsFiddle of Google Chart Example

最佳答案

只需要三列,像这样...

  ['Month', '2015', '2016'],
  ['Jan', 10, 15],
  ['Feb', 12, 18],
  ['Mar', 14, 21],
  ['Apr', 16, 24]

然后您可以使用 DataView 通过计算列添加注释...

var view = new google.visualization.DataView(data);
view.setColumns([0, 1, {
    calc: 'stringify',
    sourceColumn: 1,
    type: 'string',
    role: 'annotation'
  }, 2, {
    calc: 'stringify',
    sourceColumn: 2,
    type: 'string',
    role: 'annotation'
}]);

请参阅以下工作片段...

google.charts.load('current', {
  callback: function () {
    var data = google.visualization.arrayToDataTable([
      ['Month', '2015', '2016'],
      ['Jan', 10, 15],
      ['Feb', 12, 18],
      ['Mar', 14, 21],
      ['Apr', 16, 24]
    ]);

    var view = new google.visualization.DataView(data);
    view.setColumns([0, 1, {
        calc: 'stringify',
        sourceColumn: 1,
        type: 'string',
        role: 'annotation'
      }, 2, {
        calc: 'stringify',
        sourceColumn: 2,
        type: 'string',
        role: 'annotation'
    }]);

    var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    chart.draw(view, {});
  },
  packages: ['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

编辑

所有谷歌图表都需要一个特定的 DataFormat

意思是,如果您的数据不以这种格式存在,则需要进行操作

可视化库确实提供了一些 Data Manipulation Methods ,例如 group()

可用于将数据转换为所需格式

1) group the data by Month and Year
2) create a new DataTable with the Month column
3) add a column for each Year from the grouped table
4) add the rows for each month

请参阅以下工作片段,使用问题中的数据...

google.charts.load('current', {
  callback: function () {
    var data = google.visualization.arrayToDataTable([
      ['Group', 'Count', 'Month', 'Year'],
      ['A', 10, 'February', 2015],
      ['B', 8, 'February', 2015],
      ['C' , 15, 'February', 2016]
    ]);

    // group by month / year
    var dataGroup = google.visualization.data.group(
      data,
      [2, 3],
      [{column: 1, aggregation: google.visualization.data.sum, type: 'number', label: 'Count'}]
    );
    dataGroup.sort([{column: 0},{column: 1}]);

    // build final data table
    var yearData = new google.visualization.DataTable({
      cols: [
        {label: 'Month', type: 'string'}
      ]
    });

    // add column for each year
    var years = dataGroup.getDistinctValues(1);
    for (var i = 0; i < years.length; i++) {
      yearData.addColumn(
        {label: years[i], type: 'number'}
      );
    }

    // add row for each month
    var rowMonth = null;
    var rowIndex = null;
    for (var i = 0; i < dataGroup.getNumberOfRows(); i++) {
      if (rowMonth !== dataGroup.getValue(i, 0)) {
        rowMonth = dataGroup.getValue(i, 0);
        rowIndex = yearData.addRow();
        yearData.setValue(rowIndex, 0, rowMonth);
      }
      for (var x = 1; x < yearData.getNumberOfColumns(); x++) {
        if (yearData.getColumnLabel(x) === dataGroup.getValue(i, 1).toString()) {
          yearData.setValue(rowIndex, x, dataGroup.getValue(i, 2));
        }
      }
    }

    var view = new google.visualization.DataView(yearData);
    view.setColumns([0, 1, {
        calc: 'stringify',
        sourceColumn: 1,
        type: 'string',
        role: 'annotation'
      }, 2, {
        calc: 'stringify',
        sourceColumn: 2,
        type: 'string',
        role: 'annotation'
    }]);

    var container = document.getElementById('chart_div');
    var chart = new google.visualization.ColumnChart(container);
    chart.draw(view);
  },
  packages: ['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

关于javascript - 有两列的谷歌柱形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38703689/

相关文章:

jquery - 在 ASP.NET Mvc 中使用 Dropzone 传递隐藏的表单值

jquery Flot Chart - 如何隐藏周末日期时间 - 对于外汇图表?

javascript - 使用 NVD3 的响应式饼图

javascript - Redux 获取操作

javascript - 替换 'New' 的规则

javascript - 掌握 JS Promise

javascript - 将 <span> 绝对定位在 &lt;textarea&gt; 的底部

javascript - 如果多个跨度有不同的值,则显示警报

javascript - jquery 添加/删除父 div 之外的多个元素的类

javascript - 道场图表上的多线图例