jfreechart中文乱码问题

最近在开发中使用到报表,所以在研究jfreechart,在使用过程中遇到一些问题将它记录下来:

1、中文乱码问题。

2、数值显示(位置)问题。

3、百分比怎么显示。

4、设置背景、修改颜色等等

下面来解决这些问题:

ps:因为在项目中只用到了柱状图、饼状图、折线图,所以只以这三种为示例。

我是根据项目中的使用情况将每种图封装成工具类。

中文乱码:

柱状图要设置三个地方:

1、主题

//创建主题样式 

StandardChartTheme standardChartTheme = new StandardChartTheme("CN");//设置标题字体
standardChartTheme.setExtraLargeFont(new Font("宋书", Font.BOLD, 20));//设置图例的字体
standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 15));//设置轴向的字体
standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 15));//应用主题样式
ChartFactory.setChartTheme(standardChartTheme);

2、设置横轴

//取得横轴
CategoryAxis categoryAxis = plot.getDomainAxis();
categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 20));//横轴显示标签的字体

3、设置纵轴

NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();//取得纵轴(Y轴)
rangeAxis.setLabelFont(new Font("宋体", Font.BOLD, 20));//设置纵轴(Y轴)显示标签的字体

饼状图中文乱码

// JFreeChart主要由三个部分构成:title(标题),legend(图释),plot(图表主体)。          
//三个部分设置字体的方法分别如下:            
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("宋体", Font.BOLD, 20));
LegendTitle leg = chart.getLegend();
if (leg != null) {
leg.setItemFont(new Font("宋体", Font.BOLD, 20));
}
PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelFont(new Font("宋体", Font.BOLD, 12));

折线图中文乱码

TextTitle textTitle = chart.getTitle();
Font font = new Font("宋体", Font.BOLD, 20);
Font titleFont = new Font("宋书", Font.PLAIN, 15);
textTitle.setFont(font); // 设置标题字体  
// 设置图例类别字体  
LegendTitle leg = chart.getLegend();
if (leg != null) {
leg.setItemFont(font);
}
chart.setBackgroundPaint(Color.WHITE);// 设置背景色   

数值显示(位置)和百分比:

//设置每个柱子代表的柱的颜色
//renderer.setSeriesPaint(0, new Color(0, 0, 255));
//renderer.setSeriesPaint(1, new Color(0, 100, 255));
//renderer.setSeriesPaint(2, Color.GREEN);
renderer.setItemMargin(0.3);//设置每个地区所包含的平行柱的之间距离
//设置每个柱的数值
//renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
//在柱上显示百分比
java.text.NumberFormat nf = java.text.NumberFormat.getPercentInstance();
nf.setMaximumFractionDigits(2);
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", nf));
renderer.setBaseItemLabelFont(new java.awt.Font("黑体", Font.TRUETYPE_FONT, 20));
renderer.setBaseItemLabelsVisible(true);//显示每个柱的数值
//默认的数字显示在柱子中,通过如下两句可调整数字的显示  
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
renderer.setItemLabelAnchorOffset(10D);
plot.setRenderer(renderer);//让上面对柱子的设置生效

饼状图显示数值和百分比:

//是否显示线 fasle就不显示了
plot.setLabelLinksVisible(true);
//{0}分类、{1}原值、{2}百分比
//如果百分比要包括一位小数,则使用:
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{2}",
new DecimalFormat("0.00"),
new DecimalFormat("0.00%")));
//显示实际数值
//plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{1}"));
//标签与图形之间的距离
plot.setLabelLinkMargin(-0.05);
//设置连线的颜色
//plot.setLabelLinkPaint(new Color(0, 180, 205));
//为分区设置指定的颜色
//plot.setSectionPaint("未用容量", new Color(125, 125, 125));
//plot.setSectionPaint("已用容量", new Color(0, 180, 205));

设置背后修改其他属性:

柱状图

