Jump to content

Individual colours for chart columns?


martinjones

Recommended Posts

  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

Are these charts static or generated during report generation?  If static, have 3 differently colored charts in the exact same spot, one for each color.  Then in their "Print when expression..." property, implement the logic so that for each detail or depending on a variable, only one chart will print.  This was just a quick thought.  You really need to include more information about how you're making them (or better yet the report itself) for more detailed help.

Link to comment
Share on other sites

Hi,

Thanks for getting back to me. I will try to be more clear and explain a bit more:

I am using iReport to make the charts, and using JasperServer to display them. I guess they are static charts as I am not generating them through java code.

I don't think the 3 overlayed charts will work, since this seems to only display the entire chart if the condition is met. I wish to have individual bars on the same chart in different colours.

e.g. The first 5 columns (>75%) will be green, then next 4 columns (>65 & < 75) will be yellow, and everything less than 65 will be red.

Is this possible?

Thanks

Martin

Link to comment
Share on other sites

I have managed to implement the desired functionality by overlaying three separate charts, with different series colours for each chart.

FYI, in each chart, in each category series, I had to put the following in the value expression

So, chart 1 has the value expression ($V{Amount} >= 75) ? $V{Amount} : 0

Chart 2 has ($V{Amount} >= 65 & $V{Amount} < 75) ? $V{Amount} : 0

and chart 3 has ($V{Amount} < 65) ? $V{Amount} : 0

That way, the charts overlay each other with separate colours, but do not display the bars if they are out of the range I wish to display.

Thanks for the inspiration about hiding and overlaying.

Link to comment
Share on other sites

I managed it with Chart customization, i selected the color also as a part of the query itself

 

SELECT
 to_char(tms_production_fact."productiondate",'dd-mon') AS tms_production_fact_productiondate,
 tms_production_fact."workedspindles"/tms_production_fact."allottedspindles" *100 AS Utilisation,
 case
    when tms_production_fact."workedspindles"/tms_production_fact."allottedspindles" *100 <70
           then '<70     #FF0000'
    when tms_production_fact."workedspindles"/tms_production_fact."allottedspindles" *100 between 70 and 90
           then '70>#<90 #FFFF00'
   when tms_production_fact."workedspindles"/tms_production_fact."allottedspindles" *100 >=90
           then '>=90    #00FF00'
  end as color
 FROM "public"."tms_production_fact" tms_production_fact

 

and in the bar chart give the "color" field as a series expression

 

Now write the below customizer class

Code:
import java.awt.Color;import java.math.BigDecimal;import net.sf.jasperreports.engine.JRChartCustomizer;import org.jfree.chart.plot.CategoryPlot;import org.jfree.chart.renderer.category.BarRenderer;import org.jfree.data.category.CategoryDataset;import org.jfree.data.category.DefaultCategoryDataset;import org.jfree.data.general.Dataset; public class BarchartColor implements JRChartCustomizer {    public void customize(org.jfree.chart.JFreeChart param1, net.sf.jasperreports.engine.JRChart param2) {    	// get the "plot" from the JFreeChart        CategoryPlot plot = (CategoryPlot)param1.getPlot();        // get the dataset        CategoryDataset pd = plot.getDataset();        // create a new empty dataset        DefaultCategoryDataset pdWithoutColor = new DefaultCategoryDataset();        BarRenderer renderer = new BarRenderer();        // simple counter        int serieNum = 0;        // loop on series        for(int i = 0; i < pd.getRowCount(); i++) {        				// get the key. Here it is the legend followed by the color                String serie = (String) pd.getRowKey(i);                // get the legend only                String legende = serie.substring(0,serie.length()-7);                // get the color only                String graphcolor = serie.substring(serie.length()-7);                for(int j = 0; j < pd.getColumnCount(); ++j){                    // get the value                    BigDecimal valeur = (BigDecimal)pd.getValue(i, j);                   // add the cleaned key/legend to the new dataset                   pdWithoutColor.setValue(valeur, (Comparable)legende,pd.getColumnKey(j));                                                            }                // set the bar filling color                renderer.setSeriesPaint(serieNum, Color.decode(graphcolor));                // set the border filling collor               // renderer.setSeriesOutlinePaint(serieNum, Color.decode(graphcolor));                serieNum++;        }        //       // renderer.setItemMargin(0.0);        // set the renderer (which contains our colors)        plot.setRenderer(renderer);        // set the new (cleaned) dataset        plot.setDataset(pdWithoutColor);            }}
Link to comment
Share on other sites

  • 2 weeks 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...