Cut labels and subtitles

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Morwen
Posts: 26
Joined: Sat Apr 02, 2005 6:26 pm

Cut labels and subtitles

Post by Morwen » Thu Dec 14, 2006 8:13 am

Hello everyone,

A problem I encountered with charts, when there are alot of subtitles, the size of the graph's drawing area (Actual) is not calculated well enough, and subtitles get cut off.

An example:
==============================================
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class Main2 extends ApplicationFrame
{
public Main2(String title)
{
super(title);
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, "A", "B");

CategoryPlot plot = new CategoryPlot();
BarRenderer rend = new BarRenderer();
rend.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
rend.setItemLabelsVisible(Boolean.TRUE);

plot.setDataset(0, dataset);
plot.setRangeAxis(0, new NumberAxis());
plot.setDomainAxis(0, new CategoryAxis());
plot.setRenderer(0, rend);
plot.mapDatasetToDomainAxis(0, 0);
plot.mapDatasetToRangeAxis(0, 0);

JFreeChart chart = new JFreeChart("Hello Graph", plot);

chart.addSubtitle(new TextTitle("Im a subtitle"));
chart.addSubtitle(new TextTitle("Im a subtitle"));
chart.addSubtitle(new TextTitle("Im a subtitle"));
chart.addSubtitle(new TextTitle("Im a subtitle"));
chart.addSubtitle(new TextTitle("Im a subtitle"));
chart.addSubtitle(new TextTitle("Im a subtitle"));
chart.addSubtitle(new TextTitle("Im a subtitle"));
chart.addSubtitle(new TextTitle("Im a subtitle"));
chart.addSubtitle(new TextTitle("Im a subtitle"));
chart.addSubtitle(new TextTitle("Im a subtitle"));
chart.addSubtitle(new TextTitle("Im a subtitle"));
chart.addSubtitle(new TextTitle("Im a subtitle"));
chart.addSubtitle(new TextTitle("Im a subtitle"));
chart.addSubtitle(new TextTitle("Im a subtitle"));

ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(640, 480));
setContentPane(chartPanel);
}

public static void main(String[] args)
{
Main2 demo = new Main2("Demo");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
==============================================

Does anyone have any idea how to stop this from happening?

Thanks,

Morwen.

mausbull
Posts: 15
Joined: Wed Feb 22, 2006 3:36 pm
Contact:

Post by mausbull » Mon Jan 15, 2007 3:38 pm

good you mention this - I've the same problem.

Did you find a solution to this?

regards
Stephan

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

Post by david.gilbert » Mon Jan 15, 2007 3:55 pm

When I run the posted code, I get this:

Image

I don't see any problem with the subtitles. The item label above the bar is cut off, but this is a different problem (the current workaround for that is to increase the upper margin on the range axis to leave more room for the item labels).
David Gilbert
JFreeChart Project Leader

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

mausbull
Posts: 15
Joined: Wed Feb 22, 2006 3:36 pm
Contact:

Post by mausbull » Mon Jan 15, 2007 4:02 pm

My fault sorry - posted the answer to the wrong thread.

My problem is, that the legend area gets to big if I have a lot of series added to my XYSeries. I wanted to know if it's possible to limit the amount of series which are displayed in the legend or define the size of the legend, i.e. give it a fixed height.

Is there a solution to this problem?

regards Stephan

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

Post by david.gilbert » Mon Jan 15, 2007 4:12 pm

The legend just keeps growing to accommodate all the items. Workarounds include:

- hiding the legend;
- using the setSeriesVisibleInLegend() method in the renderer to hide some series from the legend;
- using the setFixedLegendItems() method to display a customised subset of the series;
- subclassing the plot and overriding the getLegendItems() method to return a subset of the series.
David Gilbert
JFreeChart Project Leader

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

Locked