Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9005227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:05:19+00:00 2026-06-16T01:05:19+00:00

I’m attempting to create a bar chart that needs to use specific series colors

  • 0

I’m attempting to create a bar chart that needs to use specific series colors for N number of categories and a separate color for 1 other category. Here is an example:

enter image description here

In the mockup, Categories 1-3 use a collection of 5 colors to render their series while Category 4 uses a single grey color. My first approach was to override the getItemPaint() method using a customizer class, but I can only figure out how to define a color on the category level, not the series level. Is it possible to define colors on category and/or series levels? Something like,

If category != "Category4"
    Use colors A, B, C, D, and E in category's series
Else
    Use color F in category's series

Another thought I had was to combine iReport’s Series Colors bar chart property and the getItemPaint() override; that way I could define the 5 colors to use in iReport and use the getItemPaint() override only in the case where the category equals “Category 4”. So far I’ve had no luck combining the two; if the override is defined it overrides iReport’s Series Color property.

Update with code:

import java.awt.Color;
import java.awt.Paint;

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

import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;

public class CustomSeriesColors extends JRAbstractChartCustomizer {

    static class CustomRenderer extends BarRenderer {

        private final Paint[] colors1;
        private final Paint[] colors2;

        public CustomRenderer(Paint[] colors1, Paint[] colors2) {
            this.colors1 = colors1;
            this.colors2 = colors2;
        }

        //Set custom colors for series according to category    
        @Override
        public Paint getItemPaint(int row, int column) {
            CategoryDataset l_jfcDataset = getPlot().getDataset();
            String l_rowKey = (String)l_jfcDataset.getRowKey(row);
            String l_colKey = (String)l_jfcDataset.getColumnKey(column);

            if ("Insufficient Data".equalsIgnoreCase(l_colKey)) {
                return this.colors2[row % this.colors1.length];
            } else {
                return this.colors1[row % this.colors1.length];
            }
        }
    }

    public void customize(JFreeChart chart, JRChart jasperChart) {

        // Get plot data.
        CategoryPlot categoryPlot = chart.getCategoryPlot();

        // Define colors to be used by series.
        Color[] color1 = new Color[]{new Color(238,0,0), new Color(255,153,0), new Color(211,190,91), new Color(153,204,102), new Color(0,170,0)};
        Color[] color2 = new Color[]{new Color(200,200,200)};

        // Call CustomRenderer using defined color sets.
        categoryPlot.setRenderer(new CustomRenderer(color1,color2));
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:05:20+00:00Added an answer on June 16, 2026 at 1:05 am

    You will need to write a custom renderer to select a ‘Paint’ based on the series and category by overriding getItemPaint

    @Override
    public Paint getItemPaint(int series, int category) {
      if (category == 4)
        return this.colors2[series % this.colors1.length];
      else
        return this.colors1[series % this.colors1.length];
    }
    

    The use your renderer update for the plot:

    Color[] color1 = new Color[]{Color.red, Color.green,Color.blue};
    Color[] color2 = new Color[]{Color.gray,Color.gray.darker()};
    plot.setRenderer(new CustomRenderer(color1,color2));
    

    enter image description here

    Here is the code in full

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Paint;
    
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.axis.NumberTickUnit;
    import org.jfree.chart.axis.SymbolAxis;
    import org.jfree.chart.plot.CategoryPlot;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.renderer.category.BarRenderer;
    import org.jfree.data.category.CategoryDataset;
    import org.jfree.data.category.DefaultCategoryDataset;
    import org.jfree.ui.ApplicationFrame;
    import org.jfree.ui.RefineryUtilities;
    
    
    public class BarChartDemo7 extends ApplicationFrame {
    
        static class CustomRenderer extends BarRenderer {
    
            private final Paint[] colors1;
            private final Paint[] colors2;
    
            public CustomRenderer(Paint[] colors1,Paint[] colors2) {
                this.colors1 = colors1;
                this.colors2 = colors2;
            }
    
            @Override
            public Paint getItemPaint(int series, int category) {
                if (category == 4)
                    return this.colors2[series % this.colors1.length];
                else
                    return this.colors1[series % this.colors1.length];
            }
        }
    
    
        public BarChartDemo7(String title) {
            super(title);
            CategoryDataset dataSet=createDataset();
            JFreeChart chart=createChart(dataSet);
            ChartPanel panel=new ChartPanel(chart);
            panel.setPreferredSize(new Dimension(800,800));
            setContentPane(panel);
        }
    
    
        private static CategoryDataset createDataset() {
            // row keys...
            final String series1 = "ABC";
            final String series2 = "XYZ";
    
            // column keys...
            final String category1 = "A";
            final String category2 = "B";
            final String category3 = "C";
            final String category4 = "D";
            final String category5 = "E";
            final String category6 = "F";
    
            // create the dataset...
            final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    
            dataset.addValue(35, series1, category1);
            dataset.addValue(37, series1, category2);
            dataset.addValue(40, series1, category3);
            dataset.addValue(52, series1, category4);
            dataset.addValue(52, series1, category5);
            dataset.addValue(52, series1, category6);
    
    
            dataset.addValue(30, series2, category1);
            dataset.addValue(32, series2, category2);
            dataset.addValue(35, series2, category3);
            dataset.addValue(45, series2, category4);
            dataset.addValue(30, series2, category1);
            dataset.addValue(32, series2, category2);
            dataset.addValue(35, series2, category5);
            dataset.addValue(45, series2, category4);
    
            return dataset;
        }
    
    
    
        private static JFreeChart createChart(CategoryDataset dataset) {
    
            // create the chart...
            JFreeChart chart = ChartFactory.createBarChart(
                "ABC",             
                "",                      
                "",                         
                dataset,                         
                PlotOrientation.VERTICAL,        
                true,                            
                true,                            
                false                            
            );
    
            chart.setBackgroundPaint(Color.white);
    
            CategoryPlot plot = (CategoryPlot) chart.getPlot();
            plot.setBackgroundPaint(Color.WHITE);
            plot.setDomainGridlinePaint(Color.BLACK);
            plot.setRangeGridlinePaint(Color.BLACK);
    
            //Provide a new Symbol Axis
            String[] grade =  new String[101];
            for (int i = 0 ; i < grade.length ; i ++){
                grade[i] = Integer.toString(i);
            }
            grade[0] = "Poor    0";
            grade[50] = "Avg.   50";
            grade[100] = "Best   100";
            SymbolAxis rangeAxis = new SymbolAxis("", grade);
            rangeAxis.setTickUnit(new NumberTickUnit(10));
            rangeAxis.setAutoRange(false);
            rangeAxis.setRange(0, 100);
            plot.setRangeAxis(rangeAxis);
    
    
            Color[] color1 = new Color[]{Color.red, Color.green,Color.blue};
            Color[] color2 = new Color[]{Color.gray,Color.gray.darker()};
            plot.setRenderer(new CustomRenderer(color1,color2));
            return chart;
        }
    
        public static void main(String[] args) {
            BarChartDemo7 demo = new BarChartDemo7("ABC");
            demo.pack();
            RefineryUtilities.centerFrameOnScreen(demo);
            demo.setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.