JFreeChart中文乱码笔记.docx

上传人:b****6 文档编号:7152833 上传时间:2023-01-21 格式:DOCX 页数:8 大小:74.25KB
下载 相关 举报
JFreeChart中文乱码笔记.docx_第1页
第1页 / 共8页
JFreeChart中文乱码笔记.docx_第2页
第2页 / 共8页
JFreeChart中文乱码笔记.docx_第3页
第3页 / 共8页
JFreeChart中文乱码笔记.docx_第4页
第4页 / 共8页
JFreeChart中文乱码笔记.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

JFreeChart中文乱码笔记.docx

《JFreeChart中文乱码笔记.docx》由会员分享,可在线阅读,更多相关《JFreeChart中文乱码笔记.docx(8页珍藏版)》请在冰豆网上搜索。

JFreeChart中文乱码笔记.docx

JFreeChart中文乱码笔记

解决jfreechart中文乱码方案整理【图】

21,826浏览

这篇博文只是对网上关于jfreechart中文乱码解决方法的一个汇总整理。

 

我也是最近要使用到jfreechart这个图表工具,也是碰到了中文乱码这个问题,后来通过搜索(jfreechart图片乱码等关键词)解决了这个乱码,但发现一个问题就是有的文章只是解决了图表中乱码的某一个方面,比如图表标题,而有的文章不是解决图表标题乱码,却能够解决图表X、Y轴上文字和标题乱码以及底部中文乱码,有鉴于此,于是我就将这些解决方法都汇总在了一起,希望对碰到jfreechart中文乱码的朋友有帮助。

下图是一个柱形图表,非常典型的中文乱码(其他形式图表就不说明了,因为柱形非常有代表性):

以上图表对应的jsp文件代码为:

<%@pagecontentType="text/html;charset=UTF-8"%>

<%@pageimport="org.jfree.chart.ChartFactory,

org.jfree.chart.JFreeChart,

org.jfree.chart.plot.PlotOrientation,

org.jfree.chart.servlet.ServletUtilities,

org.jfree.data.category.CategoryDataset,

org.jfree.data.general.DatasetUtilities,

org.jfree.chart.plot.*,

org.jfree.chart.labels.*,

org.jfree.chart.renderer.category.BarRenderer3D,

java.awt.*,

org.jfree.ui.*,

org.jfree.chart.axis.AxisLocation,org.jfree.chart.title.TextTitle,org.jfree.chart.axis.CategoryAxis,org.jfree.chart.axis.NumberAxis"%>

<%

double[][]data=newdouble[][]{{1310,1220,1110,1000},{720,700,680,640},{1130,1020,980,800},{440,400,360,300}};

String[]rowKeys={"猪肉","niurou","鸡肉","鱼肉"};

String[]columnKeys={"广州","shenzhen","东莞","佛山"};

CategoryDatasetdataset=DatasetUtilities.createCategoryDataset(rowKeys,columnKeys,data);

JFreeChartchart=ChartFactory.createBarChart3D("","肉类","销量",dataset,PlotOrientation.VERTICAL,true,true,false);

CategoryPlotplot=chart.getCategoryPlot();

//设置网格背景颜色

plot.setBackgroundPaint(Color.white);

//设置网格竖线颜色

plot.setDomainGridlinePaint(Color.pink);

//设置网格横线颜色

plot.setRangeGridlinePaint(Color.pink);

//显示每个柱的数值,并修改该数值的字体属性

BarRenderer3Drenderer=newBarRenderer3D();

renderer.setBaseItemLabelGenerator(newStandardCategoryItemLabelGenerator());

renderer.setBaseItemLabelsVisible(true);

//默认的数字显示在柱子中,通过如下两句可调整数字的显示

//注意:

此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题

renderer.setBasePositiveItemLabelPosition(newItemLabelPosition(ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT));

renderer.setItemLabelAnchorOffset(10D);

renderer.setItemLabelFont(newFont("宋体",Font.PLAIN,12));

renderer.setItemLabelsVisible(true);

//设置每个地区所包含的平行柱的之间距离

//renderer.setItemMargin(0.3);

plot.setRenderer(renderer);

//设置地区、销量的显示位置

//将下方的“肉类”放到上方

plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);

//将默认放在左边的“销量”放到右方

plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);

Stringfilename=ServletUtilities.saveChartAsPNG(chart,700,400,null,session);

StringgraphURL=request.getContextPath()+"/DisplayChart?

filename="+filename;

%>

"width=700height=400border=0usemap="#<%=filename%>">

注意到在以上的图表中,有四个位置的中文乱码:

1、 图表标题以及副标题乱码

2、 X轴乱码

3、 Y轴乱码

4、 图表底部乱码

如下图所示:

对这四个位置的乱码分别进行解决:

NumberAxisnumberaxis=(NumberAxis)plot.getRangeAxis();

CategoryAxisdomainAxis=plot.getDomainAxis();

1、 图表标题以及副标题乱码

Fontfont=newFont("宋体",Font.BOLD,16);

TextTitletitle=newTextTitle("肉类销量统计图",font);

//副标题

TextTitlesubtitle= new TextTitle(“副标题”, new Font(“黑体”,Font.BOLD,12));

chart.addSubtitle(subtitle);

chart.setTitle(title);//标题

2、 X轴乱码

2.1、X轴坐标上的文字:

domainAxis.setTickLabelFont(newFont(“sans-serif”,Font.PLAIN,11));

2.2、X轴坐标标题(肉类)

domainAxis.setLabelFont(newFont(“宋体”,Font.PLAIN,12));

