来自PHP的flot piechart

我在使用PHP数据在Flot中创建一个饼图时遇到了一个非常奇怪的问题。</ p>
\ n

它似乎绘图不正确,我无法弄清楚原因。 </ p>

我的PHP代码(用于测试)是:</ p>

  echo json_encode(
'[{label:“Series1”,data: 10},
{标签:“系列2”,数据:3},
{标签:“系列3”,数据:9},
{标签:“系列4”,数据:7},
{标签: “Series5”,数据:8},
{标签:“Series6”,数据:17}]'
);
</ code> </ pre>

我的JS文件是: </ p>

  $。ajax({
type:'GET',
dataType:“json”,
url:'.. / phpfile.php',
成功:function( 数据){
console.log(data);
$ .plot($(“#popchart”),data,{
series:{
pie:{
show:true
}
}

});
}
});
</ code> </ pre>

consol日志显示:</ p>

  [  {label:“Series1”,data:10},
{label:“Series2”,data:3},
{label:“Series3”,data:9},
{label:“Series4”,data :7},
{标签:“Series5”,数据:8},
{标签:“Series6”,数据:17}]
</ code> </ pre>

其中 我认为是正确的flot格式... </ p>

但它的图形如下:
</ p>

有没有人有任何想法?</ p>
</ div>

展开原文

原文

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个回答

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

dongwo6477
dongwo6477 别客气! =]
大约 7 年之前 回复
doubeng9407
doubeng9407 完善! 非常感谢。 这么简单的解决方案,但它以某种方式让我无法理解。 再次感谢
大约 7 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问