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("创建垂直柱状图成功");
}
}
效果图:
绘制普通柱状图——水平
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("创建水平柱状图成功");
}
}
效果图:
多数据类型柱状图
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("创建柱状图成功");
}
}
效果图:
饼状图
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("创建饼状图成功");
}
}
效果图:
条形图
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("创建折线图成功");
}
}
效果图:
希望这些代码和图片可以帮助到到大家,仔细学习所用。