java插件- jfreeChart画图

   1、折线图demo

package com.pdf;

import java.awt.Font;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;

//JFreeChart Line Chart(折线图)  
public class CreateJFreeChartLine {
    public static void main(String[] args) {
        // 步骤1:创建CategoryDataset对象(准备数据)
        CategoryDataset dataset = createDataset();
        // 步骤2:根据Dataset 生成JFreeChart对象,以及做相应的设置
        JFreeChart freeChart = createChart(dataset);
        // 步骤3:将JFreeChart对象输出到文件,Servlet输出流等
        saveAsFile(freeChart, "c:jfreechartline.jpg", 600, 400);
    }

    // 保存为文件
    public static void saveAsFile(JFreeChart chart, String outputPath,int weight, int height) {
        FileOutputStream out = null;
        try {
            File outFile = new File(outputPath);
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdirs();
            }
            out = new FileOutputStream(outputPath);
            // 保存为PNG
            // ChartUtilities.writeChartAsPNG(out, chart, 600, 400);
            // 保存为JPEG
            ChartUtilities.writeChartAsJPEG(out, chart, 600, 400);
            out.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    // do nothing
                }
            }
        }
    }

    // 根据CategoryDataset创建JFreeChart对象
    public static JFreeChart createChart(CategoryDataset categoryDataset) {
        //这里的"name"参数;是什么意思我也不知道,反正这样可以用
        StandardChartTheme standardChartTheme = new StandardChartTheme("name");
        //可以改变轴向的字体
        standardChartTheme.setLargeFont(new Font("楷体",Font.BOLD, 12));
        //可以改变图例的字体
        standardChartTheme.setRegularFont(new Font("宋体",Font.BOLD, 8));
        //可以改变图标的标题字体
        standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD, 20));
        ChartFactory.setChartTheme(standardChartTheme);//设置主题
        // 创建JFreeChart对象:ChartFactory.createLineChart
        JFreeChart jfreechart = ChartFactory.createLineChart3D("不同类别按小时计算拆线图", "年分", "数量", categoryDataset, PlotOrientation.VERTICAL, true, true, false);
        // 使用CategoryPlot设置各种参数。以下设置可以省略。
        CategoryPlot plot = (CategoryPlot) jfreechart.getPlot();
        // 背景色 透明度
        plot.setBackgroundAlpha(0.5f);
        // 前景色 透明度
        plot.setForegroundAlpha(0.5f);
        // 其他设置 参考 CategoryPlot类
        return jfreechart;
    }
    /**
     * 创建数据集
     * @return
     */
    public static CategoryDataset createDataset() {

        String[] rowKeys = {"A平台","你妹"};
        String[] colKeys = {"0:00", "1:00", "2:00", "7:00", "8:00", "9:00","10:00", "11:00", "12:00", "13:00", "16:00", "20:00", "21:00","23:00" };

        double[][] data = {{4, 8, 1, 1, 1, 1, 2, 2, 2, 1, 8, 2, 1, 1 },{9, 3, 5, 8, 1, 7, 2, 2, 5, 1, 8, 2, 3, 1}};

        return DatasetUtilities.createCategoryDataset(rowKeys, colKeys, data);
    }
}

 

2、折线图自定义x轴demo

package com.pdf;

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;
 
public class LineChartDemo2 extends ApplicationFrame
{
  public LineChartDemo2(String paramString)
  {
    super(paramString);
    XYDataset localXYDataset = createDataset();
    JFreeChart localJFreeChart = createChart(localXYDataset);
    ChartPanel localChartPanel = new ChartPanel(localJFreeChart);
    localChartPanel.setPreferredSize(new Dimension(500, 270));
    setContentPane(localChartPanel);
  }
 
  private static XYDataset createDataset()
  {
    XYSeries localXYSeries1 = new XYSeries("First");
    localXYSeries1.add(1, 1);
    localXYSeries1.add(2, 4);
    localXYSeries1.add(3, 3);
    localXYSeries1.add(4, 5);
    localXYSeries1.add(5, 5);
    localXYSeries1.add(6, 7);
    localXYSeries1.add(7, 7);
    localXYSeries1.add(21, 8);
   
    XYSeries localXYSeries2 = new XYSeries("Second");
    localXYSeries2.add(1, 5);
    localXYSeries2.add(2, 7);
    localXYSeries2.add(3, 6);
    localXYSeries2.add(4, 8);
    localXYSeries2.add(5, 4);
    localXYSeries2.add(6, 4);
    localXYSeries2.add(7, 2);
    localXYSeries2.add(8, 1);
    XYSeries localXYSeries3 = new XYSeries("Third");
    localXYSeries3.add(3, 4);
    localXYSeries3.add(4, 3);
    localXYSeries3.add(5, 2);
    localXYSeries3.add(6, 3);
    localXYSeries3.add(7, 6);
    localXYSeries3.add(8, 3);
    localXYSeries3.add(9, 4);
    localXYSeries3.add(10, 3);
    XYSeriesCollection localXYSeriesCollection = new XYSeriesCollection();
    localXYSeriesCollection.addSeries(localXYSeries1);
    localXYSeriesCollection.addSeries(localXYSeries2);
    localXYSeriesCollection.addSeries(localXYSeries3);
    return localXYSeriesCollection;
  }
 
