JFreeChart - 简记

Posted pjlhf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JFreeChart - 简记相关的知识,希望对你有一定的参考价值。

一.步骤:(发现另一位博主写的更详细:https://www.cnblogs.com/dmir/p/4976550.html

  1. 创建数据集(准备数据)
  2. 根据数据集生成JFreeChart对象,并对其做相应的设置(标题,图例,x轴,Y轴,对象渲染等)
  3. 将JFreeChart对象输出到文件或者Servlet输出流等

1.饼状图

package com.jfreechart;

import java.awt.Font;
import java.io.File;
import java.io.IOException;

import javax.swing.plaf.FontUIResource;

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.data.general.DefaultPieDataset;

public class JFreeChartTestPie {

    public static void main(String[] args) throws IOException {
        DefaultPieDataset ds = new DefaultPieDataset();
        ds.setValue("IBM", 5000);
        ds.setValue("ORACLE", 6000);
        ds.setValue("JBOSS", 7000);
        ds.setValue("用友", 8000);

        JFreeChart chart = ChartFactory.createPieChart3D("标题", ds, true, false, false);

        // 设定标题字体
        chart.getTitle().setFont(new FontUIResource("宋体", Font.BOLD, 20));
        // 提示条字体
        chart.getLegend().setItemFont(new FontUIResource("宋体", Font.PLAIN, 15));

        // 绘图区
        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setLabelFont(new FontUIResource("宋体", Font.ITALIC, 12));
        plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({1}), {2}"));

        // 设置绘图区背景
        // plot.setBackgroundImage(ImageIO.read(new File("D:/Hydrangeas.jpg")));

        // 设置分离效果,3d不支持分离效果
        plot.setExplodePercent("IBM", 0.1F);
        plot.setExplodePercent("JBOSS", 0.1F);

        // 设置透明度
        plot.setForegroundAlpha(0.7f);

        try {
            ChartUtilities.saveChartAsJPEG(new File("D:/Piechart.jpg"), chart, 800, 500);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

2.条形图

package com.jfreechart;

import java.awt.Font;
import java.io.File;
import java.io.IOException;

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.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

public class JFreeChartTestBar {

    public static void main(String[] args) {
        DefaultCategoryDataset ds = new DefaultCategoryDataset();
        ds.setValue(3400, "IBM", "一季度");
        ds.setValue(3600, "ORACLE", "一季度");
        ds.setValue(3100, "JBOSS", "一季度");
        ds.setValue(2800, "用友", "一季度");

        ds.setValue(3600, "IBM", "二季度");
        ds.setValue(3800, "ORACLE", "二季度");
        ds.setValue(4000, "JBOSS", "二季度");
        ds.setValue(2900, "用友", "二季度");

        ds.setValue(3400, "IBM", "三季度");
        ds.setValue(3600, "ORACLE", "三季度");
        ds.setValue(4000, "JBOSS", "三季度");
        ds.setValue(2900, "用友", "三季度");

        JFreeChart chart = ChartFactory.createBarChart3D("前三季度销量比较", "季度", "销量(单位:万台)", ds, PlotOrientation.VERTICAL, true, false, false);

        // 设定标题字体
        chart.getTitle().setFont(new Font("宋体", Font.BOLD, 20));
        // 提示条字体
        chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 15));

        // 绘图区
        CategoryPlot plot = chart.getCategoryPlot();
        plot.getDomainAxis().setLabelFont(new Font("宋体", Font.PLAIN, 15));
        plot.getDomainAxis().setTickLabelFont(new Font("宋体", Font.PLAIN, 15));

        plot.getRangeAxis().setLabelFont(new Font("宋体", Font.PLAIN, 15));
        plot.getRangeAxis().setTickLabelFont(new Font("宋体", Font.PLAIN, 15));

        try {
            ChartUtilities.saveChartAsJPEG(new File("D:/Barchart.jpg"), chart, 800, 500);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

3.折线图

package com.jfreechart;

import java.awt.Font;
import java.io.File;
import java.io.IOException;

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.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

public class JFreeChartTestLine {

    public static void main(String[] args) {
        DefaultCategoryDataset ds = new DefaultCategoryDataset();
        ds.setValue(3400, "IBM", "一季度");
        ds.setValue(3600, "ORACLE", "一季度");
        ds.setValue(3100, "JBOSS", "一季度");
        ds.setValue(2800, "用友", "一季度");

        ds.setValue(3600, "IBM", "二季度");
        ds.setValue(3800, "ORACLE", "二季度");
        ds.setValue(4000, "JBOSS", "二季度");
        ds.setValue(2900, "用友", "二季度");

        ds.setValue(3400, "IBM", "三季度");
        ds.setValue(3600, "ORACLE", "三季度");
        ds.setValue(4000, "JBOSS", "三季度");
        ds.setValue(2900, "用友", "三季度");

        JFreeChart chart = ChartFactory.createLineChart("前三季度销量比较", "季度", "销量(单位:万台)", ds, PlotOrientation.VERTICAL, true, false, false);

        // 设定标题字体
        chart.getTitle().setFont(new Font("宋体", Font.BOLD, 20));
        // 提示条字体
        chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 15));

        // 绘图区
        CategoryPlot plot = chart.getCategoryPlot();
        plot.getDomainAxis().setLabelFont(new Font("宋体", Font.PLAIN, 15));
        plot.getDomainAxis().setTickLabelFont(new Font("宋体", Font.PLAIN, 15));

        plot.getRangeAxis().setLabelFont(new Font("宋体", Font.PLAIN, 15));
        plot.getRangeAxis().setTickLabelFont(new Font("宋体", Font.PLAIN, 15));
        plot.getRangeAxis().setRangeWithMargins(2000, 5000);
        try {
            ChartUtilities.saveChartAsJPEG(new File("D:/Linechart.jpg"), chart, 800, 500);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
 

以上是关于JFreeChart - 简记的主要内容,如果未能解决你的问题,请参考以下文章

JFreeChart - Java 内存泄漏

JFreeChart工具类

数据库中jfreechart中的散点图

JAVA Swing使用JFreeChart实现折线图绘制

JFreechart - 想要自定义 Y 轴值

jfreechart折线图示例