【问题标题】:Highcharts with data from mysql database来自mysql数据库的数据的Highcharts
【发布时间】:2014-03-31 15:09:48
【问题描述】:

我正在使用 highcharts 插件在 codeigniter 站点中打印统计信息。

现在我从我的数据库中获取数据并对其进行管理,但是当我在 highcharts 中打印它时,什么也没有打印出来。

这是我的 php 代码:

    $arr_point = array();
    $check_price = array();

    for($i = 1; $i <= 12; $i++){
        $sum = 0;
        $this->load->model('backend/Notification_model');
        $this->db->select('*,');
        $this->db->from('booking');     
        $query = $this->db->get();
        foreach ($query->result() as $row){
           $sum+=(float)$row->total_with_markup;
        }

        $arr_point[] = str_replace(',', '.', $sum);
    }
    $result = array();
    array_push($result,$arr_point);

    return(json_encode($result));

这是我的 highchart 配置:

$('#container').highcharts({
        chart: {
                type: 'column'
            },
            title: {
                text: $('#riepilogo option:selected').text()
            },
            subtitle: {
                text: ''
            },
            xAxis: {
                categories: [
                    'Jan',
                    'Feb',
                    'Mar',
                    'Apr',
                    'May',
                    'Jun',
                    'Jul',
                    'Aug',
                    'Sep',
                    'Oct',
                    'Nov',
                    'Dec'
                ]
            },
            yAxis: {
                min: 0,
                title: {
                    text: 'Euro'
                }
            },
            tooltip: {
                headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
                pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                    '<td style="padding:0"><b>{point.y:.2f} euro</b></td></tr>',
                footerFormat: '</table>',
                shared: true,
                useHTML: true
            },
            plotOptions: {
                column: {
                    pointPadding: 0.2,
                    borderWidth: 0
                }
            },
            series: [ {
                name: $('#riepilogo options:selected').text(),
                data: "<?php echo json_decode($data_chart); ?>"
            }]
        });
});

如果我打印 json_decode($data_chart) 我会得到这个:

Array

如果我打印 ($data_chart 我得到这个:

[["0","297.23","74.41","65.57","2167.32","7649.77","2058.05","146.95","92.55","0","1754.72","0"]]

我尝试不使用json_encode,使用json_decode,什么都没有。有人能帮我吗?谢谢

【问题讨论】:

  • 我的问题很清楚,我认为“没有打印任何内容”highchart 空无行@AamirAfridi
  • 去掉""
  • 如果我删除它相同的东西,空图表但结果出现在我的变量@Dieter
  • 尝试打印 $data_chart[0] 似乎您的数组中有一个数组,因为您的数组周围有双 [[ ]] 的数组
  • 它打印“[”,你说得对,如何解决? @迪特

标签: php jquery mysql codeigniter highcharts


【解决方案1】:

问题是您在脚本中创建了一个数组:

 $arr_point = array();

尝试只返回数组并删除: $结果 = 数组(); array_push($result,$arr_point);

并返回 $array_point;

或者试试:

    $arr_point = "";
$check_price = array();

for ($i = 1; $i <= 12; $i++) {
    $sum = 0;
    $this->load->model('backend/Notification_model');
    $this->db->select('*,');
    $this->db->from('booking');
    $query = $this->db->get();
    foreach ($query->result() as $row) {
        $sum+=(float) $row->total_with_markup;
    }

    if($arr_point == ""){
        $arr_point .= "[";
    }

    if($arr_point != ""){
        $arr_poin .= ",";
    }

     $arr_point .= str_replace(',', '.', $sum);


}

$arr_point .= "]";


return $arr_point;

【讨论】:

  • 如果我使用你的脚本并打印结果,我有这个:0[297.23[74.41[65.57[2167.32[7649.77[2058.05[146.95[92.55[0[1754.72[0]]
  • 早上好!抱歉回答晚了,我编辑了我的答案,其中有一个逻辑错误..请再试一次:)
【解决方案2】:

您的代码包含许多注入,因此我建议在您的 php 文件中打印 json,例如:

echo json_encode($arr, JSON_NUMERIC_CHECK);

然后在javascript中调用$.getJSON()函数并在highcahrts中使用它,跳过解码/丢失类型的数据。

【讨论】:

    猜你喜欢
    • 2012-06-11
    • 2018-10-30
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    • 2015-05-26
    • 2021-05-22
    • 2014-07-16
    • 2016-04-07
    相关资源
    最近更新 更多