weixin_33730836 2016-10-03 08:42 采纳率: 0%
浏览 11

无法将阵列显示为图表

I got a problem using Highcharts with ajax..

I'm make a datasource for giving the highcharts an array..

This is the datasource

function reportPertanyaan1()
{
    $result = [];
    $this->load->model("surveymodel");
    for ($point=1; $point <=10 ; $point++) { 
        array_push($result, $this->surveymodel->GetData("1",$point));
    }

    echo json_encode($result);
}

And here's the ajax code that receive the array

$.ajax({
    url: site_url+"datasource/reportPertanyaan1",
    success: function(data){
        console.log(data);
        pertanyaan1(data);
    }
});

From the console.log(data), I get this kind of array [2,1,0,1,1,0,1,0,0,0]

And then, from that ajax, I call pertanyaan1 function. This is the code

function pertanyaan1(jawaban){
    $('#container1').highcharts({
        chart: {
            type: 'line'
        },
        title: {
            text: ''
        },
        xAxis: {
            categories: ['1', '2', '3','4', '5', '6','7', '8', '9','10']
        },
        yAxis: {
            title: {
                text: 'Banyak Data'
            }
        },
        series: [{
            name: 'Pertanyaan 1',
            data: jawaban
        }],
    });
}

And the code is not showing the chart. But it's doubled the xAxis label look like this

enter image description here

But, when I'm replace the data: jawaban to data: [2,1,0,1,1,0,1,0,0,0]. The chart can show the data..

Any help apriciated

  • 写回答

1条回答 默认 最新

  • 乱世@小熊 2016-10-03 08:53
    关注

    Actually you are getting JSON from your ajax call. You need to define it as Js object. And don't send your data as array. Send your data as object.

    Try this:

    $data= [];
    $this->load->model("surveymodel");
    for ($point=1; $point <=10 ; $point++) { 
        array_push($data, $this->surveymodel->GetData("1",$point));
    }
    
    $result = new stdClass();
    $result->data = $data;
    echo json_encode($result);
    

    And get data like:

    $.ajax({
        url: site_url+"datasource/reportPertanyaan1",
        success: function(result){
            var resultData = JSON.parse(result);
            pertanyaan1(resultData.data);
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办