JfreeChart常用表格绘制

JfreeChart常用表格绘制

绘制普通柱状图——垂直

package com.offcn.utils;

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

import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;

public class BarChart1 {
     
    public static void main(String[] args) throws IOException {
     
        //创建主题样式
        StandardChartTheme standardChartTheme=new StandardChartTheme("CN");
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD,20));
        //设置图例的字体
        standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));
        //设置轴向的字体
        standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));
        //创建数据集
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(250,"深圳","橘子");
        dataset.addValue(300,"广东","绿茶");
        dataset.addValue(170,"海南","菠萝");
        dataset.addValue(280,"西藏","马奶");
        dataset.addValue(115,"北京","烤鸭");
        //使用样式
        ChartFactory.setChartTheme(standardChartTheme);
        //调用工厂方法生成柱状图
        JFreeChart chart = ChartFactory.createBarChart3D("商品营销图","商品","销售",dataset, PlotOrientation.VERTICAL,true,true,false);
        FileOutputStream out = new FileOutputStream("C:\\Users\\85780\\Desktop\\表格\\bar1.jpg");
        //生成图片
        ChartUtilities.writeChartAsJPEG(out,chart,700,500);
        System.out.println("创建垂直柱状图成功");

    }
}

效果图:
JfreeChart常用表格绘制_第1张图片

绘制普通柱状图——水平

package com.offcn.utils;

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

import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;

public class BarChart1 {
     
    public static void main(String[] args) throws IOException {
     
        //创建主题样式
        StandardChartTheme standardChartTheme=new StandardChartTheme("CN");
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD,20));
        //设置图例的字体
        standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));
        //设置轴向的字体
        standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));
        //创建数据集
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(250,"深圳","橘子");
        dataset.addValue(300,"广东","绿茶");
        dataset.addValue(170,"海南","菠萝");
        dataset.addValue(280,"西藏","马奶");
        dataset.addValue(115,"北京","烤鸭");
        //使用样式
        ChartFactory.setChartTheme(standardChartTheme);
        //调用工厂方法生成柱状图
        JFreeChart chart = ChartFactory.createBarChart3D("商品营销图","商品","销售",dataset, PlotOrientation.HORIZONTAL,true,true,false);
        FileOutputStream out = new FileOutputStream("C:\\Users\\85780\\Desktop\\表格\\bar2.jpg");
        //生成图片
        ChartUtilities.writeChartAsJPEG(out,chart,700,500);
        System.out.println("创建水平柱状图成功");

    }
}

效果图:
JfreeChart常用表格绘制_第2张图片

多数据类型柱状图

package com.offcn.utils;

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

import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;

public class BarChart2 {
     
    public static void main(String[] args) throws IOException {
     
        //创建主题样式
        StandardChartTheme standardChartTheme=new StandardChartTheme("CN");
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD,20));
        //设置图例的字体
        standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));
        //设置轴向的字体
        standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));
        //创建数据集
        double data[][] ={
     {
     250,188,340,450},{
     180,245,543,109},{
     356,321,524,221},{
     289,105,180,199}};
        String rowKeys[] ={
     "苹果","香蕉","橘子","梨子"};
        String columsKeys[]={
     "深圳","北京","广州","上海"};
        CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columsKeys, data);
        //使用样式
        ChartFactory.setChartTheme(standardChartTheme);
        //调用工厂方法生成柱状图
        JFreeChart chart = ChartFactory.createBarChart3D("水果销售图", "水果", "销售", dataset, PlotOrientation.VERTICAL, true, true, false);

        //调用工具类生成图片
        FileOutputStream out = new FileOutputStream("C:\\Users\\85780\\Desktop\\表格\\bar3.jpg");
        ChartUtilities.writeChartAsJPEG(out,chart,700,500);
        System.out.println("创建柱状图成功");
    }
}

效果图:
JfreeChart常用表格绘制_第3张图片

饼状图

package com.offcn.utils;



import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.data.general.DefaultPieDataset;

import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;

public class BarChart3 {
     
    public static void main(String[] args) throws IOException {
     
        //创建主题样式
        StandardChartTheme standardChartTheme=new StandardChartTheme("CN");
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD,20));
        //定义数据集
        DefaultPieDataset defaultPieDataset = new DefaultPieDataset();
        defaultPieDataset.setValue("JAVA",9);
        defaultPieDataset.setValue("php",15);
        defaultPieDataset.setValue("web",25);
        defaultPieDataset.setValue("ui",30);
        //使用样式
        ChartFactory.setChartTheme(standardChartTheme);
        //创建对象
        JFreeChart chart = ChartFactory.createPieChart3D("学员比例图", defaultPieDataset, true, true, false);
        //调用工具类生成图片
        FileOutputStream out = new FileOutputStream("C:\\Users\\85780\\Desktop\\表格\\bar4.jpg");
        ChartUtilities.writeChartAsJPEG(out,chart,700,500);
        System.out.println("创建饼状图成功");
    }
}

效果图:
JfreeChart常用表格绘制_第4张图片

条形图

package com.offcn.utils;

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

import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;

public class BarChart4 {
     
    public static void main(String[] args) throws IOException {
     
        //创建主题样式
        StandardChartTheme standardChartTheme=new StandardChartTheme("CN");
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD,20));
        //设置图例的字体
        standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));
        //设置轴向的字体
        standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));
       //创建数据集
        DefaultCategoryDataset datas = new DefaultCategoryDataset();
        datas.addValue(150,"北京","苹果");
        datas.addValue(200,"上海","苹果");
        datas.addValue(300,"广州","苹果");
        datas.addValue(200,"北京","梨子");
        datas.addValue(300,"上海","梨子");
        datas.addValue(400,"广州","梨子");
        datas.addValue(120,"北京","葡萄");
        datas.addValue(220,"上海","葡萄");
        datas.addValue(250,"广州","葡萄");
        datas.addValue(200,"北京","香蕉");
        datas.addValue(300,"上海","香蕉");
        datas.addValue(320,"广州","香蕉");
        datas.addValue(300,"北京","荔枝");
        datas.addValue(380,"上海","荔枝");
        datas.addValue(420,"广州","荔枝");
        //使用样式
        ChartFactory.setChartTheme(standardChartTheme);
        //创建图表
        JFreeChart chart = ChartFactory.createLineChart("水果销量表", "水果", "销量", datas, PlotOrientation.VERTICAL, true, true, false);
        CategoryPlot plot = chart.getCategoryPlot();
        LineAndShapeRenderer renderer = (LineAndShapeRenderer)plot.getRenderer();
        //设置折现上显示数据
        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setBaseItemLabelsVisible(true);
        //调用工具类生成图片
        FileOutputStream out = new FileOutputStream("C:\\Users\\85780\\Desktop\\表格\\bar5.jpg");
        ChartUtilities.writeChartAsJPEG(out,chart,700,500);
        System.out.println("创建折线图成功");
    }
}

效果图:
JfreeChart常用表格绘制_第5张图片
希望这些代码和图片可以帮助到到大家,仔细学习所用。

你可能感兴趣的:(jfreechart,java)