php - 来自 php 的 Highcharts 多个系列 json

标签 php jquery highcharts

大家好,我需要 Highcharts 库的帮助,我有这个来自 php 的数组,

[{"name":"25% en cambio de aceite 76 lubricants","data":[["2015-09-07",1],["2015-09-23",2],["2015-09-24",3],["2015-09-30",3]]},{"name":"10% Descuento en filtro de aceite","data":[["2015-09-07",1],["2015-09-23",2],["2015-09-24",3],["2015-09-30",3],["2015-10-03",3],["2015-10-05",1],["2015-10-09",1],["2015-10-10",1]]}]

我需要动态地将其显示为折线图,但无法做到这一点,我相信错误来自日期中的引号,需要采用格式 [Date.UTC(2015, 2, 6), 3 ]

这是我的 php 函数,它返回 json 数据

public function actionTransactionsRedeemed() {
    // Transacciones Totales redimidas por merchant
    $sql = "SELECT DISTINCT `transaction`.idPromotion, promotion.`name` FROM `transaction` INNER JOIN promotion ON `transaction`.idPromotion = promotion.idPromotion WHERE `transaction`.idMerchant = 2 AND `transaction`.idPromotion IS NOT NULL";
    $idPromotion = Yii::app()->db->createCommand($sql)->queryAll();
    $idPromotions = array();

    $tempArray = array();
    $result = array();
    $i = 1;
    $rs = array();

    foreach($idPromotion as $i){
        //process each item here
        $id = $i["idPromotion"];
        $tempArray['name'] = $i["name"];

        $sql = "SELECT count(*) AS count, DATE(`transaction`.date) AS `date` FROM `transaction` WHERE `transaction`.idMerchant = 2 AND `transaction`.idPromotion = $id GROUP BY DATE(`transaction`.date)";
        $transactionsRedeemed = Yii::app()->db->createCommand($sql)->queryAll();
        foreach($transactionsRedeemed as $item2){
            $rs[0] = $item2['date'];
            $rs[1] = $item2['count'];
            $tempArray['data'][] = $rs;
            $rs = array();
        }
        $i++;
        array_push($result, $tempArray);
    }
    //$result = json_encode($result, JSON_NUMERIC_CHECK);

    //echo json_decode($result);
    print  json_encode($result, JSON_NUMERIC_CHECK);
}

这是构建图表的 Jquery

$(document).ready(function() {
    var options = {
        chart: {
            type: 'spline',
            renderTo: 'chart-merchant-day',
            defaultSeriesType: 'spline',
            marginRight: 130,
            marginBottom: 25
        },
        title: {
            text: 'Total de promociones redimidas',
            x: -20 //center
        },
        subtitle: {
            text: '',
            x: -20
        },
        xAxis: {
            type: 'datetime',
            dateTimeLabelFormats: { // don't display the dummy year
                month: '%e. %b',
                year: '%b'
            },
            labels: {
                align: 'center',
                x: -3,
                y: 20,
                formatter: function() {
                    return Highcharts.dateFormat('%l%p', this.value);
                }
            }
        },
        yAxis: {
            title: {
                text: 'Transacciones'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            formatter: function() {
                    return Highcharts.dateFormat('%l%p', this.x-(24000*3600)) +'-'+ Highcharts.dateFormat('%l%p', this.x) +': <b>'+ this.y + '</b>';
            }
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'top',
            x: -10,
            y: 100,
            borderWidth: 0
        },
        series: [{
            name: 'Count'
        }],
        credits: false
    }
    // Load data asynchronously using jQuery. On success, add the data
    // to the options and initiate the chart.
    jQuery.get('?r=/transactions/transactionsRedeemed', null, function(tsv) {
        var lines = [];
        traffic = [];

        var data = $.parseJSON(tsv);
        var x = 0;

        //console.log(tsv);

        $.each(data, function(i, item) {
            //alert(item);
            //console.log(item);

            $.each(item, function(y, item2) {

                if(y == "data"){
                    //console.log(item2);
                    try {
                        tsv = item2;
                        // split the data return into lines and parse them
                        tsv = tsv.split(/\n/g);
                        jQuery.each(tsv, function(i, line) {
                            line = line.split(/\t/);
                            options.series[x].data.push([Date.parse(line[0]),line[1]]);
                            /*date = Date.parse(line[0] +' UTC');
                            traffic.push([
                                date,
                                parseInt(line[1].replace(',', ''), 10)
                            ]);*/
                        });
                    } catch (e) {  }

                    options.series[x].data = traffic;

                } else if(y == "name"){
                    options.series[x].name = item2;
                }
            });
            x++;
        });
        chart = new Highcharts.Chart(options);
        //console.log(tsv.replace(/\"/g, ""));
        //tsv = tsv.replace(/\"/g, "");
    });
});

任何帮助将不胜感激,我现在​​已经筋疲力尽了。

最佳答案

功能其实更简单,

jQuery.get('?r=/transactions/transactionsRedeemed', null, function(tsv) {

    var data = $.parseJSON(tsv);
    $.each(data, function (i, item) {
        options.series.push({
            name: item['name'],
            data: []
        });
        $.each(item['data'], function (j, dataitem) {
            var dataitemvalue = null;
            try {
                dataitemvalue = [Date.parse(dataitem[0]), dataitem[1]];
            } catch (e) {}
            options.series[i].data.push(dataitemvalue);
        });
    });

    chart = new Highcharts.Chart(options);

});

JSFiddle demo

关于php - 来自 php 的 Highcharts 多个系列 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33073250/

相关文章:

javascript - 将 jquery 调用附加到图像加载

javascript - Highcharts 中的绘图带图标显示不正确

javascript - 在 Highcharts 中,当图表具有高数据时,工具提示不会移动,这与图表具有低数据时不同

php - 如何在php页面中添加或嵌入CKEditor

javascript - PHP 密码哈希算法结果不同于 Javascript 结果

javascript - Jquery 点击自动化有延迟?

javascript - 将标签和标记对齐在同一垂直线上(Highcharts)

php - 在 Laravel 5 迁移中使列不可为空

javascript - 从数组中填充 href

javascript - Angular Highcharts 动态 x 轴不工作