sql >> Databasteknik >  >> RDS >> Mysql

Hur gör du ett tillväxtbebisdiagram med data i mysql + PDO

Detta är det grundläggande ramverket du behöver för att få det här att fungera:

/*
 * $normal is an array of (edad => peso) key/value pairs
 * $desnutricion is an array of (edad => peso) key/value pairs
 * $desnutricionSevera is an array of (edad => peso) key/value pairs
 * 
 * you can hard-code these or pull them from a database, whatever works for you
 */
$sql = $conn->prepare("SELECT edad, peso FROM ESTATURA WHERE <criteria to select baby>");
$sql->execute();
$data = array(array('Meses', $apellido, 'Normal', 'Desnutricion', 'Desnutricion Severa'));
while($row = $sql->fetch(PDO::FETCH_ASSOC))  {
    $edad = $row['edad'];
    // use (int) to parse the value as an integer
    // or (float) to parse the value as a floating point number
    // use whichever is appropriate
    $edad = (int) preg_replace('/\D/', '', $edad);
    $peso = $row['peso'];
    $peso = (float) $peso;

    $data[] = array($edad, $peso, $normal[$edad], $desnutricion[$edad], $desnutricionSevera[$edad]);
}

Sedan, i ditt javascript:

function drawChart() {
    var data = google.visualization.arrayToDataTable(<?php echo json_encode($data); ?>);
    // sort the data by "Meses" to make sure it is in the right order
    data.sort(0);

    var options = {
        title: 'Grafica de Crecimiento de niñas de 0 a 24 meses',
        hAxis: {
            title: 'Meses',
            titleTextStyle: {color: '#333'}
        },
        vAxis: {
            minValue: 0
        },
        series: {
            0: {
                // series options for this babys weight
                type: 'line'
            },
            1: {
                // series options for normal weight
                type: 'area'
            },
            2: {
                // series options for desnutricion
                type: 'area'
            },
            3: {
                // series options for desnutricion severa
                type: 'area'
            }
        }
    };

    var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
    chart.draw(data, options);
}

Prova det och se om det fungerar för dig.




  1. Rekursionsgränsen har överskridits i icke-rekursiv procedur

  2. SQL Hämta hela raden baserat på minimivärdet för den beräknade kolumnen

  3. Hur man skapar en webbplatskarta med PHP &MySQL

  4. mysql2 pärla kompilerad för fel mysql-klientbibliotek