如何将JSP数组传递给高图的Javascript以生成柱形图

时间:2013-08-12 17:59:30

标签: javascript json jsp highcharts

我是Highcharts,JSP和Javascript的新手,需要你对这个问题的意见和建议,我一直在努力4-5天。请帮帮我。

问题是我能够从JSP中获取2个输出数组,我需要在高级JS中传递以生成列图。

[99, 90, 87, 82, 80, 77, 70, 65, 65, 60] and
['orcl2','orcl2','orcl2','orcl2','orcl1','orcl1','orcl3','orcl2','orcl3','orcl1']

但是我无法将值传递给JS来生成列图。以下是我正在使用的整个代码。请告诉我我哪里出错了。

<%@ page language="java" import="java.sql.*, java.io.*, java.util.Date, java.util.*,javax.servlet.*, java.text.SimpleDateFormat, java.util.Calendar " %>
<%  Class.forName("oracle.jdbc.driver.OracleDriver"); %>
 <%
     Connection connection=DriverManager.getConnection ("jdbc:oracle:thin:@RAC1.dinu.com:1521:orcl2","cog","cog123");
     Statement statement12 = connection.createStatement();
          ResultSet resultset12 = 
            statement12.executeQuery("select * from(select HOST_NAME,INSTANCE_NAME,PID,PCPU, to_char(TIME, 'yyyy-mm-dd hh24:mi:ss') from ORA_CPU_STATUS where trunc(TIME)=trunc(sysdate) order by PCPU desc) where rownum<=10");
 %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highcharts Example</title>
<script type="text/javascript" src="/DBdashboard/1.js"></script>
<script type="text/javascript" src="/DBdashboard/2a.js"></script>
<script type="text/javascript" src="/DBdashboard/3a.js"></script>
<script type="text/javascript" src="/DBdashboard/highcharts-more.js"></script>
<script type="text/javascript" src="/DBdashboard/json2.js"></script>
        <script>
$(function () {
    var chart;

   <%
List<String> list = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
while(resultset12.next())
{
    String val = resultset12.getString(1);
    list.add(val);
    String val2 = resultset12.getString(2);
    list2.add(val2);

String csv = list2.toString().replace("[", "").replace("]", "");
String csvWithQuote = list.toString().replace("[", "'").replace("]", "'").replace(", ", "','");
%>
var dincpu = '<%=csv%>';
var dinpcat = '<%=csvWithQuote%>';
var input = JSON.parse("[" + dincpu + "]"),
       data = [],
categories = JSON.parse("[" + dinpcat + "]");
    $.each(input, function(index, value){
        var color;
        if (value > 80) color = 'red';
        else if (value > 60) color = 'Orange';
        else color = 'green';
        data.push({y:value, color: color, url:'https://www.google.com'});
    });

    $(document).ready(function() {
        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'COL',
                type: 'column'
            },
            title: {
                text: 'Current Top 10 CPU Consumers',
                    style: {fontSize: '10px'}
            },
            xAxis: {
                categories: categories,
                labels: {
                    rotation: -35,
                    align: 'center'
                }
            },
           yAxis: {
                title: {
                    text: 'Percentage',
                    style: {fontSize: '11px'}
                }
            },
            exporting: { enabled: false },
             legend: {
                enabled: false,
            },
            tooltip: {
                formatter: function() {
                    return '<b>'+ this.x +'</b>' +'- Oracle User Process CPU Consumed :'+'<b>'+ this.y +' % ' +'</b>' ;
                }
            },
       plotOptions: {
            series: {
                cursor: 'pointer',
                point: {
                    events: {
                        click: function() {
                            location.href = this.options.url;
                        }
                    }
                }
            }
        },
                series: [{
                name: 'CPU Consumed',
        pointWidth: 28,
                data: data

            }]
        });
    });
});
</script>
</head>
<body>
<div id="COL" style="min-width: 100px; height: 300px; margin: 0 auto"></div>
</body>
</html>

提前致谢...

3 个答案:

答案 0 :(得分:0)

我不确定你要使用此代码

String csv = list2.toString().replace("[", "").replace("]", "");
String csvWithQuote = list.toString().replace("[", "'").replace("]", "'").replace(", ", "','");
%>
var dincpu = '<%=csv%>';
var dinpcat = '<%=csvWithQuote%>';
var input = JSON.parse("[" + dincpu + "]"),
       data = [],
