JSON and Highcharts

12,140

Solution 1

The code is ok. The problem was that I was using a free server.

With XAMPP, at the localhost it works perfect!

Thanks for all!

Solution 2

One way to sort this is to set the categories and data separaately:

$(function () {
var options = {
    chart: {
        renderTo: 'container',
        type: 'column'
    },
    xAxis: {
        categories: ["A", "B"]
    },
    series: [{
        data: [
            4,9                
        ]
    }]
};
chart = new Highcharts.Chart(options);

});

Your getJson could return an object like:

{
    categories:["January","February"],
    data:[4,9]
}

and then your javascript could set the chart options like:

options.xAxis.categories = json.categories;
options.series[0] = json.data;
Share:
12,140
Anfuca
Author by

Anfuca

A web developer camouflaged like a UI/UX Designer that is playing with SEO and...

Updated on June 04, 2022

Comments

  • Anfuca
    Anfuca almost 2 years

    I'm recently new using JSON and i´m not able to draw any kind of charts from Highcharts. Since last Friday i'm reading and reading over the internet, i'm learning a lot, for sure, but right now i'm desperate. I just want a simple bar and a pie!

    From my php file, Json_encode prints something this:

    [["January",4],["February",9]]
    

    I think that is the correct format, string with the "" and int without it.

    And this is the code that i'm trying (i'm putting the entire example from some web i found):

    <!DOCTYPE HTML>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Example</title>
        <script src="http://code.highcharts.com/highcharts.js"></script>
        <script src="http://code.highcharts.com/modules/exporting.js"></script>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">       </script>
        <script type="text/javascript">
        $(document).ready(function() {
            var options = {
                chart: {
                    renderTo: 'container',
                    type: 'column',
                    marginRight: 130,
                    marginBottom: 25
                },
                title: {
                    text: 'Project Requests',
                    x: -20 //center
                },
                subtitle: {
                    text: '',
                    x: -20
                },
                xAxis: {
                    categories: []
                },
                yAxis: {
                    title: {
                        text: 'Requests'
                    },
                    plotLines: [{
                        value: 0,
                        width: 1,
                        color: '#808080'
                    }]
                },
                tooltip: {
                    formatter: function() {
                            return '<b>'+ this.series.name +'</b><br/>'+
                            this.x +': '+ this.y;
                    }
                },
                legend: {
                    layout: 'vertical',
                    align: 'right',
                    verticalAlign: 'top',
                    x: -10,
                    y: 100,
                    borderWidth: 0
                }, {
                series: []
            }
    
            $.getJSON("myphpname.php", function(json) {
                options.xAxis.categories = json[0]['data'];
                options.series[0] = json[1];
                chart = new Highcharts.Chart(options);
            });
        });
        </script>
    
    </head>
    <body>
        <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
    </body>
    </html>
    

    Thanks for all!!!

  • Anfuca
    Anfuca over 10 years
    Thanks for trying helping me @SteveP. I'm very sorry but my knowledge about formatting and getting JSON from php to highcharts javascript it's still low... I've tried your code. A column chart appears with the values A-B and 4-9 respectively. But, how i format the Json object like {categories:["January","February"],data:[4,9]} instead the [["January",4],["February",9]] that returns my php json_encode ? Excuse me about my dummies questions. Thanks so much for your help.