使用数据库中的两个变量在 jqplot 中显示图形

display graph in jqplot using two variables from a database

我是 jqplot 的新手,我一直在尝试显示一个图表,其中包含数据库中的日期(x 轴)和值(y 轴)。 我设法将日期和值保存到一个漂亮的字符串中(所有数据都用逗号分隔并且顺序正确)但是当我调用 $.jqplot('chart1', [total1], 它不起作用!:( 我已经尝试了所有方法,但我 运行 没有想法和希望。 任何帮助或指示将不胜感激。 一个绝望的新手的问候

    <?php

        $conn = mysql_connect($host, $user, $password);
        mysql_select_db($database);

 $MenuSelection = $_POST['dropDownMenu'];// select field from dropdownmenu   
        $conn = mysql_connect($host, $user, $password);
        mysql_select_db($database);

 $sql = "SELECT date," . $MenuSelection . " FROM errorscounted where date       between '$CurrentDate' and '$FinalDate'";

            $result = mysql_query($sql);
            $data = array();
            while ($row = mysql_fetch_assoc($result)) {
                $data[] = $row;
            }

            mysql_free_result($result);

    mysql_close();
    ?>

<script type="text/javascript" >

 $(document).ready(function () {
var total1 = "";

// create a for loop to get dates and values and save them in a string
<?php for ($x = 0; $x <= 4; $x++) { ?>
var line1 = [['<?php echo $data[$x]['date'] ?>',
<?php echo $data[$x][$myvalue] ?>], ];

// concatenated the string and seperated the dates and values by a comma
total1 = total1.concat(line1) + ",";
    <?php } ?>
//delete the last comma
 total1 = total1.substring(0, total1.length - 1);

// an alert that shows that all the data was saved correctly
 alert(total1); 

 var plot1 = $.jqplot('chart1', [total1], {
animate: !$.jqplot.use_excanvas,
title: 'Number of errors from <?php echo $_POST['dateStart'] ?> to <?php echo $_POST['dateEnd'] ?> for <?php echo $_POST['dropDownMenu'] ?> ',
                            seriesDefaults: {
                                pointLabels: {show: true}
                            },
                            axesDefaults: {
                                tickRenderer: $.jqplot.CanvasAxisTickRenderer,
                                tickOptions: {
                                    angle: -30,
                                    fontSize: '10pt'
                                }
                            },
                            axes: {
                                xaxis: {
                                    renderer: $.jqplot.CategoryAxisRenderer,
                                    // renderer: $.jqplot.DateAxisRenderer,
                                    tickOptions: {
                                    }
                                },
                                yaxis: {
                                    tickOptions: {
                                    }
                                }
                            },
                            highlighter: {
                                show: false,
                                sizeAdjust: 7.5
                            },
                        });
                    });
                </script>

感谢我的一位同事,我终于解决了我的问题! :)

我给 jqplot 一个字符串,但它需要一个数组!这是我非常简单的解决方案。希望它能帮助别人! 这是我的代码 100% 防弹

            <script type="text/javascript" >

                $(document).ready(function () {
                    var total1 = [];

 // create a for loop to get dates and values and save them in a string
<?php
$j = count($data);
for ($x = 0; $x < $j; $x++) {
    ?>

                        var line1 = ['<?php echo $data[$x]['date'] ?>',<?php echo $data[$x][$myvalue] ?>];
                        total1.push(line1);
<?php } ?>
                    // alert(total1);
                    var plot1 = $.jqplot('chart1', [total1], {
                        animate: !$.jqplot.use_excanvas,
                        title: 'Number of errors from <?php echo $_POST['dateStart'] ?> to <?php echo $_POST['dateEnd'] ?> for <?php echo $_POST['dropDownMenu'] ?> ',
                        seriesDefaults: {
                            pointLabels: {show: true}
                        },
                        axesDefaults: {
                            tickRenderer: $.jqplot.CanvasAxisTickRenderer,
                            tickOptions: {
                                angle: -30,
                                fontSize: '10pt'
                            }
                        },
                        axes: {
                            xaxis: {
                                renderer: $.jqplot.CategoryAxisRenderer,
                                //renderer: $.jqplot.DateAxisRenderer,
                                tickOptions: {
                                }
                            },
                            yaxis: {min: 0,
                            }
                        },
                        highlighter: {
                        },
                    });
                });
            </script>