Jump to content

Customize Bart chart


silvi

Recommended Posts

I want to have the stacked bar chart colors based upon their series values .

Like for "A" -- red

               "B" --- yellow

               "C" --- green

 

As I am a beginner in creating jasper class, I tried my best and came up with this code.. 

 

public class BarColors extends JRAbstractChartCustomizer implements JRChartCustomizer
{
     public class CustomBarRenderer extends BarRenderer {

  public CustomBarRenderer()
  {
    super();
  }

  public Paint getItemPaint(int row, int column)
  {
    CategoryDataset cd = getPlot().getDataset();

        if (cd != null)
        { String l_rowKey = (String)cd.getRowKey(row);
           String l_colKey = (String)cd.getColumnKey(column);
      double result  = cd.getValue(l_rowKey, l_colKey).doubleValue();
             String eg = Double.toString(result);

    if (eg.equals("A")) {
          new Color(0xE4, 0x1B, 0x1B); }
 else if (eg.equals("B"))
        {new Color(0xF1, 0xAB, 0x10); }
  else if (eg.equals("C"))
          { new Color(0xEF, 0xC3, 0x5A); }
  else if (eg.equals("D"))
           {new Color(0x0B, 0x2E, 0xF0);}
  else { }

   }

    return  (Paint)cd;

 }

}


public void customize(JFreeChart chart, JRChart jasperchart) {

       // get the "plot" from the JFreeChart

 CategoryPlot categoryplot = (CategoryPlot)chart.getCategoryPlot();

       // get the dataset

 CategoryDataset cd = (CategoryDataset)categoryplot.getDataset();

 CustomBarRenderer renderer = new CustomBarRenderer();

 chart.getCategoryPlot().setRenderer(renderer);

}

}

 

It build succesfully as .jar file but when I am executing in the report report is blank and showing error(though report is working fine without customize class).

I am sure that  code has errors in it. Please, help me It's very urgent for me. Any suggestions/comments are appreciable. 

 

Thanks

Silvi

 

 


   

Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

Here you go...

 

import java.awt.Color;

import net.sf.jasperreports.engine.JRChart;
import net.sf.jasperreports.engine.JRChartCustomizer;

import org.jfree.chart.JFreeChart;
import org.jfree.data.category.*;
import org.jfree.chart.renderer.category.*;

import java.util.HashMap;



public class StackedChartCustomizer implements JRChartCustomizer {

    public static HashMap<String,Color> engGroupColor = new HashMap<String, Color>();
    static {
        engGroupColor.put("A", new Color(0xE4, 0x1B, 0x1B));
        engGroupColor.put("B", new Color(0xF1, 0xAB, 0x10));
       }




    public void customize(JFreeChart chart, JRChart jasperChart) {

        // Category Plot: most commonly used to display bar chart
        // BarRenderer: create bar charts from data in a category dataset

        BarRenderer renderer = (BarRenderer) chart.getCategoryPlot().getRenderer();

        // Method required for reading the dataset.
        // Table of values that can be accessed using row and column keys.
        CategoryDataset cd = chart.getCategoryPlot().getDataset();

        // Row gives the series expression
        int rc = cd.getRowCount();
        for (int i = 0; i < rc; i++) {
           String egName = cd.getRowKey(i).toString();

            Color color = engGroupColor.get(egName);
            if (color == null) {
                color = newUniqueColor();
                engGroupColor.put(egName, color);
              }

            renderer.setSeriesPaint(i, color);
        }
    }

    private Color newUniqueColor() {
        // just return black for now - later some code to generate a
        // unique color not already used
        return Color.BLACK;
    }
}
 

Link to comment
Share on other sites

  • 1 year later...

Hi.. How to give different colors for different bars in iReport bar chart ?

I'm not talking about series wise colors but talking about only bar colors.

It's very very urgent need for me. Could you plz help me this ?

Thanks

SADAKAR.P

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...