public static void writeScaledChartAsPNG(OutputStream out,JFreeChart chart,int width,int height,int widthScaleFactor,int heightScaleFactor) throws IOException {
  ParamChecks.nullNotPermitted(out,"out");
  ParamChecks.nullNotPermitted(chart,"chart");
  double desiredWidth=width * widthScaleFactor;
  double desiredHeight=height * heightScaleFactor;
  double defaultWidth=width;
  double defaultHeight=height;
  boolean scale=false;
  if ((widthScaleFactor != 1) || (heightScaleFactor != 1)) {
    scale=true;
  }
  double scaleX=desiredWidth / defaultWidth;
  double scaleY=desiredHeight / defaultHeight;
  BufferedImage image=new BufferedImage((int)desiredWidth,(int)desiredHeight,BufferedImage.TYPE_INT_ARGB);
  Graphics2D g2=image.createGraphics();
  if (scale) {
    AffineTransform saved=g2.getTransform();
    g2.transform(AffineTransform.getScaleInstance(scaleX,scaleY));
    chart.draw(g2,new Rectangle2D.Double(0,0,defaultWidth,defaultHeight),null,null);
    g2.setTransform(saved);
    g2.dispose();
  }
 else {
    chart.draw(g2,new Rectangle2D.Double(0,0,defaultWidth,defaultHeight),null,null);
  }
  out.write(encodeAsPNG(image));
}

public static void writeScaledChartAsPNG ( OutputStream out , JFreeChart chart , int width , int height , int widthScaleFactor , int heightScaleFactor ) throws IOException { ParamChecks . nullNotPermitted ( out , "out" ) ; ParamChecks . nullNotPermitted ( chart , "chart" ) ; double desiredWidth = width * widthScaleFactor ; double desiredHeight = height * heightScaleFactor ; double defaultWidth = width ; double defaultHeight = height ; boolean scale = false ; if ( ( widthScaleFactor != 1 ) || ( heightScaleFactor != 1 ) ) { scale = true ; } double scaleX = desiredWidth / defaultWidth ; double scaleY = desiredHeight / defaultHeight ; BufferedImage image = new BufferedImage ( ( int ) desiredWidth , ( int ) desiredHeight , BufferedImage . TYPE_INT_ARGB ) ; Graphics2D g2 = image . createGraphics ( ) ; if ( scale ) { AffineTransform saved = g2 . getTransform ( ) ; g2 . transform ( AffineTransform . getScaleInstance ( scaleX , scaleY ) ) ; chart . draw ( g2 , new Rectangle2D . Double ( 0 , 0 , defaultWidth , defaultHeight ) , null , null ) ; g2 . setTransform ( saved ) ; g2 . dispose ( ) ; } else { chart . draw ( g2 , new Rectangle2D . Double ( 0 , 0 , defaultWidth , defaultHeight ) , null , null ) ; } out . write ( encodeAsPNG ( image ) ) ; }
writes a scaled version of a chart to an output stream in png format .
