How to localize the legends values(chinese ) in JFreeChart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Ravikumar
Posts: 4
Joined: Wed May 06, 2009 1:36 pm

How to localize the legends values(chinese ) in JFreeChart

Post by Ravikumar » Wed May 06, 2009 5:18 pm

Hi,

My requirement is I have x and y axis units i.e seriesNames are in chinese.. I have passed the unicode characters of the units ( series name ) to XYSeries class. The value is properly shown in JSP page.. But in the JFreeChart's lengend are shown as boxes.. i.e the values are not shown.. probably the font or encoding may not be set probably.. Please help meto solve this issue..

Please let me know as how to localize the legends text in chinese characters..


My example code is attached here..

Code: Select all

 XYSeriesCollection dataset = new XYSeriesCollection();
  XYSeries xy= new XYSeries(series[j]);
 dataset.addSeries(xy);    

           XYDataset data1 = new XYSeriesCollection(dataset.getSeries(0));
	    XYDataset data2 = new XYSeriesCollection(dataset.getSeries(1));
	    XYItemRenderer renderer1 = new AreaXYRenderer();
	    renderer1.setSeriesPaint(0, (Color)graphHash.get("PrimaryColor"));
	    renderer1.setToolTipGenerator(ttg);
	    ValueAxis rangeAxis = new ExtendedNumberAxis(yaxis);
	    rangeAxis.setStandardTickUnits(ExtendedNumberAxis.createIntegerTickUnits());
	    for(int i=0; i < maxStr.length; i++)
	    {
		if(Double.parseDouble(maxStr[i])>0.0d)
		{
		    rangeAxis.setStandardTickUnits(ExtendedNumberAxis.createStandardTickUnits());
		    break;
		}
	    }

	    XYPlot plot;
	    {
		DateAxis domainAxis = new DateAxis(null);
		domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
		domainAxis.setVerticalTickLabels(false);
		plot = new XYPlot(data1, domainAxis, rangeAxis, renderer1);
	    }

	    StandardXYItemRenderer renderer2 = new StandardXYItemRenderer(StandardXYItemRenderer.LINES);
	    renderer2.setToolTipGenerator(ttg);
	    renderer2.setSeriesPaint(0, (Color)graphHash.get("SecondaryColor"));
	    plot.setSecondaryDataset(0, data2);
	    plot.setSecondaryRenderer(0, renderer2);
	
	    //plot.setBackgroundAlpha((float)1.0);
	    plot.setInsets(new java.awt.Insets(10, 10, 10, 10));
	    plot.setWeight(20);
	    plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
	    plot.setDomainGridlinesVisible(true);
	    plot.setForegroundAlpha(.55f);
	    plot.setDomainGridlinePaint(Color.black);
	    plot.setRangeGridlinesVisible(true);
	    plot.setRangeGridlinePaint(Color.black);
	  
	    ChartRenderingInfo info =  new ChartRenderingInfo(new StandardEntityCollection());
	    JFreeChart chart = new JFreeChart(title,new java.awt.Font("Helvetica",java.awt.Font.BOLD,11), plot, legend);
Any help will be appreciated ...

Thanks
R.Ravikumar

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: How to localize the legends values(chinese ) in JFreeChart

Post by david.gilbert » Thu May 07, 2009 7:33 am

I'm pretty sure the problem you have is that the font you (or JFreeChart) is using doesn't have full support for the Chinese characters in your text strings. You'll have to find a font that does support them.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Ravikumar
Posts: 4
Joined: Wed May 06, 2009 1:36 pm

Re: How to localize the legends values(chinese ) in JFreeChart

Post by Ravikumar » Thu May 07, 2009 11:02 am

Hi,

You told that the font

Code: Select all

  JFreeChart chart = new JFreeChart(title,new java.awt.Font("Helvetica",java.awt.Font.BOLD,11), plot, legend);
  Font  -->  new java.awt.Font("Helvetica",java.awt.Font.BOLD,11) won't support chinese right?.. 
