Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

javascript - Populate Jqplot from remote JSON

I'm trying to populate a bar chart from an external JSON in a PHP file. I've trying to use AJAX and getJSON function to do this but still failed to generate a chart.

Here is how my JSON format look like:

[
    {
        "NAME": "a chart",
        "VALUES": "[[439, 233, 233, 495],[292, 360, 262, 173],[222, 95, 27, 27]]"
    }
]

Here is the javascript:

    $(document).ready(function() {
         urlDataJSON = 'data.php';

    $.getJSON(urlDataJSON, function (data) {

        console.log(data.output);

        var dataLabels = ['base', 'key', 'pacing', 'emerging'];
        var dataLines = json[0].VALUES;
        var chartTitle = json[0].NAME;


        $.jqplot('chart2', dataLines, {
            legend: {
                show: true
            },
            title: chartTitle,
            seriesDefaults: {
                renderer: $.jqplot.BarRenderer,
                pointLabels: {
                    show: true,
                    location: 'e',
                    edgeTolerance: -15
                },
                shadowAngle: 135,
                renderOptions: {
                    barDirection: 'horizontal'
                }
            },
            axes: {
                xaxis: {
                    renderer: $.jqplot.CategoryAxisRenderer,
                    ticks: dataLabels
                }
            }
        });
    });

Do you guys know what's wrong with my code above? If anything please let me know.


EDIT: Updated code

            var urlDataJSON = 'data.php';
/*                var json = [{
                    "Name": "Poll Results",
                    "Serie": [[2, 5, 4], [4, 1, 7], [6, 3, 1], [3, 4, 2]]}];*/
            $.getJSON(urlDataJSON, function (data) {

                console.log(data);

                var tick = ['asd','fsdf','asda','fdsf',];
                var dataLines = [];
                var title = [];

                $.each(data, function (entryindex, entry) {
                    dataLines.push(entry['VALUES']);
                    title.push(entry['NAME']);
                });

                var options = {
                    legend: {
                        show: true
                    },
                    title: 'Poll Results',
                    seriesDefaults: {
                        renderer: $.jqplot.BarRenderer,
                        pointLabels: {
                            show: true,
                            location: 'e',
                            edgeTolerance: -15
                        },
                        shadowAngle: 135,
                        renderOptions: {
                            barDirection: 'horizontal'
                        }
                    },
                    axes: {
                        xaxis: {
                            renderer: $.jqplot.CategoryAxisRenderer,
                            ticks: tick
                        }
                    }
                };
                var plot = $.jqplot('chart2', [dataLines], options);
            });

I've put .each() function to push data into an array. Now, the graph is empty. Only the background is showing.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
$(document).ready(function() {

    $.getJSON('data.php', function (data) {

        var tick = ['asd','fsdf','asda','fdsf'];

        var options = {
            legend: {
                show: true
            },
            title: 'Poll Results',
            seriesDefaults: {
                renderer: $.jqplot.BarRenderer,
                pointLabels: {
                    show: true,
                    location: 'e',
                    edgeTolerance: -15
                },
                shadowAngle: 135,
                renderOptions: {
                    barDirection: 'horizontal'
                }
            },
            axes: {
                xaxis: {
                    renderer: $.jqplot.CategoryAxisRenderer,
                    ticks: tick
                }
            }
        };
        var plot = $.jqplot('chart2', data[0].VALUES, options);
    });
});

Make sure to include these plugins

<script type="text/javascript" src="../src/plugins/jqplot.barRenderer.min.js"></script>
<script type="text/javascript" src="../src/plugins/jqplot.categoryAxisRenderer.min.js"></script>
<script type="text/javascript" src="../src/plugins/jqplot.pointLabels.min.js"></script>

This is my data.php

$arr = [
    "NAME" => "a chart",
    "VALUES" => [[439, 233, 233, 495],[292, 360, 262, 173],[222, 95, 27, 27]]
];

header('Content-Type: application/json');
print json_encode([$arr]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...