gpt4 book ai didi

javascript - 在莫里斯图表中正确显示 JSON 结果

转载 作者:行者123 更新时间:2023-11-29 19:58:13 26 4
gpt4 key购买 nike

我使用以下代码在 Morris Chart 中显示 MySQL 数据。一切都很好,除非我在同一天有两条或更多记录,在莫里斯图表中仅显示为一条。

PHP 文件

$mysqli = mysqli_connect($hostname_membership, $username_membership, $password_membership, $database_membership);

if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT id FROM details WHERE MemberStatus = 'Active' AND PaymentStatus NOT REGEXP 'Not'";
$result = mysqli_query($mysqli,$query);
$total_rows = $result->num_rows;

$array = array();

foreach($mysqli->query('SELECT DateAdded, COUNT(*) FROM details GROUP BY DateAdded') as $row) {
$year = date("Y-m-d",strtotime($row['DateAdded']));
array_push($array,array('Year'=>$year, 'Numb'=>$row['COUNT(*)'],'Total'=>$total_rows));
}

echo json_encode($array);

$mysqli->close();

莫里斯·JS

$.getJSON("js/morris.php", function (json) { 
Morris.Area({
element: 'morris-area-chart',
data: json,
xkey: 'Year',
xLabelFormat: function (x) {
var IndexToMonth = [ "Jan", "Feb", "Mar", "Apr", "Mar", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
var month = IndexToMonth[ x.getMonth() ];
var year = x.getFullYear();
return month + ' ' + year;
},
xLabels: 'month',
ykeys: ['Numb','Total'],
labels: ['Signups / Renewals','Total Active Members'],
pointSize: 2,
hideHover: 'auto',
resize: true,
dateFormat: function (x) {
var d = new Date(x);
var MyDateString;
d.setDate(d.getDate());
var monthNames = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
return ("0" + d.getDate()).slice(-2) +' '+monthNames[d.getMonth()]+' '+d.getFullYear();
}
});
return false;
});

我的 JSON 输出是

[{"Year":"2016-10-27","Numb":"1","Total":11},{"Year":"2016-10-28","Numb":"1","Total":11},{"Year":"2016-10-31","Numb":"1","Total":11},{"Year":"2016-11-02","Numb":"1","Total":11},{"Year":"2016-11-05","Numb":"1","Total":11},{"Year":"2016-11-07","Numb":"1","Total":11},{"Year":"2016-11-08","Numb":"1","Total":11},{"Year":"2016-11-09","Numb":"1","Total":11},{"Year":"2016-11-10","Numb":"1","Total":11},{"Year":"2016-11-10","Numb":"1","Total":11},{"Year":"2016-11-16","Numb":"1","Total":11}]

从上面可以看到,JSON 输出有两条相同的记录

{"Year":"2016-11-10","Numb":"1","Total":11},{"Year":"2016-11-10","Numb":"1","Total":11}

这就是莫里斯图表上仅显示的一条记录。在本例中,2016 年 11 月 10 日只有一个人注册,而实际上是两个人...

仅供引用 - TOTAL 就是 - 注册总数。虽然我喜欢将其作为运行总数,但我也无法计算出来。所以我不用担心,除非有一个简单的解决方案。

我错过了什么?

最佳答案

已经想出了一个解决方案 - 似乎有效 - 到目前为止......

已替换

SELECT DateAdded, COUNT(*) FROM details GROUP BY DateAdded

SELECT DateAdded, COUNT(*) FROM details GROUP BY DATE_FORMAT(DateAdded,"%Y-%m-%d")

现在正在计算运行总计..​​.

关于javascript - 在莫里斯图表中正确显示 JSON 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40621672/

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