jfreechart生成折线图 | 饼图 | 柱状图 | 堆栈柱状图
1.所需包
(1) jfreechart-1.0.8a.jar
(2) jcommon-1.0.12.jar
2.运行环境
JDK 1.5以上
3.源码
package jfreechart;
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
/**
* 实际取色的时候一定要16位的,这样比较准确
*
* @author new
*/
public class TestCreateChart {
private static final String CHART_PATH = "d:/test/";
public static void main(String[] args) {
// TODO Auto-generated method stub
TestCreateChart pm = new TestCreateChart();
// 生成饼状图
pm.makePieChart();
// 生成单组柱状图
pm.makeBarChart();
// 生成多组柱状图
pm.makeBarGroupChart();
// 生成堆积柱状图
pm.makeStackedBarChart();
// 生成折线图
pm.makeLineAndShapeChart();
}
/**
* 生成折线图
*/
public void makeLineAndShapeChart() {
double[][] data = new double[][] { { 672, 766, 223, 540, 126 },
{ 325, 521, 210, 340, 106 }, { 332, 256, 523, 240, 526 } };
String[] rowKeys = { "苹果", "梨子", "葡萄" };
String[] columnKeys = { "北京", "上海", "广州", "成都", "深圳" };
CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
createTimeXYChar("折线图", "x轴", "y轴", dataset, "lineAndShap.png");
}
/**
* 生成分组的柱状图
*/
public void makeBarGroupChart() {
double[][] data = new double[][] { { 672, 766, 223, 540, 126 },
{ 325, 521, 210, 340, 106 }, { 332, 256, 523, 240, 526 } };
String[] rowKeys = { "苹果", "梨子", "葡萄" };
String[] columnKeys = { "北京", "上海", "广州", "成都", "深圳" };
CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
createBarChart(dataset, "x坐标", "y坐标", "柱状图", "barGroup.png");
}
/**
* 生成柱状图
*/
public void makeBarChart() {
double[][] data = new double[][] { { 672, 766, 223, 540, 126 } };
String[] rowKeys = { "苹果" };
String[] columnKeys = { "北京", "上海", "广州", "成都", "深圳" };
CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
createBarChart(dataset, "x坐标", "y坐标", "柱状图", "bar.png");
}
/**
* 生成堆栈柱状图
*/
public void makeStackedBarChart() {
double[][] data = new double[][] { { 0.21, 0.66, 0.23, 0.40, 0.26 },
{ 0.25, 0.21, 0.10, 0.40, 0.16 } };
String[] rowKeys = { "苹果", "梨子" };
String[] columnKeys = { "北京", "上海", "广州", "成都", "深圳" };
CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
createStackedBarChart(dataset, "x坐标", "y坐标", "柱状图", "stsckedBar.png");
}
/**
* 生成饼状图
*/
public void makePieChart() {
double[] data = { 9, 91 };
String[] keys = { "失败率", "成功率" };
createValidityComparePimChar(getDataPieSetByUtil(data, keys), "饼状图",
"pie2.png", keys);
}
// 柱状图,折线图 数据集
public CategoryDataset getBarData(double[][] data, String[] rowKeys,
String[] columnKeys) {
return DatasetUtilities
.createCategoryDataset(rowKeys, columnKeys, data);
}
// 饼状图 数据集
public PieDataset getDataPieSetByUtil(double[] data,
String[] datadescription) {
if (data != null && datadescription != null) {
if (data.length == datadescription.length) {
DefaultPieDataset dataset = new DefaultPieDataset();
for (int i = 0; i < data.length; i++) {
dataset.setValue(datadescription[i], data[i]);
}
return dataset;
}
}
return null;
}
/**
* 柱状图
*
* @param dataset
* 数据集
* @param xName
* x轴的说明(如种类,时间等)
* @param yName
* y轴的说明(如速度,时间等)
* @param chartTitle
* 图标题
* @param charName
* 生成图片的名字
* @return
*/
public String createBarChart(CategoryDataset dataset, String xName,
String yName, String chartTitle, String charName) {
JFreeChart chart = ChartFactory.createBarChart(chartTitle, // 图表标题
xName, // 目录轴的显示标签
yName, // 数值轴的显示标签
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平、垂直
true, // 是否显示图例(对于简单的柱状图必须是false)
false, // 是否生成工具
false // 是否生成URL链接
);
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);
/*
* VALUE_TEXT_ANTIALIAS_OFF表示
补充:软件开发 , Java ,