dongxie2613 2014-09-28 11:00
浏览 41
已采纳

尝试为NVD3图表创建JSON代码

I'm absolutely stuck with this piece of code, I've searched and searched to get it working and have failed miserably.

My Json output currently looks like this

 [
    {
        "key": "OWM1",
        "values": [
            [
                "x : EW",
                "y :4"
            ],
            [
                "x : RSE",
                "y :3"
            ],
            [
                "x : SWE",
                "y :2"
            ],
            [
                "x : WTE",
                "y :1"
            ],
            [
                "x : WWE",
                "y :1"
            ]
        ]
    },
    {
        "key": "OWM2",
        "values": [
            [
                "x : EW",
                "y :4"
            ],
            [
                "x : RSE",
                "y :2"
            ],
            [
                "x : SWE",
                "y :1"
            ],
            [
                "x : WTE",
                "y :3"
            ],
            [
                "x : WWE",
                "y :2"
            ]
        ]
    }
]

But I need it to look like this for the sake of NVD3 chart to be able to process it correctly.

[
  {
    "key": "OWM1",

    "values":
      [      
        { x : "EW", y : 4 },
        { x : "RSE", y : 3 },
        { x : "SWE",   y : 2 }  
        { x : "WTE",   y : 1 }  
        { x : "WWE",   y : 21 }  
      ]
  },
   {
    key: "OWM2",

    values:
      [      
        { x : "EW", y : 4 },
        { x : "RSE", y : 2 },
        { x : "SWE",   y : 1 }  
        { x : "WTE",   y : 3 }  
        { x : "WWE",   y : 2 } 
      ]
  }]

Here is my PHP code that I am producing the first output of json with.

$result = mysqli_query($con,"SELECT CODE, _POS, COUNT(CODE) AS COUNT from defects WHERE FPOS IN ('OWM1','OWM2') GROUP BY POS, CODE");


if($result) {
    $jsonData = convert($result);
}

function convert($result) {

    $intermediate = array();

    while($rows = mysqli_fetch_assoc($result)) {
        $key = $rows['POS'];
        $x = $rows['CODE'];
        $y = $rows[''];
        $intermediate[$key][] = array('x : ' .$x,'y :' . $y);
    }


   $output = array();

    foreach($intermediate as $key => $values) {
        $output[] = array(
            "key" => $key,
            'values' => $values
        );
    }

    echo json_encode($output);

}





mysqli_close($con);

Is there any way anyone could help me manipulate the code I have to look exactly like the second set of JSON code that is compatible with NVD3 charts?

Thanks alot.

  • 写回答

1条回答 默认 最新

  • dongningce9075 2014-09-28 12:42
    关注

    It looks like you are creating a non-associative array here...

    $intermediate[$key][] = array('x : ' .$x,'y :' . $y);
    

    Replace that with...

    $intermediate[$key][] = array('x' => $x, 'y' => $y);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding