简体   繁体   English

ChartJS来自Java程序的折线图或条形图

[英]ChartJS line chart or bar graph from a Java program

I have a MVC design java program which returns Year wise counts of a given table from two different databases, I have returned three lists (Years, OracleCounts and HiveCounts) from databases. 我有一个MVC设计java程序,它从两个不同的数据库返回给定表的Year wise计数,我从数据库返回了三个列表(Years,OracleCounts和HiveCounts)。 Right now I have come till returning these three lists to the servlet from database after the user selects a drop down option from a JSP page, however I am not sure how to display the chart js graph (either line or bar graph) in the jsp page. 现在我已经来到用户从JSP页面选择下拉选项后将这三个列表从数据库返回到servlet,但是我不知道如何在jsp中显示图表js图形(行或条形图)页。 I would like to display years (range would be from 2000 to 2016) in X-axis and counts in Y-axis with two bars one for each database. 我希望在X轴上显示年份(范围从2000到2016)并在Y轴上计数,每个数据库有两个条形。

JSP code as below JSP代码如下

 <table>
  <tr>
  <td>
    <div class ="canvasdemochartjs">
          <select class="selectClass" name="CHARTJSDEMO" id="CHARTJSDEMO" >
    <option value="none">-- Select a Table --</option>
    <option value="XXCSS_O.XXCSS_IBAE_SxxxT">XXCSS_O.XXCSS_</option>
     </select>

    </div>
   <canvas id="canvasdemo" height="300" width="300"></canvas>
   <div class=""  id="result1" style="display: none"  ></div>
  </td>
  </tr>
  </table>

JQuery: JQuery的:

$(document).ready(function() {
$('#CHARTJSDEMO').change(function() {
  var CHARTJSDEMORUNOPTION = $('#CHARTJSDEMO').val();
  var chartData = {
          labels: [], // currently empty will contain all the labels for the data points
          datasets: [
            {
              label: "Oracle",
              fillColor: "rgba(220,220,220,0.2)",
              strokeColor: "rgba(220,220,220,1)",
              pointColor: "rgba(220,220,220,1)",
              pointStrokeColor: "#fff",
              pointHighlightFill: "#fff",
              pointHighlightStroke: "rgba(220,220,220,1)",
              data: [] // currently empty will contain all the data points for bills
            },
            {
              label: "Hive",
              fillColor: "rgba(151,187,205,0.2)",
              strokeColor: "rgba(151,187,205,1)",
              pointColor: "rgba(151,187,205,1)",
              pointStrokeColor: "#fff",
              pointHighlightFill: "#fff",
              pointHighlightStroke: "rgba(151,187,205,1)",
              data: [] // currently empty will contain all the data points for bills
            }
          ]
        };  

$.ajax({
type:'POST',    
url: "DemoChartJS",
data: {CHARTJSDEMORUNOPTION: CHARTJSDEMORUNOPTION},
cache: false,
success: function(result) {
     alert("inside jquery"+result);
    $.each(result , function (index, value){
        console.log(value);
        alert("inside first list: "+value)

var ctx = document.getElementById("canvasdemo").getContext("2d");
    ctx.canvas.width = 1000;
    ctx.canvas.height = 800;

    var myChart = new Chart(ctx).Bar(chartData);
 }
});
});
});

Servlet: Servlet的:

@SuppressWarnings("rawtypes")
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    int num = Integer.parseInt(request.getParameter("CHARTJSDEMORUNOPTION"));
    System.out.println("option in dopost: "+num);
    response.setContentType("text/html");
    List CompositeList =new ArrayList();
    ChartJSDemo_Implementation ChartJSDemo =new ChartJSDemo_Implementation();

    CompositeList=ChartJSDemo.GetCountsData("XXCSS_O.XXCSS_IBAE_SxxT", "xxxxx", "xxxxx", "xxxxxx$");



}

I referred here and here but could not understand how to pass the data from servlet to Jquery 在这里这里提到但是无法理解如何将数据从servlet传递给Jquery

After few browsing I tried to print the data from servlet to Jquery it prints as expected, showing three lists, however I could not iterate it and push it to chart data 几次浏览之后,我尝试将数据从servlet打印到Jquery,它按预期打印,显示三个列表,但是我无法迭代它并将其推送到图表数据

@Vinod your Servlet code is wrong. @Vinod你的Servlet代码是错误的。 Why are you calling doGet() method inside doPost() ? 你为什么在doPost()中调用doGet()方法?

And the content type returned should be application/json not text/html. 返回的内容类型应该是application / json而不是text / html。

Define a PrintWriter & write the data in the format your canvas chart expects. 定义PrintWriter并以画布图表期望的格式写入数据。 Typically as a key value pair. 通常作为键值对。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM