如何在JSP中将Oracle变量导入Amchart

时间:2017-08-09 10:51:46

标签: jsp oracle11g amcharts

我有一个非常基本的JSP代码。它从Oracle数据库中返回3个变量

    String program_name = "";
        Integer projects = 0;
        Integer operations = 0;

        statement = connection.createStatement();
        if(deger.equals("All Priorities"))
        {
        rs = statement.executeQuery("SELECT PROGRAM_NAME prog_name, (SELECT COUNT(PRJ_TYPE) FROM EAD_PRJ_CURR_LIST LST WHERE LST.PROGRAM_ID= PG.PGM_PROGRAM_ID AND LST.PRJ_TYPE = 'Project') Project,(SELECT COUNT(PRJ_TYPE) FROM EAD_PRJ_CURR_LIST LST WHERE LST.PROGRAM_ID= PG.PGM_PROGRAM_ID AND LST.PRJ_TYPE = 'Operation') Operation FROM KCRT_FG_PFM_PROGRAM PG");
        }
        else
        {
        rs = statement.executeQuery("SELECT PROGRAM_NAME prog_name, (SELECT COUNT(PRJ_TYPE) FROM EAD_PRJ_CURR_LIST LST WHERE LST.PROGRAM_ID= PG.PGM_PROGRAM_ID AND LST.PRJ_TYPE = 'Project') AS Project,(SELECT COUNT(PRJ_TYPE) FROM EAD_PRJ_CURR_LIST LST WHERE LST.PROGRAM_ID= PG.PGM_PROGRAM_ID AND LST.PRJ_TYPE = 'Operation') as Operation FROM KCRT_FG_PFM_PROGRAM PG");
        }
        while(rs.next())



          program_name = rs.getString("prog_name");
          projects = rs.getInt("Project");
          operations = rs.getInt("Operation");

这是图表的代码,它是条形图 -

    <!-- Chart code -->
                <script>
                var chart = AmCharts.makeChart("chartdiv", {
                    "type": "serial",
                     "theme": "light",
                    "categoryField": "List_Of_Programs",
                    "rotate": true,
                    "startDuration": 1,
                    "categoryAxis": {
                        "gridPosition": "start",
                        "position": "left"
                    },
                    "trendLines": [],
                    "graphs": [
                        {
                            "balloonText": "Projects:[[value]]",
                            "fillAlphas": 0.8,
                            "id": "AmGraph-1",
                            "lineAlpha": 0.2,
                            "title": "Projects",
                            "type": "column",
                            "valueField": "Projects"
                        },
                        {
                            "balloonText": "Operations:[[value]]",
                            "fillAlphas": 0.8,
                            "id": "AmGraph-2",
                            "lineAlpha": 0.2,
                            "title": "Operations",
                            "type": "column",
                            "valueField": "Operations"
                        }
                    ],
                    "guides": [],
                    "valueAxes": [
                        {
                            "id": "ValueAxis-1",
                            "position": "top",
                            "axisAlpha": 0
                        }
                    ],
                    "allLabels": [],
                    "balloon": {},
                    "titles": [],
                    "dataProvider": [
                        {
                            "List_Of_Programs": <%=projects%>,
                            "Projects": <%=projects%>,
                            "Operations": <%=operations%>
                        }],
                    "export": {
                        "enabled": true
                     }

                });
                </script>


                  <div id="chartdiv"></div>    

我将这两个封装在JSP文件中。代码编译正常,没有任何问题。但是,我需要帮助将先前查询(大约有20行)中的数据打印到图表中。

请协助。

1 个答案:

答案 0 :(得分:0)

dataProvider需要是一个对象 s (注意,复数)的数组。您正在做的事情似乎是将每个属性中的单个数组(?)设置为dataProvider数组中的单个对象。 dataProvider对象应该类似于这样(使用随机数据作为示例):

"dataProvider": [{
  "List_Of_Programs": "Program A",
  "Projects": 13,
  "Operations": 10
}, {
  "List_Of_Programs": "Program B",
  "Projects": 25,
  "Operations": 28
}. 
// continue for each array element
]

(请注意,此格式用于AmCharts&#39;网站上的每个演示。)

我不是JSP或最佳实践方面的专家(虽然this popped up in my brief foray into JSP,这可能是你应该研究的东西),但你可以使用像org.json这样的JSON库来放置所有将您的值放入一个对象数组中,然后将该数组序列化为类似于上面示例的字符串。例如,使用上面的代码:

//assumes you're using the org.json library
JSONArray jsonArray = new JSONArray();
while(rs.next()) {
  JSONObject obj = new JSONObject();
  obj.put("List_Of_Programs", rs.getString("prog_name"));
  obj.put("Projects", rs.getInt("Project"));
  obj.put("Operations", rs.getInt("Operation"));
  jsonArray.put(obj);
}

获得数组后,请在dataProvider部分的makeChart调用中将其打印出来:

var chart = AmCharts.makeChart("chartdiv", {
   // other properties omitted
  "dataProvider": <%=jsonArray%> //will serialize as the array from above
});