silvi Posted February 1, 2012 Share Posted February 1, 2012 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. ThanksSilvi Link to comment Share on other sites More sharing options...
silvi Posted February 7, 2012 Author Share Posted February 7, 2012 Hi everyone, I have solved my problem. If anyone needs the code, feel free to aske me. Link to comment Share on other sites More sharing options...
pmartineau Posted February 11, 2012 Share Posted February 11, 2012 Great, I have never had success get 'my' colors to display on any charts.Could you please post your code? Thanks a lot.Phil Link to comment Share on other sites More sharing options...
silvi Posted February 16, 2012 Author Share Posted February 16, 2012 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 More sharing options...
sadakar Posted June 7, 2013 Share Posted June 7, 2013 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now