从远程JSON填充Jqplot

时间:2013-10-31 06:02:56

标签: javascript php ajax json jqplot

我正在尝试从PHP文件中的外部JSON填充条形图。我试图使用AJAX和getJSON函数来执行此操作,但仍无法生成图表。

以下是我的JSON格式:

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

这是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
                }
            }
        });
    });

你们知道我的代码上面有什么问题吗?如果有的话请告诉我。


编辑:更新了代码

            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);
            });

我已经使用.each()函数将数据推送到数组中。现在,图表是空的。只显示背景。

1 个答案:

答案 0 :(得分:1)

$(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);
    });
});

确保包含这些插件

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

这是我的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]);