Friday, July 20, 2012

JfreeChart using restful service without stored on disk

 Hello Friends 


package com;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

@Path("helloworld")
public class test {

        @GET
    @Produces("image/png")
    public Response getMessage() throws IOException {
                DefaultCategoryDataset dataset = new DefaultCategoryDataset();
                dataset.setValue(10, "Profit1", "Jane");
                dataset.setValue(10, "Profit2", "Jane");
                dataset.setValue(7, "Profit1", "Tom");
                dataset.setValue(10, "Profit2", "Tom");
                dataset.setValue(8, "Profit1", "Jill");
                dataset.setValue(8, "Profit2", "Jill");
                dataset.setValue(5, "Profit1", "John");
                dataset.setValue(6, "Profit2", "John");
                dataset.setValue(1, "Profit1", "Fred");
                dataset.setValue(5, "Profit2", "Fred");

               
                final JFreeChart chart = ChartFactory.createBarChart3D(
                   "Comparison between Salesman",  // Chart name
                   "Salesman",                     // X axis label
                   "Value ($)",                    // Y axis value
                   dataset,                        // data set
                   PlotOrientation.VERTICAL,
                   true, true, false);
               
       
                BufferedImage image  = chart.createBufferedImage(600, 600);
                ByteArrayOutputStream byteArray = new ByteArrayOutputStream() ;
                ChartUtilities.writeBufferedImageAsPNG(byteArray, image) ;
                return Response.ok(new ByteArrayInputStream(byteArray.toByteArray())).build();
            }
              }

don't call methods in for loop for checking condition e.g. length() size() etc.


don't call methods in for loop for checking condition e.g. length() size() etc.
insted of  doing stored in variable
  when you write call method in for loop condition then every times call method
so that you can see example



public class FOR
{
    public static void main(String str[])
    {
       
        String s="vipul";
   
        for(int i =0 ; i<s.length(); i++)//every times length methods called
            System.out.println(i);
}
}

output
0
1
2
3
4

see example 2
public class FOR
{
    public static void main(String str[])
    {
      
        String s="vipul";
      
        for(int i =0 ; i<LENGTH(); i++)    //  called every times LENGTH() methods
            System.out.println(i);
          
      
    }
    public static int LENGTH()
    {
        System.out.println("LENGTH");
        return 5;
    }
}

outputLENGTH
0
LENGTH
1
LENGTH
2
LENGTH
3
LENGTH
4
LENGTH

correct example 3
public class FOR
{
    public static void main(String str[])
    {
       
        String s="vipul";
   
        int len=s.length();  //only one times methods called
        for(int i =0 ; i<len; i++)
            System.out.println(i);
       
    }
}

output
0
1
2
3
4