how to show values in bars of a stackedbarchart?
how to show values in bars of a stackedbarchart?
hello guys,
i need to show values in stacked bars of a StackedBarChart.
how do i do that?
pls somebody let me know.
thanks,
amruth
i need to show values in stacked bars of a StackedBarChart.
how do i do that?
pls somebody let me know.
thanks,
amruth
Funnny you should ask since I just had to do that this morning in StackedXYBarRenderer. If you mean the regular category StackedBarRenderer, I think that just turning on ItemLabelsVisible (see AbstractRenderer javadoc or look at the BarChartDemo3.java in the sample code for some good settings with the regular BarRenderer).
If you mean the StackedXYBarRenderer, it doesn't support it right now, but if you insert this block of code into its drawItem (near the bottom, right after the g2.draw(bar) call), you can see them:
They look better if you explicitely center the item label position when you setup your chart:
If you mean the StackedXYBarRenderer, it doesn't support it right now, but if you insert this block of code into its drawItem (near the bottom, right after the g2.draw(bar) call), you can see them:
Code: Select all
// draw the item label if there is one...
if (isItemLabelVisible(series, item)) {
drawItemLabel(
g2, orientation, dataset, series, item, bar.getCenterX(), bar.getCenterY(), (value < 0.0)
);
}
Code: Select all
XYItemRenderer renderer = xyPlot.getRenderer();
ItemLabelPosition p = new ItemLabelPosition(
ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, 0.0
);
renderer.setPositiveItemLabelPosition(p);
More Sample Code
I actually pulled out the code to a temporary subclass so I don't have to custom build the jfreechart jars. Here is is. All but the last couple of lines are just copied from the parent class to get the proper coordinates. I use the .9.21 code base if that makes a difference to folks:
Code: Select all
public class StackedXYBarLabelRenderer extends StackedXYBarRenderer
{
public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea,
PlotRenderingInfo info, XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis,
XYDataset dataset, int series, int item, CrosshairState crosshairState, int pass)
{
super.drawItem(g2, state, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item, crosshairState, pass);
//have to do a lot of the same computations as the parent. Just don't draw anything
//except the label
IntervalXYDataset intervalData = (IntervalXYDataset) dataset;
// Get height adjustment based on stack and translate to Java2D values
double ph = getPreviousHeight(dataset, series, item);
double value = intervalData.getYValue(series, item);
if (Double.isNaN(value))
{
return;
}
double translatedStartY = rangeAxis.valueToJava2D(ph, dataArea, plot.getRangeAxisEdge());
double translatedEndY = rangeAxis.valueToJava2D(value + ph, dataArea, plot.getRangeAxisEdge());
RectangleEdge location = plot.getDomainAxisEdge();
double startX = intervalData.getStartXValue(series, item);
if (Double.isNaN(startX))
{
return;
}
double translatedStartX = domainAxis.valueToJava2D(startX, dataArea, location);
double endX = intervalData.getEndXValue(series, item);
if (Double.isNaN(endX))
{
return;
}
double translatedEndX = domainAxis.valueToJava2D(endX, dataArea, location);
double translatedWidth = Math.max(1, Math.abs(translatedEndX - translatedStartX));
double translatedHeight = Math.abs(translatedEndY - translatedStartY);
if (getMargin() > 0.0)
{
double cut = translatedWidth * getMargin();
translatedWidth = translatedWidth - cut;
translatedStartX = translatedStartX + cut / 2;
}
Rectangle2D bar = null;
PlotOrientation orientation = plot.getOrientation();
if (orientation == PlotOrientation.HORIZONTAL)
{
bar = new Rectangle2D.Double(Math.min(translatedStartY, translatedEndY), translatedEndX,
translatedHeight, translatedWidth);
}
else if (orientation == PlotOrientation.VERTICAL)
{
bar = new Rectangle2D.Double(translatedStartX, Math.min(translatedStartY, translatedEndY),
translatedWidth, translatedHeight);
}
// draw the item label if there is one...
if (isItemLabelVisible(series, item))
{
drawItemLabel(g2, orientation, dataset, series, item, bar.getCenterX(), bar.getCenterY(),
(value < 0.0));
}
}
got the solution
my case was of reqular StackedBarChart.
i was using StackedBarRenderer to setItemLabelsVisible(true).
but it was not working.
i changed this to CategoryItemRenderer, setLabelGenerator(new StandardCategoryLabelGenerator()) and called setItemLabelsVisible(true)
and it worked.
thank you
i was using StackedBarRenderer to setItemLabelsVisible(true).
but it was not working.
i changed this to CategoryItemRenderer, setLabelGenerator(new StandardCategoryLabelGenerator()) and called setItemLabelsVisible(true)
and it worked.
thank you
No prob. Glad it was of use. If you want the latest version with some better calcs for when to draw labels and some more stuff, I just pushed it to the sourceforge patches tracker:
http://sourceforge.net/tracker/index.ph ... tid=315494
Javadoc of what it does:
Handles some additional drawing for StackedXYBarCharts. It can draw labels with the total value of all the series at each point above the bar. It also draws regular item labels since the default <code>StackedXYBarRenderer</code> doesn't do that yet (though that may included in a new release of jfreechart). It also allows for setting the stroke and paint of individual items in a series. This is useful for hilighting or focusing on an item.
http://sourceforge.net/tracker/index.ph ... tid=315494
Javadoc of what it does:
Handles some additional drawing for StackedXYBarCharts. It can draw labels with the total value of all the series at each point above the bar. It also draws regular item labels since the default <code>StackedXYBarRenderer</code> doesn't do that yet (though that may included in a new release of jfreechart). It also allows for setting the stroke and paint of individual items in a series. This is useful for hilighting or focusing on an item.
Problem
I have problems.
I need to show the values of StackedBar using CategoryRenderer...
But and dont have success, i implement the CategoryItemRenderer, setLabelGenerator(new StandardCategoryLabelGenerator()) and called setItemLabelsVisible(true) and dont work !!!!
This method setLabelGenerator(new StandardCategoryLabelGenerator()) dont exist ...!!!
I need a example please
I need to show the values of StackedBar using CategoryRenderer...
But and dont have success, i implement the CategoryItemRenderer, setLabelGenerator(new StandardCategoryLabelGenerator()) and called setItemLabelsVisible(true) and dont work !!!!
This method setLabelGenerator(new StandardCategoryLabelGenerator()) dont exist ...!!!
I need a example please
code
CategoryDataset dataset =
DatasetUtilities.createCategoryDataset(
eventTypes,
monthNames,
result);
JFreeChart chart =
ChartFactory.createStackedBarChart(
"",
"",
"",
dataset,
org.jfree.chart.plot.PlotOrientation.VERTICAL,
true,
true,
false);
chart.getCategoryPlot().setDomainGridlinesVisible(true);
chart.getCategoryPlot().setRangeGridlinesVisible(true);
CategoryItemRenderer cir = chart.getCategoryPlot().getRenderer();
///////////////////////////////////// IN THIS MOMENT !!!!!!!!!!
WHAT I DO TO ADD LABELS IN CategoryItemRenderer ???
I trying this (its not work):
StandardCategoryItemLabelGenerator labelGenerator = new StandardCategoryItemLabelGenerator(NumberFormat.getInstance());
cir.setItemLabelGenerator(labelGenerator);
cir.setItemLabelsVisible(Boolean.TRUE);
chart.getCategoryPlot().setRenderer(cir);
DatasetUtilities.createCategoryDataset(
eventTypes,
monthNames,
result);
JFreeChart chart =
ChartFactory.createStackedBarChart(
"",
"",
"",
dataset,
org.jfree.chart.plot.PlotOrientation.VERTICAL,
true,
true,
false);
chart.getCategoryPlot().setDomainGridlinesVisible(true);
chart.getCategoryPlot().setRangeGridlinesVisible(true);
CategoryItemRenderer cir = chart.getCategoryPlot().getRenderer();
///////////////////////////////////// IN THIS MOMENT !!!!!!!!!!
WHAT I DO TO ADD LABELS IN CategoryItemRenderer ???
I trying this (its not work):
StandardCategoryItemLabelGenerator labelGenerator = new StandardCategoryItemLabelGenerator(NumberFormat.getInstance());
cir.setItemLabelGenerator(labelGenerator);
cir.setItemLabelsVisible(Boolean.TRUE);
chart.getCategoryPlot().setRenderer(cir);