zoukankan      html  css  js  c++  java
  • 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();
            }
    
        }
    
    }
     
  • 相关阅读:
    springboot集成mockito与powermock
    不一样的go语言-玩转语法之二
    不一样的go语言-玩转语法之一
    不一样的go语言-athens源码概览
    不一样的go语言-athens私仓安装
    不一样的go语言-构建系统与构件系统
    不一样的go语言-error
    不一样的go语言-gopher
    jssip中文开发文档(完整版)
    echarts属性的设置(完整大全)
  • 原文地址:https://www.cnblogs.com/pjlhf/p/8777753.html
Copyright © 2011-2022 走看看