Google Chart: Get Column Count in Dashboard for Bar Chart

纵然是瞬间 提交于 2019-12-02 03:49:45

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