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

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

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)