1、 图表标题以及副标题乱码 2、 X轴乱码 3、 Y轴乱码 4、 图表底部乱码 如下图所示:
对这四个位置的乱码分别进行解决:
NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
CategoryAxis domainAxis = plot.getDomainAxis(); 1、 图表标题以及副标题乱码
Font font = new Font(\宋体\
TextTitle title = new TextTitle(\肉类销量统计图\//副标题
TextTitle subtitle = new TextTitle(“副标题”, new Font(“黑体”, Font.BOLD, 12));
chart.addSubtitle(subtitle); chart.setTitle(title); //标题 2、 X轴乱码
2.1、X轴坐标上的文字:
domainAxis.setTickLabelFont(new Font(“sans-serif”, Font.PLAIN, 11)); 2.2、X轴坐标标题(肉类)
domainAxis.setLabelFont(new Font(“宋体”, Font.PLAIN, 12)); 3、 Y轴乱码
3.1、Y轴坐标上的文字:
numberaxis.setTickLabelFont(new Font(“sans-serif”, Font.PLAIN, 12)); 3.2、Y轴坐标标题(销量):
numberaxis.setLabelFont(new Font(“黑体”, Font.PLAIN, 12)); 4、 图表底部乱码(猪肉等文字)
chart.getLegend().setItemFont(new Font(“宋体”, Font.PLAIN, 12)); 通过以上设置就解决了中文乱码了,解决中文乱码后的图表如下所示:
对应的完整代码为(jsp):
<%@ page contentType=\%>
<%@ page import=\
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 = new double[][] {{1310, 1220, 1110, 1000},{720, 700, 680, 640},{1130, 1020, 980, 800},{440, 400, 360, 300}};
String[] rowKeys = {\猪肉\鸡肉\鱼肉\
String[] columnKeys = {\广州\东莞\佛山\
CategoryDataset dataset =
DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
JFreeChart chart = ChartFactory.createBarChart3D(\肉类\销量\true,true,false);
CategoryPlot plot = chart.getCategoryPlot();
//设置字体,不然会中文乱码的
Font font = new Font(\宋体\
TextTitle title = new TextTitle(\肉类销量统计图\
//副标题
TextTitle subtitle = new TextTitle(\副标题\new Font(\黑体\Font.BOLD, 12));
chart.addSubtitle(subtitle);
chart.setTitle(title); //标题
//////////////////////////
NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
CategoryAxis domainAxis = plot.getDomainAxis();
/*------设置X轴坐标上的文字-----------*/
domainAxis.setTickLabelFont(new Font(\
/*------设置X轴的标题文字------------*/
domainAxis.setLabelFont(new Font(\宋体\
/*------设置Y轴坐标上的文字-----------*/
numberaxis.setTickLabelFont(new Font(\
/*------设置Y轴的标题文字------------*/
numberaxis.setLabelFont(new Font(\黑体\
/*------这句代码解决了底部汉字乱码的问题-----------*/
chart.getLegend().setItemFont(new Font(\宋体\
//设置网格背景颜色
plot.setBackgroundPaint(Color.white);
//设置网格竖线颜色
plot.setDomainGridlinePaint(Color.pink);
//设置网格横线颜色
plot.setRangeGridlinePaint(Color.pink);
//显示每个柱的数值,并修改该数值的字体属性
BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
//默认的数字显示在柱子中,通过如下两句可调整数字的显示
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库JFreeChart使用教程(含下载、安装)(6)在线全文阅读。
相关推荐: