Jump to content
Changes to the Jaspersoft community edition download ×

Bar Chart question - Control color of bars


svenn

Recommended Posts

I need help creating a bar chart

The colors of the bars need to be a certain color depending on the range of the values.

Example

0 -2 red

2-4 orange

4 -> green

I tried setting up the bar chart with categories and series with each series representing the color. This works great if I have at least one items that fits into each of the color buckets. If I fill up less than the three color buckets my bars do not cover the right range. For example if all my bar values are greater than 4 then my bars are all red. Want I want is for all the bars to be green.

Attaches is an example of what I'm trying to achieve.

 

 

 



Post Edited by svenn at 09/21/2010 23:25
Link to comment
Share on other sites

  • Replies 7
  • Created
  • Last Reply

Top Posters In This Topic

My new approach is to use a chart customizer. Now I’m stuck as I cannot figure out how to go about changing the color of the bars based on the bar values. I've figure out how to find the value assigned to each bar but I cannot figure out how to use the value to get each bar element and change its color.

If you look at "Get values assigned to bar chart" I manage to get the value  for the bars. Now I’m not sure where to go from here. I've been searching through JFreeCharts properties but cannot find any that would allow me to use these values to change the bar colors

Here's my code

public class MyChartCustomizer extends JRAbstractChartCustomizer
{

    public void customize(JFreeChart chart, JRChart jasperChart) {
          
     //Chart is a bar chart
     if(jasperChart.getChartType() == JRChart.CHART_TYPE_BAR) {
      
      BarRenderer renderer = (BarRenderer) chart.getCategoryPlot().getRenderer();
     
      //Removes gray line around the bar
      renderer.setDrawBarOutline(false);
         
      //Removes the label displayed at the top of the bar
         renderer.setBaseItemLabelGenerator(null);
         
      //Set maximum bar width
         renderer.setMaximumBarWidth(0.10);
                  
         //Create no data message
         CategoryPlot categoryplot = (CategoryPlot) chart.getCategoryPlot();
         categoryplot.setNoDataMessage("No data available");
         categoryplot.setNoDataMessageFont(new Font("SansSerif",Font.BOLD,14));
         categoryplot.setNoDataMessagePaint(Color.WHITE);
         
         //Set background as transparent
         categoryplot.setBackgroundPaint(null);
         
            CategoryDataset cd = (CategoryDataset) categoryplot.getDataset();
         
   //Get values assigned to bar chart
         if(cd != null) {
          System.out.println("Bar Row " + cd.getRowCount());
          System.out.println("Column Row " + cd.getColumnCount());
          
          for (int row = 0; row < cd.getRowCount(); row++) {
           for (int col = 0; col < cd.getColumnCount(); col++) {
            String l_rowKey = (String)String.valueOf(cd.getRowKey(row));
            String l_colKey = (String)cd.getColumnKey(col);
            double l_value  = cd.getValue(cd.getRowKey(row), l_colKey).doubleValue();
            double s_value = cd.getValue(row,col).doubleValue();

            System.out.println("l_rowKey " + l_rowKey + " l_colKey  " + l_colKey + " l_value " + l_value + " s_value " + s_value);
          
           }
          }
         
         }         
                   
         //Set space for labels so it does not truncate
         categoryAxis.setMaximumCategoryLabelWidthRatio(25.0f);
         categoryAxis.setMaximumCategoryLabelLines(2);
         
         //set background grid color
         categoryplot.setRangeGridlinePaint(Color.red);
                   
     }        
    }

 

Link to comment
Share on other sites

The BarRenderer class does not allow individual color settings per bar item. But it uses the getItemPaint(row, column) method when drawing individual bars.

A possible workaround would be to create your own BarRenderer class which extends the JFreeChart BarRenderer and to override the getItemPaint(row, column) in order to return the desired color. Then use the chart customizer to set the category plot with your renderer object:

The CustomBarRenderer class:

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 l_value  = cd.getValue(l_rowKey, l_colKey).doubleValue();
      return l_value > 4
             ? Color.GREEN
             : (l_value > 2
                ? Color.ORANGE
                : Color.RED);
    }
  }
}

And then add these lines in the customize(JFreeChart chart, JRChart jrChart) method in the BarChartCustomizer class:

CustomBarRenderer renderer = new CustomBarRenderer();
chart.getCategoryPlot().setRenderer(renderer);

Hope this helps,

sanda

 

 



Post Edited by shertage at 09/28/2010 11:12
Link to comment
Share on other sites

Well, this would be a little more complicate, but not impossible.

In the CustomBarRenderer class you should:

  • Do override the drawDomainGridline(...) and drawRangeGridline(...) methods. These methods are inherited from the AbstractCategoryItemRenderer class (see its source). In your drawDomainGridline(...) method just replace the

Paint paint = plot.getDomainGridlinePaint(); line with Paint paint = getDomainGridlinePaint(value);

In drawRangeGridline(...) method replace the

Paint paint = plot.getRangeGridlinePaint(); line with Paint paint = getRangeGridlinePaint(value); 

  • Do implement your own methods getDomainGridlinePaint(double value) and getRangeGridlinePaint(double value), in order to get the desired gridline color depending on the value input.

And these 2 steps should solve together the gridlines problem.

HTH,

sanda

 

 



Post Edited by shertage at 09/29/2010 09:12
Link to comment
Share on other sites

  • 3 months later...
  • 9 years later...

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...