Php Highcharts-显示所有日期,即使没有值
我使用PHP和MySQL获取所选时间段内每天的订单总量,然后使用PHP将它们保存在*.csv文件中。我从csv文件生成图表,一切正常,但我需要包括没有订单的天数。*。csv文件内容类似于:Php Highcharts-显示所有日期,即使没有值,php,jquery,highcharts,Php,Jquery,Highcharts,我使用PHP和MySQL获取所选时间段内每天的订单总量,然后使用PHP将它们保存在*.csv文件中。我从csv文件生成图表,一切正常,但我需要包括没有订单的天数。*。csv文件内容类似于: 1,05-01 1,05-02 2,05-04 1,05-06 因此,在图表上,它们显示为1张5月1日的订单,1张5月2日的订单,然后跳过5月3日的订单-我需要在图表上显示值为0的订单。可以使用jQuery/highcharts(我对javascript没有太多经验)来完成吗?或者我应该编辑PHP来创建具有
1,05-01
1,05-02
2,05-04
1,05-06
因此,在图表上,它们显示为1张5月1日的订单,1张5月2日的订单,然后跳过5月3日的订单-我需要在图表上显示值为0的订单。可以使用jQuery/highcharts(我对javascript没有太多经验)来完成吗?或者我应该编辑PHP来创建具有这些值的文件,如下所示:
1,05-01
1,05-02
0,05-03
2,05-04
0,05-05
1,05-06
$result2 = mysqli_query($con,"$query2");
$fp = fopen('data/temp.csv', 'w');
$dayCompare=date("n-d", strtotime($theDay)); //I know which day is the first one selected so I save it as variable #theDay
while ($row = mysqli_fetch_array($result2)) {
if ($row['Date'] !== $dayCompare){
while ($row['Date'] !== $dayCompare){ //This "while" keeps on looping till the date from database matches the day
fputcsv($fp, array('0,'.$dayCompare));
$theDay= date("Y/m/d", strtotime($theDay . "+1 day"));
$dayCompare=date("n-d", strtotime($theDay));
}
} //Rest of the code is to be executed when dates match
fputcsv($fp, array($row['Orders'].",".$row['Date']));
$theDay= date("Y/m/d", strtotime($theDay . "+1 day"));
$dayCompare=date("n-d", strtotime($theDay));
}
for ($theDay; $theDay <= $lastDay; strtotime($theDay . "+1 day")) { //I added a "for" loop to fill in the empty days if the day of last order is earlier than the last day selected/current day.
fputcsv($fp, array('0,'.$dayCompare));
$theDay= date("Y/m/d", strtotime($theDay . "+1 day"));
$dayCompare=date("n-d", strtotime($theDay));
}
我使用以下PHP代码保存文件:
$result = mysqli_query($con,"$query");
$fp = fopen('data/temp.csv', 'w');
if ($fp && $result) {
while ($row = $result->fetch_array(MYSQLI_NUM)) {
fputcsv($fp, array_values($row));
}
}
和以下脚本生成图表:
var myCategories = [];
var myData = [];
var myChart;
$(document).ready(function() {
var options = {
chart: {
renderTo: 'chart-container',
defaultSeriesType: 'column'
},
title: {
text: 'Orders received in selected days'
},
xAxis: {
title: {text: 'Date'},
categories: []
},
yAxis: {
title: {
text: 'Orders'
}
},
series: []
};
$.get('data/temp.csv', function(data) {
var lines = data.split('\n').slice(0, -1); //generated csv file has \n after last entry so there is always an empty line I have to delete
$.each(lines, function(lineNo, line) {
var items = line.split(',');
myCategories.push(items[1]);
myData.push(parseInt(items[0]));
});
options.xAxis.categories = myCategories;
options.series = [{ data: myData }];
myChart = new Highcharts.Chart(options);
});
});
我找到的唯一解决方案是在MySQL端创建一些疯狂的查询(我倾向于在PHP中处理该逻辑),似乎HighCharts无法做到这一点,所以最后我稍微修改了我的PHP代码,添加了一些额外的循环。如果有人感兴趣,现在看起来是这样的:
1,05-01
1,05-02
0,05-03
2,05-04
0,05-05
1,05-06
$result2 = mysqli_query($con,"$query2");
$fp = fopen('data/temp.csv', 'w');
$dayCompare=date("n-d", strtotime($theDay)); //I know which day is the first one selected so I save it as variable #theDay
while ($row = mysqli_fetch_array($result2)) {
if ($row['Date'] !== $dayCompare){
while ($row['Date'] !== $dayCompare){ //This "while" keeps on looping till the date from database matches the day
fputcsv($fp, array('0,'.$dayCompare));
$theDay= date("Y/m/d", strtotime($theDay . "+1 day"));
$dayCompare=date("n-d", strtotime($theDay));
}
} //Rest of the code is to be executed when dates match
fputcsv($fp, array($row['Orders'].",".$row['Date']));
$theDay= date("Y/m/d", strtotime($theDay . "+1 day"));
$dayCompare=date("n-d", strtotime($theDay));
}
for ($theDay; $theDay <= $lastDay; strtotime($theDay . "+1 day")) { //I added a "for" loop to fill in the empty days if the day of last order is earlier than the last day selected/current day.
fputcsv($fp, array('0,'.$dayCompare));
$theDay= date("Y/m/d", strtotime($theDay . "+1 day"));
$dayCompare=date("n-d", strtotime($theDay));
}
现在它给我的唯一问题是数据保存在带有引号的*.csv文件中,但我通过修改*.js文件解决了这个问题,我的脚本更改了这一部分:
$.each(lines, function(lineNo, line) {
var items = line.split(',');
myCategories.push(items[1]);
myData.push(parseInt(items[0]));
});
为此:
$.each(lines, function(lineNo, line) {
var items = line.split(',');
myCategories.push(items[1].replace(/"/g,""));
myData.push(parseInt(items[0].replace(/"/g,"")));
});
由于修剪了引号,输出与预期一致。带日期的变量仍然是非常详细的,但它似乎是这样的,因为PHP在做数学方程时识别短日期有问题,我也希望有一天修改这个三重循环,但这至少要等到下一次部署
最后,我又添加了一个循环,以在最后一天选择的日期晚于最后一个订单日期的空日中循环,例如,如果选择了5月9日,最后一个订单是在7日,那么第8和第9天将有2行添加0值。当然,添加0值,如
0,05-03
将是一个好主意。我认为这会奏效,而且会是更容易的方法。