Multiple Datasets on Bar Chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
tal
Posts: 8
Joined: Fri Mar 24, 2006 4:48 pm

Multiple Datasets on Bar Chart

Post by tal » Fri Mar 24, 2006 8:04 pm

Hello everyone,

I have a wierd problem going on. When I have 1 dataset on the Bar Chart, I'm getting the number for each item above the bar if it's small, or inside it if it's big. This is great! I love that! But when I have multiple datasets, the bars get printed, but the labels are not presented on the big bars:

Image

Any one knows how to fix that?!

Here is my code:

Code: Select all

        if (orientation == null) {
            throw new IllegalArgumentException("Null 'orientation' argument.");
        }
        CategoryAxis categoryAxis = new CategoryAxis3D(categoryAxisLabel);
        /* Changes */
        categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(0.9));
        categoryAxis.setMaximumCategoryLabelWidthRatio(15.0f);
        categoryAxis.setCategoryLabelPositionOffset(1);
		/* End changes */
		ValueAxis valueAxis = new NumberAxis3D(valueAxisLabel);

        BarRenderer3D renderer = new BarRenderer3D();
		renderer.setItemMargin(0);
		renderer.setDrawBarOutline(false);
        /* Changes */
        renderer.setItemLabelsVisible(true);
		renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		/* End Changes */
        if (urls) {
            renderer.setBaseItemURLGenerator(
                new StandardCategoryURLGenerator()
            );
        }
		/* Changes */
        ItemLabelPosition position1 = new ItemLabelPosition(
        		dataset.getColumnCount()>20?ItemLabelAnchor.OUTSIDE9:ItemLabelAnchor.CENTER, TextAnchor.BOTTOM_CENTER,
				TextAnchor.TOP_CENTER, -1.5708
        );      
		renderer.setPositiveItemLabelPosition(position1);
        ItemLabelPosition position2 = new ItemLabelPosition(
        		ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER,
				TextAnchor.TOP_CENTER, -1.5708
        );      
		renderer.setNegativeItemLabelPosition(position2);
		/* End Changes */
        CategoryPlot plot = new CategoryPlot(
            dataset, categoryAxis, valueAxis, renderer
        );
        plot.setOrientation(orientation);
        if (orientation == PlotOrientation.HORIZONTAL) {
            // change rendering order to ensure that bar overlapping is the 
            // right way around
            plot.setRowRenderingOrder(SortOrder.DESCENDING);
            plot.setColumnRenderingOrder(SortOrder.DESCENDING);
        }
        plot.setForegroundAlpha(0.75f);
		
        JFreeChart chart = new JFreeChart(
            title, JFreeChart.DEFAULT_TITLE_FONT, plot, dataset.getRowCount()>1
        );

		Iterator<String> itr = subtitle.iterator();
		while(itr.hasNext())
			chart.addSubtitle(new TextTitle(itr.next()));
        return chart;
Thanks,

Tal

tal
Posts: 8
Joined: Fri Mar 24, 2006 4:48 pm

Anyone?!

Post by tal » Wed Apr 05, 2006 9:55 pm

Anyone could suggest me anything at all that might solve this?

Thanks,

Tal

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 » Thu Apr 06, 2006 10:42 am

I couldn't spot anything obviously wrong in your code - but reading code fragments is no fun. Can you post a self-contained demo that reproduces the problem? That is, something that I can copy, paste, compile and run...
David Gilbert
JFreeChart Project Leader

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

tal
Posts: 8
Joined: Fri Mar 24, 2006 4:48 pm

Post by tal » Thu Apr 06, 2006 3:38 pm

Thanks David,

Here is jar file contain my code (and the part of JFreeChart I'm using):

http://www.csfi.com/tal/BarError.jar

Changes I've made in the code (in JFreeChart Code) are marked with the comment "Tal". What I've done is basically modified the JFreeChart package to only contain the charts I need, and changed the code a little a little (instead of checking negative, I'm checking the bar size).

FYI, when using the BarError class with the release version of JFreeChart I don't get any label!

Command line to run the example :

java -cp BarError.jar;jcommon-1.0.0.jar BarError


Some more info from my debugging so far:

This is got to do with the positive position. When I don't rotate the label (i.e. using

Code: Select all

ItemLabelAnchor.INSIDE12, TextAnchor.TOP_CENTER
) I don't get any label when the bar is too thin, and will get the label when I increase the size of the window. If I'm using my own code, and start changing the angle from 0 down, I will get the label when the bar is wide enough, until I'm reaching -0.2... Then I can't get the lables even if the the window is as wide as I can get it.

Thanks,

Tal

tal
Posts: 8
Joined: Fri Mar 24, 2006 4:48 pm

Post by tal » Thu Apr 20, 2006 6:37 pm

Anyone?! Any clue?!

tal
Posts: 8
Joined: Fri Mar 24, 2006 4:48 pm

Post by tal » Tue May 02, 2006 3:49 pm

Dear David,

Should I submit this as a bug? It doesn't seems like any one can find something wrong with my code...

Thanks,

Tal

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 May 03, 2006 6:37 am

It's not a bug. You've specified that the label should appear inside the bar. If there isn't enough space for the label, JFreeChart will look to the "fallback" position and if it is not null the label will be drawn at that position. In your case, the labels don't fit within the bar (because of the rotation point you chose), and you didn't specify the fallback position. I think you need settings like this:

Code: Select all

        renderer.setPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.BOTTOM_CENTER, TextAnchor.CENTER, -Math.PI / 2));
        renderer.setPositiveItemLabelPositionFallback(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER, TextAnchor.CENTER, -Math.PI / 2));
David Gilbert
JFreeChart Project Leader

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

tal
Posts: 8
Joined: Fri Mar 24, 2006 4:48 pm

Thanks

Post by tal » Wed May 03, 2006 5:10 pm

Thanks David,

The CENTER instead of TOP_CENTER took care of that!

Cheers!

Locked