How to make the word wrap of the Legends in XYSeries

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 make the word wrap of the Legends in XYSeries

Post by Ravikumar » Tue Oct 20, 2009 4:28 pm

Hi,

I have created MultiLine chart and one of the seriesName is too large say lenegth will be 500 charcters or more. But the chart area is 350*350. So some of the tailing seriesName characters in the Legends part are not visible. How to make the word wrap for the legends. If seriesName reached the chart area width, it will write into next line, so that all the characters to be visible.

My Sample code base is as follows,

Code: Select all

  XYSeries series1 = new XYSeries(" Ravikumar R, Software Development Private Limited,  Sarathy Nagar, Anna Nagar, Chennai -4, India.  contact me @ ravikumar@gmail.com ");
	series1.add(1.0,2.0);
	series1.add(2.0,5.0);
	series1.add(3.0,7.0);
	series1.add(4.0,8.0);
	series1.add(5.0,9.0);
	series1.add(6.0,9.0);

	XYSeries series2 = new XYSeries("Ramesh R, Compute Network  Development Private Limited, Tharamani, Velachery, Chennai -42");
	series2.add(1.0,3.0);
	series2.add(2.0,7.0);
	series2.add(3.0,8.0);
	series2.add(4.0,8.0);
	series2.add(5.0,10.0);

  XYSeriesCollection dataset = new XYSeriesCollection();
	dataset.addSeries(series1);	 
       dataset.addSeries(series2);	 
	     

          JFreeChart chart = ChartFactory.createXYLineChart("Company Details","CompanyName","count",dataset,PlotOrientation.VERTICAL,true,true,false); 
Now the first series Name will be shwon as Ravikumar R, Software Development Private Limited, Sara only.

How to make the whole content to be shown in the legends without changing the chart area.

I am appreciate the early response.

Thanks & Regards
R.Ravikumar

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

Re: How to make the word wrap of the Legends in XYSeries

Post by Ravikumar » Wed Oct 21, 2009 3:45 pm

Hi,

Can you please let me know how to show the full seriesName using word wrap in the legends.

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 make the word wrap of the Legends in XYSeries

Post by david.gilbert » Thu Oct 22, 2009 9:31 pm

I don't know an easy solution for this.
David Gilbert
JFreeChart Project Leader

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

bjorkli
Posts: 1
Joined: Tue Jun 22, 2010 11:33 am
antibot: No, of course not.

Re: How to make the word wrap of the Legends in XYSeries

Post by bjorkli » Tue Jun 22, 2010 11:38 am

How to make legends line wrap
Made this method to do it.
Kinda a hack but I think it does the job

Code: Select all

private void wrapLongLegends(CategoryPlot plot) {
		LegendItemCollection chartLegend = plot.getLegendItems();
		LegendItemCollection res = new LegendItemCollection();
		for (int i = 0; i < chartLegend.getItemCount(); i++) {
			LegendItem item = chartLegend.get(i);
			String label = item.getLabel();
			String[] parts = StringUtil.wrapText(label, 30);
			int p = 0;
			for (String part : parts) {
				if (p == 0) {
					res.add(new LegendItem(part, item.getDescription(), item.getToolTipText(), item.getURLText(), item.isShapeVisible(), item.getShape(), item.isShapeFilled(), item.getFillPaint(), item.isShapeOutlineVisible(), item.getOutlinePaint(), item.getOutlineStroke(), item.isLineVisible(), item.getLine(), item.getLineStroke(), item.getLinePaint()));
				} else {
					res.add(new LegendItem(part, item.getDescription(), item.getToolTipText(), item.getURLText(), false, item.getShape(), item.isShapeFilled(), item.getFillPaint(), false, item.getOutlinePaint(), item.getOutlineStroke(), false, item.getLine(), item.getLineStroke(), item.getLinePaint()));
				}
				p++;
			}
		}
		plot.setFixedLegendItems(res);
	}
The StringUtil.wrapText method I grabbed from this source.
I guess you can use WordUtils from apache also.
http://progcookbook.blogspot.com/2006/0 ... -java.html

Have fun 8)

Locked