1.中文乱码问题。
# 产生乱码的原因分析。
创建图表的时候,会调用 ChartFactory 工厂类,工厂类里的创建函数的内部,会创建 JFreeChart 对象,如下:
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
其中有个参数是JFreeChart.DEFAULT_TITLE_FONT,跟进之后会发现其实就是 SansSerif 字体,如下:
public static final Font DEFAULT_TITLE_FONT = new Font("SansSerif", 1, 18);
很多文章说到,用 SansSerif 字体就会产生乱码。真的是这样吗? 用 jfreechart-1.0.13.jar 会产生乱码,但用 jfreechart-1.0.10.jar 就不会。 对比两个 jar 包中的 JFreeChart 类,发现 draw 函数进行了更新。 结论:根本原因不是 SansSerif 字体,而是 JFreeChart 的作者对默认字体的处理方式发生了变化。
2.TimeSeriesChart X坐标轴显示问题。包括显示的单位和现实的格式。
# 产生此问题的原因是,程序员对 API 的不了解造成的,很过功能在开发 API 的时候已经考虑进去了。
3.LineChart 底部乱码问题。
# Winux 将 JFreeChart 做成了 Servlet 。
Servlet 从 URL 中获得参数之后,并没有转码,直接提交到了,JFreeChart 数据构建函数。 用如下方法转码之后,问题成功解决。
new String(str.getBytes("iso-8859-1"))
目前已解决 1 和 2,3正在研究。
解决方案如下:
package com.winux.GetChart;
import java.awt.Color;
import java.awt.Font;
import java.text.SimpleDateFormat;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.*;
public class NewJFreeChart {
//轴字体
static Font font = new Font("宋体", Font.PLAIN, 16);
//标题字体
static Font titleFont = new Font("隶书", Font.BOLD, 25);
//设置标题和底部字体
private static void setTitleAndLegend(JFreeChart chart){
chart.getTitle().setFont(titleFont);
chart.getLegend().setItemFont(font);
}
// 设置柱状图坐标轴字体
public static void setBarChartLabelFont(JFreeChart chart) {
setTitleAndLegend(chart);
//X轴字体 & 颜色 & 倾斜
chart.getCategoryPlot().getDomainAxis().setLabelFont(font);
chart.getCategoryPlot().getDomainAxis().setTickLabelFont(font);
chart.getCategoryPlot().getDomainAxis().setTickLabelPaint(Color.BLUE);
chart.getCategoryPlot().getDomainAxis().setCategoryLabelPosition s(CategoryLabelPositions.UP_45);
//Y轴字体 & 颜色
chart.getCategoryPlot().getRangeAxis().setLabelFont(font);
chart.getCategoryPlot().getRangeAxis().setTickLabelFont(font);
chart.getCategoryPlot().getRangeAxis().setTickLabelPaint(Color.BLUE);
}
// 设置饼状图坐标轴字体
public static void setPieChartLabelFont(JFreeChart chart) {
setTitleAndLegend(chart);
//饼图字体
((PiePlot) chart.getPlot()).setLabelFont(font);
}
// 设置时序图坐标轴字体 --- 第二个参数是例如“YYYY”的字符串
public static void setTimeSeriesChartLabelF ont(JFreeChart chart, String DateFormat) {
setTitleAndLegend(chart);
//日期显示格式
DateAxis dateaxis = (DateAxis) ((XYPlot) chart.getPlot()).getDomainAxis();
dateaxis.setDateFormatOverride(new SimpleDateFormat(DateFormat));
//设置坐标间隔。DateTickUnitType.YEAR 是单位,后面 1 是间隔。
dateaxis.setTickUnit(new DateTickUnit(DateTickUnitType.YEAR, 1));
//X轴字体 & 颜色
((XYPlot) chart.getPlot()).getDomainAxis().setLabelFont(font);
((XYPlot) chart.getPlot()).getDomainAxis().setTickLabelFont(font);
((XYPlot) chart.getPlot()).getDomainAxis().setTickLabelPaint(Color.BLUE);
//Y轴字体 & 颜色
((XYPlot) chart.getPlot()).getRangeAxis().setLabelFont(font);
((XYPlot) chart.getPlot()).getRangeAxis().setTickLabelFont(font);
((XYPlot) chart.getPlot()).getRangeAxis().setTickLabelPaint(Color.BLUE);
}
// 设置线图图坐标轴字体
public static void setLineChartLabelFont(JFreeChart chart) {
setTitleAndLegend(chart);
//X轴字体 & 颜色 & 倾斜
chart.getCategoryPlot().getDomainAxis().setTickLabelFont(font);
chart.getCategoryPlot().getDomainAxis().setLabelFont(font);
chart.getCategoryPlot().getDomainAxis().setTickLabelPaint(Color.BLUE);
chart.getCategoryPlot().getDomainAxis().setCategoryLabelPosition s(CategoryLabelPositions.UP_45);
//Y轴字体 & 颜色
((NumberAxis) chart.getCategoryPlot().getRangeAxis()).setTickLabelFont(font);
((NumberAxis) chart.getCategoryPlot().getRangeAxis()).setLabelFont(font);
((NumberAxis) chart.getCategoryPlot().getRangeAxis()).setTickLabelPaint(Color.BLUE);
}
}