  private static JFreeChart createChart(XYDataset paramXYDataset)
  {
    JFreeChart localJFreeChart = ChartFactory.createXYLineChart("Line Chart Demo 2", "X", "Y", paramXYDataset, PlotOrientation.VERTICAL, true, true, false);
    localJFreeChart.setBackgroundPaint(Color.white);
    XYPlot localXYPlot = (XYPlot)localJFreeChart.getPlot();
    localXYPlot.setBackgroundPaint(Color.lightGray);
    localXYPlot.setAxisOffset(new RectangleInsets(10, 10, 10, 10));
    localXYPlot.setDomainGridlinePaint(Color.white);
    localXYPlot.setRangeGridlinePaint(Color.white);
    XYLineAndShapeRenderer localXYLineAndShapeRenderer = (XYLineAndShapeRenderer)localXYPlot.getRenderer();
    localXYLineAndShapeRenderer.setBaseShapesVisible(true);
    localXYLineAndShapeRenderer.setBaseShapesFilled(true);
    NumberAxis localNumberAxis = (NumberAxis)localXYPlot.getRangeAxis();
    localNumberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    return localJFreeChart;
  }
 
  public static JPanel createDemoPanel()
  {
    JFreeChart localJFreeChart = createChart(createDataset());
    return new ChartPanel(localJFreeChart);
  }
 
  public static void main(String[] paramArrayOfString)
  {
    LineChartDemo2 localLineChartDemo2 = new LineChartDemo2("Line Chart Demo 2");
    localLineChartDemo2.pack();
    RefineryUtilities.centerFrameOnScreen(localLineChartDemo2);
    localLineChartDemo2.setVisible(true);
  }
}

 

 

3、将柱状图的Y轴,显示为百分比的demo

package com.pdf;

import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;

//JFreeChart Line Chart(柱状图)  
public class CreateJFreeChartColumn {
    public static void main(String[] args) {
        // 步骤1:创建CategoryDataset对象(准备数据)
        CategoryDataset dataset = createDataset();
        // 步骤2:根据Dataset 生成JFreeChart对象,以及做相应的设置
        JFreeChart freeChart = createChart(dataset);
        // 步骤3:将JFreeChart对象输出到文件,Servlet输出流等
        saveAsFile(freeChart, "c:jfreechartline.jpg", 600, 400);
    }

    // 保存为文件
    public static void saveAsFile(JFreeChart chart, String outputPath,int weight, int height) {
        FileOutputStream out = null;
        try {
            File outFile = new File(outputPath);
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdirs();
            }
            out = new FileOutputStream(outputPath);
            // 保存为PNG
            // ChartUtilities.writeChartAsPNG(out, chart, 600, 400);
            // 保存为JPEG
            ChartUtilities.writeChartAsJPEG(out, chart, 600, 400);
            out.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    // do nothing
                }
            }
        }
    }

    // 根据CategoryDataset创建JFreeChart对象
    public static JFreeChart createChart(CategoryDataset categoryDataset) {
        //这里的"name"参数;是什么意思我也不知道,反正这样可以用
        StandardChartTheme standardChartTheme = new StandardChartTheme("name");
        //可以改变轴向的字体
        standardChartTheme.setLargeFont(new Font("楷体",Font.BOLD, 12));
        //可以改变图例的字体
        standardChartTheme.setRegularFont(new Font("宋体",Font.BOLD, 8));
        //可以改变图标的标题字体
        standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD, 20));
        ChartFactory.setChartTheme(standardChartTheme);//设置主题
        // 创建JFreeChart对象:ChartFactory.createLineChart
        JFreeChart jfreechart = ChartFactory.createBarChart("不同类别按小时计算拆线图", "年分", "数量", categoryDataset, PlotOrientation.VERTICAL, true, true, false);
        // 使用CategoryPlot设置各种参数。以下设置可以省略。
        CategoryPlot plot = (CategoryPlot) jfreechart.getPlot();
        
        //设置柱状图显示为百分比
        BarRenderer3D renderer = new BarRenderer3D();
        renderer.setBaseOutlinePaint(Color.BLACK);
        //显示每个柱的数值,并修改该数值的字体属性
        renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}",DecimalFormat.getPercentInstance()));
	    renderer.setItemLabelsVisible(true);
	    plot.setRenderer(renderer);
	    
        // 背景色 透明度
        plot.setBackgroundAlpha(0.5f);
        // 前景色 透明度
        plot.setForegroundAlpha(0.5f);
        // 其他设置 参考 CategoryPlot类
        return jfreechart;
    }
    /**
     * 创建数据集
     * @return
     */
    public static CategoryDataset createDataset() {

        String[] rowKeys = {"A平台"};
        String[] colKeys = {"0:00", "1:00", "2:00", "7:00", "8:00", "9:00","10:00", "11:00", "12:00", "13:00", "16:00", "20:00", "21:00","23:00" };

        double[][] data = {{0.8, 0.5, 0.124, 0.58, 0.25, 0.23, 0.12, 0.47, 0.29, 0.86, 0.75, 0.64, 0.31, 0.123 }};

        return DatasetUtilities.createCategoryDataset(rowKeys, colKeys, data);
    }
}

 

 

你可能感兴趣的:(JfreeChar,java画图插件)