1

我想删除我使用 .php 获得的 [0] [等等],以便我可以单独使用它们并使用 RickShaw 库创建图表。

这是目前我得到的 JSON 代码:

Array ( [0] => [{"x":"0","y":"35"},{"x":"1","y":"34"},{"x":"2","y":"36"},{"x":"3","y":"35"},{"x":"4","y":"40"},{"x":"5","y":"35"},{"x":"6","y":"37"},{"x":"7","y":"40"},{"x":"8","y":"45"},{"x":"9","y":"46"},{"x":"10","y":"55"},{"x":"11","y":"63"},{"x":"12","y":"61"},{"x":"13","y":"45"},{"x":"14","y":"48"},{"x":"15","y":"49"},{"x":"16","y":"45"},{"x":"17","y":"44"},{"x":"18","y":"52"},{"x":"19","y":"43"},{"x":"20","y":"37"},{"x":"21","y":"36"},{"x":"22","y":"37"},{"x":"23","y":"34"}] 
[1] => [{"x":"0","y":"10"},{"x":"1","y":"15"},{"x":"2","y":"13"},{"x":"3","y":"15"}, "x":"4","y":"14"},{"x":"5","y":"16"},{"x":"6","y":"17"},{"x":"7","y":"25"},{"x":"8","y":"23"},{"x":"9","y":"24"},{"x":"10","y":"25"},{"x":"11","y":"28"},{"x":"12","y":"27"},{"x":"13","y":"21"},{"x":"14","y":"23"},{"x":"15","y":"19"},{"x":"16","y":"18"},{"x":"17","y":"16"},{"x":"18","y":"15"},{"x":"19","y":"14"},{"x":"20","y":"15"},{"x":"21","y":"16"},{"x":"22","y":"15"},{"x":"23","y":"16"}] 
[2] => [{"x":"0","y":"45"},{"x":"1","y":"49"},{"x":"2","y":"49"},{"x":"3","y":"50"},{"x":"4","y":"54"},{"x":"5","y":"51"},{"x":"6","y":"54"},{"x":"7","y":"65"},{"x":"8","y":"68"},{"x":"9","y":"70"},{"x":"10","y":"80"},{"x":"11","y":"91"},{"x":"12","y":"88"},{"x":"13","y":"66"},{"x":"14","y":"71"},{"x":"15","y":"68"},{"x":"16","y":"63"},{"x":"17","y":"60"},{"x":"18","y":"67"},{"x":"19","y":"57"},{"x":"20","y":"52"},{"x":"21","y":"52"},{"x":"22","y":"52"},{"x":"23","y":"50"}] 
[3] => [{"x":"0","y":"10"},{"x":"1","y":"15"},{"x":"2","y":"12"},{"x":"3","y":"5"},{"x":"4","y":"9"},{"x":"5","y":"15"},{"x":"6","y":"45"},{"x":"7","y":"125"},{"x":"8","y":"345"},{"x":"9","y":"256"},{"x":"10","y":"312"},{"x":"11","y":"345"},{"x":"12","y":"299"},{"x":"13","y":"165"},{"x":"14","y":"354"},{"x":"15","y":"368"},{"x":"16","y":"254"},{"x":"17","y":"213"},{"x":"18","y":"312"},{"x":"19","y":"165"},{"x":"20","y":"54"},{"x":"21","y":"32"},{"x":"22","y":"10"},{"x":"23","y":"5"}] [4] => [{"x":"0","y":"2"},{"x":"1","y":"3"},{"x":"2","y":"2"},{"x":"3","y":"1"},{"x":"4","y":"1"},{"x":"5","y":"2"},{"x":"6","y":"3"},{"x":"7","y":"15"},{"x":"8","y":"45"},{"x":"9","y":"27"},{"x":"10","y":"40"},{"x":"11","y":"42"},{"x":"12","y":"35"},{"x":"13","y":"18"},{"x":"14","y":"42"},{"x":"15","y":"40"},{"x":"16","y":"30"},{"x":"17","y":"25"},{"x":"18","y":"40"},{"x":"19","y":"20"},{"x":"20","y":"6"},{"x":"21","y":"4"},{"x":"22","y":"2"},{"x":"23","y":"1"}] )

[0] 到 [3] 是不同的行

人力车图书馆似乎是我需要的最简单的。我可以插入这样的系列:多个图表系列。谁能教我/告诉我如何更改我的数据?

我想获得这个模板(如上面发布的链接所示):

