Google Chart:在仪表板中获取条形图的列数

时间:2016-10-12 06:35:03

标签: charts google-visualization google-chartwrapper

我有一个类似下面的JSON数据来构建Google图表中的信息中心。 我想绘制一个月线条图,其中x轴为scan_time(月),y轴为该月的tag_id的计数。 tag_ids可以是重复的。

以JSON输入数据:

 [ 
       {  
          "tag_id":"04:0f",
          "scan_time":"2016-09-29 06:47:47",
       },
       {  
          "tag_id":"04:0f",
          "scan_time":"2016-09-29 14:48:42",
       },
       {  
          "tag_id":"99:9n",
          "scan_time":"2016-10-29 06:47:47",
       },
       {  
          "tag_id":"05:m8",
          "scan_time":"2016-11-29 06:48:42",
       }
    ]

类似

Month       Count
    Sept        2
    Oct         1
    Nov         1

1 个答案:

答案 0 :(得分:2)

将JSON加载到DataTable后,

使用数据操作方法 - > group()

返回聚合的DataTable

请参阅以下工作片段,
group方法将第一列转换为'month'字符串,
并汇总每个'月'的标签ID的数量

google.charts.load('current', {
  callback: drawChart,
  packages:['table']
});

function drawChart() {
  var data = google.visualization.arrayToDataTable([
    ['date', 'tag_id'],
    [new Date('2016-09-29 06:47:47'), '04:0f'],
    [new Date('2016-09-29 14:48:42'), '04:0f'],
    [new Date('2016-10-29 06:47:47'), '99:9n'],
    [new Date('2016-11-29 06:48:42'), '05:m8'],
  ]);

  var formatDate = new google.visualization.DateFormat({pattern: 'MMMM'});

  var dataGroup = google.visualization.data.group(
    data,

    // group by column
    [{
      column: 0,
      label: 'Month',
      modifier: function (val) {
        return formatDate.formatValue(val);
      },
      type: 'string'
    }],

    // agg columns
    [{
      aggregation: google.visualization.data.count,
      column: 1,
      label: 'Tag Count',
      type: 'number'
    }]
  );

  var table = new google.visualization.Table(document.getElementById('chart_div'));
  table.draw(dataGroup);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

编辑

使用DataView自定义行排序

var view = new google.visualization.DataView(dataGroup);
view.setRows([2, 1, 0]);

使用view绘制表格图表和任何其他图表......

以下代码段包含ColumnChart ...

google.charts.load('current', {
  callback: drawChart,
  packages:['corechart', 'table']
});

function drawChart() {
  var data = google.visualization.arrayToDataTable([
    ['date', 'tag_id'],
    [new Date('2016-09-29 06:47:47'), '04:0f'],
    [new Date('2016-09-29 14:48:42'), '04:0f'],
    [new Date('2016-10-29 06:47:47'), '99:9n'],
    [new Date('2016-11-29 06:48:42'), '05:m8'],
  ]);

  var formatDate = new google.visualization.DateFormat({pattern: 'MMMM'});

  var dataGroup = google.visualization.data.group(
    data,

    // group by column
    [{
      column: 0,
      label: 'Month',
      modifier: function (val) {
        return formatDate.formatValue(val);
      },
      type: 'string'
    }],

    // agg columns
    [{
      aggregation: google.visualization.data.count,
      column: 1,
      label: 'Tag Count',
      type: 'number'
    }]
  );

  var view = new google.visualization.DataView(dataGroup);
  view.setRows([2, 1, 0]);

  var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
  chart.draw(view);

  var table = new google.visualization.Table(document.getElementById('table_div'));
  table.draw(view);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
<div id="table_div"></div>

编辑2

使用ChartWrapper,将view设置为dataTable属性,例如

var table = new google.visualization.ChartWrapper({
  chartType: 'Table',
  containerId: 'table_div',
  dataTable: view
});

请参阅以下工作代码段...

google.charts.load('current', {
  callback: drawChart,
  packages:['corechart', 'table']
});

function drawChart() {
  var data = google.visualization.arrayToDataTable([
    ['date', 'tag_id'],
    [new Date('2016-09-29 06:47:47'), '04:0f'],
    [new Date('2016-09-29 14:48:42'), '04:0f'],
    [new Date('2016-10-29 06:47:47'), '99:9n'],
    [new Date('2016-11-29 06:48:42'), '05:m8'],
  ]);

  var formatDate = new google.visualization.DateFormat({pattern: 'MMMM'});

  var dataGroup = google.visualization.data.group(
    data,

    // group by column
    [{
      column: 0,
      label: 'Month',
      modifier: function (val) {
        return formatDate.formatValue(val);
      },
      type: 'string'
    }],

    // agg columns
    [{
      aggregation: google.visualization.data.count,
      column: 1,
      label: 'Tag Count',
      type: 'number'
    }]
  );

  var view = new google.visualization.DataView(dataGroup);
  view.setRows([2, 1, 0]);

  var chart = new google.visualization.ChartWrapper({
    chartType: 'ColumnChart',
    containerId: 'chart_div',
    dataTable: view
  });
  chart.draw();

  var table = new google.visualization.ChartWrapper({
    chartType: 'Table',
    containerId: 'table_div',
    dataTable: view
  });
  table.draw();
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
<div id="table_div"></div>