2015-04-27 30 views
1

我試圖通過jQuery ajax調用將一個php調用json傳遞給一個highcharts對象。如何將json發送到高級圖像從php

下面是PHP腳本

[{"name":"Positive","data":"[1426896000000,0.5,1],[1427760000000,0.333333333333,1],[1427846400000,0.333333333333,1],[1427932800000,0.353553390593,1],[1428278400000,0.408248290464,1],[1428364800000,0.301511344578,1],[1428451200000,0.377964473009,1],[1428537600000,0.686886723927,2],[1428624000000,2.38658259877,7],[1428710400000,0.4472135955,1],[1428883200000,0.333333333333,1],[1429142400000,0.333333333333,1],[1429574400000,0.316227766017,1],[1429747200000,1.10948233661,2],[1429833600000,0.408248290464,1],[1429920000000,1.34375333838,3],[1430092800000,1.13976615407,3]"},{"name":"Negative","data":"[1427673600000,-0.353553390593,1],[1428105600000,-0.353553390593,1],[1428278400000,-1.0850712542,3],[1428537600000,-1.20901527656,3],[1428624000000,-0.377964473009,1],[1428883200000,-0.353553390593,1],[1429056000000,-0.408248290464,1],[1429574400000,-0.377964473009,1],[1429660800000,-0.353553390593,1],[1429747200000,-1,3],[1429833600000,-1.02022005726,3],[1429920000000,-0.755928946018,2],[1430006400000,-0.632455532034,1]"}] 

我用下面的調用getChart返回:

function getChart(searchstring){ 
$.ajax({ 
    type: "GET", 
    url: "searchString.php", 
    dataType: "json", 
    data: {name:searchstring}, 
    success: function(news) { 
     renderPosNegChart(news); 
    } 
    }); 
} 
function renderPosNegChart(data){ 
var newschart = { 
    chart: { 
     type: 'spline' 
    }, 
    title: { 
     text: "Test" 
    }, 
    xAxis: { 
     type: "datetime" 
    }, 
    yAxis: { 
    }, 
    series: [data[0]] 
} 
$("#newschart").highcharts(newschart); 

}

不幸的是,它顯示的是: enter image description here 最終,我希望它顯示「正面」和「負面」兩條線。我已經在網上關注了一些教程,但他們沒有幫助。

如果我能澄清任何事情,請告訴我。

回答

2

每個系列數據應該是數組的數組而不是字符串。

例子:

[{"name":"Positive","data":[[1426896000000,0.5,1], ... ,[1430006400000,-0.632455532034,1]]}] 
+0

您可以添加有關如何從傳遞給json_encode(data,JSON_NUMERIC_CHECK)的mysql調用構建上述對象的任何見解?假設您有一系列標記爲'x','y','z'(其中x是時間戳)的行?理想情況下,我想在PHP方面做到這一點。 – Optimus

+0

我發現了幾個問題,這是由於我的php腳本中對json對象的理解不夠。我發佈了最終結果供其他人蔘考,但我真的很感謝你的幫助。 – Optimus

0

它看起來像你的JSON是錯誤的

其失蹤[和]數據

應該

[{"name":"Positive","data":"[[1426896000000,0.5,1],...[1430006400000,-0.632455532034,1]]"}] 

然後

var newschart = { 
    chart: { 
     type: 'spline' 
    }, 
    title: { 
     text: "Test" 
    }, 
    xAxis: { 
     type: "datetime" 
    }, 
    yAxis: { 
    }, 
    series: data // or JSON.parse(data) 
} 
$("#newschart").highcharts(newschart); 

入住這JSFiddle

+0

謝謝,但簡單地添加括號並不能解決問題。翻譯中丟失了一些東西。 – Optimus

+1

請參閱Kacper的回答 –

1

使用卡茨珀的方向,我解決了一些問題,但我認爲這是值得張貼我的最終答案。從本質上講,我已經將每個數據系列的數組轉換爲單個字符串,而不是讓json_encode這樣做。

// format as array for json conversion 
$positive = array(); 
$positive['name'] = "Positive"; 
foreach ($positive_news as $key => $value){ 
    $ts = $key * 1000 - $adjustment; // adjusting for certain timezone 
    //$positive['data'][] = "[" . $ts . "," . $value['polarity']. "," . $value['count'] . "]"; // What I had previous 
    $positive['data'][] = array($ts, $value['polarity'], $value['count']); // What I changed it to 
} 
$negative = array(); 
$negative['name'] = "Negative"; 
foreach ($negative_news as $key => $value){ 
    $ts = $key * 1000 - $adjustment; 
    //$negative['data'][] = "[" . $keyadj . "," . $value['polarity'] . "," . $value['count'] . "]"; // What I had previously 
    $negative['data'][] = array($ts, $value['polarity'], $value['count']); // What I changed 
} 

// I also imploded the arrays to strings here (I removed that after reading Kacper's suggesting) 

$news = array(); 
array_push($news, $positive); 
array_push($news, $negative); 

echo json_encode($news, JSON_NUMERIC_CHECK); // this is returned to javascript via ajax and sent directly to the below function 

function renderPosNegChart(data){ 
var newschart = { 
    chart: { 
     type: 'spline' 
    }, 
    title: { 
     text: "Test" 
    }, 
    xAxis: { 
     type: "datetime" 
    }, 
    yAxis: { 
    }, 
    series: [data[0]] 
} 
$("#newschart").highcharts(newschart); 

我希望這可以幫助別人解決這個問題。