3、 Y轴乱码

3.1、Y轴坐标上的文字:

numberaxis.setTickLabelFont(newFont(“sans-serif”,Font.PLAIN,12));

3.2、Y轴坐标标题(销量):

numberaxis.setLabelFont(newFont(“黑体”,Font.PLAIN,12));

4、 图表底部乱码(猪肉等文字)

chart.getLegend().setItemFont(newFont(“宋体”,Font.PLAIN,12));

通过以上设置就解决了中文乱码了,解决中文乱码后的图表如下所示:

对应的完整代码为(jsp):

<%@pagecontentType="text/html;charset=UTF-8"%>

<%@pageimport="org.jfree.chart.ChartFactory,

org.jfree.chart.JFreeChart,

org.jfree.chart.plot.PlotOrientation,

org.jfree.chart.servlet.ServletUtilities,

org.jfree.data.category.CategoryDataset,

org.jfree.data.general.DatasetUtilities,

org.jfree.chart.plot.*,

org.jfree.chart.labels.*,

org.jfree.chart.renderer.category.BarRenderer3D,

java.awt.*,

org.jfree.ui.*,

org.jfree.chart.axis.AxisLocation,org.jfree.chart.title.TextTitle,org.jfree.chart.axis.CategoryAxis,org.jfree.chart.axis.NumberAxis"%>

<%

double[][]data=newdouble[][]{{1310,1220,1110,1000},{720,700,680,640},{1130,1020,980,800},{440,400,360,300}};

String[]rowKeys={"猪肉","niurou","鸡肉","鱼肉"};

String[]columnKeys={"广州","shenzhen","东莞","佛山"};

CategoryDatasetdataset=DatasetUtilities.createCategoryDataset(rowKeys,columnKeys,data);

JFreeChartchart=ChartFactory.createBarChart3D("","肉类","销量",dataset,PlotOrientation.VERTICAL,true,true,false);

CategoryPlotplot=chart.getCategoryPlot();

//设置字体,不然会中文乱码的

Fontfont=newFont("宋体",Font.BOLD,16);

TextTitletitle=newTextTitle("肉类销量统计图",font);

//副标题

TextTitlesubtitle=newTextTitle("副标题",newFont("黑体",Font.BOLD,12));

chart.addSubtitle(subtitle);

chart.setTitle(title);//标题

//////////////////////////

NumberAxisnumberaxis=(NumberAxis)plot.getRangeAxis();

CategoryAxisdomainAxis=plot.getDomainAxis();

/*------设置X轴坐标上的文字-----------*/

domainAxis.setTickLabelFont(newFont("sans-serif",Font.PLAIN,11));

/*------设置X轴的标题文字------------*/

domainAxis.setLabelFont(newFont("宋体",Font.PLAIN,12));

/*------设置Y轴坐标上的文字-----------*/

numberaxis.setTickLabelFont(newFont("sans-serif",Font.PLAIN,12));

/*------设置Y轴的标题文字------------*/

numberaxis.setLabelFont(newFont("黑体",Font.PLAIN,12));

/*------这句代码解决了底部汉字乱码的问题-----------*/

chart.getLegend().setItemFont(newFont("宋体",Font.PLAIN,12));

//设置网格背景颜色

plot.setBackgroundPaint(Color.white);

//设置网格竖线颜色

plot.setDomainGridlinePaint(Color.pink);

//设置网格横线颜色

plot.setRangeGridlinePaint(Color.pink);

//显示每个柱的数值,并修改该数值的字体属性

BarRenderer3Drenderer=newBarRenderer3D();

renderer.setBaseItemLabelGenerator(newStandardCategoryItemLabelGenerator());

renderer.setBaseItemLabelsVisible(true);

//默认的数字显示在柱子中,通过如下两句可调整数字的显示

//注意:

此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题

renderer.setBasePositiveItemLabelPosition(newItemLabelPosition(ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT));

renderer.setItemLabelAnchorOffset(10D);

renderer.setItemLabelFont(newFont("宋体",Font.PLAIN,12));

renderer.setItemLabelsVisible(true);

//设置每个地区所包含的平行柱的之间距离

//renderer.setItemMargin(0.3);

plot.setRenderer(renderer);

//设置地区、销量的显示位置

//将下方的“肉类”放到上方

plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);

//将默认放在左边的“销量”放到右方

plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);

Stringfilename=ServletUtilities.saveChartAsPNG(chart,700,400,null,session);

StringgraphURL=request.getContextPath()+"/DisplayChart?

filename="+filename;

%>

"width=700height=400border=0usemap="#<%=filename%>">

附录:

1、 在使用前必须先在web.xml中配置一下servlet,配置很简单,可以根据自己具体情况配置,以下是我配置的:

DisplayChart

org.jfree.chart.servlet.DisplayChart

1

DisplayChart

/DisplayChart

2、附件中是关于饼图、柱形图、曲线图的使用写法例子,也是从网上搜集而来,感觉还是很不错的,也整理在这里了。

下载地址:

3、关于Jfreechart的简单介绍:

JFreeChart是一组功能强大、灵活易用的Java绘图API,使用它可以生成多种通用性的报表,包括柱状图、饼图、曲线图、甘特图等。

它能够用在Swing和Web等中制作自定义的图表或报表中,并且得到广泛的应用。

JFreeChart是开放源代码的免费软件,但是它的支持文档需要付费才能得到。

其下载地址为:

我使用的就是目前最新的版本1.0.13。

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 考试认证 > 交规考试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1