Item Labels get cut

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
mhilpert
Posts: 497
Joined: Wed Apr 02, 2003 1:57 pm
Location: Germany

Item Labels get cut

Post by 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.
Java 11, JFreeChart 1.0.15, JFreeSVG 4.0

mhilpert
Posts: 497
Joined: Wed Apr 02, 2003 1:57 pm
Location: Germany

Post by 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?
Java 11, JFreeChart 1.0.15, JFreeSVG 4.0

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 » 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: Support JFree via the Github sponsorship program

mhilpert
Posts: 497
Joined: Wed Apr 02, 2003 1:57 pm
Location: Germany

Post by mhilpert » Thu Jan 31, 2008 12:19 pm

Also problems with other labels:

Image


Image
Java 11, JFreeChart 1.0.15, JFreeSVG 4.0

nimowy
Posts: 19
Joined: Fri Jan 25, 2008 10:16 pm

Post by 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.

mhilpert
Posts: 497
Joined: Wed Apr 02, 2003 1:57 pm
Location: Germany

Post by 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.
Java 11, JFreeChart 1.0.15, JFreeSVG 4.0

nimowy
Posts: 19
Joined: Fri Jan 25, 2008 10:16 pm

Post by 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.

Joshi
Posts: 8
Joined: Tue Sep 26, 2017 12:46 pm
antibot: No, of course not.

Re: Item Labels get cut

Post by Joshi » Tue Dec 12, 2017 10:29 am

hello, am facing the same problem with the cut labels now,my chart is dynamic.Does any one found any answer to this problem. Thank you

Locked