如何使用mysql + PDO中的数据制作增长宝贝图表

时间:2013-10-04 20:28:07

标签: mysql pdo charts highcharts google-visualization

我正在努力制作一张包含我在DB中的增长婴儿餐桌的图表...我失去了想法,现在我不知道该怎么做... The chart I need to show这是图表我当医生插入每个孩子的身高和体重时需要显示...需要显示插入的百分位数据,这将取决于婴儿的身高和体重来显示图表(灰线)...

这是我的代码,直到现在 (使用新代码编辑)

<?php
include 'includes/configs.php';
/*
 * $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 * FROM ESTATURA WHERE edad<>'' AND peso<>'' AND id_paciente = 1");
$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) preg_replace('/\D/', '', $peso);

    $data[] = array($peso, $edad, $normal[$edad], $desnutricion[$edad], $desnutricionSevera[$edad]);
$data1[] = array($peso, $edad);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title> </title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
        google.load("visualization", "1", {packages:["corechart"]});
        google.setOnLoadCallback(drawChart);
        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: {
                        <?php echo implode(",", $peso); ?>
                        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);
        }
    </script>
</head>
<body>
    <div id="chart_div" style="width: 800px; height: 400px;"></div>
</body>
</html>

我不明白如何使用baby变量插入默认变量(normal,desnutricion和desnutricion severa)..我需要创建一个带有默认数据的新表然后建立联合?或者只是在每个系列中插入变量??

- 旧代码 -

<?php
include 'includes/configs.php';

$sql = $conn->prepare("SELECT nombre, apellido, edad, peso FROM ESTATURA WHERE edad<>'' AND peso<>'' ");
$sql->execute();
while($row = $sql->fetch(PDO::FETCH_ASSOC))  {    
    $nombre = trim(addslashes($row['nombre']));
    $lapellido = trim(addslashes($row['apellido']));
    $edad = $row['edad'];
    $edad = preg_replace('/\D/', '', $edad);
    $peso = $row['peso'];
    $peso  = preg_replace('/\D/', '', $peso);

    $myurl[] = "['".$nombre." ".$apellido."', ".$edad.",".$peso."]";


    }    

    print_r($myurl);
    echo implode(",", $myurl);
?>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Meses', 'Normal', 'Desnutrición', 'Desnutrición Severa'],
          /*['0',  4.23,      2.39,      2.00],
          ['1',  5.55,      3.10,      2.85],
          ['2',  6.75,      3.95,      3.41],
          ['3',  7.60,      4.50,      4.00],
          ['4',  8.23,      5.00,      4.40],
          ['5',  8.81,      5.38,      4.80],
          ['6',  9.30,      5.71,      5.11],
          ['7',  9.87,      6.00,      5.38],
          ['8',  10.19,      6.21,      5.58],
          ['9',  10.56,      6.47,      5.76],
          ['10',  10.95,      6.66,      5.95],
          ['11',  11.20,      6.80,      6.10],
          ['12',  11.55,      7.00,      6.21],
          ['13',  11.91,      7.20,      6.40],
          ['14',  12.10,      7.38,      6.58],
          ['15',  12.37,      7.54,      6.77],
          ['16',  12.60,      7.75,      6.85],
          ['17',  12.96,      7.86,      7.00],
          ['18',  13.16,      8.05,      7.20],
          ['19',  13.41,      8.20,      7.31],
          ['20',  13.72,      8.38,      7.42],
          ['21',  14.02,      8.49,      7.61],
          ['22',  14.24,      8.70,      7.79],
          ['23',  14.68,      8.90,      7.95],
          ['24',  14.90,      9.00,      8.00]*/
            <?php echo implode(",", $myurl); ?>
        ]);

        var options = {
          title: 'Grafica de Crecimiento de niñas de 0 a 24 meses',
          hAxis: {title: 'Meses',  titleTextStyle: {color: '#333'}},
          vAxis: {minValue: 0}
        };

        var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>

<div id="chart_div" style="width: 800px; height: 400px;"></div>
<{1>}里面的/*.....*/是我需要用mysql中的数据显示的百分位数......但是我发了一下,因为当数据没有/*...*/

现在是图表.. enter image description here

你可以用我的图表来帮助我吗?

最好的问候

AndrésValencia

1 个答案:

答案 0 :(得分:0)

这是使这项工作所需的基本框架:

/*
 * $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]);
}

然后,在你的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);
}

尝试一下,看看它是否适合你。