English 中文(简体)
谷歌 图1
原标题:Generating Google Chart on the fly with JSP

我正在利用谷歌线图从一个利用共同项目数据库提取的图表价值。 目前,我有工作要做。 <代码>data.addColumn报表在网页上载时通过数据库查询生成。

我的问题是,我也想有两条 com子,可以用来为轴心 pick和最大值,然后更新图表。 目前,我能想做到这一点的唯一途径是用新的参数更新网页。

我更希望只重述图表,而不重载整页。 这是可能的吗? 是否有办法超越文字标签,然后重载图表? 或者还有其他办法?

EDIT:我对如何将数据从数据库转至装货页有麻烦。 我的守则是:

<>strong>loader.html (page the hold themap anddate areas):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
        google.load( visualization ,  1 , {packages: [ corechart ]});
    </script>
    <script type="text/javascript">
        function loadChart()
        {
            var xmlhttp;            
            var minDate = document.getElementById( startDate ).value;
            var maxDate = document.getElementById( endDate ).value;

            if (!minDate)
                minDate =  none ;
            if (!maxDate)
                maxDate =  none ;

            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                {
                    document.getElementById( lineChart ).innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET", "chart-loader.jsp?min=" + minDate +  &max=  + maxDate, true);
            xmlhttp.send();
        }
    </script>
</head>

<body>
    <div id="container">

        <div id="lineChart"></div>

        <div id="startDateBox">
            <label for="startDate">Min Date:</label>
            <input id="startDate" type="text" />
        </div>
        <div id="endDateBox">
            <label for="endDate">Max Date:</label>
            <input id="endDate" type="text" />
        </div>
        <input type="button" value="Update" id="updateBtn" onclick="loadChart()" />

    </div> <!-- End container -->
</body>
</html>

<

<%@ page import="java.sql.*, java.util.ArrayList" %>
<%      
    Connection conn;

    try {
        Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
        conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://...", "user", "pass");
    }
    catch(SQLException e) 
    {
        out.println("SQLException: " + e.getMessage() + "<br/>");
        while((e = e.getNextException()) != null)
            out.println(e.getMessage() + "<br/>");
        throw new UnavailableException(this, "Cannot connect with the specified database.");
    }

    String data = "";
    String minDate = (String)request.getParameter("min");
    String maxDate = (String)request.getParameter("max");
    try
    {
        ResultSet rs;
        Statement stmt = conn.createStatement();
        if (minDate.equals("none") || maxDate.equals("none")) // First load, use whole table
        {
            rs = stmt.executeQuery("SELECT ...");
        }
        // ... more possible queries

        ArrayList<String> ptVals = new ArrayList<String>();
        ArrayList<String> colDates = new ArrayList<String>();

        while (rs.next())
        {
            ptVals.add(rs.getString("avgvalue"));
            colDates.add(rs.getString("collectiondate"));
        }           
        // ptVals.size()
        for (int i = 0; i < 5; i++)
            data += "			data.addRow([ " + colDates.get(i) + " ," + ptVals.get(i) + "]);
";  

        // ... Use out.print to spit out script. 
    }
    catch(SQLException e)
    {
        data = "SQLException: " + e.getMessage() + "<br/>";
        while((e = e.getNextException()) != null)
            data += e.getMessage() + "<br/>";           
    }       
    finally
    {   // Clean up resources, close the connection.
        if(conn != null)
        {
            try { conn.close(); }
            catch (Exception ignored) {}
        }
    }
%>

Original script to load the chart

<script type="text/javascript">
    function drawVisualization() 
    {
        // Create and populate the data table.
        var data = new google.visualization.DataTable();
        data.addColumn( string ,  Date );
        data.addColumn( number ,  Temperature );
        data.addRow(["01/01/11", 70.2]);
        data.addRow(["01/02/11", 70.0]);
        data.addRow(["01/03/11", 69.8]);
        data.addRow(["01/04/11", 70.1]);
        // Create and draw the visualization.
        new google.visualization.LineChart(document.getElementById( lineChart )).
            draw(data, {backgroundColor:  transparent ,
                        width: 700, height: 400,
                        legend:  none ,
                        hAxis: {title:  Dates , titleTextStyle: {color:  black , fontSize: 12, fontName:  Verdana, Arial }},
                        vAxis: {title:  Temperature , titleTextStyle: {color:  black , fontSize: 12, fontName:  Verdana, Arial }},
                        chartArea: {left: 80, top: 20}
                    }
                );
    }

    google.setOnLoadCallback(drawVisualization);
</script> 

Thanks, Ryan

最佳回答

我得以与谷歌图像化预报合作。 在这方面:

我有一张超文本页面,有一张集装箱<代码>div,用于图表<div id=“lineChart”></div>

接下来,我在超文本页上有两个 Java本功能。 )功能是宇宙航空研究开发机构的一个功能,其日期从两个投入领域不等,并发送给空间研究小组的网页。 The JSP page use thedate to query the database for records in that range, Format them as a Javacast Object, and sents re-ROM using out.print().

传真网页一旦收到数据,就使用eval(>,将收到的文本列为 Java本。 最后,数据标的发送到第二个 Java功能loadChart(),该功能使用数据称谷歌功能,并将图表输入div

注:使用<代码>eval() ,将responseText 至Java 标本,是使之真正发挥作用的。 这使你能够把许多数据带回。 当你回到超文本页后,你就把他们从联合材料中删除。 您甚至可以相互通过几个联合材料。

var paramsObj;
var dataObj;
eval( paramsObj =   + xmlhttp.responseText + ";");
eval( dataObj = paramsObj.data; );
loadChart(dataObj, paramsObj.title, paramsObj.xLabel);

在该案中,Im在一份联合材料中穿过三个参数。 <代码>paramsObj.data是图表数据,该数据是按谷歌视觉化预报格式编排的密封物体。 我不得不使用<代码>eval() ,再次将所封存的联合物体确定为一个单独的变量,或者对<代码>上载体(<>功能正确操作。

问题回答

我们确实是,在我工作的地方(我只想再说一角图)。 我们做的是没有图表的页数,只是Ajax载到它所需要的任何集装箱。 然后,当你改变任何情况时,他会重新上载,把你需要的任何参数寄给总统府(新图表)。

EDIT: In case that didn t make sense, the chart itself is actually on it s own JSP that gets injected into the page (and refreshed) independently.

因此,我们的进程是:

Render main page (charts are NOT rendered yet). On this page, we put in something like this:

<div id="chartForData1"></div>

<script>Ajax_inject (图表ForData1 , pageThatGeneratesData1 );</script>

在<代码>Ajax_inject<>/code>上,仅是Ajax呼吁把数据输入一个元件的包裹。 现在,在用户计算机上张贴网页时,Ajax_inject (和所有其他)将执行,从而装入相关的<条码>div,相应的已提交联合执行监督委员会。 这些共同财产计划的唯一目的就是采用两种参数(或无,使用缺省),使我们成为冰冰和粗gle图。

这是否更有意义?





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签