您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)JFreeChart中文亂碼的解決方法,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
由于JFreeChart組件的版本、操作平臺、JDK的設(shè)置等因素,在使用JFreeChart組件時(shí)可能會(huì)出現(xiàn)中文亂碼的現(xiàn)象。遇到此問題時(shí),可通過設(shè)置文字的字體來解決問題。在此提供以下兩種解決此問題的方法。
一、設(shè)置主題的樣式(強(qiáng)烈推薦)
在制圖前,創(chuàng)建主題樣式并制定樣式中的字體,通過ChartFactory的setChartTheme()方法設(shè)置主題樣式。
//創(chuàng)建主題樣式 StandardChartTheme standardChartTheme=new StandardChartTheme("CN"); //設(shè)置標(biāo)題字體 standardChartTheme.setExtraLargeFont(new Font("隸書",Font.BOLD,20)); //設(shè)置圖例的字體 standardChartTheme.setRegularFont(new Font("宋書",Font.PLAIN,15)); //設(shè)置軸向的字體 standardChartTheme.setLargeFont(new Font("宋書",Font.PLAIN,15)); //應(yīng)用主題樣式 ChartFactory.setChartTheme(standardChartTheme);
例如:
package com.zzs.jfreechart.demo; import java.awt.Font; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.JFreeChart; import org.jfree.chart.StandardChartTheme; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.title.LegendTitle; import org.jfree.chart.title.TextTitle; import org.jfree.data.category.DefaultCategoryDataset; public class JfreeChartTest { public static void main(String[] args) { // 創(chuàng)建類別圖(Category)數(shù)據(jù)對象 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(100, "北京", "蘋果"); dataset.addValue(100, "上海", "蘋果"); dataset.addValue(100, "廣州", "蘋果"); dataset.addValue(200, "北京", "梨子"); dataset.addValue(200, "上海", "梨子"); dataset.addValue(200, "廣州", "梨子"); dataset.addValue(300, "北京", "葡萄"); dataset.addValue(300, "上海", "葡萄"); dataset.addValue(300, "廣州", "葡萄"); dataset.addValue(400, "北京", "香蕉"); dataset.addValue(400, "上海", "香蕉"); dataset.addValue(400, "廣州", "香蕉"); dataset.addValue(500, "北京", "荔枝"); dataset.addValue(500, "上海", "荔枝"); dataset.addValue(500, "廣州", "荔枝"); //創(chuàng)建主題樣式 StandardChartTheme standardChartTheme=new StandardChartTheme("CN"); //設(shè)置標(biāo)題字體 standardChartTheme.setExtraLargeFont(new Font("隸書",Font.BOLD,20)); //設(shè)置圖例的字體 standardChartTheme.setRegularFont(new Font("宋書",Font.PLAIN,15)); //設(shè)置軸向的字體 standardChartTheme.setLargeFont(new Font("宋書",Font.PLAIN,15)); //應(yīng)用主題樣式 ChartFactory.setChartTheme(standardChartTheme); JFreeChart chart=ChartFactory.createBarChart3D("水果產(chǎn)量圖", "水果", "水果", dataset, PlotOrientation.VERTICAL, true, true, true); // TextTitle textTitle = chart.getTitle(); // textTitle.setFont(new Font("宋體", Font.BOLD, 20)); // LegendTitle legend = chart.getLegend(); // if (legend != null) { // legend.setItemFont(new Font("宋體", Font.BOLD, 20)); // } ChartFrame frame=new ChartFrame ("水果產(chǎn)量圖 ",chart,true); frame.pack(); frame.setVisible(true); } }
二、制定亂碼文字的字體
使用JFreeChart繪制圖表的時(shí)候,如果使用默認(rèn)的字體會(huì)導(dǎo)致圖標(biāo)中的漢字顯示為亂碼。解決方法如下:
JFreeChart是用戶使用該庫提供的各類圖標(biāo)的統(tǒng)一接口,JFreeChart主要由三個(gè)部分構(gòu)成:title(標(biāo)題),legend(圖釋),plot(圖表主體)。三個(gè)部分設(shè)置字體的方法分別如下:
1.Title
TextTitle textTitle = freeChart.getTitle(); textTitle.setFont(new Font("宋體",Font.BOLD,20));
2.Legent
LegendTitle legend = freeChart.getLegend(); if (legend!=null) { legend.setItemFont(new Font("宋體", Font.BOLD, 20)); }
3.Plot
對于不同類型的圖表對應(yīng)Plot的不同的實(shí)現(xiàn)類,設(shè)置字體的方法也不完全相同。
對于使用CategoryPlot的圖表(如柱狀圖):
CategoryPlot plot = (CategoryPlot)freeChart.getPlot(); CategoryAxis domainAxis = plot.getDomainAxis();//(柱狀圖的x軸) domainAxis.setTickLabelFont(new Font("宋體",Font.BOLD,20));//設(shè)置x軸坐標(biāo)上的字體 domainAxis.setLabelFont(new Font("宋體",Font.BOLD,20));//設(shè)置x軸上的標(biāo)題的字體 ValueAxis valueAxis = plot.getRangeAxis();//(柱狀圖的y軸) valueAxis.setTickLabelFont(new Font("宋體",Font.BOLD,20));//設(shè)置y軸坐標(biāo)上的字體 valueAxis.setLabelFont(new Font("宋體",Font.BOLD,20));//設(shè)置y軸坐標(biāo)上的標(biāo)題的字體 CategoryPlot plot = (CategoryPlot)freeChart.getPlot(); CategoryAxis domainAxis = plot.getDomainAxis();//(柱狀圖的x軸) domainAxis.setTickLabelFont(new Font("宋體",Font.BOLD,20));//設(shè)置x軸坐標(biāo)上的字體 domainAxis.setLabelFont(new Font("宋體",Font.BOLD,20));//設(shè)置x軸上的標(biāo)題的字體 ValueAxis valueAxis = plot.getRangeAxis();//(柱狀圖的y軸) valueAxis.setTickLabelFont(new Font("宋體",Font.BOLD,20));//設(shè)置y軸坐標(biāo)上的字體 valueAxis.setLabelFont(new Font("宋體",Font.BOLD,20));//設(shè)置y軸坐標(biāo)上的標(biāo)題的字體
對于使用PiePlot的圖標(biāo)(如餅狀圖):
PiePlot plot = (PiePlot)freeChart.getPlot(); plot.setLabelFont(new Font("宋體",Font.BOLD,15));
對于使用PiePlot的圖標(biāo)(如餅狀圖):
PiePlot plot = (PiePlot)freeChart.getPlot(); plot.setLabelFont(new Font("宋體",Font.BOLD,15));
下面一個(gè)實(shí)例:
package com.zzs.jfreechart.demo; import java.awt.Font; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PiePlot; import org.jfree.chart.title.LegendTitle; import org.jfree.chart.title.TextTitle; import org.jfree.data.general.DefaultPieDataset; import org.jfree.data.general.PieDataset; import org.jfree.ui.ApplicationFrame; public class JfreeChartOne extends ApplicationFrame { private static final long serialVersionUID = 1L; public JfreeChartOne(String s) { super(s); setContentPane(createJPanel()); } public static void main(String[] args) { JfreeChartOne one = new JfreeChartOne("CityInfoPort公司組織架構(gòu)圖"); one.pack(); one.setVisible(true); } // 利用靜態(tài)方法設(shè)定數(shù)據(jù)源(餅狀圖) public static PieDataset createPieDataset() { DefaultPieDataset defaultpiedataset = new DefaultPieDataset(); defaultpiedataset.setValue("管理人員", 10.02D); defaultpiedataset.setValue("市場人員", 20.23D); defaultpiedataset.setValue("開發(fā)人員", 60.02D); defaultpiedataset.setValue("OEM人員", 10.02D); defaultpiedataset.setValue("其他人員", 5.11D); return defaultpiedataset; } // 通過ChartFactory創(chuàng)建JFreeChart的實(shí)例 public static JFreeChart createJFreeChart(PieDataset p) { JFreeChart a = ChartFactory.createPieChart("CityInfoPort公司組織架構(gòu)圖", p, true, true, true); // JFreeChart主要由三個(gè)部分構(gòu)成:title(標(biāo)題),legend(圖釋),plot(圖表主體)。 //三個(gè)部分設(shè)置字體的方法分別如下: TextTitle textTitle = a.getTitle(); textTitle.setFont(new Font("宋體", Font.BOLD, 20)); LegendTitle legend = a.getLegend(); if (legend != null) { legend.setItemFont(new Font("宋體", Font.BOLD, 20)); } PiePlot pie = (PiePlot) a.getPlot(); pie.setLabelFont(new Font("宋體", Font.BOLD, 12)); pie.setNoDataMessage("No data available"); pie.setCircular(true); pie.setLabelGap(0.01D);// 間距 return a; } public static JPanel createJPanel() { JFreeChart jfreechart = createJFreeChart(createPieDataset()); return new ChartPanel(jfreechart); } }
下面這個(gè)修改坐標(biāo)軸:
package com.zzs.jfreechart.demo; import java.awt.Color; import java.awt.Font; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.title.LegendTitle; import org.jfree.chart.title.TextTitle; import org.jfree.data.time.Month; import org.jfree.data.time.TimeSeries; import org.jfree.data.time.TimeSeriesCollection; import org.jfree.ui.RectangleInsets; public class ShiJianXuLieTu01 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //時(shí)間序列圖 TimeSeries timeseries = new TimeSeries("L&G European Index Trust",Month.class); timeseries.add(new Month(2, 2001), 181.8D);//這里用的是Month.class,同樣還有Day.class Year.class 等等 timeseries.add(new Month(3, 2001), 167.3D); timeseries.add(new Month(4, 2001), 153.8D); timeseries.add(new Month(5, 2001), 167.6D); timeseries.add(new Month(6, 2001), 158.8D); timeseries.add(new Month(7, 2001), 148.3D); timeseries.add(new Month(8, 2001), 153.9D); timeseries.add(new Month(9, 2001), 142.7D); timeseries.add(new Month(10, 2001), 123.2D); timeseries.add(new Month(11, 2001), 131.8D); timeseries.add(new Month(12, 2001), 139.6D); timeseries.add(new Month(1, 2002), 142.9D); timeseries.add(new Month(2, 2002), 138.7D); timeseries.add(new Month(3, 2002), 137.3D); timeseries.add(new Month(4, 2002), 143.9D); timeseries.add(new Month(5, 2002), 139.8D); timeseries.add(new Month(6, 2002), 137D); timeseries.add(new Month(7, 2002), 132.8D); TimeSeries timeseries1 = new TimeSeries("L&G UK Index Trust曾召帥",Month.class); timeseries1.add(new Month(2, 2001), 129.6D); timeseries1.add(new Month(3, 2001), 123.2D); timeseries1.add(new Month(4, 2001), 117.2D); timeseries1.add(new Month(5, 2001), 124.1D); timeseries1.add(new Month(6, 2001), 122.6D); timeseries1.add(new Month(7, 2001), 119.2D); timeseries1.add(new Month(8, 2001), 116.5D); timeseries1.add(new Month(9, 2001), 112.7D); timeseries1.add(new Month(10, 2001), 101.5D); timeseries1.add(new Month(11, 2001), 106.1D); timeseries1.add(new Month(12, 2001), 110.3D); timeseries1.add(new Month(1, 2002), 111.7D); timeseries1.add(new Month(2, 2002), 111D); timeseries1.add(new Month(3, 2002), 109.6D); timeseries1.add(new Month(4, 2002), 113.2D); timeseries1.add(new Month(5, 2002), 111.6D); timeseries1.add(new Month(6, 2002), 108.8D); timeseries1.add(new Month(7, 2002), 101.6D); TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(); timeseriescollection.addSeries(timeseries); timeseriescollection.addSeries(timeseries1); timeseriescollection.setDomainIsPointsInTime(true); //domain軸上的刻度點(diǎn)代表的是時(shí)間點(diǎn)而不是時(shí)間段 JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("合法 & General Unit Trust Prices", "日期", "暗示的話發(fā)神經(jīng)提防", timeseriescollection, true, true, false); jfreechart.setBackgroundPaint(Color.white); TextTitle textTitle = jfreechart.getTitle(); textTitle.setFont(new Font("宋體", Font.BOLD, 20)); LegendTitle legend = jfreechart.getLegend(); if (legend != null) { legend.setItemFont(new Font("宋體", Font.BOLD, 20)); } XYPlot xyplot = (XYPlot)jfreechart.getPlot(); //獲得 plot : XYPlot!! ValueAxis domainAxis=xyplot.getDomainAxis(); domainAxis.setTickLabelFont(new Font("宋體",Font.BOLD,20));//設(shè)置x軸坐標(biāo)上的字體 domainAxis.setLabelFont(new Font("宋體",Font.BOLD,20));//設(shè)置x軸坐標(biāo)上的標(biāo)題的字體 ValueAxis rangeAxis=xyplot.getRangeAxis(); rangeAxis.setTickLabelFont(new Font("宋體",Font.BOLD,20));//設(shè)置y軸坐標(biāo)上的字體 rangeAxis.setLabelFont(new Font("宋體",Font.BOLD,20));//設(shè)置y軸坐標(biāo)上的標(biāo)題的字體 xyplot.setBackgroundPaint(Color.lightGray); xyplot.setDomainGridlinePaint(Color.white); xyplot.setRangeGridlinePaint(Color.white); xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D)); xyplot.setDomainCrosshairVisible(true); xyplot.setRangeCrosshairVisible(true); ChartFrame frame=new ChartFrame ("折線圖 ",jfreechart,true); frame.pack(); frame.setVisible(true); } }
看完上述內(nèi)容,你們對JFreeChart中文亂碼的解決方法有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。