//得到图表中的Plot对象
CategoryPlot plot = (CategoryPlot) chart.getPlot();
//取得横轴
CategoryAxis categoryAxis = plot.getDomainAxis();
categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 20));//横轴显示标签的字体
//categoryAxis.setLowerMargin(0.1);//设置距离图片左端距离此时为10%
//categoryAxis.setUpperMargin(0.1);//设置距离图片右端距离此时为百分之10
categoryAxis.setCategoryLabelPositionOffset(10);//图表横轴与标签的距离(10像素)
categoryAxis.setCategoryMargin(0.3);//横轴标签之间的距离30%
//categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);//分类标签以45度倾斜
//categoryAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 20));
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();//取得纵轴(Y轴)
rangeAxis.setLabelFont(new Font("宋体", Font.BOLD, 20));//设置纵轴(Y轴)显示标签的字体
//设置最高的一个柱与图片顶端的距离
rangeAxis.setUpperMargin(0.5);
//设置最低的一个 Item 与图片底端的距离
rangeAxis.setLowerMargin(0.15);
// 数据轴数据标签的显示格式    
rangeAxis.setNumberFormatOverride(new DecimalFormat("0%"));
BarRenderer3D renderer = (BarRenderer3D) plot.getRenderer();
renderer.setBaseOutlinePaint(Color.BLACK);
renderer.setWallPaint(Color.gray);//设置 Wall 的颜色
//设置网格背景颜色  
plot.setBackgroundPaint(Color.white);
//设置网格竖线颜色  
plot.setDomainGridlinePaint(Color.pink);
//设置网格横线颜色  
plot.setRangeGridlinePaint(Color.pink);
renderer.setItemMargin(0.3);//设置每个地区所包含的平行柱的之间距离

饼状

//设置绘图面板外边以及阴影的填充颜色
plot.setOutlinePaint(Color.BLACK);
plot.setShadowPaint(Color.WHITE);
//设置整个面板的背景色
plot.setBackgroundPaint(Color.WHITE);
//设置标签背景颜色、边框颜色、阴影颜色和文字颜色
plot.setLabelBackgroundPaint(null);
plot.setLabelOutlinePaint(null);
plot.setLabelShadowPaint(null);
plot.setLabelPaint(new Color(120, 120, 120));

折线图

chart.setBackgroundPaint(Color.WHITE);// 设置背景色   
//获取绘图区对象  
CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.WHITE); // 设置绘图区背景色  
plot.setRangeGridlinePaint(Color.WHITE); // 设置水平方向背景线颜色  
plot.setRangeGridlinesVisible(true);// 设置是否显示水平方向背景线,默认值为true  
plot.setDomainGridlinePaint(Color.WHITE); // 设置垂直方向背景线颜色  
plot.setDomainGridlinesVisible(true); // 设置是否显示垂直方向背景线,默认值为false  
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLabelFont(font); // 设置横轴字体  
domainAxis.setTickLabelFont(font);// 设置坐标轴标尺值字体  
domainAxis.setLowerMargin(0.01);// 左边距 边框距离  
domainAxis.setUpperMargin(0.06);// 右边距 边框距离,防止最后边的一个数据靠近了坐标轴。  
domainAxis.setMaximumCategoryLabelLines(2);
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setLabelFont(font);
//rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());//Y轴显示整数  
rangeAxis.setNumberFormatOverride(new DecimalFormat("0%"));//Y轴显示百分比
rangeAxis.setAutoRangeMinimumSize(1); //最小跨度  
rangeAxis.setUpperMargin(0.18);//上边距,防止最大的一个数据靠近了坐标轴。     
rangeAxis.setLowerBound(0); //最小值显示0  
rangeAxis.setAutoRange(false); //不自动分配Y轴数据  
rangeAxis.setTickMarkStroke(new BasicStroke(1.6f)); // 设置坐标标记大小  
rangeAxis.setTickMarkPaint(Color.BLACK); // 设置坐标标记颜色  
// 获取折线对象  
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
BasicStroke realLine = new BasicStroke(1.2f); // 设置实线  
// 设置虚线  
float dashes[] = { 5.0f };
BasicStroke brokenLine = new BasicStroke(1.6f, // 线条粗细  
BasicStroke.CAP_ROUND, // 端点风格  
BasicStroke.JOIN_ROUND, // 折点风格  
8f, dashes, 0.6f);
for (int i = 0; i < dataSet.getRowCount(); i++) {
if (i % 2 == 0) {
renderer.setSeriesStroke(i, realLine); // 利用实线绘制  
} else {
renderer.setSeriesStroke(i, brokenLine); // 利用虚线绘制  
}
}
//绘制折点样式
renderer.setBaseShapesVisible(true);
renderer.setDrawOutlines(true);
renderer.setUseFillPaint(true);
renderer.setBaseFillPaint(java.awt.Color.white);
plot.setNoDataMessage("无对应的数据,请重新查询。");
plot.setNoDataMessageFont(titleFont);//字体的大小  
plot.setNoDataMessagePaint(Color.RED);//字体颜色 
plot.setRenderer(renderer);

源码下载:http://download.csdn.net/detail/qq_20039385/9445164

其实这些管网的dome中都有介绍,多花时间去看看官方的dome和文档会大有收获。

大神勿喷,有不对的地方需要改进的地方大神们跟帖,共同学习。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值