Jfreechart中文乱码

JFreeChart是用户使用该库提供的各类图标的统一接口,JFreeChart主要由三个部分构成:title(标题),legend(图释),plot(图表主体)。三个部分设置字体的方法分别如下:
 
1.Title

 TextTitle textTitle = freeChart.getTitle();   
textTitle.setFont(new Font("宋体",Font.BOLD,20));  


2.Legend
 
 LegendTitle legend = freeChart.getLegend();   
if (legend!=null) {   
   legend.setItemFont(new Font("宋体", Font.BOLD, 20));
}  


3.Plot
 
 
 
    对于不同类型的图表对应Plot的不同的实现类,设置字体的方法也不完全相同。
 
    对于使用CategoryPlot的图表(如柱状图):
 
 CategoryPlot plot = (CategoryPlot)freeChart.getPlot();   
CategoryAxis domainAxis = plot.getDomainAxis();//(柱状图的x轴)   
domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴坐标上的字体   
domainAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴上的标题的字体     
ValueAxis valueAxis = plot.getRangeAxis();//(柱状图的y轴)   
valueAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//设置y轴坐标上的字体   
valueAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//设置y轴坐标上的标题的字体  
CategoryPlot plot = (CategoryPlot)freeChart.getPlot();
 CategoryAxis domainAxis = plot.getDomainAxis();//(柱状图的x轴)
 domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴坐标上的字体
 domainAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//设置x轴上的标题的字体 
 ValueAxis valueAxis = plot.getRangeAxis();//(柱状图的y轴)
 valueAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));//设置y轴坐标上的字体
 valueAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//设置y轴坐标上的标题的字体
 

对于使用PiePlot的图标(如饼状图):
 

 PiePlot plot = (PiePlot)freeChart.getPlot();   
plot.setLabelFont(new Font("宋体",Font.BOLD,15));  



对于使用PiePlot的图标(如饼状图):
 

view plaincopy to clipboardprint?
 PiePlot plot = (PiePlot)freeChart.getPlot();   
plot.setLabelFont(new Font("宋体",Font.BOLD,15)); 


freechart中文乱码问题解决方案(转)
柱状图(CategoryPlot):
   CategoryPlot plot=chart.getCategoryPlot();//获取图表区域对象
   CategoryAxis domainAxis=plot.getDomainAxis();
    //水平底部列表
    domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14));
    //水平底部标题
    domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));
    //垂直标题
    ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状
    rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));
     chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));

饼图(PiePlot):
     JFreeChart chart = ChartFactory.createPieChart3D("IT行业职业分布图", dataset, true, false, false);
    chart.getTitle().setFont(new Font("黑体",Font.BOLD,20));//设置标题字体
    PiePlot piePlot= (PiePlot) chart.getPlot();//获取图表区域对象
    piePlot.setLabelFont(new Font("黑体",Font.BOLD,10));
    chart.getLegend().setItemFont(new Font("黑体",Font.BOLD,10));

时序图(TimeSeries)  
   XYPlot plot = (XYPlot) chart.getPlot();
    //纵轴字体
    plot.getRangeAxis().setLabelFont(new Font("宋体", Font.BOLD, 15));
    //横轴框里的标题字体
    chart.getLegend().setItemFont(new Font("宋体", Font.ITALIC, 15));
    //横轴列表字体
    plot.getDomainAxis().setTickLabelFont(new Font("新宋体", 1, 15));
    //横轴小标题字体
    plot.getDomainAxis().setLabelFont(new Font("新宋体", 1, 12));

折线图
chart.getTitle().setFont(new Font("宋体", Font.BOLD, 15));
   chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
   CategoryAxis domainAxis = plot.getDomainAxis();   
   /*------设置X轴坐标上的文字-----------*/ 
   domainAxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 11));   
   /*------设置X轴的标题文字------------*/ 
   domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));   
   NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();   
   /*------设置Y轴坐标上的文字-----------*/ 
   numberaxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 12));   
   /*------设置Y轴的标题文字------------*/ 
   numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
jfreechart 中文乱码的问题通常是由于字体设置不正确所导致的。可以尝试以下方法来解决这个问题: 1. 设置字体:在创建图表之前,使用以下代码设置字体: ``` Font font = new Font("SimSun", Font.PLAIN, 12); ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme() .deriveLargeFont(font) .deriveRegularFont(font) .deriveExtraLargeFont(font) .deriveSmallFont(font)); ``` 2. 设置编码:在创建图表之前,使用以下代码设置编码: ``` ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme()); ChartFactory.getChartTheme().setExtraLargeFont(new Font("宋体", Font.BOLD, 20)); ChartFactory.getChartTheme().setLargeFont(new Font("宋体", Font.BOLD, 14)); ChartFactory.getChartTheme().setRegularFont(new Font("宋体", Font.PLAIN, 12)); ChartFactory.getChartTheme().setSmallFont(new Font("宋体", Font.PLAIN, 10)); ChartFactory.getChartTheme().setAxisLabelPaint(new Color(51, 51, 51)); ChartFactory.getChartTheme().setChartBackgroundPaint(Color.white); ChartFactory.getChartTheme().setLegendBackgroundPaint(Color.white); ChartFactory.getChartTheme().setLegendItemPaint(new Color(51, 51, 51)); ChartFactory.getChartTheme().setPlotBackgroundPaint(Color.white); ChartFactory.getChartTheme().setPlotOutlinePaint(Color.white); ChartFactory.getChartTheme().setTickLabelPaint(new Color(67, 67, 72)); ChartFactory.getChartTheme().setAxisOffset(new RectangleInsets(0, 0, 0, 0)); ChartFactory.getChartTheme().setBarPainter(new StandardBarPainter()); ChartFactory.getChartTheme().setXYBarPainter(new StandardXYBarPainter()); ChartFactory.getChartTheme().setAxisLabelInsets(new RectangleInsets(0, 0, 0, 0)); ChartFactory.getChartTheme().setAxisTickStroke(new BasicStroke(1.0f)); ChartFactory.getChartTheme().setBarPainter(new StandardBarPainter()); ChartFactory.getChartTheme().setXYBarPainter(new StandardXYBarPainter()); ``` 以上两种方法都可以尝试一下,看看是否能解决中文乱码的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值