하이 차트를 사용하여 basic column chart을 만들어야합니다.올바른 데이터 형식을 SQL에서 PHP로 전달하는 방법
$query = "SELECT campaign_id, kpi, label, val FROM table WHERE id = $1";
$result = pg_prepare($db, "report", $query);
$result = pg_execute($db, "report", array($campaignID));
while ($row = pg_fetch_array($result, NULL, PGSQL_ASSOC))
{
$response['xlabels'][] = $row['label'];
$response['xdata'][] = (float)$row['val'];
}
pg_free_result($result);
pg_close($db);
echo json_encode($response);
PostgreSQL의 쿼리 반환 데이터를 다음과 같이 (CAMPAIGN_ID = 5, 예를 들어) : 여기 내 PHP 코드의
id | kpi | label | val
----+-----------+-------+------
5 | Voter | East | 0.18
5 | Non-Voter | East | 0.12
5 | Voter | West | 0.82
5 | Non-Voter | West | 0.88
내 API는 저장 xlabels
및 xdata
xAxis.categories
및 series
:
$.get('vote_api.php', {'id' : id}, function(data) {
options.chart.type = 'column';
options.xAxis.categories = data.xlabels;
options.series = data.xdata;
chart = new Highcharts.Chart(options);
}, "json");
kpi
(Voter vs. Non-Voter)에 대한 올바른 데이터 형식은 내 시리즈이며,내 x 축이 될 (JSFiddle 참조)한다로 (서부 대 동부) :
[xlabels] => Array
(
[0] => East
[1] => East
[2] => West
[3] => West
)
[xdata] => Array
(
[0] => 0.18
[1] => 0.12
[2] => 0.82
[3] => 0.88
)
것은 어떤 하나의 방법 몇 가지 힌트를 줄 것이다 :
xAxis.categories = ['East', 'West'];
series = [{name: 'Voter',
data: [0.18, 0.82]},
{name: 'Non-Voter',
data: [0.12, 0.88]}]
그러나 내 while loop
부분 $response['xlabels'][] = $row['label']; $response['xdata'][] = (float)$row['val'];
나에게 이런 식으로 뭔가를 줄 이것을하기 위해? 나는 정말로 그것을 바르게 평가할 것이다!!
왜 downvote ??? – Rock