Getting values on top of the bar chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Steve
Posts: 53
Joined: Mon Dec 22, 2003 8:29 pm

Getting values on top of the bar chart

Post by Steve » Thu Aug 26, 2004 8:20 pm

Ok, recently I've seen 3-4 topics about this and no straight answer has been given, so I'm making a dedicated topic for it.
I've checked out the demo source files and I can't see any way to get a barchart's displayed values over the bar itself, so I'm asking, please explain it so we all can see it.

Here's what I mean for any who aren't clear:

Top of the bar:
Image

On top of the bar:
Image

I've seen the latter done on a few charts, and can't figure it out for myself.
I've tried OUTSIDE12 & INSIDE12. They don't move the value on top of it, just to the inside top.

Thanks.

Steve
Posts: 53
Joined: Mon Dec 22, 2003 8:29 pm

Post by Steve » Mon Aug 30, 2004 8:58 pm

Someone?

:(

angel
Posts: 899
Joined: Thu Jan 15, 2004 12:07 am
Location: Germany - Palatinate

Post by angel » Tue Aug 31, 2004 1:32 pm

This works with 2d barcharts

Code: Select all

CategoryItemRenderer renderer = plot.getRenderer();
renderer.setItemLabelsVisible(true);

Steve
Posts: 53
Joined: Mon Dec 22, 2003 8:29 pm

Post by Steve » Tue Aug 31, 2004 9:25 pm

But that's a 2d chart, not 3d.
And I have setItemLabelsVisisble(true) already, and it makes the labels appear, but not on top of the bar. :(

Here's the code for the bar creation:

Code: Select all

		try
		{
			// Create the chart object
			JFreeChart chart = ChartFactory.createBarChart3D(
		            "Number of Databases per Engine",    // chart title
		            "Engine",                            // domain axis label
		            "Number of Databases",               // range axis label
		            myCatSet,                            // data
		            PlotOrientation.VERTICAL,            // orientation
		            false,                               // include legend
		            true,                                // tooltips
		            false                                // urls
		        );
			
			chart.setBackgroundPaint(new Color(255, 255, 255));
			
			CategoryPlot plot = chart.getCategoryPlot();
			
			plot.setForegroundAlpha(1);
			//plot.setURLGenerator(new StandardCategoryURLGenerator("xy_chart.jsp", "section"));
			
			CategoryAxis categoryAxis = plot.getDomainAxis(); // Lower axis
			
			// Set the rotation angle of the lower labels.
			categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 4));
			categoryAxis.setCategoryMargin(.15); //Set the width between categories
			
			BarRenderer3D renderer = (BarRenderer3D) plot.getRenderer();
			renderer.setLabelGenerator(new StandardCategoryLabelGenerator()); 
			renderer.setItemLabelsVisible(true);
			renderer.setPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.TOP_CENTER));
			renderer.setItemMargin(0); // Distance between each bar in a group.
			renderer.setMaxBarWidth(.25); // The max width of the bar
			renderer.setSeriesItemURLGenerator(0, new StandardCategoryURLGenerator(
					"/redi/modules/pasta/Charts/ViewDbCountByEngine.jsp",
					"series",
					"engine"));
			
			
		  //******************************************************************
		  // Set colors
		  //******************************************************************
			//System.out.println("Setting colors");
			Paint rediPaint = new Color(65, 65, 200);
			//renderer.setOutlinePaint(rediPaint); // Bar outline color
			//renderer.setWallPaint(rediPaint); // Left wall of graph color
			//renderer.setPaint(rediPaint); // Main Bar color
		
		  //******************************************************************

			//  Write the chart image to the temporary directory
			ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
			filename = ServletUtilities.saveChartAsPNG(chart, 800, 550, info, session);
			
			//System.out.println("Writing Image file");
			//  Write the image map to the PrintWriter
			ChartUtilities.writeImageMap(pw, filename, info);
			pw.flush();
			//System.out.println("Done");

		}
		catch (Exception e)
		{
			System.out.println("Exception - " + e.toString());
			e.printStackTrace(System.out);
			filename = "public_error_500x300.png";
		}

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 Sep 01, 2004 5:36 pm

This is the important line:

Code: Select all

renderer.setPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.TOP_CENTER));
The anchor position is a point just above the top of the bar (OUTSIDE12). You then align a fixed point on the label to the anchor position - in your case, you've specified that the TOP_CENTER of the label should be aligned, change that to BOTTOM_CENTER and you'll get closer to what you want.
David Gilbert
JFreeChart Project Leader

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

Steve
Posts: 53
Joined: Mon Dec 22, 2003 8:29 pm

Post by Steve » Fri Sep 03, 2004 3:31 pm

Cool, that did it, but because it's a 3d bar, the value is somewhat obscured.

Is there anything I can do about that?

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 » Fri Sep 03, 2004 5:01 pm

You can increase the distance between the top of the bar and the anchor point using the setItemLabelAnchorOffset(double) method in the renderer.
David Gilbert
JFreeChart Project Leader

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

pedrobrio

Post by pedrobrio » Mon Sep 06, 2004 10:00 am

Hello everyone,
I have an other problem...
The values of the bar are to higth and are not visible.

What shall I do ?

angel
Posts: 899
Joined: Thu Jan 15, 2004 12:07 am
Location: Germany - Palatinate

Post by angel » Mon Sep 06, 2004 12:40 pm

pedrobrio wrote:Hello everyone,
I have an other problem...
The values of the bar are to higth and are not visible.

What shall I do ?
I can ontly tell you about the 2d chart. When you put the values on top, you must also adjust the axis.

Code: Select all

NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setUpperMargin(0.3);

Steve
Posts: 53
Joined: Mon Dec 22, 2003 8:29 pm

Post by Steve » Tue Sep 07, 2004 2:36 pm

david.gilbert wrote:You can increase the distance between the top of the bar and the anchor point using the setItemLabelAnchorOffset(double) method in the renderer.
Thanks alot, that did it.

Now I just need to how to center it "3d". :)

pedrobrio

Post by pedrobrio » Tue Sep 07, 2004 6:03 pm

angel wrote:
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setUpperMargin(0.3);
Thanks a lot Angel.
It works really fine.

I'm convinced that this jfreechart is a real good peace of software,
like the persons who use it...

Locked