转自:http://blog.csdn.net/crazy1235/article/details/8535999
介绍:
- package
chart.demo02; -
- import
java.awt.Font; - import
java.awt.RenderingHints; - import
java.io.FileOutputStream; -
- import
org.jfree.chart.ChartFactory; - import
org.jfree.chart.ChartFrame; - import
org.jfree.chart.ChartUtilities; - import
org.jfree.chart.JFreeChart; - import
org.jfree.chart.axis.CategoryAxis; - import
org.jfree.chart.axis.ValueAxis; - import
org.jfree.chart.plot.CategoryPlot; - import
org.jfree.chart.plot.PlotOrientation; - import
org.jfree.chart.title.TextTitle; - import
org.jfree.data.category.CategoryDataset; - import
org.jfree.data.category.DefaultCategoryDataset; -
- public
class BarChartDemo { -
public static void main(String[] args) { -
// 1. 得到数据 -
CategoryDataset dataset = getDataSet(); -
// 2. 构造chart -
JFreeChart chart = ChartFactory.createBarChart3D( -
"水果产量图", // 图表标题 -
"水果", // 目录轴的显示标签--横轴 -
"产量", // 数值轴的显示标签--纵轴 -
dataset, // 数据集 -
PlotOrientation.VERTICAL, // 图表方向:水平、 -
true, // 是否显示图例(对于简单的柱状图必须 -
false, // 是否生成工具 -
false // 是否生成URL链接 -
); -
// 3. 处理chart中文显示问题 -
processChart(chart); -
-
// 4. chart输出图片 -
writeChartAsImage(chart); -
-
// 5. chart 以swing形式输出 -
ChartFrame pieFrame = new ChartFrame("XXX", chart); -
pieFrame.pack(); -
pieFrame.setVisible(true); -
-
} -
-
-
private static CategoryDataset getDataSet() { -
DefaultCategoryDataset dataset = new DefaultCategoryDataset(); -
dataset.addValue(100, "北京", "苹果"); -
dataset.addValue(120, "上海", "苹果"); -
dataset.addValue(160, "广州", "苹果"); -
dataset.addValue(210, "北京", "梨子"); -
dataset.addValue(220, "上海", "梨子"); -
dataset.addValue(230, "广州", "梨子"); -
dataset.addValue(330, "北京", "葡萄"); -
dataset.addValue(340, "上海", "葡萄"); -
dataset.addValue(340, "广州", "葡萄"); -
dataset.addValue(420, "北京", "香蕉"); -
dataset.addValue(430, "上海", "香蕉"); -
dataset.addValue(400, "广州", "香蕉"); -
dataset.addValue(510, "北京", "荔枝"); -
dataset.addValue(530, "上海", "荔枝"); -
dataset.addValue(510, "广州", "荔枝"); -
return dataset; -
} -
-
-
private static void processChart(JFreeChart chart) { -
CategoryPlot plot = chart.getCategoryPlot(); -
CategoryAxis domainAxis = plot.getDomainAxis(); -
ValueAxis rAxis = plot.getRangeAxis(); -
chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, -
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); -
TextTitle textTitle = chart.getTitle(); -
textTitle.setFont(new Font("宋体", Font.PLAIN, 20)); -
domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); -
domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12)); -
rAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); -
rAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12)); -
chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12)); -
// renderer.setItemLabelGenerator(new LabelGenerator(0.0)); -
// renderer.setItemLabelFont(new Font("宋体", Font.PLAIN, 12)); -
// renderer.setItemLabelsVisible(true); -
} -
-
-
private static void writeChartAsImage(JFreeChart chart) { -
FileOutputStream fos_jpg = null; -
try { -
fos_jpg = new FileOutputStream("D:\\test\\fruit.jpg"); -
ChartUtilities.writeChartAsJPEG(fos_jpg, 1, chart, 400, 300, null); -
} catch (Exception e) { -
e.printStackTrace(); -
} finally { -
try { -
fos_jpg.close(); -
} catch (Exception e) { -
} -
} -
} - }
package chart.demo02; import java.awt.Font; import java.awt.RenderingHints; import java.io.FileOutputStream; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.title.TextTitle; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; public class BarChartDemo { public static void main(String[] args) { // 1. 得到数据 CategoryDataset dataset = getDataSet(); // 2. 构造chart JFreeChart chart = ChartFactory.createBarChart3D( "水果产量图", // 图表标题 "水果", // 目录轴的显示标签--横轴 "产量", // 数值轴的显示标签--纵轴 dataset, // 数据集 PlotOrientation.VERTICAL, // 图表方向:水平、 true, // 是否显示图例(对于简单的柱状图必须 false, // 是否生成工具 false // 是否生成URL链接 ); // 3. 处理chart中文显示问题 processChart(chart); // 4. chart输出图片 writeChartAsImage(chart); // 5. chart 以swing形式输出 ChartFrame pieFrame = new ChartFrame("XXX", chart); pieFrame.pack(); pieFrame.setVisible(true); } private static CategoryDataset getDataSet() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(100, "北京", "苹果"); dataset.addValue(120, "上海", "苹果"); dataset.addValue(160, "广州", "苹果"); dataset.addValue(210, "北京", "梨子"); dataset.addValue(220, "上海", "梨子"); dataset.addValue(230, "广州", "梨子"); dataset.addValue(330, "北京", "葡萄"); dataset.addValue(340, "上海", "葡萄"); dataset.addValue(340, "广州", "葡萄"); dataset.addValue(420, "北京", "香蕉"); dataset.addValue(430, "上海", "香蕉"); dataset.addValue(400, "广州", "香蕉"); dataset.addValue(510, "北京", "荔枝"); dataset.addValue(530, "上海", "荔枝"); dataset.addValue(510, "广州", "荔枝"); return dataset; } private static void processChart(JFreeChart chart) { CategoryPlot plot = chart.getCategoryPlot(); CategoryAxis domainAxis = plot.getDomainAxis(); ValueAxis rAxis = plot.getRangeAxis(); chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); TextTitle textTitle = chart.getTitle(); textTitle.setFont(new Font("宋体", Font.PLAIN, 20)); domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12)); rAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); rAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12)); chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12)); // renderer.setItemLabelGenerator(new LabelGenerator(0.0)); // renderer.setItemLabelFont(new Font("宋体", Font.PLAIN, 12)); // renderer.setItemLabelsVisible(true); } private static void writeChartAsImage(JFreeChart chart) { FileOutputStream fos_jpg = null; try { fos_jpg = new FileOutputStream("D:\\test\\fruit.jpg"); ChartUtilities.writeChartAsJPEG(fos_jpg, 1, chart, 400, 300, null); } catch (Exception e) { e.printStackTrace(); } finally { try { fos_jpg.close(); } catch (Exception e) { } } } }
效果图如下:
接下来看其他的图表的生成:
饼状图:
- public
void createPieChart() { -
DefaultPieDataset dataset = new DefaultPieDataset(); -
dataset.setValue("苹果", 250); -
dataset.setValue("桔子", 350); -
dataset.setValue("梨子", 200); -
dataset.setValue("香蕉", 50); -
dataset.setValue("荔枝", 150); -
JFreeChart chart = ChartFactory.createPieChart("水果产量比率图", dataset, -
true, true, true); -
-
Plot cp = chart.getPlot(); -
cp.setBackgroundPaint(ChartColor.WHITE); // 背景色设置 -
-
// ChartFrame frame = new ChartFrame("水果产量比率图 ", chart, true); -
// frame.pack(); -
// frame.setVisible(true); -
-
try { -
ChartUtilities.saveChartAsPNG(new File("D:\\test\\PieChart.png"), -
chart, 800, 500); -
} catch (IOException e) { -
e.printStackTrace(); -
} -
}
public void createPieChart() { DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("苹果", 250); dataset.setValue("桔子", 350); dataset.setValue("梨子", 200); dataset.setValue("香蕉", 50); dataset.setValue("荔枝", 150); JFreeChart chart = ChartFactory.createPieChart("水果产量比率图", dataset, true, true, true); Plot cp = chart.getPlot(); cp.setBackgroundPaint(ChartColor.WHITE); // 背景色设置 // ChartFrame frame = new ChartFrame("水果产量比率图 ", chart, true); // frame.pack(); // frame.setVisible(true); try { ChartUtilities.saveChartAsPNG(new File("D:\\test\\PieChart.png"), chart, 800, 500); } catch (IOException e) { e.printStackTrace(); } }
3D饼状图:
- public
void createPieChart3D() { -
DefaultPieDataset dataset = new DefaultPieDataset(); -
dataset.setValue("苹果", 250); -
dataset.setValue("桔子", 350); -
dataset.setValue("梨子", 200); -
dataset.setValue("香蕉", 50); -
dataset.setValue("荔枝", 150); -
JFreeChart chart = ChartFactory.createPieChart3D("水果产量比率图", dataset, -
true, true, true); -
PiePlot3D plot = (PiePlot3D) chart.getPlot(); -
// 设置开始角度 -
plot.setStartAngle(150D); -
// 设置方向为"顺时针方向" -
plot.setDirection(Rotation.CLOCKWISE); -
// 设置透明度,0.5F为半透明,1为不透明,0为全透明 -
plot.setForegroundAlpha(1F); -
// 没有数据的时候显示的内容 -
plot.setNoDataMessage("无数据显示"); -
// 背景色设置 -
plot.setBackgroundPaint(ChartColor.WHITE); -
// ChartFrame frame = new ChartFrame("水果产量比率图 ", chart, true); -
// frame.pack(); -
// frame.setVisible(true); -
-
try { -
ChartUtilities.saveChartAsPNG(new File("D:\\test\\PieChart3D.png"), -
chart, 800, 500); -
} catch (IOException e) { -
e.printStackTrace(); -
} -
}
public void createPieChart3D() { DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("苹果", 250); dataset.setValue("桔子", 350); dataset.setValue("梨子", 200); dataset.setValue("香蕉", 50); dataset.setValue("荔枝", 150); JFreeChart chart = ChartFactory.createPieChart3D("水果产量比率图", dataset, true, true, true); PiePlot3D plot = (PiePlot3D) chart.getPlot(); // 设置开始角度 plot.setStartAngle(150D); // 设置方向为"顺时针方向" plot.setDirection(Rotation.CLOCKWISE); // 设置透明度,0.5F为半透明,1为不透明,0为全透明 plot.setForegroundAlpha(1F); // 没有数据的时候显示的内容 plot.setNoDataMessage("无数据显示"); // 背景色设置 plot.setBackgroundPaint(ChartColor.WHITE); // ChartFrame frame = new ChartFrame("水果产量比率图 ", chart, true); // frame.pack(); // frame.setVisible(true); try { ChartUtilities.saveChartAsPNG(new File("D:\\test\\PieChart3D.png"), chart, 800, 500); } catch (IOException e) { e.printStackTrace(); } }
柱状图:
- public
void createColumnChart() { -
DefaultCategoryDataset dataset = new DefaultCategoryDataset(); -
dataset.addValue(100, "北京", "苹果"); -
dataset.addValue(100, "上海", "苹果"); -
dataset.addValue(100, "广州", "苹果"); -
dataset.addValue(200, "北京", "梨子"); -
dataset.addValue(200, "上海", "梨子"); -
dataset.addValue(200, "广州", "梨子"); -
dataset.addValue(300, "北京", "葡萄"); -
dataset.addValue(300, "上海", "葡萄"); -
dataset.addValue(300, "广州", "葡萄"); -
dataset.addValue(400, "北京", "香蕉"); -
dataset.addValue(400, "上海", "香蕉"); -
dataset.addValue(400, "广州", "香蕉"); -
dataset.addValue(500, "北京", "荔枝"); -
dataset.addValue(500, "上海", "荔枝"); -
dataset.addValue(500, "广州", "荔枝"); -
-
JFreeChart chart = ChartFactory.createBarChart("水果产量图", "水量", "产量", -
dataset, PlotOrientation.VERTICAL, true, true, true); -
-
CategoryPlot cp = chart.getCategoryPlot(); -
cp.setBackgroundPaint(ChartColor.WHITE); // 背景色设置 -
cp.setRangeGridlinePaint(ChartColor.GRAY); // 网格线色设置 -
-
// ChartFrame frame=new ChartFrame ("水果产量图 ",chart,true); -
// frame.pack(); -
// frame.setVisible(true); -
-
try { -
ChartUtilities.saveChartAsPNG(new File("D:\\test\\ColumnChart.png"), -
chart, 800, 500); -
} catch (IOException e) { -
e.printStackTrace(); -
} -
}
public void createColumnChart() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(100, "北京", "苹果"); dataset.addValue(100, "上海", "苹果"); dataset.addValue(100, "广州", "苹果"); dataset.addValue(200, "北京", "梨子"); dataset.addValue(200, "上海", "梨子"); dataset.addValue(200, "广州", "梨子"); dataset.addValue(300, "北京", "葡萄"); dataset.addValue(300, "上海", "葡萄"); dataset.addValue(300, "广州", "葡萄"); dataset.addValue(400, "北京", "香蕉"); dataset.addValue(400, "上海", "香蕉"); dataset.addValue(400, "广州", "香蕉"); dataset.addValue(500, "北京", "荔枝"); dataset.addValue(500, "上海", "荔枝"); dataset.addValue(500, "广州", "荔枝"); JFreeChart chart = ChartFactory.createBarChart("水果产量图", "水量", "产量", dataset, PlotOrientation.VERTICAL, true, true, true); CategoryPlot cp = chart.getCategoryPlot(); cp.setBackgroundPaint(ChartColor.WHITE); // 背景色设置 cp.setRangeGridlinePaint(ChartColor.GRAY); // 网格线色设置 // ChartFrame frame=new ChartFrame ("水果产量图 ",chart,true); // frame.pack(); // frame.setVisible(true); try { ChartUtilities.saveChartAsPNG(new File("D:\\test\\ColumnChart.png"), chart, 800, 500); } catch (IOException e) { e.printStackTrace(); } }
折线图:
- public
void createLineChart() { -
DefaultCategoryDataset dataset = new DefaultCategoryDataset(); -
dataset.addValue(100, "北京", "苹果"); -
dataset.addValue(200, "上海", "苹果"); -
dataset.addValue(300, "广州", "苹果"); -
dataset.addValue(400, "北京", "梨子"); -
dataset.addValue(500, "上海", "梨子"); -
dataset.addValue(600, "广州", "梨子"); -
dataset.addValue(700, "北京", "葡萄"); -
dataset.addValue(800, "上海", "葡萄"); -
dataset.addValue(900, "广州", "葡萄"); -
dataset.addValue(800, "北京", "香蕉"); -
dataset.addValue(700, "上海", "香蕉"); -
dataset.addValue(600, "广州", "香蕉"); -
dataset.addValue(500, "北京", "荔枝"); -
dataset.addValue(400, "上海", "荔枝"); -
dataset.addValue(300, "广州", "荔枝"); -
JFreeChart chart = ChartFactory.createLineChart("水果产量图 ", "水果", "产量", -
dataset, PlotOrientation.VERTICAL, true, true, true); -
-
CategoryPlot cp = chart.getCategoryPlot(); -
cp.setBackgroundPaint(ChartColor.WHITE); // 背景色设置 -
cp.setRangeGridlinePaint(ChartColor.GRAY); // 网格线色设置 -
-
// ChartFrame frame=new ChartFrame ("水果产量图 ",chart,true); -
// frame.pack(); -
// frame.setVisible(true); -
-
try { -
ChartUtilities.saveChartAsPNG(new File("D:\\test\\LineChart.png"), -
chart, 800, 500); -
} catch (IOException e) { -
e.printStackTrace(); -
} -
}
public void createLineChart() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(100, "北京", "苹果"); dataset.addValue(200, "上海", "苹果"); dataset.addValue(300, "广州", "苹果"); dataset.addValue(400, "北京", "梨子"); dataset.addValue(500, "上海", "梨子"); dataset.addValue(600, "广州", "梨子"); dataset.addValue(700, "北京", "葡萄"); dataset.addValue(800, "上海", "葡萄"); dataset.addValue(900, "广州", "葡萄"); dataset.addValue(800, "北京", "香蕉"); dataset.addValue(700, "上海", "香蕉"); dataset.addValue(600, "广州", "香蕉"); dataset.addValue(500, "北京", "荔枝"); dataset.addValue(400, "上海", "荔枝"); dataset.addValue(300, "广州", "荔枝"); JFreeChart chart = ChartFactory.createLineChart("水果产量图 ", "水果", "产量", dataset, PlotOrientation.VERTICAL, true, true, true); CategoryPlot cp = chart.getCategoryPlot(); cp.setBackgroundPaint(ChartColor.WHITE); // 背景色设置 cp.setRangeGridlinePaint(ChartColor.GRAY); // 网格线色设置 // ChartFrame frame=new ChartFrame ("水果产量图 ",chart,true); // frame.pack(); // frame.setVisible(true); try { ChartUtilities.saveChartAsPNG(new File("D:\\test\\LineChart.png"), chart, 800, 500); } catch (IOException e) { e.printStackTrace(); } }
3D折线图:
- public
void createLineChart3D() { -
DefaultCategoryDataset dataset = new DefaultCategoryDataset(); -
dataset.addValue(300, "北京", "苹果"); -
dataset.addValue(100, "上海", "苹果"); -
dataset.addValue(900, "广州", "苹果"); -
dataset.addValue(200, "北京", "梨子"); -
dataset.addValue(200, "上海", "梨子"); -
dataset.addValue(700, "广州", "梨子"); -
dataset.addValue(300, "北京", "葡萄"); -
dataset.addValue(300, "上海", "葡萄"); -
dataset.addValue(300, "广州", "葡萄"); -
dataset.addValue(400, "北京", "香蕉"); -
dataset.addValue(800, "上海", "香蕉"); -
dataset.addValue(400, "广州", "香蕉"); -
dataset.addValue(100, "北京", "荔枝"); -
dataset.addValue(500, "上海", "荔枝"); -
dataset.addValue(500, "广州", "荔枝"); -
JFreeChart chart = ChartFactory.createLineChart3D("水果产量图 ", "水果", "产量", -
dataset, PlotOrientation.VERTICAL, true, true, true); -
-
CategoryPlot cp = chart.getCategoryPlot(); -
cp.setBackgroundPaint(ChartColor.WHITE); // 背景色设置 -
cp.setRangeGridlinePaint(ChartColor.GRAY); // 网格线色设置 -
-
// ChartFrame frame=new ChartFrame ("水果产量图 ",chart,true); -
// frame.pack(); -
// frame.setVisible(true); -
-
try { -
ChartUtilities.saveChartAsPNG(new File("D:\\test\\LineChart3D.png"), -
chart, 800, 500); -
} catch (IOException e) { -
e.printStackTrace(); -
} -
}