categories = JSON.parse("[" + dinpcat + "]");

如果您的数据采用以下格式:

[99, 90, 87, 82, 80, 77, 70, 65, 65, 60] and
['orcl2','orcl2','orcl2','orcl2','orcl1','orcl1','orcl3','orcl2','orcl3','orcl1']

为什么不这样做(删除's并摆脱JSON.parse)?

var dincpu = <%=csv%>;
var dinpcat = <%=csvWithQuote%>;

http://jsfiddle.net/VYLTW/

答案 1 :(得分:0)

@Barbara, 它终于用

工作了
var dincpu = <%=csv%>;
var dinpcat = <%=csvWithQuote%>;

以及你提供的小提琴http://jsfiddle.net/VYLTW/。 我将整个代码放在实际有用的答案中。

<%@ page language="java" import="java.sql.*, java.io.*, java.util.Date, java.util.*,javax.servlet.*, java.text.SimpleDateFormat, java.util.Calendar " %>
<%  Class.forName("oracle.jdbc.driver.OracleDriver"); %>
 <%
     Connection connection=DriverManager.getConnection ("jdbc:oracle:thin:@RAC1.dinu.com:1521:orcl2","cog","cog123");
     Statement statement12 = connection.createStatement();
          ResultSet resultset12 = 
            statement12.executeQuery("select * from(select INSTANCE_NAME,PCPU from ORA_CPU_STATUS order by PCPU desc) where rownum<=10");


List<String> list = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
while(resultset12.next())
{
    String val = resultset12.getString(1);
    list.add(val);
    String val2 = resultset12.getString(2);
    list2.add(val2);

}

String csv = list2.toString();
String csvWithQuote = list.toString().replace("[", "['").replace("]", "']").replace(", ", "','");

%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highcharts Example</title>
<script type="text/javascript" src="/DBdashboard/1.js"></script>
<script type="text/javascript" src="/DBdashboard/2a.js"></script>
<script type="text/javascript" src="/DBdashboard/3a.js"></script>
<script type="text/javascript" src="/DBdashboard/highcharts-more.js"></script>
<script type="text/javascript" src="/DBdashboard/json2.js"></script>
<script>
$(function () {
    var dincpu=<%=csv%>;
    var dinpcat = <%=csvWithQuote%>;
    var input = dincpu,
        data = [],
        categories =dinpcat;
        $.each(input, function(index, value){
            var color;
            if (value > 80) color = 'red';
            else if (value > 60) color = 'Orange';
            else color = 'green';
            data.push({y:value, color: color, url:'https://www.google.com'});
        });

    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'COL',
            type: 'column'
        },
        title: {
            text: 'Current Top 10 CPU Consumers',
            style: {fontSize: '10px'}
        },
        xAxis: {
            categories: categories,
            labels: {
                rotation: -35,
                align: 'center'
            }
        },
        yAxis: {
            title: {
                text: 'Percentage',
                style: {fontSize: '11px'}
            }
        },
        exporting: { enabled: false },
        legend: {
            enabled: false,
        },
        tooltip: {
            formatter: function() {
                return '<b>'+ this.x +'</b>' +'- Oracle User Process CPU Consumed :'+'<b>'+ this.y +' % ' +'</b>' ;
            }
        },
        plotOptions: {
            series: {
                cursor: 'pointer',
                point: {
                    events: {
                        click: function() {
                            location.href = this.options.url;
                        }
                    }
                }
            }
        },
        series: [{
            name: 'CPU Consumed',
            pointWidth: 28,
            data: data

        }]
    });
});
</script>
</head>
<body>
<div id="COL" style="min-width: 100px; height: 300px; margin: 0 auto"></div>
</body>
</html>

答案 2 :(得分:0)

@Barbara, 1)如果您看到最新的代码,我已将所有JSP scriptlet“&lt; %%&gt;”在顶部然后调用

var dincpu = <%=csv%>;
var dinpcat = <%=csvWithQuote%>;

之后我可以在网页的视图源中看到值被传递给JS函数中的JS变量。 2)然后把你放在小提琴中的相同JS代码。 3)我觉得函数$(document).ready(function()导致了我删除的问题。 另一个重要的事情是,因为我将所有代码都放在一个虚拟的linux框中,所以当我从windows文本编辑器(Editplus)处理时,可能会有一些隐藏的字符被复制。

我发现所有这三件事都是造成这个问题的原因。