sgolestane Posted December 3, 2008 Share Posted December 3, 2008 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 More sharing options...
sgolestane Posted December 5, 2008 Author Share Posted December 5, 2008 wanted to mention that except not being able to customize colors of chart, everyhting else works fine. Link to comment Share on other sites More sharing options...
shrije Posted December 5, 2008 Share Posted December 5, 2008 Not sure if this is an issue but your jrxml shows no value in the dataset tag too. May it needs some dataset expression. Link to comment Share on other sites More sharing options...
sgolestane Posted December 5, 2008 Author Share Posted December 5, 2008 I'm using keyExpression, valueExpression and labelExpression to pass data to the chart and chart renders fine. Link to comment Share on other sites More sharing options...
sgolestane Posted December 12, 2008 Author Share Posted December 12, 2008 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 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