how to show values in bars of a stackedbarchart?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
amruth_b
Posts: 2
Joined: Thu Apr 07, 2005 9:57 am
Location: Bangalore

how to show values in bars of a stackedbarchart?

Post by amruth_b » Thu Apr 07, 2005 10:08 am

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

esword
Posts: 10
Joined: Tue Apr 05, 2005 7:53 pm
Location: Virginia

Post by esword » Thu Apr 07, 2005 2:48 pm

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:

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)
            );
        }
They look better if you explicitely center the item label position when you setup your chart:

Code: Select all

            XYItemRenderer renderer = xyPlot.getRenderer();
            ItemLabelPosition p = new ItemLabelPosition(
                    ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, 0.0
                );
            renderer.setPositiveItemLabelPosition(p);

esword
Posts: 10
Joined: Tue Apr 05, 2005 7:53 pm
Location: Virginia

More Sample Code

Post by esword » Thu Apr 07, 2005 3:47 pm

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));
        }
    }

amruth_b
Posts: 2
Joined: Thu Apr 07, 2005 9:57 am
Location: Bangalore

got the solution

Post by amruth_b » Fri Apr 08, 2005 6:03 am

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

derek

Post by derek » Fri Jun 10, 2005 9:28 am

thanks esword, this helped me out!

esword
Posts: 10
Joined: Tue Apr 05, 2005 7:53 pm
Location: Virginia

Post by esword » Fri Jun 10, 2005 1:13 pm

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.



rafametal
Posts: 3
Joined: Thu Jun 16, 2005 3:43 pm

Problem

Post by rafametal » Thu Jun 16, 2005 4:05 pm

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

rafametal
Posts: 3
Joined: Thu Jun 16, 2005 3:43 pm

code

Post by rafametal » Thu Jun 16, 2005 5:02 pm

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);

Locked