Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The solution of Chinese garbled Code in JFreeChart

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about the solution of JFreeChart Chinese garbled code, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Due to the version of JFreeChart components, operating platform, JDK settings and other factors, Chinese garbled code may occur when using JFreeChart components. When you encounter this problem, you can solve the problem by setting the font of the text. Here are two ways to solve this problem.

Set the style of the theme (highly recommended)

Before drawing, create the theme style and define the font in the style, and set the theme style through the setChartTheme () method of ChartFactory.

/ / create theme style StandardChartTheme standardChartTheme=new StandardChartTheme ("CN"); / / set title font standardChartTheme.setExtraLargeFont (new Font ("official script", Font.BOLD,20)); / / set legend font standardChartTheme.setRegularFont (new Font ("Song Shu", Font.PLAIN,15)); / / set axial font standardChartTheme.setLargeFont ("Song Shu", Font.PLAIN,15)) / / apply theme style ChartFactory.setChartTheme (standardChartTheme)

For example:

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) {/ / create category diagram (Category) data objects DefaultCategoryDataset dataset = new DefaultCategoryDataset (); dataset.addValue (100, "Beijing", "Apple"); dataset.addValue (100, "Shanghai", "Apple"); dataset.addValue (100, "Guangzhou", "Apple") Dataset.addValue (200, "Beijing", "Pear"); dataset.addValue (200, "Shanghai", "Pear"); dataset.addValue (200, "Guangzhou", "Pear"); dataset.addValue (300, "Beijing", "Grape"); dataset.addValue (300, "Shanghai", "Grape") Dataset.addValue (300, "Guangzhou", "Grape"); dataset.addValue (400, "Beijing", "Banana"); dataset.addValue (400, "Shanghai", "Banana"); dataset.addValue (400, "Guangzhou", "Banana"); dataset.addValue (500, "Beijing", "Litchi") Dataset.addValue (500, "Shanghai", "litchi"); dataset.addValue (500, "Guangzhou", "litchi"); / / create a theme style StandardChartTheme standardChartTheme=new StandardChartTheme ("CN"); / / set the title font standardChartTheme.setExtraLargeFont ("official script", Font.BOLD,20)) / / set the font of the legend standardChartTheme.setRegularFont (new Font (Song Book, Font.PLAIN,15)); / / set the axial font standardChartTheme.setLargeFont (new Font (Song Book, Font.PLAIN,15)); / / apply the theme style ChartFactory.setChartTheme (standardChartTheme) JFreeChart chart=ChartFactory.createBarChart3D ("Fruit yield Map", "Fruit", "Fruit", dataset, PlotOrientation.VERTICAL, true, true, true); / / TextTitle textTitle = chart.getTitle (); / / textTitle.setFont (new Font ("Song style", Font.BOLD, 20); / / LegendTitle legend = chart.getLegend () / / if (legend! = null) {/ / legend.setItemFont (new Font (Song style, Font.BOLD, 20)); / /} ChartFrame frame=new ChartFrame (Fruit yield Map, chart,true); frame.pack (); frame.setVisible (true);}}

Second, formulate the font of garbled characters

When drawing a chart with JFreeChart, using the default font will cause the Chinese characters in the icon to appear garbled. The solution is as follows:

JFreeChart is a unified interface for users to use all kinds of icons provided by the library. JFreeChart is mainly composed of three parts: title (title), legend (illustration), and plot (chart body). The methods for setting fonts in the three parts are as follows:

1.Title

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

2.Legent

