Jump to content
We've recently updated our Privacy Statement, available here ×

Customizing Pie Chart Colors


sgolestane

Recommended Posts

I'm trying to use a ChartCustomizer to set color of Pie chart sections. the problem i have is that i can't get to the dataset. datasert that i get by calling piePlot.getDataset() is empty.

Code:
<pieChart>        <chart hyperlinkTarget="Self" customizerClass="jasper.ChartLabelCustomizer">                <reportElement x="0" y="0" width="555" height="300" style="chart"/>                <box></box>                <chartLegend textColor="#000000" backgroundColor="#FFFFFF" position="Left">                </chartLegend>        </chart>        <pieDataset>                <dataset></dataset>                <keyExpression><![CDATA[$F{source} + " - " + new java.text.DecimalFormat("##.#").format($F{clickPercentage}) + " %"]]></keyExpression>                <valueExpression><![CDATA[$F{clickPercentage}]]></valueExpression>                <labelExpression><![CDATA[$F{source}]]></labelExpression>        </pieDataset>        <piePlot isCircular="true" >                <plot backcolor="#FFFFFF" orientation="Horizontal"></plot>        </piePlot></pieChart>public class ChartLabelCustomizer extends JRAbstractChartCustomizer{    public void customize(JFreeChart chart, JRChart jasperChart)    {        // Check the type of plot        Plot plot = chart.getPlot();        if (plot instanceof PiePlot) {            PiePlot piePlot = (PiePlot) plot;            piePlot.setMaximumLabelWidth(0.20);            piePlot.setInsets(new                    RectangleInsets(UnitType.ABSOLUTE, 0.0, 0.0,1.0, 1.0));            piePlot.setLegendItemShape(new Rectangle2D.Double(1.0, 2.0, 30.0, 6.0));            RectangleInsets margin = chart.getLegend().getMargin();            chart.getLegend().setMargin(margin.getTop(), 20D,                    margin.getBottom(), margin.getRight());                        PieDataset dataset = piePlot.getDataset();            System.out.println("Printing keys, count : " + dataset.getItemCount());            for (int i = 0; i < dataset.getItemCount(); i++) {                            }                    }    }}
Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Here is how i fixed this problem:

in ChartLabelCustomizer  I add a ChangeListener to piePlot to listen for DATASET_UPDATED events and set section colors based on the value of dataset keys.

see the attached code for more details

Code:
        colorMap.put("KEY-1", Color.decode("#ff6666"));        colorMap.put("KEY-2", Color.decode("#6666ff"));        colorMap.put("KEY-3", Color.decode("#66ff66"));            final PieDataset dataset = piePlot.getDataset();                        piePlot.addChangeListener(new PlotChangeListener() {                public void plotChanged(PlotChangeEvent event)                {                    if(event.getType() == ChartChangeEventType.DATASET_UPDATED) {                        //Assign color to each section of pie chart based on value of key                        for (int i = 0; i < dataset.getItemCount(); i++) {                            setSectionColor(piePlot, dataset.getKey(i), "KEY-1");                            setSectionColor(piePlot, dataset.getKey(i), "KEY-2");                            setSectionColor(piePlot, dataset.getKey(i), "KEY-3");                        }                    }                }            });     private void setSectionColor(PiePlot piePlot, Comparable key, String parterName)    {        if(key.toString().toLowerCase().contains(parterName)) {            piePlot.setSectionPaint(key, colorMap.get(parterName));        }              }   
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...