JFreeChart Bar Graph Labels
Asked Answered
S

4

5

I have made it possible for value labels to appear on top of the bar in JFreeChart. However, it would look better if the labels are inside the bars. How do I make this work? The image below shows what I wanted the graph to look like.

Bar Graph with Label inside bar

Springhead answered 23/8, 2011 at 1:9 Comment(0)
S
10

I used the following code to make it work:

StackedBarRenderer renderer = new StackedBarRenderer(false);
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
chart.getCategoryPlot().setRenderer(renderer);`
Springhead answered 24/8, 2011 at 6:46 Comment(0)
C
5

Just specify the desired ItemLabelPosition in your CategoryItemLabelGenerator. BarChartDemo3 is an example, shown here.

Ceilometer answered 23/8, 2011 at 8:7 Comment(1)
Your example relies on the default ItemLabelPosition instances in AbstractRenderer.Ceilometer
M
5

You can use ItemLabelPosition DOC Here

renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,TextAnchor.TOP_CENTER  ))

Output :-

enter image description here

Monongahela answered 5/9, 2012 at 11:17 Comment(0)
P
0

As fixing positions use ItemLabelPosition

CategoryPlot plot = chart.getCategoryPlot();
CategoryItemRenderer renderer = plot.getRenderer(); 
CategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator("{2}", NumberFormat.getInstance()); 

renderer.setItemLabelGenerator(generator); 
renderer.setItemLabelFont(new Font("SansSerif", Font.PLAIN, 12)); //just font change optional 
renderer.setItemLabelsVisible(true); 
renderer.setPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, - 0 / 2)); 

It will show your value in the Center of your bar. You can modify the position as per your requirements.

Pilar answered 22/8, 2020 at 17:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.