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.
开发者_如何学JAVAI used the following code to make it work:
StackedBarRenderer renderer = new StackedBarRenderer(false);
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
chart.getCategoryPlot().setRenderer(renderer);`
You can use ItemLabelPosition DOC Here
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,TextAnchor.TOP_CENTER ))
Output :-
Just specify the desired ItemLabelPosition
in your CategoryItemLabelGenerator
. BarChartDemo3
is an example, shown here.
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.
精彩评论