2

我正在尝试用我在 DB 中的成长婴儿表制作一个图表...我失去了这个想法,现在我不知道该怎么做...我需要展示的图表 这是我需要在医生插入时显示的图表每个孩子的身高和体重...需要显示插入的百分位数数据,这将取决于婴儿的身高和体重以显示图表(灰线)...

这是我到目前为止的代码(用新代码编辑)

<?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>

我不明白如何使用婴儿变量插入默认变量(正常、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>

里面/*.....*/是我需要用mysql中的数据显示的百分位数......但我评论了因为当数据没有时图表没有显示/*...*/

这里现在的图表..在此处输入图像描述

你能帮我看看我的图表类型吗?

此致

安德烈斯·瓦伦西亚

4

1 回答 1

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

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

于 2013-10-04T22:10:51.883 回答