Announcement Announcement Module
Collapse
No announcement yet.
Not able to render jfreechart image in spring mvc using ajax Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Not able to render jfreechart image in spring mvc using ajax

    Hi, I am working on spring mvc project in which I have to render an image file(based on the data from the database) on the JSP. The image is being generated using the jFreeChart api. The call to the controller is made using ajax. For some reason I am not able to get it to work. Am I missing something here? Any help is highly appreciated.

    *********Snippet from JSP file**********
    <div id="chartButton">
    <input type="button" onclick="return submitToGetCountsAndCharts();" value="Get Form Counts Charts"/>
    </div>

    <div id="chartDisplay" style="float:right;padding-left : 1em;">

    </div>

    function submitToGetCountsAndCharts() {
    $.ajax({
    type:"get",
    data:{statsForYear:$('#yearSelect').val()},
    url:"/myTools/getFormCountsCharts",
    async: false,
    success: function(){
    $('#chartDisplay').html('<img src="/myTools/getFormCountsCharts"/>');
    }
    });
    }

    **********Snippet from the Controller class***********
    @RequestMapping(value = "/getFormCountsCharts", method = RequestMethod.GET)
    public ModelAndView getFormCountsAndRenderChart(@RequestParam(value="s tatsForYear", required=false) String countsForYear,
    HttpServletResponse res) {

    System.out.println("Inside getFormCountsAndRenderChart()");

    if(countsForYear != null) {
    List<FormsCountsDTO> resultSetForYear = formsCountsService.getCountsForYearGiven(countsFor Year);

    try {
    res.setContentType("image/png");

    System.out.println("Generating the charts");
    JFreeChart chart = generateChart(resultSetForYear);
    ChartUtilities.writeChartAsPNG(res.getOutputStream (), chart, 400, 400);

    res.getOutputStream().close();

    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    return null;
    }
Working...
X