PHP JSON Highcharts加载数据库结果


PHP JSON Highcharts load database result

卡住了!

我需要创建json结果highchart。我在这里找到了一些资源,但无法将其应用于工作。

我能得到的最接近的是这样做:

图表选项:

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: []
};

ajax调用:

$.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加载ok,用Series 1, Series 2 ....

填充序列

但没有图形,他保持空白。(类似于:Demo).

想知道我是否错过了什么或一切。

感谢

更新:我改变了sql,现在我正在使用2个字段,并且有了这个,图形工作完美,现在我只是想知道如果这样做它的ok。

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);

中的 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);  
});

另外,考虑到您正在接收的JSON -

{"省":"TRANSFORMADOR 250 va 0 - 230 - 380 v/0, 24 v-0-48v","莫德罗":"TRANSFORMADOR",《马卡报》:"SEIT"、"英勇":"318.87 | 542.08","qtdade":"0"? ?}

你在$.each做什么-

series.data = value;

没有意义

系列。data是一个数组。因此,它可以看起来像或者,如下所示-

[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与上述数组之一匹配,然后$.each中的series.data = value将工作。

更新:对不起,我学过Java,从来没有学过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);  
});

应该画一个饼状图

看起来问题出在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函数的内部更改为如下内容:

$.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);