被jreeechat虐了,收拾心情,整理一下成果:
1、解决Jfreechat The type org.jfree.util.PublicCloneable cannot be resolved错误
靠了 今天用jfreechat整个报表 NND 老报 The type org.jfree.util.PublicCloneable cannot be resolved. It is indirectly referenced from required .class files。烦躁
整了半天 发现少了个报:
jcommon-1.0.14.jar
在此铭记!
2、乱码问题
设置字体就不会出现乱码问题。
/**
* 配置字体
* @param chart JFreeChart 对象
*/
private void configFont(JFreeChart chart){
// 配置字体
Font xfont = new Font("宋体",Font.PLAIN,12) ;// X轴
Font yfont = new Font("宋体",Font.PLAIN,12) ;// Y轴
Font kfont = new Font("宋体",Font.PLAIN,12) ;// 底部
Font titleFont = new Font("隶书", Font.BOLD , 25) ; // 图片标题
CategoryPlot plot = chart.getCategoryPlot();// 图形的绘制结构对象
// 图片标题
chart.setTitle(new TextTitle(chart.getTitle().getText(),titleFont));
// 底部
chart.getLegend().setItemFont(kfont);
// X 轴
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLabelFont(xfont);// 轴标题
domainAxis.setTickLabelFont(xfont);// 轴数值
domainAxis.setTickLabelPaint(Color.BLUE) ; // 字体颜色
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 横轴上的label斜显示
// Y 轴
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setLabelFont(yfont);
rangeAxis.setLabelPaint(Color.BLUE) ; // 字体颜色
rangeAxis.setTickLabelFont(yfont);
}
3、设置不同柱子的颜色
static class CustomRenderer extends BarRenderer {
private Paint colors[];
public Paint getItemPaint(int i, int j) {
return colors[j % colors.length];
}
public CustomRenderer(Paint apaint[]) {
colors = apaint;
}
}
CustomRenderer renderer = new CustomRenderer(new Paint[] { Color.red, Color.blue, Color.green, Color.yellow,
Color.orange, Color.cyan, Color.magenta, Color.blue });
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
4、
public JFreeChart getChart()
{
chart = ChartFactory.createBarChart3D(
"热点讨论投票结果", // 图表标题
"", // 目录轴的显示标签
"", // 数值轴的显示标签
getDataSet(), // 数据集
//PlotOrientation.HORIZONTAL , // 图表方向:水平
PlotOrientation.VERTICAL , // 图表方向:垂直
false, // 是否显示图例(对于简单的柱状图必须是false)
true, // 是否生成工具
true // 是否生成URL链接
);
//重新设置图标标题,改变字体
chart.setTitle(new TextTitle("热点讨论投票结果", new Font("黑体", Font.ITALIC , 18)));
//取得统计图标的第一个图例
//LegendTitle legend = chart.getLegend(0);
//修改图例的字体,必须把显示图例设置为true,否则会报空指针异常
//legend.setItemFont(new Font("宋体", Font.BOLD, 14));
//获得柱状图的Plot对象
CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.pink); // 设定图表数据显示部分背景色
//取得横轴
CategoryAxis categoryAxis = plot.getDomainAxis();
//设置横轴显示标签的字体
categoryAxis.setLabelFont(new Font("宋体" , Font.BOLD , 18));
//分类标签以45度角倾斜
//categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
categoryAxis.setTickLabelFont(new Font("宋体" , Font.BOLD , 18));
//取得纵轴
NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
//设置纵轴显示标签的字体
numberAxis.setLabelFont(new Font("宋体" , Font.BOLD , 18));
//设置最高的一个柱与图片顶端的距离
numberAxis.setUpperMargin(0.1);
//numberAxis.setFixedAutoRange(100);
//设置整数显示
//numberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
//numberAxis.setNegativeArrowVisible(true);
//取最大数Math.max(supportCount, blackballCount)
numberAxis.setUpperBound(1);
numberAxis.setLowerBound(0.01);
//设置百分比显示
numberAxis.setNumberFormatOverride(new DecimalFormat("0%"));
//numberAxis.setNumberFormatOverride(new DecimalFormat("0.00%"));
//设置最小显示数,小于的话会显示在中间(正负)
//numberAxis.setAutoRangeMinimumSize(1);
plot.setNoDataMessage("没有可供使用的数据!");
plot.setNoDataMessagePaint(Color.blue);
BarRenderer3D renderer = new BarRenderer3D();
//设置柱子宽度
renderer.setMaximumBarWidth(0.1);
//设置柱子高度
renderer.setMinimumBarLength(0.2);
//设置柱子的颜色
renderer.setSeriesPaint(0, new Color(0, 0, 255));
//设置柱子边框可见
//renderer.setDrawBarOutline(true);
//设置柱子默认的边框颜色,必须设置边框可见才起效
//renderer.setBaseOutlinePaint(Color.gray);
//设置分类柱子的边框色,覆盖默认的边框颜色,必须设置边框可见才起效
//renderer.setSeriesOutlinePaint(0,Color.red);
//设置柱子的纵横背景色
//renderer.setWallPaint(Color.gray);
//设置平行柱的之间距离
renderer.setItemMargin(0.5);
//显示每个柱的数值,并修改该数值的字体属性
renderer.setIncludeBaseInRange(true);
//将修改后的属性值保存到图中,这一步很重要,否则上面对颜色的设置都无效
plot.setRenderer(renderer);
//设置柱子的透明度,0.8相当于80%的透明度
plot.setForegroundAlpha(0.8f);
return chart;
}
//返回一个CategoryDataset实例
private static CategoryDataset getDataSet()
{
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
int total = supportCount+blackballCount;
if(total < 1) total = 1;
dataset.addValue((double)supportCount/total, "" , "正方(" + supportCount + ")");
dataset.addValue((double)blackballCount/total, "" , "反方(" + blackballCount + ")");
//dataset.addValue(supportCount, "佛山" , "正方");
//dataset.addValue(blackballCount, "佛山" , "反方");
//dataset.addValue(supportCount , "广州" , "正方");
//dataset.addValue(blackballCount , "广州" , "反方");
return dataset;
}