본문 바로가기
JAVA

[ JAVA ] jfreechart pie, bar 차트 예제 (영역 색깔 넣기)

by 정윤재 2012. 11. 9.

jfreechart 에 대한 실전 코드 이다.

 

pie 와 bar 차트만 준비 해 보았다.

 

===================================

 

 

package com.incross.chart;


import java.awt.Color;
import java.awt.Paint;
import java.io.File;
import java.io.FileOutputStream;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

public class makeChart {

 /**
  * @param args
  */
 public static void main(String[] args) {

  //1. pie chart
  
   DefaultPieDataset dataset=new DefaultPieDataset();
  
   dataset.setValue("first",100);
   dataset.setValue("second",150);
   dataset.setValue("third",100);
  
   System.setProperty("java.awt.headless", "true");
  
   JFreeChart chart = ChartFactory.createPieChart("title", dataset, false,true, false);
  
    chart.setBackgroundPaint(new Color(246, 246, 246));
    //backgroud color (RGB)
    chart.getTitle().setPaint(new Color(102, 102, 102)); 
    //title color (RGB)
  
    PiePlot plot = (PiePlot)chart.getPlot();
   
   
    Paint paint0 = new Color(236,138,168);
    //RGB color
    Paint paint1 = new Color(231,204,100);
    Paint paint2 = new Color(140,211,115);
    Paint paint3 = new Color(142,202,222);
    Paint paint4 = new Color(174,110,196);
    //range color
   
    plot.setSectionPaint(0, paint0);
    plot.setSectionPaint(1, paint1);
    plot.setSectionPaint(2, paint2);
    plot.setSectionPaint(3, paint3);
    plot.setSectionPaint(4, paint4);
   
    File chartFile =  new File("c:\\piechart.jpg");
   
    try{
    
     FileOutputStream fos=new FileOutputStream(chartFile);
    
     ChartUtilities.writeChartAsPNG(fos, chart, 200, 200);
     //FileOutputStream, JFreeChart, width, height

     fos.flush();
    
     fos.close();
    
    }catch(Exception e){
     e.printStackTrace();
    }
   
   
   //**********************************************
   
   
   // 2. bar chart
   
    DefaultCategoryDataset barDataset=new DefaultCategoryDataset ();
   
    barDataset.setValue(100,"first1","first2");
    //data, 주석 , data 표기
    barDataset.setValue(150,"second1","second2");
    barDataset.setValue(100,"third1","third3");
   
   
    System.setProperty("java.awt.headless", "true");

   
   
   
    chart =  ChartFactory.createBarChart("bar chart",null,null,barDataset,PlotOrientation.VERTICAL,true,true,false);

 

    chart.setBackgroundPaint(new Color(246, 246, 246));
    chart.getTitle().setPaint(new Color(102, 102, 102));

     CategoryPlot cplot = (CategoryPlot)chart.getPlot();

     Paint bpaint0 = new Color(236,138,168);
     Paint bpaint1 = new Color(231,204,100);
     Paint bpaint2 = new Color(140,211,115);
     Paint bpaint3 = new Color(142,202,222);
     Paint bpaint4 = new Color(174,110,196);
    
     CategoryItemRenderer renderer = cplot.getRenderer();

     renderer.setSeriesPaint(0, bpaint0);
     renderer.setSeriesPaint(1, bpaint1);
     renderer.setSeriesPaint(2, bpaint2);
     renderer.setSeriesPaint(3, bpaint3);
     renderer.setSeriesPaint(4, bpaint4);
    
    
     File chartFile2 =  new File("c:\\barchart.jpg");
    
     try{
     
      FileOutputStream fos=new FileOutputStream(chartFile2);
     
      ChartUtilities.writeChartAsPNG(fos, chart, 200, 200);

      fos.flush();
     
      fos.close();
     
     }catch(Exception e){
      e.printStackTrace();
     }
   
 }

}

 

==============================================

 

jcommon-1.0.17.jar
다운로드

 

jfreechart-1.0.14.jar
다운로드

 


댓글