Search

'분류 전체보기'에 해당되는 글 142건

  1. 2014.08.07 JFreeChat 사용법 예제

JFreeChat 사용법 예제

it/Java 2014. 8. 7. 14:32 Posted by 하얀나다

출처 - http://androphil.tistory.com/441 

 

 

jfreechart-1.0.19.zip

 

 

package chart;

 

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Font;

import java.awt.GradientPaint;

import java.awt.Paint;

 

import org.jfree.chart.ChartFrame;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.axis.CategoryAxis;

import org.jfree.chart.axis.CategoryLabelPositions;

import org.jfree.chart.axis.NumberAxis;

import org.jfree.chart.labels.CategoryItemLabelGenerator;

import org.jfree.chart.labels.ItemLabelAnchor;

import org.jfree.chart.labels.ItemLabelPosition;

import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;

import org.jfree.chart.plot.CategoryPlot;

import org.jfree.chart.plot.DatasetRenderingOrder;

import org.jfree.chart.plot.PlotOrientation;

import org.jfree.chart.renderer.category.BarRenderer;

import org.jfree.chart.renderer.category.CategoryItemRenderer;

import org.jfree.chart.renderer.category.LineAndShapeRenderer;

import org.jfree.chart.renderer.category.StandardBarPainter;

import org.jfree.chart.title.TextTitle;

import org.jfree.data.category.DefaultCategoryDataset;

import org.jfree.ui.GradientPaintTransformType;

import org.jfree.ui.HorizontalAlignment;

import org.jfree.ui.StandardGradientPaintTransformer;

import org.jfree.ui.TextAnchor;

 

/**

 * A simple demonstration application showing how to create a bar chart overlaid

 * with a line chart.

 */

public class PolylineBarChart {

 

      

       // Run As > Java Application 으로 실행하면 바로 확인할 있음.

    public static void main(final String[] args) {

       PolylineBarChart demo = new PolylineBarChart();

             JFreeChart chart = demo.getChart();

             ChartFrame frame1=new ChartFrame("Bar Chart",chart);

             frame1.setSize(800,400); 

             frame1.setVisible(true);

    }

   

