momonja
momonja

Reputation: 11

HighChart Pie Dynamic JSON in Codeigniter

Any help for this? The problem is I cannot get the data in pie. Any ideas? I tried to echo it outside the pie graph view, and the data appears in JSON as [{"Terminal":"13"}]. The Hightchart needs the data as ["Sample", 2]? Any suggestion sir on how to convert it like that? Thanks.

Heres my code:

VIEW

$(document).ready(function () {

$(function () {
    var chart;

    // Build the chart
    $('.widget-lower-left#widget').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: 'Availability'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage}%</b>',
            percentageDecimals: 1
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: false
                },
                showInLegend: true
            }
        },
        series: [{
            type: 'pie',
            name: 'Availability',
            data: []
        }]

});

});


function requestData() {
$.ajax({
    url: 'home',
    datatype: "json",
    success: function(data) {

        alert(data);
        console.log(data);

      chart.series[0].setData(data);

    },
    cache: false
});

};

});

then in the MODEL

$results = $this->db->query("SELECT COUNT(get_jeeps_availability) as Terminal FROM get_jeeps WHERE get_jeeps_availability = 'Terminal'");

return $results->result_array();

in the CONTROLLER

public function index()
{   


    $data['pie'] = json_encode($this->get_model->dashboard_jeep_widget());


    $this->load->view('home',$data);
}

Upvotes: 1

Views: 1732

Answers (1)

Paweł Fus
Paweł Fus

Reputation: 45079

In requestData() function, before setting data, preprocess 'data' that way:

var newData = [];
for( var i = 0, len = data.length; i < len; i++ ) {
    var item = data[i];
    for(var j in item){
        newData.push([j,item[j]]);
    }
}
chart.series[0].setData(newData);

Upvotes: 1

Related Questions