var graph = new Rickshaw.Graph( {
        element: document.querySelector("#chart"),
        width: 540,
        height: 250,
        series: [
                {
                        name: "Northeast",
                        data: [ { x: -1893456000, y: 25868573 }, { x: -1577923200, y: 29662053 }, { x: -1262304000, y: 34427091 }, { x: -946771200, y: 35976777 }, { x: -631152000, y: 39477986 }, { x: -315619200, y: 44677819 }, { x: 0, y: 49040703 }, { x: 315532800, y: 49135283 }, { x: 631152000, y: 50809229 }, { x: 946684800, y: 53594378 }, { x: 1262304000, y: 55317240 } ],
                        color: palette.color()
                },
                {
                        name: "Midwest",
                        data: [ { x: -1893456000, y: 29888542 }, { x: -1577923200, y: 34019792 }, { x: -1262304000, y: 38594100 }, { x: -946771200, y: 40143332 }, { x: -631152000, y: 44460762 }, { x: -315619200, y: 51619139 }, { x: 0, y: 56571663 }, { x: 315532800, y: 58865670 }, { x: 631152000, y: 59668632 }, { x: 946684800, y: 64392776 }, { x: 1262304000, y: 66927001 } ],

这是我的实际 .php :

<?php include 'connexion.php' ?>

<?php
////////////////// out_evo_tat   
$requete = $db->prepare('SELECT * FROM out_evo_tat');
$requete->execute();

while($row = $requete->fetch()) { 

    $tableau[]= array (
        array($row['Heure'],$row['TAT_CHAINE']),
        array($row['Heure'], $row['TAT_PREA']),
        array($row['Heure'], $row['TAT_GLOBAL']),
        array($row['Heure'], $row['NBR_TESTS']),
        array($row['Heure'], $row['NBR_TUBES'])
        );  
}

array_unshift($tableau, null);
$tableau = call_user_func_array('array_map', $tableau);
$json = array();
foreach ($tableau as $key => $val){
    $out = array();
    foreach ($val as $key2 => $val2){
        $item = new stdClass;
        $item->x = $val2[0];
        $item->y = $val2[1];
        $out[] = $item;
    }
    $json[] = json_encode($out);
}
print_r($json);

?>

十分感谢。马修

4

1 回答 1

0

因为我不知道你从哪里得到 的值$name,所以我只是硬编码了值“Northeast”。下面不是像以前那样创建一个 json 值数组,而是从您的数组生成 JavaScript 代码$tableau

$name = 'Northeast';
array_unshift($tableau, null);
$tableau = call_user_func_array('array_map', $tableau);
$js = <<<EOTAAA
var graph = new Rickshaw.Graph( {
    element: document.querySelector("#chart"),
    width: 540,
    height: 250,
    series: [

EOTAAA;
foreach ($tableau as $key => $val){
    $js .= "\t\t{\n";
    $js .= "\t\t\tname: \"".$name."\",\n";
    $out = array();
    foreach ($val as $key2 => $val2){
        $item = new stdClass;
        $item->x = $val2[0];
        $item->y = $val2[1];
        $out[] = $item;
    }
    $js .= "\t\t\tdata: ".str_replace('"', '', json_encode($out)).",\n";
    $js .= "\t\t\tcolor: palette.color()\n";
    $js .= "\t\t},\n";
}
$js = rtrim($js, ",\n");//remove last comma
$js .= "\n\t\t]});\n";
echo $js;

输出:

var graph = new Rickshaw.Graph( {
        element: document.querySelector("#chart"),
        width: 540,
        height: 250,
        series: [
                {
                    name: "Northeast",
                    data: [{x:0,y:35},{x:1,y:34},{x:2,y:36},{x:3,y:35}],
                    color: palette.color()
                },
                {
                    name: "Northeast",
                    data: [{x:0,y:10},{x:1,y:15},{x:2,y:13},{x:3,y:15}],
                    color: palette.color()
                },
                {
                    name: "Northeast",
                    data: [{x:0,y:45},{x:1,y:49},{x:2,y:49},{x:3,y:50}],
                    color: palette.color()
                },
                {
                    name: "Northeast",
                    data: [{x:0,y:10},{x:1,y:15},{x:2,y:12},{x:3,y:5}],
                    color: palette.color()
                },
                {
                    name: "Northeast",
                    data: [{x:0,y:2},{x:1,y:3},{x:2,y:2},{x:3,y:1}],
                    color: palette.color()
                }
                ]});
于 2013-08-07T16:07:48.407 回答