从数据库中获取数据并将其插入折线图中

时间:2017-06-11 00:01:19

标签: javascript java mysql highcharts

我有mysql数据库中的数据,我想在线图中绘制我正在使用highcharts,我想将这些数据插入到数组中, 我正在使用这段代码,问题是数据不会出现在图表中,请帮助我PLZ

<div id="container"
                style="min-width: 310px; height: 400px; margin: 0 auto"></div>
            <script type="text/javascript">
            <%
    String hostdb = "localhost:3306"; // MySQl host
        String userdb = "root"; // MySQL username
        String passdb = "Jenkov31994"; // MySQL password
        String namedb = "akbd"; // MySQL database name

        // Establish a connection to the database
        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        Connection con = DriverManager.getConnection("jdbc:mysql://" + hostdb + "/" + namedb, userdb, passdb);

        String sql = "SELECT * FROM climdata";
        PreparedStatement pt = con.prepareStatement(sql);
        ResultSet rs = pt.executeQuery();
        ResultSet rsize = rs;
        int i = 0;
        int j = 0;
        while (rsize.next()) {
    j++;
        }

        String cat[] = new String[j];
        int ht[] = new int [j];
        int lt[] = new int[j];
        int at[] = new int[j];

        while (rs.next()) { 

    java.sql.Date date = rs.getDate("dateinsert");
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    String dateStr = dateFormat.format(date);
    cat[i] = dateStr; 
   ht[i] = rs.getInt("hightemp");
   lt[i] = rs.getInt("lowtemp");
   at[i] = rs.getInt("avgtemp");
        i++;
    }
    out.print("chart();");
%>
function chart (){
                Highcharts.chart('container', {
                    chart : {
                        type : 'line'
                    },
                    title : {
                        text : 'daily Temperature'
                    },
                    subtitle : {
                        text : 'ZONE H'
                    },
                    xAxis : {
                        categories :'<%=cat%>'

                },
                    yAxis : {
                        title : {
                            text : 'emperature (C)'
                        }
                    },
                    plotOptions : {
                        line : {
                            dataLabels : {
                                enabled : true
                            },
                            enableMouseTracking : false
                        }
                    },
                    series : [
                            {
                                name : 'MaxTemperature',
                                data : '<%=ht%>'
                            },
                            {
                                name : 'AVGTemperature',
                                data : '<%=at%>'
                            },
                            {
                                name : 'MinTemperature',
                                data : '<%=lt%>'
                            }, ]
                });}
            </script>

0 个答案:

没有答案