Various factors on the jfreechart component operating platform may cause Chinese garbled characters. Next I will summarize the solutions for jfreechart Chinese garbled characters.
Due to the JFreeChart component version, operating platform, JDK settings, and other factors, Chinese characters may be garbled when using the JFreeChart component.
When this problem occurs, you can set the font of the text to solve the problem.
The following provides a solution that I personally think is better:
| The Code is as follows: |
Copy code |
|
// Create a topic Style
StandardChartTheme standardChartTheme = new StandardChartTheme ("CN ");
// Set the title Font
StandardChartTheme. setExtraLargeFont (new Font ("", Font. BOLD, 20 ));
// Set the font of the legend
StandardChartTheme. setRegularFont (new Font ("", Font. PLAIN, 15 ));
// Set the axial font
StandardChartTheme. setLargeFont (new Font ("", Font. PLAIN, 15 ));
// Apply the topic Style
ChartFactory. setChartTheme (standardChartTheme ); |
In this way, Chinese garbled characters in most charts can be solved.
Add a Chinese garbled Function
| The Code is as follows: |
Copy code |
|
CategoryDataset dataset = getDataSet2 ();
JFreeChart chart = ChartFactory. createBarChart3D ("", // chart title
"", // Display label of the Directory axis
"", // Display label of the numeric axis
Dataset, // dataset
PlotOrientation. VERTICAL, // chart direction: horizontal and VERTICAL
True, // whether to display the legend (for a simple bar chart, it must be false)
False, // whether to generate a tool
False // whether to generate a URL link
);
// Set the font
ConfigFont (chart );
// Font configuration method (solving Chinese problems)
Private static void configFont (JFreeChart chart ){
// Configure the font
Font xfont = new Font ("", Font. PLAIN, 12); // X axis
Font yfont = new Font ("", Font. PLAIN, 12); // y axis
Font kfont = new Font ("", Font. PLAIN, 12); // bottom
Font titleFont = new Font ("", Font. BOLD, 25); // picture title
CategoryPlot plot = chart. getCategoryPlot (); // Drawing Structure object of the image
// Image title
Chart. setTitle (new TextTitle (chart. getTitle (). getText (), titleFont ));
// Bottom
Chart. getLegend (). setItemFont (kfont );
// X axis
CategoryAxis domainAxis = plot. getDomainAxis ();
DomainAxis. setLabelFont (xfont); // axis title
DomainAxis. setTickLabelFont (xfont); // axis Value
DomainAxis. setTickLabelPaint (Color. BLUE); // font Color
DomainAxis. setCategoryLabelPositions (CategoryLabelPositions. UP_45); // oblique label display on the horizontal axis
// Y axis
ValueAxis rangeAxis = plot. getRangeAxis ();
RangeAxis. setLabelFont (yfont );
RangeAxis. setLabelPaint (Color. BLUE); // font Color
RangeAxis. setTickLabelFont (yfont );
} |