LegendTitle legend = freeChart.getLegend (); if (legendary null) {legend.setItemFont (new Font ("Verdana", Font.BOLD, 20);}

3.Plot

For different types of charts corresponding to different implementation classes of Plot, the method of setting fonts is not exactly the same.

For charts that use CategoryPlot (such as bar charts):

CategoryPlot plot = (CategoryPlot) freeChart.getPlot (); CategoryAxis domainAxis = plot.getDomainAxis (); / / (x-axis of histogram) domainAxis.setTickLabelFont (new Font ("Arial", Font.BOLD,20)); / / set font domainAxis.setLabelFont (new Font ("Arial", Font.BOLD,20)) on x-axis coordinates; / / set font ValueAxis valueAxis = plot.getRangeAxis () on x-axis / (y axis of histogram) valueAxis.setTickLabelFont (new Font ("Arial", Font.BOLD,20)); / / set font valueAxis.setLabelFont (new Font ("Arial", Font.BOLD,20)) on y axis coordinates; / / set font CategoryPlot plot = (CategoryPlot) freeChart.getPlot (); CategoryAxis domainAxis = plot.getDomainAxis () / / (the x-axis of the bar chart) domainAxis.setTickLabelFont (new Font ("Arial", Font.BOLD,20)); / / sets the font domainAxis.setLabelFont (new Font ("Arial", Font.BOLD,20)) on the x-axis coordinates; / / sets the font ValueAxis valueAxis = plot.getRangeAxis () of the title on the x-axis; / / (the y-axis of the bar chart) valueAxis.setTickLabelFont (new Font ("Arial", Font.BOLD,20)) / / sets the font valueAxis.setLabelFont (new Font ("Arial", Font.BOLD,20)) on the y-axis coordinates; / / sets the font of the title on the y-axis coordinates

For icons that use PiePlot (such as pie charts):

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

For icons that use PiePlot (such as pie charts):

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

The following is an example:

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 Company Organization Chart"); one.pack (); one.setVisible (true) } / / use static method to set data source (pie chart) public static PieDataset createPieDataset () {DefaultPieDataset defaultpiedataset = new DefaultPieDataset (); defaultpiedataset.setValue ("Manager", 10.02D); defaultpiedataset.setValue ("marketer", 20.23D); defaultpiedataset.setValue ("developer", 60.02D) Defaultpiedataset.setValue ("OEM personnel", 10.02D); defaultpiedataset.setValue ("others", 5.11D); return defaultpiedataset;} / / create an instance of JFreeChart through ChartFactory public static JFreeChart createJFreeChart (PieDataset p) {JFreeChart a = ChartFactory.createPieChart ("CityInfoPort Company Organization Chart", p, true, true, true) / / JFreeChart is mainly composed of three parts: title (title), legend (illustration), and plot (chart body). / / the methods for setting fonts in the three parts are as follows: TextTitle textTitle = a.getTitle (); textTitle.setFont (new Font ("Arial", Font.BOLD, 20)); LegendTitle legend = a.getLegend (); if (legend! = null) {legend.setItemFont ("Arial", Font.BOLD, 20)) } PiePlot pie = (PiePlot) a.getPlot (); pie.setLabelFont (new Font ("Song style", Font.BOLD, 12)); pie.setNoDataMessage ("No data available"); pie.setCircular (true); pie.setLabelGap (0.01D); / / spacing return a } public static JPanel createJPanel () {JFreeChart jfreechart = createJFreeChart (createPieDataset ()); return new ChartPanel (jfreechart);}}

The following changes the axes:

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 / / time series diagram TimeSeries timeseries = new TimeSeries ("Lingg European Index Trust", Month.class) Timeseries.add (new Month (2, 2001), 181.8D); / / Month.class is used here, as well as Day.class Year.class and other 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 (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.6 D); timeseries1.add (new Month (6, 2002), 108.8D); timeseries1.add (new Month (7, 2002), 101.6 D); TimeSeriesCollection timeseriescollection = new TimeSeriesCollection (); timeseriescollection.addSeries (timeseries); timeseriescollection.addSeries (timeseries1); timeseriescollection.setDomainIsPointsInTime (true) / / the tick point on the domain axis represents the time point rather than the time period JFreeChart jfreechart = ChartFactory.createTimeSeriesChart ("legal & General Unit Trust Prices", "date", "nervous caution if hinted", timeseriescollection, true, true False) Jfreechart.setBackgroundPaint (Color.white); TextTitle textTitle = jfreechart.getTitle (); textTitle.setFont (new Font (Song style), Font.BOLD, 20); LegendTitle legend = jfreechart.getLegend () If (legend! = null) {legend.setItemFont (new Font ("Song style", Font.BOLD, 20));} XYPlot xyplot = (XYPlot) jfreechart.getPlot (); / / get plot: XYPlottery! ValueAxis domainAxis=xyplot.getDomainAxis (); domainAxis.setTickLabelFont (new Font (Font.BOLD,20)); / / set the font domainAxis.setLabelFont (new Font ("Arial", Font.BOLD,20)) on the x-axis coordinates; / / set the font ValueAxis rangeAxis=xyplot.getRangeAxis () of the title on the x-axis coordinates. RangeAxis.setTickLabelFont (new Font ("Arial", Font.BOLD,20)); / / set the font rangeAxis.setLabelFont (new Font ("Arial", Font.BOLD,20)) on the y-axis coordinates; / / set the font xyplot.setBackgroundPaint (Color.lightGray) of the title on the y-axis coordinates; 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 ("line chart", jfreechart,true); frame.pack () Frame.setVisible (true);}} after reading the above content, do you have any further understanding of the solution to Chinese garbled in JFreeChart? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report