    public JFreeChart getChart() {

      

        // 데이터 생성

        DefaultCategoryDataset dataset1 = new DefaultCategoryDataset();                // bar chart 1

        DefaultCategoryDataset dataset12 = new DefaultCategoryDataset();         // bar chart 2

        DefaultCategoryDataset dataset2 = new DefaultCategoryDataset();                // line chart 1

 

        // 데이터 입력 ( , 범례, 카테고리 )

        // 그래프 1       

        dataset1.addValue(1.0, "S1", "1");

        dataset1.addValue(4.0, "S1", "2");

        dataset1.addValue(3.0, "S1", "3");

        dataset1.addValue(5.0, "S1", "4");

        dataset1.addValue(5.0, "S1", "5");

        dataset1.addValue(7.0, "S1", "6");

        dataset1.addValue(7.0, "S1", "7");

        dataset1.addValue(8.0, "S1", "8");

        dataset1.addValue(0, "S1", "9");

        dataset1.addValue(0, "S1", "10");

        dataset1.addValue(0, "S1", "11");

        dataset1.addValue(0, "S1", "12");

 

        // 그래프 2       

        dataset12.addValue(0, "S2", "1");

        dataset12.addValue(0, "S2", "2");

        dataset12.addValue(0, "S2", "3");

        dataset12.addValue(0, "S2", "4");

        dataset12.addValue(0, "S2", "5");

        dataset12.addValue(0, "S2", "6");

        dataset12.addValue(0, "S2", "7");

        dataset12.addValue(0, "S2", "8");

        dataset12.addValue(6.0, "S2", "9");

        dataset12.addValue(0, "S2", "10");

        dataset12.addValue(0, "S2", "11");

        dataset12.addValue(0, "S2", "12");

 

        // 그래프 3       

        dataset2.addValue(9.0, "T1", "1");

        dataset2.addValue(7.0, "T1", "2");

        dataset2.addValue(2.0, "T1", "3");

        dataset2.addValue(6.0, "T1", "4");

        dataset2.addValue(6.0, "T1", "5");

        dataset2.addValue(9.0, "T1", "6");

        dataset2.addValue(5.0, "T1", "7");

        dataset2.addValue(4.0, "T1", "8");

        dataset2.addValue(8.0, "T1", "9");

        dataset2.addValue(8.0, "T1", "10");

        dataset2.addValue(8.0, "T1", "11");

        dataset2.addValue(8.0, "T1", "12");

 

        // 렌더링 생성 세팅

        // 렌더링 생성

        final BarRenderer renderer = new BarRenderer();

        final BarRenderer renderer12 = new BarRenderer();

        final LineAndShapeRenderer renderer2 = new LineAndShapeRenderer();

       

        // 공통 옵션 정의

        final CategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator();

        final ItemLabelPosition p_center = new ItemLabelPosition(

                ItemLabelAnchor.CENTER, TextAnchor.CENTER

            );

        final ItemLabelPosition p_below = new ItemLabelPosition(

                     ItemLabelAnchor.OUTSIDE6, TextAnchor.TOP_LEFT

                     );

        Font f = new Font("Gulim", Font.BOLD, 14);

        Font axisF = new Font("Gulim", Font.PLAIN, 14);

       

        // 렌더링 세팅

        // 그래프 1

        renderer.setBaseItemLabelGenerator(generator);

        renderer.setBaseItemLabelsVisible(true);

        renderer.setBasePositiveItemLabelPosition(p_center);

        renderer.setBaseItemLabelFont(f);

//        renderer.setGradientPaintTransformer(new StandardGradientPaintTransformer(

//                GradientPaintTransformType.VERTICAL));

//        renderer.setSeriesPaint(0, new GradientPaint(1.0f, 1.0f, Color.white, 0.0f, 0.0f, Color.blue));

        renderer.setSeriesPaint(0, new Color(0,162,255));

 

        // 그래프 2       

        renderer12.setSeriesPaint(0, new Color(232,168,255));

        renderer12.setBaseItemLabelFont(f);

        renderer12.setBasePositiveItemLabelPosition(p_center);

        renderer12.setBaseItemLabelGenerator(generator);

        renderer12.setBaseItemLabelsVisible(true);

       

        // 그래프 3       

        renderer2.setBaseItemLabelGenerator(generator);

        renderer2.setBaseItemLabelsVisible(true);

        renderer2.setBaseShapesVisible(true);

        renderer2.setDrawOutlines(true);

        renderer2.setUseFillPaint(true);

        renderer2.setBaseFillPaint(Color.WHITE);

        renderer2.setBaseItemLabelFont(f);

        renderer2.setBasePositiveItemLabelPosition(p_below);

        renderer2.setSeriesPaint(0,new Color(219,121,22));

        renderer2.setSeriesStroke(0,new BasicStroke(

                                               2.0f,

                                               BasicStroke.CAP_ROUND,

                                               BasicStroke.JOIN_ROUND,

                                               3.0f)

        );

       

        // plot 생성

        final CategoryPlot plot = new CategoryPlot();

       

        // plot 데이터 적재

        plot.setDataset(dataset1);

        plot.setRenderer(renderer);

        plot.setDataset(1,dataset12);

        plot.setRenderer(1,renderer12);

        plot.setDataset(2, dataset2);

        plot.setRenderer(2, renderer2);

 

        // plot 기본 설정

        plot.setOrientation(PlotOrientation.VERTICAL);             // 그래프 표시 방향

        plot.setRangeGridlinesVisible(true);                       // X 가이드 라인 표시여부

        plot.setDomainGridlinesVisible(true);                      // Y 가이드 라인 표시여부

 

        // 렌더링 순서 정의 : dataset 등록 순서대로 렌더링 ( , 먼저 등록한게 아래로 깔림 )

        plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

       

        // X 세팅

        plot.setDomainAxis(new CategoryAxis());              // X 종류 설정

        plot.getDomainAxis().setTickLabelFont(axisF); // X 눈금라벨 폰트 조정

        plot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.STANDARD);       // 카테고리 라벨 위치 조정

 

        // Y 세팅

        plot.setRangeAxis(new NumberAxis());                 // Y 종류 설정

        plot.getRangeAxis().setTickLabelFont(axisF);  // Y 눈금라벨 폰트 조정

       

        // 세팅된 plot 바탕으로 chart 생성

        final JFreeChart chart = new JFreeChart(plot);

//        chart.setTitle("Overlaid Bar Chart"); // 차트 타이틀

//        TextTitle copyright = new TextTitle("JFreeChart WaferMapPlot", new Font("SansSerif", Font.PLAIN, 9));

//        copyright.setHorizontalAlignment(HorizontalAlignment.RIGHT);

//        chart.addSubtitle(copyright);  // 차트 서브 타이틀

        return chart;

    }

 

}

'it > Java' 카테고리의 다른 글

간단한 자바문제 메서드  (0) 2015.01.06
간단한 자바문제 제어문  (0) 2015.01.06
자바 기초 입,출력 연산자, if문  (0) 2014.08.19
JDBC mybatis 마이바티즈  (0) 2014.08.12
자바 설치  (0) 2014.07.25