Highcharts - 解析json系列

时间:2014-01-03 19:23:29

标签: php json highcharts

我用它来绘制列高图jsfiddle 我用它来gt JSON:

<?php
    $query = mysql_query("SELECT
                                sales_raport_all.from_date,
                                sales_raport_all.to_date,
                                sales_raport_all.konto,
                                SUM(sales_raport_all.saldo_sprzedazy),
                                SUM(sales_raport_all.wartosc_kosztowa),
                                SUM(sales_raport_all.marza),
                                klienci_ax_all.sales_group,
                                klienci_ax_all.nazwa
                            FROM
                                sales_raport_all
                            INNER JOIN
                                klienci_ax_all
                            ON
                                sales_raport_all.konto=klienci_ax_all.konto_odbiorcy
                            WHERE
                                YEAR(from_date) = YEAR(CURDATE())
                            GROUP BY
                                sales_raport_all.from_date,
klienci_ax_all.sales_group
                            ORDER BY
                                sales_raport_all.from_date,
klienci_ax_all.sales_group");

$raw = array();
$dates = array();
while ($r = mysql_fetch_array($query)) {
$date = $r['from_date'];
if (!in_array($date, $dates)) $dates[] = $date;
$sales_group = $r['sales_group'];
$raw[$sales_group][$date] = intval($r['SUM(sales_raport_all.saldo_sprzedazy)']);
}
$data = array();
$data[0] = array('name' => "Date", 'data' => $dates);
foreach ($raw as $name => $d) {
$new_data = array('name' => $name, 'data' => array());
foreach ($dates as $date) {
    $new_data['data'][] = isset($d[$date]) ? $d[$date] : 0;
}
$data[] = $new_data;
}

print json_encode($data);

小提琴我用

chart3Options.series[0] = json[1];
...

有一种简单的方法来定义json中的所有数据吗?这个数据是可变的,如果我声明了11个变量,那么只有7个,那么图表就不会绘制 一个日期的JSON输出:

[{"name":"Date","data":["2014-01-01"]},{"name":"IN","data":[2580]},{"name":"KD","data":[5030]},{"name":"\u0141S","data":[12628]},{"name":"NN","data":[400]},{"name":"SG","data":[12979]},{"name":"TD","data":[15096]}]

//编辑

我创建新文件:

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highcharts Example</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function() {
            var options = {
                chart: {
                    renderTo: 'container',
                    type: 'column',
                    marginRight: 130,
                    marginBottom: 25
                },
                title: {
                    text: 'test',
                    x: -20 //center
                },
                subtitle: {
                    text: '',
                    x: -20
                },
                xAxis: {
                    categories: []
                },
                yAxis: {
                    title: {
                        text: 'test'
                    },
                    plotLines: [{
                        value: 0,
                        width: 1,
                        color: '#808080'
                    }]
                },
                tooltip: {
                    formatter: function() {
                            return '<b>'+ this.series.name +'</b><br/>'+
                            this.x +': '+ this.y;
                    }
                },
                legend: {
                    layout: 'vertical',
                    align: 'right',
                    verticalAlign: 'top',
                    x: -10,
                    y: 100,
                    borderWidth: 0
                },
                series: []
            }

            $.getJSON("test2.php", function(json) {
                options.xAxis.categories = json[0]['category'];
                options.series[0] = {};
                options.series[0].name = json[0]['name'];
                options.series[0].data = json[0]['data'];
                chart = new Highcharts.Chart(options);
            });
        });
        </script>
        <script src="http://code.highcharts.com/highcharts.js"></script>
        <script src="http://code.highcharts.com/modules/exporting.js"></script>
    </head>
    <body>
        <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
    </body>
</html>

test2.php

<?php
        $db_host        = '******';
        $db_user        = '******';
        $db_pass        = '******';
        $db_database        = '******';

        $link = mysql_connect($db_host,$db_user,$db_pass) or die('Nawiązanie połączenia z bazą danych nie było możliwe');

        mysql_select_db($db_database,$link);
        mysql_query("SET names UTF8");

        $query = mysql_query("SELECT
                                    sales_raport_all.from_date,
                                    sales_raport_all.to_date,
                                    sales_raport_all.konto,
                                    SUM(sales_raport_all.saldo_sprzedazy),
                                    SUM(sales_raport_all.wartosc_kosztowa),
                                    SUM(sales_raport_all.marza),
                                    klienci_ax_all.sales_group,
                                    klienci_ax_all.nazwa
                                FROM
                                    sales_raport_all
                                INNER JOIN
                                    klienci_ax_all
                                ON
                                    sales_raport_all.konto=klienci_ax_all.konto_odbiorcy
                                WHERE
                                    YEAR(from_date) = YEAR(CURDATE())
                                GROUP BY
                                    sales_raport_all.from_date,
    klienci_ax_all.sales_group
                                ORDER BY
                                    sales_raport_all.from_date,
    klienci_ax_all.sales_group");


$result = array();



while($r = mysql_fetch_array($query)) {
     $grupa = $r['sales_group'];
     $datetime = $r['from_date'];
     $result['name'][] = $datetime;
     $result['category'][] = $grupa;
     $result['data'][] = intval($r['SUM(sales_raport_all.saldo_sprzedazy)']);
}

$json = array();
array_push($json,$result);
print json_encode($json);

?>

JSON给我:

[{"name":["2014-01-01","2014-01-01","2014-01-01","2014-01-01","2014-01-01","2014-01-01"],"category":["IN","KD","\u0141S","NN","SG","TD"],"data":[2580,5030,12628,400,12979,15096]}]

系列看起来很棒,但我不知道如何更改类别,例如 http://jsfiddle.net/rubJS/

2 个答案:

答案 0 :(得分:1)

你可以在javascript中处理它。换句话说,不是为每个<x,y>对定义变量,而是返回数据并让javascript构建系列。 例如,给定当前输出,您可以在单独的数组中准备X和Y值(在javascript中),并编写一个函数将这些值推送到系列中。它可以这样做(使用jQuery作为例子):

function build_chart(x_values, y_values, options)
{
   jQuery.each(x_values, function(item) {
        options.xAxis.categories.push(x_values[item]);
    });

    var series = {
        data: []
    };

    jQuery.each(y_values, function(item) {
        series.data.push(parseInt(y_values[item]));
    });

    options.series.push(series);
    chart = new Highcharts.Chart(options);

    return chart;

} 

变量options定义没有series成员的图表模板(由上述函数添加)

修改

在问题中进行编辑后,这里是jsFiddle支持它。 请注意,JSON中的数据表示为数组数组。每个数组中的第一个元素对应于第一个类别,每个数组中的第二个元素对应于第二个类别等。

答案 1 :(得分:0)

除了不正确的json格式(已被编辑)之外,您需要将日期作为高级图表中的分类或使用日期时间类型的轴,然后将日期解析为时间戳。