JSON for jqPlot

前端 未结 4 476
余生分开走
余生分开走 2020-12-10 07:53

I would like to use jqPlot usinge data from server side coming in JSON, like described in this example: http://www.jqplot.com/tests/data-renderers.php

My code is nea

4条回答
  •  -上瘾入骨i
    2020-12-10 08:09

    Do not parse the result on the client side, jquery will do much better. Actually, the array you need for jqplot is in fact valid json. All you have to do is prepare your data and create the appropriate array structure in PHP:

    $pairs = array(1=>2, 3=>5, 4=>7, 5=>12, 7=>23); // simple example
    
    $result = array();
    
    foreach ($pairs as $label => $value) {
        $result[] = array($label,$value); // make a "small" array for each pair
    }
    
    echo json_encode(array($result)); // wrap the result into another array; multiple plot data go like "array($result, $result2, ...)"
    

    The result looks like this:

    [[[1,2],[3,5],[4,7],[5,12],[7,23]]] 
    

    and is excatly what you need.

提交回复
热议问题