1

To create REST service i used Jersey API.i can create REST service and retrieve data as well.But the problem that i can not draw chart using REST call.

REST Service

    @Path("/student")
public class Student {

 @Path("/list")
 @GET
 @Produces(MediaType.TEXT_PLAIN)
 public String getStudent() {

  DataTable data = new DataTable();  
         ArrayList<ColumnDescription> cd = new ArrayList<ColumnDescription>();  
         cd.add(new ColumnDescription("name", ValueType.TEXT, "Name"));  
         cd.add(new ColumnDescription("physics", ValueType.NUMBER, "Physics"));  
         cd.add(new ColumnDescription("chemistry", ValueType.NUMBER, "Chemistry"));  
         cd.add(new ColumnDescription("math", ValueType.NUMBER, "Math"));  
         data.addColumns(cd);  
         // Fill the data table.  
         try {  
              data.addRowFromValues("Tasawwar", 60, 50, 70);  
              data.addRowFromValues("Naveed", 40, 80, 60);  
              data.addRowFromValues("Abhi", 50, 40, 80);  
              data.addRowFromValues("Vikas", 80, 60, 40);  
         } catch (TypeMismatchException e) {  
              System.out.println("Invalid type!");  
         }  

         CharSequence charSequence = JsonRenderer.renderDataTable(data, true, true, true);
         return charSequence.toString();
   }
}

HTML Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>
      Google Visualization with ReST web Services
    </title>

    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
    <script type="text/javascript">
   //initialize the lib
    google.load('visualization', '1.0', {packages: ['corechart']});
    google.setOnLoadCallback(drawVisualization);
    function drawVisualization() {
      // Create and populate the data table.
     //ajax call to the web service 
     var data = $.ajax({
          url: "http://localhost:8181/GoogleVizWithRest/rest/student/list",
          dataType:"json",
          async: false
          }).responseText;    

        // Create and draw the visualization.
          var dt = new google.visualization.DataTable(data);

    //ColumnChart
       var table = new google.visualization.ColumnChart(document.getElementById('columnChart_div'));
       var options = {
        title: 'Student Performance',
        width: 400,
          height: 300,
          vAxis: {title: "Total Marks"},
          hAxis: {title: "Student"}
       };
       table.draw(dt, options);
    }
    </script>
  </head>

  <body style="font-family: Arial;border: 0 none;">
      <div id="columnChart_div"></div>
  </body>
</html>

JSON String

{cols:[{id:'name',label:'Name',type:'string',pattern:''},{id:'physics',label:'Physics',type:'number',pattern:''},{id:'chemistry',label:'Chemistry',type:'number',pattern:''},{id:'math',label:'Math',type:'number',pattern:''}],rows:[{c:[{v:'Tasawwar'},{v:60.0},{v:50.0},{v:70.0}]},{c:[{v:'Naveed'},{v:40.0},{v:80.0},{v:60.0}]},{c:[{v:'Abhi'},{v:50.0},{v:40.0},{v:80.0}]},{c:[{v:'Vikas'},{v:80.0},{v:60.0},{v:40.0}]}]}

Just for testing i used above mention example from internet and even that code snippet did not work fine.Please can someone let me know that what was the problem.

gihan-maduranga
  • 4,381
  • 5
  • 41
  • 74

1 Answers1

0

Finally i found a solution for that with the help of following reference. Google charts API JSON not valid

The problem was on Google Visualization DataSource it return not valid json object.To overcome that i used jackson API to rearrange the json obj.

Correct Way

JsonNode root = null;
        String json = JsonRenderer.renderDataTable(data, true, false).toString();

        try{
            JsonParser parser = new JsonFactory().createJsonParser(json)
                .enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES)
                .enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
                 root = new ObjectMapper().readTree(parser);
            }catch(Exception e){
               System.out.println(e.toString());
            }




      return root.toString();
Community
  • 1
  • 1
gihan-maduranga
  • 4,381
  • 5
  • 41
  • 74