0

我是 Highcharts、JSP 和 Javascript 的新手,需要您对这个问题的意见和建议,我从 4 到 5 天一直在苦苦挣扎。请帮我。

问题是我能够从 JSP 中获取 2 个输出数组,我需要将它们传递给 highchart 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>

提前致谢...

4

3 回答 3

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>
于 2013-08-13T09:26:07.537 回答
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/

于 2013-08-12T18:52:12.043 回答
0

@Barbara,1)如果您看到最新的代码,我已将所有 JSP 脚本“<% %>”放在顶部,然后调用

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

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

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

于 2013-08-13T16:02:52.153 回答