简体   繁体   English

如何基于Month对Json数据进行分组,并使用谷歌图表绘制它

[英]How to group Json data based on Month and plot it using google chart

I am using google chart plugin to plot a area chart,by using JSON data as show below code, which contains value and a date by the chart should be printed based on month. 我使用谷歌图表插件绘制区域图表,通过使用JSON数据显示如下代码,其中包含值和图表的日期应根据月份打印。

Link show in image enter image description here 链接显示在图像中输入图像描述

how to plot the chart based on month using google chart 如何使用谷歌图表绘制基于月份的图表

is it possible to do it with google charts buit in features and need to write some customized javascript for that achive? 是否可以使用谷歌图表buit功能,并需要为该achive编写一些自定义的JavaScript?

 <script type="text/javascript"> google.charts.load('current', { 'packages': ['corechart'] }); google.charts.setOnLoadCallback(drawChart); function drawChart() { var data = google.visualization.arrayToDataTable([ ['Date', 'value'], [ "1/Jan/2017", 28 ], [ "5/Feb/2019", 174 ], [ "8/Mar/2017", 150 ], [ "2/Apr/2019", 174 ], [ "18/May/2019", 100 ], [ "22/Jun/2019", 5 ], [ "30/Jul/2019", 178 ], [ "28/Aug/2019", 59 ], [ "1/Sep/2019", 87 ], [ "10/Oct/2019", 50 ], [ "15/Nov/2019", 123 ], [ "20/Dec/2019", 108 ] ]); var options = { title: 'Company Performance', hAxis: { title: 'Date', titleTextStyle: { color: '#333' } }, curveType: 'function', legend: { position: 'bottom' }, pointSize: 5, dataOpacity: 0.5, vAxis: { minValue: 0 } }; var chart = new google.visualization.AreaChart(document.getElementById('chart_div')); chart.draw(data, options); } </script> 
 <div id="chart_div" style="width: 100%; height: 500px;"></div> 

in order to format the x-axis as a date, 为了将x轴格式化为日期,
need to convert the first column in the data table from a string to an actual date... 需要将数据表中的第一列从字符串转换为实际日期...

we can use a data view with a calculated column to convert the date... 我们可以使用带有计算列的数据视图来转换日期...

var view = new google.visualization.DataView(data);
view.setColumns([{
  type: 'date',
  label: 'Date',
  calc: function (dt, row) {
    return new Date(dt.getValue(row, 0));
  }
}, 1]);

also, since the data is not in order by date, we can convert the view back to a data table and sort it... 此外,由于数据不按日期排序,我们可以将视图转换回数据表并对其进行排序......

data = view.toDataTable();
data.sort([{column: 0}]);

see following working snippet... 请参阅以下工作代码段...

 google.charts.load('current', {'packages':['corechart']}); google.charts.setOnLoadCallback(drawChart); function drawChart() { var data = google.visualization.arrayToDataTable([ ['Date', 'value'], [ "1/Jan/2017", 28 ], [ "5/Feb/2019", 174 ], [ "8/Mar/2017", 150 ], [ "2/Apr/2019", 174 ], [ "18/May/2019", 100 ], [ "22/Jun/2019", 5 ], [ "30/Jul/2019", 178 ], [ "28/Aug/2019", 59 ], [ "1/Sep/2019", 87 ], [ "10/Oct/2019", 50 ], [ "15/Nov/2019", 123 ], [ "20/Dec/2019", 108 ] ]); var view = new google.visualization.DataView(data); view.setColumns([{ type: 'date', label: 'Date', calc: function (dt, row) { return new Date(dt.getValue(row, 0)); } }, 1]); data = view.toDataTable(); data.sort([{column: 0}]); var options = { chartArea: {bottom: 56}, title: 'Company Performance', hAxis: {format: 'MMM', title: 'Date', titleTextStyle: {color: '#333'} }, curveType: 'function', legend: { position: 'bottom'}, pointSize: 5, dataOpacity: 0.5, vAxis: {minValue: 0} }; var chart = new google.visualization.AreaChart(document.getElementById('chart_div')); chart.draw(data, options); } 
 <script src="https://www.gstatic.com/charts/loader.js"></script> <div id="chart_div"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM