2017-08-09 59 views
0

我有一個非常基本的JSP代碼。它返回從Oracle數據庫如何獲得甲骨文變量納入Amchart在JSP

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"); 

3個變量這是圖表的代碼,它是一個柱狀圖 -

<!-- 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行)到圖表的邏輯幫助。

請協助。

回答

0

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'網站每個演示使用。)

我不是專家在JSP或最佳做法(雖然this popped up in my brief foray into JSP,這可能是你應該考慮),但你可以使用JSON庫像org.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); 
} 

一旦你有你的陣列,其打印出來的數據提供程序部分的makeChart電話:

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