【问题标题】:Encoding json files to create flotcharts编码 json 文件以创建流程图
【发布时间】:2015-04-08 17:40:33
【问题描述】:

我需要为一个小项目创建一些图表,我正在尝试学习如何使用来自http://www.flotcharts.org/ 的图表。 我的问题是我需要从 mysql 查询动态地插入数据(每次刷新页面 - 所以不需要 AJAX -),我不明白如何构建所需的 json 文件。 以下是请求的 json 示例:

//Flot Pie Chart
$(function() {
    var data = [{
        label: "Series 0",
        data: 1
    }, {
        label: "Series 1",
        data: 3
    }, {
        label: "Series 2",
        data: 9
    }, {
        label: "Series 3",
        data: 20
    }];

    var plotObj = $.plot($("#flot-pie-chart"), data, {
        series: {
            pie: {
                show: true
            }
        },
        grid: {
            hoverable: true
        },
        tooltip: true,
        tooltipOpts: {
            content: "%p.0%, %s", // show percentages, rounding to 2 decimal places
            shifts: {
                x: 20,
                y: 0
            },
            defaultTheme: false
        }
    });

});
有人可以给我帮助或一些好的建议吗? 谢谢你。

【问题讨论】:

    标签: javascript jquery mysql json charts


    【解决方案1】:

    你必须在后端输出 json。 示例:如果您使用 php,则可以使用 json_encode() 函数对数据(来自 MySQL 查询)进行编码并将其设置为 javascript 变量。

    例子:

    <script type="text/javascript">
    // pass PHP variable declared above to JavaScript variable
    var data = <?php echo json_encode($myqueryresult) ?>;
    </script>
    

    发件人:http://www.dyn-web.com/tutorials/php-js/json/array.php

    【讨论】:

      猜你喜欢
      • 2021-12-10
      • 1970-01-01
      • 2018-11-29
      • 1970-01-01
      • 2018-07-10
      • 1970-01-01
      • 1970-01-01
      • 2011-02-26
      • 2011-01-18
      相关资源
      最近更新 更多