jfreechart 학습 노트


package com.yong;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;

/**
 * 
 * @author    
 *
 */
public class Pie {
	public static void main(String[] args) {
		//  
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("  ", 43.2);
		dataset.setValue("Category 2", 27.9);
		dataset.setValue("Category 3", 79.5);
		dataset.setValue("Category 4", null);
		dataset.setValue("Category 5", 0);
		dataset.setValue("Category 6", -50.0f);
		//   Jfreechart    
		JFreeChart jfreechart = ChartFactory.createPieChart("Simple Pie Chart", dataset, true, true, false);
		PiePlot piePlot = (PiePlot) jfreechart.getPlot();
		//        
		piePlot.setSectionPaint(33,Color.blue);
		piePlot.setSectionPaint(2,Color.red);
		piePlot.setSectionPaint(3,Color.yellow);
		
		//  Pie       
		piePlot.setSectionOutlinesVisible(true);
		//         (0.0-1.0)
		piePlot.setForegroundAlpha(1.0f);
		//       
		piePlot.setBaseSectionOutlinePaint(Color.green);
		//       ,new BasicStroke(2.0f)
		piePlot.setBaseSectionOutlineStroke(new BasicStroke(1));
		//    ,0 ,        ,        false
		piePlot.setIgnoreNullValues(true);
		piePlot.setIgnoreZeroValues(true);
		
		//       ,0  KEY,1  VALUE,2      ,DecimalFormat          
		piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}=>{1}({2})",NumberFormat.getNumberInstance(),new DecimalFormat("0.00%")));
		
