Item Labels get cut

A free public discussion forum for the JFreeChart class library.

Item Labels get cut

Postby mhilpert » Tue Jan 22, 2008 10:53 am

I'm playing around with a simple scatter chart showing item labels:

Code: Select all
    /**
     * Scatter chart wanted.
     *
     * @return JFreeChart.
     */
    private JFreeChart testScatterChart2() {
        JFreeChart result = null;
       
        final XYSeriesCollection c = new XYSeriesCollection();
       
        final XYSeries s1 = new XYSeries("1");
        s1.add(1.0, 2.6);
        c.addSeries(s1);
       
        final XYSeries s2 = new XYSeries("2");
        s2.add(2.0, -3.7);
        c.addSeries(s2);
       
        final XYSeries s3 = new XYSeries("3");
        s3.add(3.0, 2.5);
        c.addSeries(s3);
       
        final XYSeries s4 = new XYSeries("Gesamt");
        s4.add(4.0, 1.5);
        c.addSeries(s4);
       
        result = ChartFactory.createScatterPlot("Example", "X", "Y", c, PlotOrientation.VERTICAL, true, true, false);
       
        //show item labels:
        final XYPlot plot = result.getXYPlot();
        final XYItemRenderer renderer = plot.getRenderer();
        final StandardXYItemLabelGenerator generator = new StandardXYItemLabelGenerator("{1} %", NumberFormat.getInstance(), NumberFormat.getInstance());
        renderer.setSeriesItemLabelGenerator(0, generator); //0 = 1st series
        renderer.setSeriesItemLabelsVisible(0, true); //0 = first series
       
       
        return result;
    }//testScatterChart2()


Dependend on the ChartPanel's size, the resize algorithm doesn't take the item labels into account. When I make the panel smaller, the labels just move out of the visible plot and get cut at the top plot border. :(

Image

Same with right plot border, I I set the ItemLabelPosition to TextAnchor.CENTER_LEFT.
using JFreeChart 1.0.15 with JDK 1.7.0_25
mhilpert
 
Posts: 493
Joined: Wed Apr 02, 2003 1:57 pm
Location: Germany

Postby mhilpert » Wed Jan 30, 2008 4:18 pm

Hello, David? Those label cuttings are a real problem. Is there any hope to get this solved?
using JFreeChart 1.0.15 with JDK 1.7.0_25
mhilpert
 
Posts: 493
Joined: Wed Apr 02, 2003 1:57 pm
Location: Germany

Postby david.gilbert » Wed Jan 30, 2008 4:54 pm

Yes, it would be nice to solve this (long-standing) problem. The auto-range mechanism could compute the bounds of every item label in Java2D space then convert this to appropriate data values for inclusion in the "auto range"...it would be a little more compute intensive, but probably not so hard to implement (and it could easily be made optional for those that don't want it).
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
david.gilbert
JFreeChart Project Leader
 
Posts: 11319
Joined: Fri Mar 14, 2003 10:29 am

Postby mhilpert » Thu Jan 31, 2008 12:19 pm

Also problems with other labels:

Image


Image
using JFreeChart 1.0.15 with JDK 1.7.0_25
mhilpert
 
Posts: 493
Joined: Wed Apr 02, 2003 1:57 pm
Location: Germany

Postby nimowy » Thu Jan 31, 2008 4:31 pm

Just in case anyone else is seeing things cropped like this, you can set the range of each axis so that things like labels aren't cropped:

myAxis.setRange(0, 105);

You can also set the label locations, so they are on the bottom of the data points:
ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE6,TextAnchor.TOP_CENTER);
renderer2.setSeriesPositiveItemLabelPosition(0, position );
Last edited by nimowy on Thu Jan 31, 2008 5:37 pm, edited 1 time in total.
nimowy
 
Posts: 19
Joined: Fri Jan 25, 2008 10:16 pm

Postby mhilpert » Thu Jan 31, 2008 5:01 pm

nimowy wrote:Just in case anyone else is seeing things cropped like this, you can set the range of each axis so that things like labels aren't cropped:

myAxis.setRange(0, 105);


But this doesn't help much as charts can be dynamic. I even worked around this (a bit) by setting the margin (setLowerMargin(), setUpperMargin()) ... but there are still cases with big or small axis ranges where these margins are still not good enough. Everytime I find a workaround for this, a few weeks/months later I got a print out on my table with another chart that shows cut off labels.

The real problem is, that labels (with custom formats!) need to be taken into account for the size calculations of (each kind of) labels, like axis tick labels, itemGenerator labels, pielabels, etc.
using JFreeChart 1.0.15 with JDK 1.7.0_25
mhilpert
 
Posts: 493
Joined: Wed Apr 02, 2003 1:57 pm
Location: Germany

Postby nimowy » Thu Jan 31, 2008 5:39 pm

Sure, that would be nice. I certainly don't have the time to fix that, but maybe someone does.

I was posting my work-arounds so that other people can see them. It's particularly helpful if you are new to JFreeChart like me.
nimowy
 
Posts: 19
Joined: Fri Jan 25, 2008 10:16 pm


Return to JFreeChart - General

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Yahoo [Bot] and 14 guests