[/color]
The value which I have passed to the below method is unicode

Code: Select all

String series = "\u8D85\u65F6";
   XYSeries xy= new XYSeries(series);
  dataset.addSeries(xy);   

Why I am asking is that the same text is shown properly in JSP page only problem in JFreeChart..

Can I use setItemFont() of StandardLegend class ? . Also I tried with Font as "Arial Unicode MS" but still i am facing the problemm.

Please let me any docs or examples which works perfectly in chinese..

Thanks
R.Ravikumar

zmicer
Posts: 1
Joined: Mon Aug 24, 2009 11:00 am
antibot: No, of course not.

Re: How to localize the legends values(chinese ) in JFreeChart

Post by zmicer » Mon Aug 24, 2009 12:12 pm

Hi, Ravikumar.

Not think you are still working under this issue, but may be this could be usefull to someone alse. Author of library is right - the fonts are mentioned do not support traditional chinise symbols - and this is only reason why you see not correct characters, but those blocks.

Here http://www.sino.uni-heidelberg.de/edv/s ... _fonts.htm you could download TTF font files for traditional chinese. Be noticed family names of these fonts are in chinese, so from java you could do the following:

1) in debug mode perform java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames() and save this list
2) copy a TTF font file into %jre%/lib/fonts/ and perform the same code, check a difference
3) write difference into UTF-8 text file
4) to get UTF-8 representation - we need perform the following native2ascii -encoding UTF-8 %file1% > %file2%, and %file2% would contain a file name in UTF to be used when calling new Font() constructor. E.g.
private static final Font tcFont = new Font("\u4861\u6e44\u696e\u672d\u4353\u2d46\u6f6e\u7473", 0,
chartLabelFontSize);

One note to author of framework. I did not find a way to provide a font for the legend title. There is a constructor of JFreeChart class taking a last parameter boolean if to create legend, and there is a way to add TextTitle to JFreeChart, but there is not way to set LegendTitle. Still LegendTitle has method setItemFont, but again, it is not possible to add it to JFreeChart.

kind regards,
Zmicer Harachka
http://www.zmicer.com

gshen
Posts: 1
Joined: Fri Feb 05, 2010 7:01 pm
antibot: No, of course not.

Re: How to localize the legends values(chinese ) in JFreeChart

Post by gshen » Fri Feb 05, 2010 7:08 pm

Hi Zmicer,
Actually you can set the legend title's font by (Say the variable for the JFreeChart instance is chart):
chart.getLegend.setItemFont(new java.awt.Font("Sans-serif", java.awt.Font.Plain, 12));

Once you get the reference of the LegentTitle, you can set it to any font. Apparently, JFreeChart's default is "Tahoma" and it doesn't support Chinese characters. Change it to any logical fonts, e.g., Serif, Sans-serif, Monospaced, Dialog, and DialogInput, and the legend title will figure out a way to display the Chinese characters at ease. I haven't tried other physical fonts which might also display correctly, but it is quite easy to figure out.

Hope this helps. :)

G

vernonwayne
Posts: 1
Joined: Thu Apr 22, 2010 9:12 am
antibot: No, of course not.

Re: How to localize the legends values(chinese ) in JFreeChart

Post by vernonwayne » Thu Apr 22, 2010 9:20 am

Hi gshen,

It helps after setting the font to sans-serif. Chinese characters are displayed correctly in my chart now. Thank you!

Wayne

yumex85
Posts: 4
Joined: Tue Aug 25, 2009 5:12 pm
antibot: No, of course not.

Re: How to localize the legends values(chinese ) in JFreeChart

Post by yumex85 » Tue Sep 28, 2010 1:35 pm

It worked for me too, I was trying to set the chart title with japanese characters (many japanese chars r like chinese). Thank you very much.

chart.getTitle().setFont(new java.awt.Font("Sans-serif", java.awt.Font.PLAIN, 12));

Locked