		//         ,0  KEY,1  VALUE,2      ,DecimalFormat          
		piePlot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}=>{1}({2})",NumberFormat.getNumberInstance(),	new DecimalFormat("0.00%")));
		
		//    , Pie       ,   3D
		piePlot.setExplodePercent(1, 0.10);
		piePlot.setExplodePercent(2, 0.10);
		piePlot.setExplodePercent(3, 0.10);
		
		//      
		Font font = new Font("    ", Font.CENTER_BASELINE, 12);
		//      
		TextTitle title = new TextTitle("Pie     ");
		//       
		title.setFont(font);
		//          
		jfreechart.setTitle(title);
		
		//    ,            ,     
		jfreechart.getLegend().setItemFont(font);
		//Pie    
		piePlot.setLabelFont(font);
		
		//     
		FileOutputStream fos_jpg = null;
		try {			
			fos_jpg = new FileOutputStream("c:\\Pie.jpg");
			//          ,      ,JPG,PNG,   MAP
			ChartUtilities.writeChartAsJPEG(fos_jpg,0.99f,jfreechart,640,480,null);
			fos_jpg.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


package com.yong;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;


/**
 * 
 * @author     
 *
 */
public class Pie3D {

	public static void main(String[] args) throws UnsupportedEncodingException {
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("  123", 43.2);
		dataset.setValue("Category 2", 27.9);
		dataset.setValue("Category 3", 27.9);
		dataset.setValue("Category 4", null);
		dataset.setValue("Category 5", 0);
		dataset.setValue("Category 6", -50.0f);
		
		JFreeChart jfreechart = ChartFactory.createPieChart3D("Simple Pie Chart", dataset, true, true, false);
		PiePlot3D piePlot = (PiePlot3D) jfreechart.getPlot();
		//        
		//piePlot.setSectionPaint("Category 1",Color.blue);
		//piePlot.setSectionPaint("Category 2",Color.red);
		//piePlot.setSectionPaint("Category 3",Color.yellow);
		
		//  Pie       
		piePlot.setSectionOutlinesVisible(true);
		
		//       
		piePlot.setBaseSectionOutlinePaint(Color.green);
		//       ,new BasicStroke(2.0f)
		piePlot.setBaseSectionOutlineStroke(new BasicStroke(0));
		//    ,0 ,        ,        false
		piePlot.setIgnoreNullValues(true);
		piePlot.setIgnoreZeroValues(true);
		
		//       ,0  KEY,1  VALUE,2      ,DecimalFormat          
		piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}=>{1}({2})",NumberFormat.getNumberInstance(),new DecimalFormat("0.00%")));
		
		//         ,0  KEY,1  VALUE,2      ,DecimalFormat          
		piePlot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}=>{1}({2})",NumberFormat.getNumberInstance(),	new DecimalFormat("0.00%")));
		
		//    , Pie       ,   3D
		piePlot.setExplodePercent(11, 0.10);
		piePlot.setExplodePercent(2, 0.10);
		piePlot.setExplodePercent(3, 0.10);
		
		//      
		Font font = new Font("    ", Font.CENTER_BASELINE, 12);
		//      
		TextTitle title = new TextTitle("Pie  ");
		//       
		title.setFont(font);
		//          
		jfreechart.setTitle(title);
		
		//    ,            ,     
		jfreechart.getLegend().setItemFont(font);
		//Pie    
		piePlot.setLabelFont(font);
		
		FileOutputStream fos_jpg = null;
		try {
			fos_jpg = new FileOutputStream("c:\\Pie3D.jpg");
			ChartUtilities.writeChartAsJPEG(fos_jpg, 0.99f, jfreechart, 640, 480,null);
			fos_jpg.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}


package com.yong;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 * 
 * @author    
 *
 */
public class Bar {
	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {
		//    
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		//     
		dataset.addValue(1.0, "  ", "  ");
		dataset.addValue(7.0, "  ", "  ");
		dataset.addValue(-3.0, "  ", "  ");
		dataset.addValue(2.0, "  ", "  ");
		dataset.addValue(3.0, "  ", "  ");
		dataset.addValue(2.0, "  ", "  ");
		//   JFreeChart   
		JFreeChart chart = ChartFactory.createBarChart("Bar Chart   ","  (X)", "  (Y)", dataset,PlotOrientation.VERTICAL,true,true,false);
		//  JFreeChart      , :    ,  ,    
		Bar.configFont(chart);
		//  
		FileOutputStream fos_jpg = null;
		try {
			fos_jpg = new FileOutputStream("c:\\Bar11.jpg");
			ChartUtilities.writeChartAsJPEG(fos_jpg,0.99f,chart,800,600,	null);
			fos_jpg.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private static void configFont(JFreeChart chart) {
		//      
		chart.setBorderStroke(new BasicStroke(1));
		//        
		chart.setBorderVisible(true);
		//      
		chart.setBorderPaint(Color.cyan);		
		//      
		//chart.setBackgroundPaint(Color.YELLOW);		
		//     
		Font xfont = new Font("  ", Font.CENTER_BASELINE, 20);// X 
		Font yfont = new Font("  ", Font.CENTER_BASELINE, 20);// Y 
		Font kfont = new Font("  ", Font.CENTER_BASELINE, 18);//   
		Font titleFont = new Font("    ", Font.CENTER_BASELINE, 25); //     
		CategoryPlot plot = chart.getCategoryPlot();//          
		//         
		plot.setRangeGridlinePaint(Color.BLUE);
		//         
		plot.setRangeGridlineStroke(new BasicStroke(1.0f));
		//     
		chart.setTitle(new TextTitle(chart.getTitle().getText(),titleFont));

		//       ,    
		chart.getLegend().setItemFont(kfont);

		//X 
		CategoryAxis domainAxis = plot.getDomainAxis();
		//  X     
		domainAxis.setLabelFont(xfont);
		//  X   
		domainAxis.setTickLabelFont(xfont);
		//      
		domainAxis.setTickLabelPaint(Color.BLUE);
		//    label   
		domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); 
		//domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
		//     ,         
		//         , BAR BAR        
		//domainAxis.setCategoryMargin(0.2f);
		//    ( )  ,        
		domainAxis.setLowerMargin(0.1);
		//    ( )  ,         
		domainAxis.setUpperMargin(0.1);
		
		//Y  
		ValueAxis rangeAxis = plot.getRangeAxis();
		//  Y     
		rangeAxis.setLabelFont(yfont);
		//  Y   
		rangeAxis.setTickLabelFont(yfont);
		//     
		//rangeAxis.setLabelPaint(Color.RED); //         yong
		//  Bar   
		BarRenderer renderer = (BarRenderer) plot.getRenderer();
		
		renderer.setSeriesPaint(0, Color.gray);
		renderer.setSeriesPaint(1, Color.orange);
		//   BAR     
		renderer.setItemMargin(0.0f);
		//  BAR     
		//renderer.setMaximumBarWidth(0.5f);
	}
}

package com.yong;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;

/**
 * 
 * @author    
 *
 */
public class Line {

	public static void main(String[] args) {
		//  
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(212, "Classes ", "JDK 1.0  ");
		dataset.addValue(504, "Classes ", "JDK 1.1  ");
		dataset.addValue(1520, "Classes ", "SDK 1.2  ");
		dataset.addValue(1842, "Classes ", "SDK 1.3  ");
		dataset.addValue(2991, "Classes ", "SDK 1.4  ");
		JFreeChart chart = ChartFactory.createLineChart(	"Java Standard Class Library","Release  ","Class  ",dataset,PlotOrientation.VERTICAL,false,	true,false	);
		//  chart
		Line.configFont(chart);
		//    
		TextTitle t = new TextTitle("Number of Classes By Release");
		t.setFont(new Font("Comic Sans MS", Font.PLAIN, 15));
		chart.addSubtitle(t);
		//    ,            
		TextTitle source = new TextTitle("Source: Java In A Nutshell (4th Edition) by David Flanagan (O’Reilly)");
		source.setFont(new Font("Comic Sans MS", Font.PLAIN, 15));
		//    
		source.setPosition(RectangleEdge.BOTTOM);
		//    
		source.setHorizontalAlignment(HorizontalAlignment.RIGHT);
		//  
		chart.addSubtitle(source);
		
		//     
		chart.setBackgroundPaint(Color.white);
		
		CategoryPlot plot = (CategoryPlot)chart.getPlot();
		
		//     
		plot.setRangeGridlinePaint(Color.white);
		// customise the range axis...(         )		
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		//              ,       ,   
		LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot	.getRenderer();
		renderer.setShapesVisible(true);
		renderer.setDrawOutlines(true);
		renderer.setUseFillPaint(true);
		renderer.setFillPaint(Color.yellow);
		
		FileOutputStream fos_jpg = null;
		try {
			fos_jpg = new FileOutputStream("c:\\Line.jpg");
			ChartUtilities.writeChartAsJPEG(fos_jpg,0.99f,chart,800,600,	null);
			fos_jpg.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
         	/**
	 *           
	 * @param chart
	 */
	private static void configFont(JFreeChart chart) {
		//     
		Font xfont = new Font("  ", Font.CENTER_BASELINE, 12);// X 
		Font yfont = new Font("  ", Font.CENTER_BASELINE, 20);// Y 
		Font kfont = new Font("  ", Font.CENTER_BASELINE, 18);//   
		Font titleFont = new Font("    ", Font.CENTER_BASELINE, 25); //     
		CategoryPlot plot = chart.getCategoryPlot();//          
		//         
		plot.setRangeGridlinePaint(Color.BLUE);
		//         
		plot.setRangeGridlineStroke(new BasicStroke(1.0f));
		//     
		chart.setTitle(new TextTitle(chart.getTitle().getText(),titleFont));

		//X 
		CategoryAxis domainAxis = plot.getDomainAxis();
		//  X     
		domainAxis.setLabelFont(xfont);
		//  X   
		domainAxis.setTickLabelFont(xfont);
		//      
		domainAxis.setTickLabelPaint(Color.BLUE);
		//    label   
		//domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); 
		//domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
		//     ,         ,   Row 1   
		//domainAxis.setCategoryMargin(0.2f);
		//    ( )  ,        
		domainAxis.setLowerMargin(0.1);
		//    ( )  ,         
		domainAxis.setUpperMargin(0.1);
		
		//Y  
		ValueAxis rangeAxis = plot.getRangeAxis();
		//  Y     
		rangeAxis.setLabelFont(yfont);
		//  Y   
		rangeAxis.setTickLabelFont(yfont);
		//     
		rangeAxis.setLabelPaint(Color.RED); 
	}
}



package com.yong;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;

/**
 * 
 * @author    
 *
 */
public class TimeSeriesTest {

	public static void main(String[] args) {
		//     
		TimeSeries s1 = new TimeSeries("L&G European Index Trust  ", Month.class);
		s1.add(new Month(2, 2001), 181.8);
		s1.add(new Month(3, 2001), 167.3);
		s1.add(new Month(4, 2001), 153.8);
		s1.add(new Month(5, 2001), 167.6);
		s1.add(new Month(6, 2001), 158.8);
		s1.add(new Month(7, 2001), 148.3);
		s1.add(new Month(8, 2001), 153.9);
		s1.add(new Month(9, 2001), 142.7);
		s1.add(new Month(10, 2001), 123.2);
		s1.add(new Month(11, 2001), 131.8);
		s1.add(new Month(12, 2001), 139.6);
		s1.add(new Month(1, 2002), 142.9);
		s1.add(new Month(2, 2002), 138.7);
		s1.add(new Month(3, 2002), 137.3);
		s1.add(new Month(4, 2002), 143.9);
		s1.add(new Month(5, 2002), 139.8);
		s1.add(new Month(6, 2002), 137.0);
		s1.add(new Month(7, 2002), 132.8);
		TimeSeries s2 = new TimeSeries("L&G UK Index Trust  ", Month.class);
		s2.add(new Month(2, 2001), 129.6);
		s2.add(new Month(3, 2001), 123.2);
		s2.add(new Month(4, 2001), 117.2);
		s2.add(new Month(5, 2001), 124.1);
		s2.add(new Month(6, 2001), 122.6);
		s2.add(new Month(7, 2001), 119.2);
		s2.add(new Month(8, 2001), 116.5);
		s2.add(new Month(9, 2001), 112.7);
		s2.add(new Month(10, 2001), 101.5);
		s2.add(new Month(11, 2001), 106.1);
		s2.add(new Month(12, 2001), 110.3);
		s2.add(new Month(1, 2002), 111.7);
		s2.add(new Month(2, 2002), 111.0);
		s2.add(new Month(3, 2002), 109.6);
		s2.add(new Month(4, 2002), 113.2);
		s2.add(new Month(5, 2002), 111.6);
		s2.add(new Month(6, 2002), 108.8);
		s2.add(new Month(7, 2002), 101.6);
		TimeSeriesCollection dataset = new TimeSeriesCollection();
		dataset.addSeries(s1);
		dataset.addSeries(s2);
		//        
		JFreeChart chart = ChartFactory.createTimeSeriesChart("  ","Date  ","Price  ",dataset,true,	true,	false	);
		//       
		TimeSeriesTest.configFont(chart);
		//     ,      COPY   ,     TimeSeriesTest.configFont(chart);      
		chart.setBackgroundPaint(Color.white);
		//      
		XYPlot plot = (XYPlot) chart.getPlot();
		//   
		plot.setBackgroundPaint(Color.lightGray);
		//X     Y      
		plot.setDomainGridlinePaint(Color.white);
		plot.setRangeGridlinePaint(Color.white);
		//      
		plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
		plot.setDomainCrosshairVisible(true);
		plot.setRangeCrosshairVisible(true);
		//      
		XYItemRenderer r = plot.getRenderer();
		if (r instanceof XYLineAndShapeRenderer) {
			XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
			renderer.setBaseShapesVisible(true);
			renderer.setBaseShapesFilled(true);
		}
		//  X       
		DateAxis axis = (DateAxis) plot.getDomainAxis();
		axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
		
		FileOutputStream fos_jpg = null;
		try {
			fos_jpg = new FileOutputStream("c:\\TimeSeries.jpg");
			ChartUtilities.writeChartAsJPEG(fos_jpg,0.99f,chart,800,600,	null);
			fos_jpg.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 *        
	 * @param chart
	 */
	private static void configFont(JFreeChart chart) {
		//     
		Font xfont = new Font("  ", Font.CENTER_BASELINE, 12);// X 
		Font yfont = new Font("  ", Font.CENTER_BASELINE, 20);// Y 
		Font kfont = new Font("  ", Font.CENTER_BASELINE, 18);//   
		Font titleFont = new Font("    ", Font.CENTER_BASELINE, 25); //     
		XYPlot plot = (XYPlot) chart.getPlot();//          
		//         
		plot.setRangeGridlinePaint(Color.BLUE);
		//         
		plot.setRangeGridlineStroke(new BasicStroke(1.0f));
		//     
		chart.setTitle(new TextTitle(chart.getTitle().getText(),titleFont));
		chart.getLegend().setItemFont(kfont);

		//X 
		ValueAxis domainAxis = plot.getDomainAxis();
		//  X     
		domainAxis.setLabelFont(xfont);
		//  X   
		domainAxis.setTickLabelFont(xfont);
		//      
		domainAxis.setTickLabelPaint(Color.BLUE);
		//    label   
		//domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); 
		//domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
		//     ,         ,   Row 1   
		//domainAxis.setCategoryMargin(0.2f);
		//    ( )  ,        
		domainAxis.setLowerMargin(0.1);
		//    ( )  ,         
		domainAxis.setUpperMargin(0.1);
		
		//Y  
		ValueAxis rangeAxis = plot.getRangeAxis();
		//  Y     
		rangeAxis.setLabelFont(yfont);
		//  Y   
		rangeAxis.setTickLabelFont(yfont);
		//     
		rangeAxis.setLabelPaint(Color.RED); 

	}
}

JFreeChart jfreechart = null;
			
			//    title.getText()
			TextTitle title = jfreechart.getTitle();
			
			//    
			jfreechart.setTitle("A Chart Title");
			
			//       
			jfreechart.getTitle().setPosition(RectangleEdge.BOTTOM);
			
			//           
			TextTitle subtitle1 = new TextTitle("A Subtitle");
			jfreechart.addSubtitle(subtitle1);
			
			//     ,        ,     0 
			Title subtitle = jfreechart.getSubtitle(0);
			
			//      
			jfreechart.setBackgroundPaint(Color.blue);
			
			//             
			//              
			Paint p = new GradientPaint(0, 0, Color.white, 1000, 0, Color.green);		
			jfreechart.setBackgroundPaint(p);
			
			//      LOG
			jfreechart.setBackgroundImage(JFreeChart.INFO.getLogo());
			
			//         ,      
			jfreechart.setBackgroundImageAlignment(Align.TOP_LEFT);
			
			//Plot Attributes(   )
			//Plot(   ,)
			Plot plot1 = jfreechart.getPlot();
			
			//Plot(   ,)
			CategoryPlot plot2 = jfreechart.getCategoryPlot();
			
			//Plot(XY   )
			XYPlot plot3 = jfreechart.getXYPlot();
			
			//      (    )(      )
			plot1.setBackgroundPaint(Color.white);
			//      
			plot1.setBackgroundImage(JFreeChart.INFO.getLogo());
			
			//Axis Attributes(X Y   ,   ,  X Y )
			// get an axis reference...
			CategoryPlot plot = jfreechart.getCategoryPlot();
			
			//  X   
			CategoryAxis domainAxis = plot.getDomainAxis();
			//  Y   
			ValueAxis rangeAxis = plot.getRangeAxis();
			
			//change axis properties...    
			domainAxis.setLabel("Categories");
			domainAxis.setLabelFont(new Font("  ", Font.CENTER_BASELINE, 20));
			
			//  X   ,Y   
			domainAxis.setTickLabelFont(new Font("  ", Font.CENTER_BASELINE, 20));

좋은 웹페이지 즐겨찾기