The entire chart is divided into three parts: chart title, Chart plot, and chart legend. You need to set the font for them separately.
Let's take a look at the solution (after setting all these settings, we can fix them ):
Garbled title
Chart. gettitle (). setfont (new font ("", Font. Bold, 12 ));
Others
- Categoryaxis domainaxis = plot. getdomainaxis ();
- // Numberaxis valueaxis = (numberaxis) plot. getrangeaxis ();
- // Some people say this is the method for setting the horizontal direction.
- Valueaxis
Numberaxis = plot. getrangeaxis ();
- /* ------ Set the text -----------*/
- Domainaxis. setticklabelfont (new font ("Sans-serif", Font. Plain, 11 ));
- /* ------ Set the title Text of the X axis ------------*/
- Domainaxis. setlabelfont (new font ("", Font. Plain, 12 ));
- /* ------ Set the text -----------*/
- Numberaxis. setticklabelfont (new font ("Sans-serif", Font. Plain, 12 ));
- /* ------ Set the title Text of the Y axis ------------*/
- Numberaxis. setlabelfont (new font ("", Font. Plain, 12 ));
- /* ------ This code solves the problem of garbled Chinese characters at the bottom -----------*/
- Jfreechart. getlegend (). setitemfont (new font ("", Font. Plain, 12 ));
For the graph;
The following two methods are used to set the horizontal and vertical settings.
Jfreechart chart = chartfactory. createtimeserieschart ("", "time", "price", linedataset, true );
Xyplot plot = (xyplot) Chart. getplot ();
Vertical:
Valueaxis = plot. getdomainaxis ();
Valueaxis. setlabelfont (new font ("", Font. Bold, 12 ));
Valueaxis. setticklabelfont (new font ("", Font. Bold, 12 ));
Horizontal:
Numberaxis valueaxis = (numberaxis) plot. getrangeaxis ();
Valueaxis. setlabelfont (new font ("", Font. Bold, 12 ));
Jfreechart = chartfactory. createlinechart ("'soybean 'do not calculate line chart by hour", "time", "price", categorydataset, plotorientation. Vertical, true, false, false );
Categoryplot plot = (categoryplot) jfreechart. getplot ();
Categoryaxis domainaxis = plot. getdomainaxis ();
Horizontal:
Domainaxis. setlabelfont (new font ("", Font. Bold, 20 ));
Domainaxis. setticklabelfont (new font ("", Font. Plain, 12 ));
Vertical:
Numberaxis valueaxis = (numberaxis) plot. getrangeaxis ();
Valueaxis. setlabelfont (new font ("", Font. Bold, 20 ));
The above is for the bar chart, and the following is for setting the pie chart.
Title: Chart. settitle (New texttitle ("My title", new font ("", Font. Bold, 20 )));
Legend: legendtitle = chart. getlegend (0 );
Legendtitle. setitemfont (new font ("My title", Font. italic, 20 ));
Text on the pie:
Pieplot plot = (pieplot) Chart. getplot ();
Plot. setlabelfont (new font ("", Font. Bold, 20 ));
Cause:
Jfreechart is mainly used to dynamically generate various data graphs. When it is initially used, it may encounter Chinese garbled characters or small blocks in the images.
There are two main reasons for careful research:
1: The server lacks a Chinese font, which occurs mostly on UNIX operating systems such as HP. The solution is to download the available fonts to the system,
Some people have also proposed how to generate images on Windows and transmit them back to the unix host.
2: Software Version problems, jfreechart-1.0.10 someone said no problem, but jfreechart-1.0.11 to 13 have problems, I use the latest jfreechart-1.0.13 without setting is problematic.
The reason is that the font set inside the code is incorrect.
Let's take a look at its code:
Jfreechart chart = chartfactory. createbarchart (
"Data Statistics ",
"Device number ",
"Accumulation value ",
Dataset,
Plotorientation. Vertical,
True, true, false
);
Its Prototype
Public static jfreechart createbarchart (String title,
String categoryaxislabel,
String valueaxislabel,
Categorydataset dataset,
Plotorientation orientation,
Boolean legend,
Boolean tooltips,
Boolean URLs ){
The above prototype is called again
Jfreechart chart = new jfreechart (title, jfreechart. default_title_font,
Plot, legend );
Currenttheme. Apply (Chart );
Let's look at the definition of the default font:
Public static final font default_title_font
= New font ("sansserif", Font. Bold, 18 );
See what the current topic currenttheme is.
Private Static charttheme currenttheme = new standardcharttheme ("jfree ");
View its prototype Definition
Public standardcharttheme (string name ){
If (name = NULL ){
Throw new illegalargumentexception ("null 'name' argument .");
}
This. Name = Name;
This. extralargefont = new font ("tahoma", Font. Bold, 20 );
This. largefont = new font ("tahoma", Font. Bold, 14 );
This. regularfont = new font ("tahoma", Font. Plain, 12 );
This. smallfont = new font ("tahoma", Font. Plain, 10 );
......
As you can see, the default title Font is sansserif, which is not available in many Chinese systems. This may be the disadvantage of using foreigners to develop open-source products.
First, the title is garbled:
Public jfreechart (String title, font titlefont, plot,
Boolean createlegend ){
......
Code for title setting:
If (title! = NULL ){
If (titlefont = NULL ){
Titlefont = default_title_font;
}
This. Title = new texttitle (title, titlefont );
This. Title. addchangelistener (this );
}
It uses the default font. To solve this problem, you only need to reset the font of the title.
......
Texttitle = chart. gettitle ();
Texttitle. setfont (new font ("", Font. Plain, 20 ));
The legend is processed like other garbled characters and the font is changed.
Categoryplot plot = chart. getcategoryplot (); // obtain the chart area object
Categoryaxis domainaxis = plot. getdomainaxis ();
Domainaxis. setvisible (true );
Plot. setdomainaxis (domainaxis );
Valueaxis raxis = plot. getrangeaxis ();
/* ------ Set the text -----------*/
Domainaxis. setticklabelfont (new font ("", Font. Plain, 15 ));
/* ------ Set the title Text of the X axis ------------*/
Domainaxis. setlabelfont (new font ("", Font. Plain, 15 ));
/* ------ Set the text -----------*/
Raxis. setticklabelfont (new font ("", Font. Plain, 15 ));
/* ------ Set the title Text of the Y axis ------------*/
Raxis. setlabelfont (new font ("", Font. Plain, 15 ));
Note that if garbled characters occur, you can change the font and convert the font to the system.
In addition, someone suggested replacing the sansserif font in the jfreechart source file with the Chinese font and re-compiling it. I didn't try it once and for all, I mainly used to reset the font.
**************************************** *******************************
Jfreechart Chinese garbled problem solution (to) bar chart (categoryplot): categoryplot plot = chart. getcategoryplot (); // obtain the chart region object categoryaxis domainaxis = plot. getdomainaxis (); // list of horizontal bottom domainaxis. setlabelfont (new font ("", Font. bold, 14); // horizontal bottom title domainaxis. setticklabelfont (new font ("", Font. bold, 12); // vertical title valueaxis rangeaxis = plot. getrangeaxis (); // obtain the columnar rangeaxis. setlabelfont (new font ("", Font. bold, 15); chart. getlegend (). setitemfont (new font ("", Font. bold, 15); pie chart (pieplot): jfreechart chart = chartfactory. createpiechart3d ("IT industry career distribution chart", dataset, true, false, false); chart. gettitle (). setfont (new font ("", Font. bold, 20); // set the title Font pieplot = (pieplot) chart. getplot (); // obtain the chart area object pieplot. setlabelfont (new font ("", Font. bold, 10); chart. getlegend (). setitemfont (new font ("", Font. bold, 10); Time Sequence Chart (Timeseries) xyplot plot = (xyplot) chart. getplot (); // The vertical font plot. getrangeaxis (). setlabelfont (new font ("", Font. bold, 15); // The title Font chart in the horizontal axis. getlegend (). setitemfont (new font ("", Font. italic, 15); // horizontal axis list font plot. getdomainaxis (). setticklabelfont (new font ("" ", 1, 15); // horizontal axis title Font plot. getdomainaxis (). setlabelfont (new font ("" ", 1, 12); line chart. gettitle (). setfont (new font ("", Font. bold, 15); chart. getlegend (). setitemfont (new font ("", Font. bold, 15); categoryaxis domainaxis = plot. getdomainaxis ();/* ------ set the text on the X axis coordinate --------- */domainaxis. setticklabelfont (new font ("", Font. plain, 11);/* ------ set the title Text of the X axis ------------ */domainaxis. setlabelfont (new font ("", Font. plain, 12); numberaxis = (numberaxis) plot. getrangeaxis ();/* ------ set the text ----------- */numberaxis on the Y axis coordinate. setticklabelfont (new font ("", Font. plain, 12);/* ------ set the title Text of the Y axis ------------ */numberaxis. setlabelfont (new font ("", Font. plain, 12 ))