Koala
Koala

Reputation: 5523

Highcharts Pyramid Chart displays no data with JSON, PHP

When using JSON with a pyramid chart in Highcharts, I don't get any errors in the console log but no data is displayed.

I have charts.php which outcome is this:

{"success":1,"data":[{"name":"John Spoon","data":300}, {"name":"Dave Jones","data":200},{"name":"Other","data":500}]}

This is what I've tried, which returns no data.

<div id="chart"  style=
                        "height:600px;width:100%;"></div>

<script>
$(function () {
    $("#chart").html("Loading Activity Log Graph...");

    var options = {
        chart: {
            type: 'pyramid',
            renderTo: 'chart',
            marginRight: 100
        },
        title: {
            text: 'Activity',
            x: -50
        },
        plotOptions: {
            series: {
                dataLabels: {
                    enabled: true,
                    format: '<b>{point.name}</b> ({point.y:,.0f})',
                    color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black',
                    softConnector: true
                }
            }
        },
        legend: {
            enabled: false
        },
        name: 'Activity Count',
        series: [{}]
   };

    $.ajax({
        url: "charts.php",
        type:'post',
        dataType: "json",
        success: function(data){
            options.series = data.data;
            var chart = new Highcharts.Chart(options);          
        }
    });

}); 
</script>

This is my desired outcome, showing without the use of JSON: http://jsfiddle.net/0dyy44hz/

What do I need to change for it to show data?

Upvotes: 0

Views: 533

Answers (1)

Mark
Mark

Reputation: 108537

Looking at the pyramid example, the data must be in the form of a single series with each data point as an array of [name, value]. You have two options, change your PHP to output the correct format or modify the PHP output in javascript. Since you didn't post your PHP code, I'll do the later:

var data = {"success":1,"data":[{"name":"John Spoon","data":300}, {"name":"Dave Jones","data":200},{"name":"Other","data":500}]};    

options.series = [{
    data: $.map(data.data, function(i){ // loop outputted data
        return [[i.name, i.data]]; // coerce into an array of [name,value]
    })
}];

Here's a working example.

Upvotes: 2

Related Questions