gpt4 book ai didi

javascript - 由于 JS 月间隔 0-11 导致的图表日期错误

转载 作者:行者123 更新时间:2023-11-29 02:56:04 25 4
gpt4 key购买 nike

该脚本按天从 mysql 中汇总下载数据并将其显示为图表,但据我了解,JS(highcharts)月份间隔是从 0 到 11,而不是 PHP 输出的 1-12,使当前月份5 月 -> 6 月(UTC 2015,05),6 月 31 日不存在,导致视觉错误同时包含 5 月 31 日和 6 月 1 日的数据,数据存在但不显示。

[Date.UTC(2015, 05, 31), 4],
[Date.UTC(2015, 06, 01), 8],

enter image description here

我正在尝试修复日期偏移量。

将日期偏移一个月将导致 php 从 4 月开始查询数据(因为 UTC month 04 = May )并且还会导致 5 月 31 日(UTC 格式 2015, 04, 31)成为 6 月 1 日(UTC 格式 2015, 05) , 01)

[Date.UTC(2015, 04, 25), 3],
[Date.UTC(2015, 04, 26), 4],
[Date.UTC(2015, 04, 27), 8],
[Date.UTC(2015, 04, 28), 9],
[Date.UTC(2015, 04, 29), 5],
[Date.UTC(2015, 04, 30), 8],
[Date.UTC(2015, 05, 01), 4],
[Date.UTC(2015, 05, 01), 8],

$date_download = date("Y, m, d", strtotime($row["date"]." -1 month"));

完整代码:

<?php
$sql_download = "SELECT date, SUM(quantity) FROM downloads GROUP BY DATE(`Date`)";

?>
<script type="text/javascript">

$('#container_downloads').highcharts({
chart: {
zoomType: 'x'
},
title: {
text: 'Unity Downloads'
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: {
day: '%e. %b. %Y'
},
minRange: 14 * 24 * 3600000 // fourteen days

},


yAxis: {
title: {
text: 'Downloads'
},
allowDecimals: false,

},
legend: {
enabled: false
},

plotOptions: {
area: {
fillColor: {
linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1},
stops: [
[0, Highcharts.getOptions().colors[0]],
[1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
]
},
marker: {
radius: 2
},
lineWidth: 1,
states: {
hover: {
lineWidth: 1
}
},
threshold: null
}
},

series: [{
type: 'area',
name: 'Downloads',
data: [
<?php


$result_download = $mysqli->query($sql_download);

while($row = $result_download->fetch_assoc()){
$quantity_download = $row["SUM(quantity)"];
$date_download = date("Y, m, d", strtotime($row["date"]));
$ans_download = "[Date.UTC(" . $date_download . "), " . $quantity_download . "]";
echo $ans_download . ",\r\n";
}

?>
]
}]
});

最佳答案

while($row = $result_download->fetch_assoc()){
$quantity_download = $row["SUM(quantity)"];
$date_str = strtotime($row["date"]);
$year = date('Y', $date_str);
$month = date('n', $date_str) - 1;
$day = date('j', $date_str);
$ans_download = "[Date.UTC(" . sprintf('%s, %s, %s',$year,$month,$day) . "), " . $quantity_download . "]";
echo $ans_download . ",\r\n";
}

关于javascript - 由于 JS 月间隔 0-11 导致的图表日期错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30578270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com