使用 MySqli 和 Json 的 Highcharts

问题描述 投票:0回答:1

我正在尝试编写一个生成折线图(Highcharts)的脚本。我有一个包含许多列的数据表:

差、一般、好、优秀和未使用

每列可以包含 1(差)、2(一般)、3(好)、4(优秀)或 5(未使用)。

我返回数据的 SQL 查询是:

$query_Excellent = "
SELECT COUNT(RecordID) AS Score4, FeedBackDate  
FROM ".$FeedBack." 
WHERE FeedBackDate BETWEEN '". $Sdate  . "' AND '". $Edate . "'
AND OverallSatisfaction = 4  OR CheckIn = 4 GROUP BY MONTH(FeedBackDate)";
$Excellent =$conn->query($query_Excellent );

$rows_Excellent  = array();
$rows_Excellent ['name'] = 'Excellent';
while($row_Excellent  = mysqli_fetch_array($Excellent )) {
    $rows_Excellent ['data'][] = $row_Excellent ['Score4'];
}


$query_Good = "
SELECT COUNT(RecordID) AS Score3, FeedBackDate  
FROM ".$FeedBack." 
WHERE FeedBackDate BETWEEN '". $Sdate  . "' AND '". $Edate . "'
AND OverallSatisfaction = 3  OR CheckIn = 3  GROUP BY MONTH(FeedBackDate)";
$Good=$conn->query($query_Good);

$rows_Good = array();
$rows_Good['name'] = 'Good';
while($row_Good = mysqli_fetch_array($Good)) {
    $rows_Good['data'][] = $row_Good['Score3'];
}

$query_Average = "
SELECT COUNT(RecordID) AS Score2, FeedBackDate  
FROM ".$FeedBack." 
WHERE FeedBackDate BETWEEN '". $Sdate  . "' AND '". $Edate . "'
AND OverallSatisfaction = 2   OR CheckIn = 2 GROUP BY MONTH(FeedBackDate)";
$Average=$conn->query($query_Average);

$rows_Average = array();
$rows_Average['name'] = 'Average';
while($row_Average = mysqli_fetch_array($Average)) {
    $rows_Average['data'][] = $row_Average['Score2'];
}


$query_Poor = "
SELECT COUNT(RecordID) AS Score1, FeedBackDate  
FROM ".$FeedBack." 
WHERE FeedBackDate BETWEEN '". $Sdate  . "' AND '". $Edate . "'
AND OverallSatisfaction = 1 OR CheckIn = 1 GROUP BY MONTH(FeedBackDate)";
$Poor=$conn->query($query_Poor);

$rows_Poor = array();
$rows_Poor['name'] = 'Poor';
while($row_Poor = mysqli_fetch_array($Poor)) {
    $rows_Poor['data'][] = $row_Poor['Score1'];
}


$query_NotUsed = "
SELECT COUNT(RecordID) AS Score5, FeedBackDate  
FROM ".$FeedBack." 
WHERE FeedBackDate BETWEEN '". $Sdate  . "' AND '". $Edate . "'
AND OverallSatisfaction = 5  OR CheckIn = 5 GROUP BY MONTH(FeedBackDate)";
$NotUsed=$conn->query($query_NotUsed);

$rows_NotUsed = array();
$rows_NotUsed['name'] = 'Not used';
while($row_NotUsed = mysqli_fetch_array($NotUsed)) {
    $rows_NotUsed['data'][] = $row_NotUsed['Score5'];
}





$result = array();

array_push($result,$rows_NotUsed);
array_push($result,$rows_Excellent);
array_push($result,$rows_Good);
array_push($result,$rows_Average);
array_push($result,$rows_Poor);



print json_encode($result, JSON_NUMERIC_CHECK);

JSON格式的结果是: [{"name":"未使用","data":[1,1,1]},{"name":"优秀","data":[3]},{"name":"良好" ,"data":[1,3,1]},{"name":"平均","data":[1,1]},{"name":"差","data":[1, 1,1]}]

我需要的是一种在图表(Highcharts)中显示查询结果的方法,我尝试编写脚本但结果不正确。

$(function () {
var chart;
$(document).ready(function() {
$.getJSON("../charts/1-2-4-overall_year_chart.php", function(json) {

chart = new Highcharts.Chart({
chart: {
renderTo: 'container1',
type: 'line',
marginRight: 130,
marginBottom: 25
},
title: {
text: 'Monthly trend',
x: -20 //center
},
subtitle: {
text: '',
x: -20
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
title: {
text: 'Percentage %'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
this.x +': '+ this.y;
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 0
},
series: json
});
});

});

});

我正在尝试使图表具有一个图表,以按月份分组的百分比形式显示差、平均、良好、优秀和未用作 x 轴上水平线的图表。 我尝试过创建一个 JSFiddle,但即使这样我也无法让它工作。

My chart display

JSFiddle

任何人都可以抽出时间来帮助我吗?

php highcharts mysqli
1个回答
1
投票

每个系列中的值都是按照顺序绘制的。正如我所看到的,数组的最大长度是 3,这意味着您引用从 0 到 2 的类别。如果您需要在声明值(四月)中有一个点,请使用构造 [x,y] (3,3),其中x 是类别索引。

© www.soinside.com 2019 - 2024. All rights reserved.