PHP JSON Highcharts加载数据库结果
我卡住了 我需要用json结果创建highchart。我在这里找到了一些消息来源,但无法将其付诸实施 我能做的最接近的就是这样做: 图表选项:PHP JSON Highcharts加载数据库结果,php,jquery,database,json,highcharts,Php,Jquery,Database,Json,Highcharts,我卡住了 我需要用json结果创建highchart。我在这里找到了一些消息来源,但无法将其付诸实施 我能做的最接近的就是这样做: 图表选项: var options = { chart: { renderTo: 'tudo', defaultSeriesType: 'column', rightMargin: 80 }, title: { text: 'Weekdays' }, subti
var options = {
chart: {
renderTo: 'tudo',
defaultSeriesType: 'column',
rightMargin: 80
},
title: {
text: 'Weekdays'
},
subtitle: {
text: 'Source: somewhere in a calendar'
},
xAxis: {
labels: {
enabled: false
}
},
yAxis: [
{
min: 0,
title: {
text: 'Amount'
}
},
{
linkedTo: 0,
opposite: true
}
],
series: []
};
$.getJSON('ajax/calc.ajax.php', function(data) {
var series = [];
$.each(data, function(key, value) {
series.name = key;
series.data = value;
options.series.push(name);
});
var chart = new Highcharts.Chart(options);
});
ajax调用:
var options = {
chart: {
renderTo: 'tudo',
defaultSeriesType: 'column',
rightMargin: 80
},
title: {
text: 'Weekdays'
},
subtitle: {
text: 'Source: somewhere in a calendar'
},
xAxis: {
labels: {
enabled: false
}
},
yAxis: [
{
min: 0,
title: {
text: 'Amount'
}
},
{
linkedTo: 0,
opposite: true
}
],
series: []
};
$.getJSON('ajax/calc.ajax.php', function(data) {
var series = [];
$.each(data, function(key, value) {
series.name = key;
series.data = value;
options.series.push(name);
});
var chart = new Highcharts.Chart(options);
});
highchart加载正常,并用系列1、系列2….
但没有图形,他保持空白。(类似这样的内容:)
想知道我是不是错过了什么或什么
谢谢
更新:我更改了sql,现在我正在处理两个字段,并且有了这个,grafic工作非常完美,现在我只想知道这样做是否合适。
header('Content-Type: application/json');
while(!$res->EOF){
//$json = array("group" => "Web", "action" => "list");
$json[$res->fields["DESMAR"]] = array(intval($res->fields["QTD"]));
//$json["users"] = array(array("name" => "JulioGreff", "status" => "online"));
$res->MoveNext();
}
echo json_encode($json);
问题似乎出在PHP代码中。Highcharts预计接下来会有一系列的结果。在您的情况下,事情之所以会成功(部分原因是
name
是它所寻找的字段之一。但是,数据需要采用以下三种格式之一:
- y值数组(例如
)[0,1,2]
- 数组的数组(x,y值;例如
)[[0,0],[1,1],[2,2]]
- 使用
var series = [];
series.name = "series1";
series.data = {y: 10}; //very minimalistic example of the object format
options.series.push(name);
要使代码正常工作,可能需要更改$的内部。每个函数如下所示:
$.each(data, function(key, value) {
series.name = key;
series.data = {
y: value.property_you_are_interested_in
};
options.series.push(name);
});
…当然,如果您想使用其他形式之一,也很容易:
//First form (array of y values)
var series = {};
series.data = [];
$.each(data, function(key, value) {
series.name = key;
series.data.push(value.property_you_are_interested_in);
});
options.series.push(series);
//Second form (array of x, y values)
var series = {};
series.data = [];
$.each(data, function(key, value) {
series.name = key;
series.data.push([value.X_property_you_are_interested_in, value.Y_property_you_are_interested_in]);
});
options.series.push(series);
在您的ajax调用中-
$.getJSON('ajax/calc.ajax.php', function(data) {
var series = []; // <---------------------- must be object not array
$.each(data, function(key, value) {
series.name = key;
series.data = value;
options.series.push(name); // <-------- it should be series not name
});
var chart = new Highcharts.Chart(options);
});
没有道理
系列。数据是一个数组。因此,它可能看起来像,如下所示-
$.getJSON('ajax/calc.ajax.php', function(data) {
$.each(data, function(key, value) {
var series = {}; // <-------------------- moved and changed to object
series.name = key;
series.data = value;
options.series.push(series); // <-------- pushing series object
});
var chart = new Highcharts.Chart(options);
});
[y1, y2, y3, ....] // array of numbers (which are y values)
[[x1, y1], [x2, y2], [x3, y3], ....] // array of arrays of pair of numbers (x and y values)
// array of objects which can have x and y values as properties
[{
name: 'Point 1',
color: '#00FF00',
y: 0
}, {
name: 'Point 2',
color: '#FF00FF',
y: 5
}]
或如下-
$.getJSON('ajax/calc.ajax.php', function(data) {
$.each(data, function(key, value) {
var series = {}; // <-------------------- moved and changed to object
series.name = key;
series.data = value;
options.series.push(series); // <-------- pushing series object
});
var chart = new Highcharts.Chart(options);
});
[y1, y2, y3, ....] // array of numbers (which are y values)
[[x1, y1], [x2, y2], [x3, y3], ....] // array of arrays of pair of numbers (x and y values)
// array of objects which can have x and y values as properties
[{
name: 'Point 1',
color: '#00FF00',
y: 0
}, {
name: 'Point 2',
color: '#FF00FF',
y: 5
}]
或如下-
$.getJSON('ajax/calc.ajax.php', function(data) {
$.each(data, function(key, value) {
var series = {}; // <-------------------- moved and changed to object
series.name = key;
series.data = value;
options.series.push(series); // <-------- pushing series object
});
var chart = new Highcharts.Chart(options);
});
[y1, y2, y3, ....] // array of numbers (which are y values)
[[x1, y1], [x2, y2], [x3, y3], ....] // array of arrays of pair of numbers (x and y values)
// array of objects which can have x and y values as properties
[{
name: 'Point 1',
color: '#00FF00',
y: 0
}, {
name: 'Point 2',
color: '#FF00FF',
y: 5
}]
因此,请确保您的PHP代码生成一个与上述其中一个数组匹配的JSON,然后在$中生成series.data=value
。每个
都可以工作
更新:
对不起,我会Java,但从来没有做过PHP,所以在PHP方面帮不了你多少忙。但是,您可以尝试将以下内容作为您的PHP,看看图表是否显示出来
header('Content-Type: application/json');
$return_data = array(
array(array(10, 20), array(20, 30), array(56, 30), array(50, 20)),
array(array(10, 0), array(20, 10), array(56, 100), array(50, 40))
);
echo json_encode($return_data);
更新:在保持PHP不变的情况下呈现饼图
$.getJSON('ajax/calc.ajax.php', function(data) {
var series = { // <-------------------- create just one series object
type: 'pie',
data: [] //data array for new series
};
$.each(data, function(key, value) {
series.data.push([key, value[0]]);
});
options.series.push(series); // <-------- pushing series object
var chart = new Highcharts.Chart(options);
});
$.getJSON('ajax/calc.ajax.php',函数(数据){
var series={//ajax调用中返回的数据是什么样子的?不知道这是否是您想要的,但是通过firebug={“nome”:“TRANSFORMADOR 250VA 0-230-380V\/0,24V-0-48V”,“modelo”:“TRANSFORMADOR”,“marca”:“SEIT”,“valor”:“318.87 | 542.08”,“qtdade”:“0”}编辑一些值:{“nome”:“TRANSFORMADOR 250VA 0-230-380V\/0,24V-0-48V”,“valor:“318.87 | 542.08”,“qtdade:“0”}
,这是正确的返回注意:我只显示一个db结果,但正确的是返回多行。只是一个注释,我不知道php是否有权修改问题(php部分)只显示必要的。你能给我看一个php示例吗?系列正在运行。我在这里尝试一些东西,我将php更改为这个,yAxis出现了,但我不理解这个过程:$return\u data=array('valor'=>'15'));
是的,同时我在这里做了一些工作,将更新主要问题,看一看并告诉我它是否好!:)-编辑:检查update@ric_bfa:能否显示新PHP生成的最终json?