0

我正在尝试从 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() 函数将数据推送到数组中。现在,图表是空的。只显示背景。

4

1 回答 1

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]);
于 2013-10-31T07:25:09.517 回答