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 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