Jfreechart的最新版本,对于中文默认是不支持的,因为默认的font为英文的,所以需要单独设置。以前一直诟病于jfreechart的生成的图形颜色不好看,图片不清晰,现在新版本已经好了很多了,至于图形的颜色,可以自定义。
三种常见的图代码。
/**
* copyRight vaalhaai.com
*/
package com.vaalhaai.framework.report.chart;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.IOException;
import java.text.NumberFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryAxis3D;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberAxis3D;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
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.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.TextAnchor;
/**
* @author Jimmy.Shine 2010-5-9
*
*/
public class ChartTest {
/**
* @param args
* @throws IOException
* @throws FontFormatException
*/
public static void main(String[] args) throws FontFormatException, IOException {
// pie3D();
// bar3D();
line();
}
public static void pie3D(){
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("中国电信", 30);
dataset.setValue("中国移动", 60);
dataset.setValue("中国联通", 10);
dataset.setValue("固定电话", 40);
dataset.setValue("其它", 20);
JFreeChart chart = ChartFactory.createPieChart3D("通信分布饼图", dataset, true, true, false);
TextTitle title = chart.getTitle();
title.setFont(new Font("黑体", Font.BOLD, 18));
LegendTitle legendTitle = chart.getLegend();
legendTitle.setItemFont(new Font("微软雅黑", Font.BOLD, 12));
PiePlot3D plot = (PiePlot3D) chart.getPlot();
plot.setLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
plot.setLabelBackgroundPaint(new Color(255, 255, 255));
plot.setBackgroundPaint(new Color(255, 255, 255));
plot.setOutlineVisible(true);
plot.setBackgroundAlpha(0.75F);
plot.setForegroundAlpha(0.80F);
plot.setStartAngle(90);
//int r = 61, g = 89, b = 171;
int r = 20, g = 20, b = 20;
int rStep = 30;
int gStep = 30;
int bStep = 30;
plot.setSectionPaint("中国电信", new Color(r, g, b));
r = (r + rStep > 255 ? r : r + rStep);
g = (g + gStep > 255 ? g : g + gStep);
b = (b + bStep > 255 ? b : b + bStep);
plot.setSectionPaint("中国移动", new Color(r, g, b));
r = (r + rStep > 255 ? r : r + rStep);
g = (g + gStep > 255 ? g : g + gStep);
b = (b + bStep > 255 ? b : b + bStep);
plot.setSectionPaint("中国联通", new Color(r, g, b));
r = (r + rStep > 255 ? r : r + rStep);
g = (g + gStep > 255 ? g : g + gStep);
b = (b + bStep > 255 ? b : b + bStep);
plot.setSectionPaint("固定电话", new Color(r, g, b));
r = (r + rStep > 255 ? r : r + rStep);
g = (g + gStep > 255 ? g : g + gStep);
b = (b + bStep > 255 ? b : b + bStep);
plot.setSectionPaint("其它", new Color(r, g, b));
plot.setDarkerSides(true);
plot.setShadowPaint(new Color(0, 0, 255));
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({2})"));
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}({1}/{3}={2})"));
// plot.setExplodePercent("中国电信", 0.3F);
// plot.setExplodePercent("中国移动", 0.3F);
// plot.setExplodePercent("中国联通", 0.3F);
// plot.setExplodePercent("固定电话", 0.3F);
ChartFrame frame = new ChartFrame("通信 ", chart);
frame.pack();
frame.setVisible(true);
}
public static void bar3D(){
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(30, "移动通信", "中国电信");
dataset.addValue(60, "移动通信", "中国移动");
dataset.addValue(10, "移动通信", "中国联通");
dataset.addValue(40, "固定通信", "中国电信");
dataset.addValue(-10, "固定通信", "中国移动");
dataset.addValue(20, "固定通信", "中国联通");
JFreeChart chart = ChartFactory.createBarChart3D("通信分布柱状图", "类别", "值", dataset,PlotOrientation.VERTICAL, true, true, false);
TextTitle title = chart.getTitle();
title.setFont(new Font("黑体", Font.BOLD, 18));
LegendTitle legendTitle = chart.getLegend();
legendTitle.setItemFont(new Font("微软雅黑", Font.BOLD, 12));
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(new Color(255, 255, 255));
plot.setOutlineVisible(true);
plot.setBackgroundAlpha(0.75F);
plot.setForegroundAlpha(0.80F);
plot.setRangeGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.GRAY);
CategoryAxis3D xAxis = (CategoryAxis3D) plot.getDomainAxis();
xAxis.setLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
xAxis.setTickLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
ValueAxis yAxis = plot.getRangeAxis();
yAxis.setLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
yAxis.setTickLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
BarRenderer render = (BarRenderer) plot.getRenderer();
render.setBaseItemLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
render.setBaseLegendTextFont(new Font("微软雅黑", Font.BOLD, 12));
render.setMaximumBarWidth(0.1F);
render.setItemMargin(0.0F);
render.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{3}",NumberFormat.getInstance()));
render.setBaseItemLabelsVisible(true);
render.setItemLabelAnchorOffset(8);
render.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT));
render.setBaseNegativeItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT));
/*
ValueMarker marker = new ValueMarker(0.70, new Color(200, 200, 255),
new BasicStroke(1.0f), new Color(200, 200, 255),
new BasicStroke(1.0f), 1.0f);
plot.addRangeMarker(marker);
*/
/* GradientPaint gp0 = new GradientPaint(
0.0f, 0.0f, Color.blue,
0.0f, 0.0f, new Color(0, 0, 64)
);
GradientPaint gp1 = new GradientPaint(
0.0f, 0.0f, Color.green,
0.0f, 0.0f, new Color(0, 64, 0)
);
GradientPaint gp2 = new GradientPaint(
0.0f, 0.0f, Color.red,
0.0f, 0.0f, new Color(64, 0, 0)
);
render.setSeriesPaint(0, gp0);
render.setSeriesPaint(1, gp1);
render.setSeriesPaint(2, gp2);
*/
int r = 20, g = 20, b = 20;
int rStep = 120;
int gStep = 120;
int bStep = 120;
render.setSeriesPaint(0, new Color(r,g,b));
r = (r + rStep > 255 ? r : r + rStep);
g = (g + gStep > 255 ? g : g + gStep);
b = (b + bStep > 255 ? b : b + bStep);
render.setSeriesPaint(1, new Color(r,g,b));
ChartFrame frame = new ChartFrame("通信 ", chart);
frame.pack();
frame.setVisible(true);
}
public static void line(){
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(30, "通信", "中国电信");
dataset.addValue(60, "通信", "中国移动");
dataset.addValue(10, "通信", "中国联通");
dataset.addValue(40, "通信", "中国电信固话");
dataset.addValue(10, "通信", "其它固话");
dataset.addValue(20, "通信", "中国联通固话");
dataset.addValue(30, "3G", "中国电信");
dataset.addValue(50, "3G", "中国移动");
dataset.addValue(20, "3G", "中国联通");
dataset.addValue(50, "3G", "中国电信固话");
dataset.addValue(30, "3G", "其它固话");
dataset.addValue(20, "3G", "中国联通固话");
JFreeChart chart = ChartFactory.createLineChart("通信分布线图", "类别", "值", dataset,PlotOrientation.VERTICAL, true, true, false);
TextTitle title = chart.getTitle();
title.setFont(new Font("黑体", Font.BOLD, 18));
LegendTitle legendTitle = chart.getLegend();
legendTitle.setItemFont(new Font("微软雅黑", Font.BOLD, 12));
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(new Color(255, 255, 255));
plot.setOutlineVisible(true);
plot.setBackgroundAlpha(0.75F);
plot.setForegroundAlpha(0.80F);
plot.setRangeGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.GRAY);
CategoryAxis xAxis = (CategoryAxis) plot.getDomainAxis();
xAxis.setLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
xAxis.setTickLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
NumberAxis yAxis = (NumberAxis)plot.getRangeAxis();
yAxis.setLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
yAxis.setTickLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
yAxis.setStandardTickUnits(NumberAxis3D.createIntegerTickUnits());
LineAndShapeRenderer render = (LineAndShapeRenderer) plot.getRenderer();
render.setBaseItemLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
render.setBaseLegendTextFont(new Font("微软雅黑", Font.BOLD, 12));
render.setItemMargin(0.0F);
render.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}",NumberFormat.getInstance()));
render.setBaseItemLabelsVisible(true);
render.setBaseLinesVisible(true);
render.setBaseSeriesVisibleInLegend(true);
render.setBaseSeriesVisible(true);
render.setBaseShapesVisible(true);
render.setItemLabelAnchorOffset(2);
render.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE2,TextAnchor.BASELINE_LEFT));
render.setBaseNegativeItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE2,TextAnchor.BASELINE_LEFT));
int r = 61, g = 89, b = 171;
int rStep = 120;
int gStep = 120;
int bStep = 120;
render.setSeriesPaint(0, new Color(r,g,b));
r = (r + rStep > 255 ? r : r + rStep);
g = (g + gStep > 255 ? g : g + gStep);
b = (b + bStep > 255 ? b : b + bStep);
render.setSeriesPaint(1, new Color(r,g,b));
ChartFrame frame = new ChartFrame("通信 ", chart);
frame.pack();
frame.setVisible(true);
}
}
效果见附件