Google Chart: Get Column Count in Dashboard for Bar Chart

倖福魔咒の 提交于 2019-12-09 03:44:38

问题


I have a JSON data like below to build a dashboard in google chart. I would like to draw a monthly bar chart with x-axis as scan_time (Month) and y-axis as count of tag_id's that comes into that month. The tag_ids can be duplicate.

Input data in 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",
       }
    ]

something like

Month       Count
    Sept        2
    Oct         1
    Nov         1

回答1:


once JSON is loaded into a DataTable,

use the Data Manipulation Method --> group()

which returns an aggregated DataTable

see following working snippet,
the group method converts the first column to a 'month' string,
and aggregates the count of tag id's for each 'month'

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>

EDIT

use a DataView to custom sort the rows

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

use view to draw the Table chart, and any other charts...

the following snippet includes a 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>

EDIT 2

using a ChartWrapper, set the view as the dataTable property, e.g.

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

see following working snippet...

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>


来源:https://stackoverflow.com/questions/39991944/google-chart-get-column-count-in-dashboard-for-bar-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!