码迷,mamicode.com
首页 > 其他好文 > 详细

JFreeChart - 简记

时间:2018-04-10 13:38:58      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:www.   char   透明度   family   imp   free   int   tar   margin   

一.步骤:(发现另一位博主写的更详细: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 - 简记

标签:www.   char   透明度   family   imp   free   int   tar   margin   

原文地址:https://www.cnblogs.com/pjlhf/p/8777753.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!