Can't obtain the desired template for my array

2019-07-31 05:12发布

问题:

I'd like to remove the [0] [and so on] I get with the .php so I can use them separately and create charts using RickShaw librairy.

This is currently the JSON code i get :

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] to [3] are different lines

The library Rickshaw seems the easiest for what I need. I can insert series like this one : multiple charts series. Could anyone teach me/ tell me how i can change my datas ?

I'd like to obtain this template (as seen on the link posted right above) :

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 } ],

This is my actual .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);

?>

Thank you a lot. Mathieu

回答1:

Since I don't know where you're getting the value for $name, I just hardcoded the value 'Northeast.' The following, rather than creating an array of json values as before, generates your JavaScript code from your array $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;

Output:

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()
                }
                ]});