我正在尝试将多个系列绘制到flotchart中。我成功获得了1个标签,但我不得不用PHP获得更多标签并编码为json。
$connection = mysql_connect($server,$user,$password);
$db = mysql_select_db($database,$connection);
$query = "SELECT SC,SR FROM high";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
$int = $row['SR'];
$join = intval($int);
$int2 = $row['SC'];
$join2 = intval($int2);
$dataset1[] = array($join2,$join);
}
$final = json_encode($dataset1);
echo $final;
RESULT
[[1,3],[2,20],[3,30],[4,10],[5,4],[6,40],[7,67],[8,100],[9,5],[10,11]]
将JSON解析为Javascript
$.ajax({
dataType:'json', /*to avoid calling JSON.parse(data) in your callback function*/
url: 'chart-data.php',
success: function (data) {
console.log(data);//as mentioned in comments
//1.either call plot again
/*calling plot as seen in your code - start*/
var d1 = data;/* JSON.parse(data) */
$.plot($("#placeholder"),
[{
label: "SCR",
data: d1,
},
/*{
label: "SCR",
data: d1,
},*/
],
FOKUS ON {label:“SCR”,数据:d1,},
问题:如何将JSON数据解析为JS,如:
[ { label: "Foo", data: [ [10, 1], [17, -14], [30, 5] ] },
{ label: "Bar", data: [ [11, 13], [19, 11], [30, -7] ] }
]
我只是想使用数组:
$s= array('label'=> "aaaa",
'data'=> $final);
print_r($s);
并获得结果
Array ( [label] => aaaa [data] => [[1,3],[2,20],[3,30],[4,10],[5,4],[6,40],[7,67],[8,100],[9,5],[10,11]] )
答案 0 :(得分:1)
在使用PHP编码之前创建数组:
$dataset = array("label" => "FOO/BAR", "data" => array(/* Your `data` array */));
并将所有label/data
值附加到dataset
变量,最后对其进行编码和回显