简体   繁体   English

在Google图表中显示没有值的日期

[英]Showing dates where there are no values in Google Charts

I've got the below script & it's working perfectly. 我有以下脚本,它运行正常。

However, it may be the case that on some days there are no orders. 但是,有时候可能没有订单。 In this case, the date should still show, but the value should be zero. 在这种情况下,日期仍应显示,但该值应为零。

在此处输入图片说明

Like in the above, it jumps from 06-19 to 06-21. 像上面一样,它从06-19跳到06-21。

Is there a way to still show 06-20 and just have the value as zero? 有没有办法仍然显示06-20并将其值设为零? The missing date doesn't exist in the database as a record is only created when an expense is submitted, so I'm a bit lost. 缺少日期在数据库中不存在,因为仅在提交费用时才创建记录,所以我有点迷失了。

Thank you in advance 先感谢您

Head Script 标题脚本

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
 <script type="text/javascript">
 google.load("visualization", "1", {packages:["corechart"]});
 google.setOnLoadCallback(drawChart);
 function drawChart() {
 var data = google.visualization.arrayToDataTable([

 ['Date', 'Total Orders'],
 ['2017-9-6',200],['2017-8-6',1500],['2017-7-7',800],['2017-7-3',1,800],['2017-7-2',200],['2017-6-13',10000],['2017-10-5',800],['2017-10-12',4,500],['',], 
 ]);

 var options = {
 title: 'Orders Per Day'
 };
 var chart = new google.visualization.ColumnChart(document.getElementById("columnchart"));
 chart.draw(data, options);
 }
 </script>

Body Script 正文

<h3>Column Chart</h3>
 <div id="columnchart" style="width: 900px; height: 500px;"></div>

To address this issue you would need to define that your axes are continuous. 要解决此问题,您需要定义轴是连续的。 This can be achieved by defining the data type for your chart columns. 这可以通过定义图表列的数据类型来实现。

Below is an example that uses different data to your problem to make it easier to see the solution. 下面是一个使用与您的问题不同的数据的示例,以使查看解决方案变得更加容易。 The string dates in the array need to be converted to a date object to meet the data type criteria. 需要将数组中的字符串日期转换为日期对象,以满足数据类型条件。 This is achieved by using the new Date() object. 这是通过使用new Date()对象实现的。

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script>
google.charts.load('current', {packages: ['corechart', 'bar']});
google.charts.setOnLoadCallback(drawChart);

function drawChart() {
  var data = new google.visualization.DataTable();
  data.addColumn('date', 'Date');
  data.addColumn('number', 'Total Orders');

  data.addRows([
     [new Date('2017-9-6'),200],
     [new Date('2017-7-7'),800],
     [new Date('2017-7-3'),800],
     [new Date('2017-7-2'),200],
     [new Date('2017-6-13'),300]
 ]);

 var options = {
     title: 'Orders Per Day'
 };

 var chart = new 
   google.visualization.ColumnChart(document.getElementById('columnchart'));
 chart.draw(data, options);
}
</script>

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

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