Enterprise Integration Zone is brought to you in partnership with:

A passionate Java Web Developer and Blogger.Continuously testing various technologies and publishing it as post to my blog. Sandeep is a DZone MVB and is not an employee of DZone and has posted 15 posts at DZone. You can read more from them at their website. View Full User Profile

Integrating Chart JS Library With Java

  • submit to reddit
  • "Chart JS Library" provides API for drawing different charts.
  • Drawing is based on HTML CANVAS Element.
  • Download Link:-
  • In this Demo"We will draw a Radar Chart .The Student  input data is JSON in nature.The Servlet returns the JSON data when called by Jquery Ajax method.The Student Java class object is converted to JSON representation using GSON Library".
  • The Java web project structure,

  • The Student Servlet StudentJsonDataServlet.java ,

    package com.sandeep.chartjs.servlet;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import com.google.gson.Gson;
    import com.sandeep.chartjs.data.Student;
    public class StudentJsonDataServlet extends HttpServlet {
     private static final long serialVersionUID = 1L;
     public StudentJsonDataServlet() {
     protected void doGet(HttpServletRequest request,
       HttpServletResponse response) throws ServletException, IOException {
      List<Student> listOfStudent = getStudentData();
      Gson gson = new Gson();
      String jsonString = gson.toJson(listOfStudent);
     private List<Student> getStudentData() {
      List<Student> listOfStudent = new ArrayList<Student>();
      Student s1 = new Student();
      return listOfStudent;

  • The HTML markup chartjs-demo.html,

      <title>Chart JS Library Demo</title>
      <canvas id="canvas" height="500" width="500"></canvas>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
      <script type="text/javascript" src="./js/Chart.min.js"></script>
      <script type="text/javascript" src="./js/ts-chart-script.js"></script>
  • The java script file for radar chart ts-chart-script.js,

      /*Makes the AJAX calll (synchronous) to load a Student Data*/
      loadStudentData : function(){
       var formattedstudentListArray =[];
          async: false,
          url: "StudentJsonDataServlet",
          success: function(studentJsonData) {
       return formattedstudentListArray;
      /*Crate the custom Object with the data*/
      createChartData : function(jsonData){
       return {
        labels : ["Mathematics", "Computers", "History","Literature", "Geography"],
        datasets : [
          fillColor : "rgba(255,0,0,0.3)",
          strokeColor : "rgba(0,255,0,1)",
          pointColor : "rgba(0,0,255,1)",
          pointStrokeColor : "rgba(0,0,255,1)",
          /*As Ajax response data is a multidimensional array, we have 'student' data in 0th position*/
          data : jsonData[0]
      /*Renders the Chart on a canvas and returns the reference to chart*/
       var context2D = document.getElementById("canvas").getContext("2d"),
        myRadar = new Chart(context2D).
                   scaleShowLabels : false,
                   pointLabelFontSize : 10
        return myRadar;
      /*Initalization Student render chart*/
      initRadarChart : function(){
       var studentData = TUTORIAL_SAVVY.loadStudentData();
        chartData = TUTORIAL_SAVVY.createChartData(studentData);
        radarChartObj = TUTORIAL_SAVVY.renderStudenrRadarChart(chartData);
    • The response Json data format for student,

    • The Firebug console shows DOM Element,

    • The output in browser will look like,This radar chart shows the a student('sandeep') marks in different subject,

Published at DZone with permission of Sandeep Patel, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)