JSON and Highcharts

2019-07-23 05:12发布

问题:

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!!!

回答1:

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;


回答2:

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!