douyi3833 2013-08-23 18:15
浏览 27
已采纳

来自PHP的flot piechart

I am having a very strange issue creating a piechart in Flot with data from PHP.

It seems to be drawing incorrectly, and I can't figure out why.

My PHP code (for testing) is:

echo json_encode(
'[{ label: "Series1",  data: 10},
{ label: "Series2",  data: 3},
{ label: "Series3",  data: 9},
{ label: "Series4",  data: 7},
{ label: "Series5",  data: 8},
{ label: "Series6",  data: 17}]'
);

My JS file is:

$.ajax({
type:'GET',
dataType:"json",
url:'../phpfile.php',
success: function(data) {
    console.log(data);
    $.plot($("#piechart"),data,{
        series: {
            pie: {
                show: true
            }
        }
    });
}
});

The consol log shows:

[{ label: "Series1",  data: 10},
{ label: "Series2",  data: 3},
{ label: "Series3",  data: 9},
{ label: "Series4",  data: 7},
{ label: "Series5",  data: 8},
{ label: "Series6",  data: 17}]

Which I thought was the correct format for flot...

But it graphs like this: piechart

Does anyone have any ideas?

  • 写回答

1条回答 默认 最新

  • douliedu335997 2013-08-23 18:50
    关注

    I believe your JSON currently is invalid, at the moment, you're trying to parse an JSON String, into a JSON String (If you get what I mean!) Currently, when I echo out from the PHP end with your echo'ed json_encode(), I'm provided with:

    "[{ label: \"Series1\", data: 10},
    { label: \"Series2\"]"
    

    Furthermore, I would use PHP Arrays to encode JSON, like below:

    <?php 
        $arr = array( 
            array(
                "label" => "Series1",
                "data" => 10
            ),
            array(
                "label" => "Series2",
                "data" => 3
            ),
            array(
                "label" => "Series3",
                "data" => 9
            ),
            array(
                "label" => "Series4",
                "data" => 7
            ),
            array(
                "label" => "Series5",
                "data" => 8
            ),
            array(
                "label" => "Series7",
                "data" => 17
            )
        );
    
        echo json_encode( $arr );
    ?>
    

    PHP json_encode() does accept mixed variable types, but it's most popularly used with PHP arrays.

    With the above, I'm able to construct the PIE chart successfully:

    Finished Chart

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog