1 15 package org.apache.tapestry.workbench.chart; 16 17 import java.awt.Color ; 18 import java.awt.Paint ; 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 import org.apache.hivemind.HiveMind; 23 import org.apache.tapestry.IAsset; 24 import org.apache.tapestry.IRequestCycle; 25 import org.apache.tapestry.event.PageBeginRenderListener; 26 import org.apache.tapestry.event.PageEvent; 27 import org.apache.tapestry.html.BasePage; 28 import org.apache.tapestry.valid.IValidationDelegate; 29 import org.jCharts.Chart; 30 import org.jCharts.chartData.ChartDataException; 31 import org.jCharts.chartData.PieChartDataSet; 32 import org.jCharts.nonAxisChart.PieChart2D; 33 import org.jCharts.properties.ChartProperties; 34 import org.jCharts.properties.LegendProperties; 35 import org.jCharts.properties.PieChart2DProperties; 36 import org.jCharts.test.TestDataGenerator; 37 38 45 46 public abstract class ChartPage extends BasePage implements IChartProvider, PageBeginRenderListener 47 { 48 49 53 54 public void pageBeginRender(PageEvent event) 55 { 56 if (getPlotValues() == null) 57 { 58 List plotValues = new ArrayList (); 59 60 plotValues.add(new PlotValue("Fred", 10)); 61 plotValues.add(new PlotValue("Barney", 15)); 62 plotValues.add(new PlotValue("Dino", 7)); 63 64 setPlotValues(plotValues); 65 } 66 } 67 68 public abstract List getPlotValues(); 69 70 public abstract void setPlotValues(List plotValues); 71 72 public abstract PlotValue getPlotValue(); 73 74 public abstract List getRemoveValues(); 75 76 public abstract void setRemoveValues(List removeValues); 77 78 81 82 public boolean isMarkedForDeletion() 83 { 84 return false; 85 } 86 87 92 93 public void setMarkedForDeletion(boolean value) 94 { 95 if (value) 96 { 97 List removeValues = getRemoveValues(); 98 99 if (removeValues == null) 100 { 101 removeValues = new ArrayList (); 102 setRemoveValues(removeValues); 103 } 104 105 removeValues.add(getPlotValue()); 106 107 112 IValidationDelegate delegate = (IValidationDelegate) getBeans().getBean("delegate"); 113 114 delegate.clear(); 115 } 116 } 117 118 121 122 public void add() 123 { 124 List plotValues = getPlotValues(); 125 126 plotValues.add(new PlotValue()); 127 128 setPlotValues(plotValues); 129 } 130 131 136 137 public void delete() 138 { 139 List removeValues = getRemoveValues(); 140 141 if (removeValues != null) 142 { 143 List plotValues = getPlotValues(); 144 145 plotValues.removeAll(removeValues); 146 147 setPlotValues(plotValues); 148 } 149 } 150 151 private IAsset chartImageAsset; 152 153 public IAsset getChartImageAsset() 154 { 155 return new ChartAsset(getRequestCycle(), this); 156 } 157 158 163 164 public Chart getChart() 165 { 166 LegendProperties legendProperties = new LegendProperties(); 167 legendProperties.setNumColumns(2); 168 legendProperties.setPlacement(LegendProperties.RIGHT); 169 ChartProperties chartProperties = new ChartProperties(); 170 chartProperties.setBackgroundPaint(Color.decode("#ffffcc")); 171 172 Chart result = new PieChart2D(getData(), legendProperties, chartProperties, 400, 350); 173 174 return result; 175 } 176 177 private PieChartDataSet getData() 178 { 179 List plotValues = getPlotValues(); 180 int count = plotValues.size(); 181 double[] data = new double[count]; 182 String [] labels = new String [count]; 183 PieChart2DProperties properties = new PieChart2DProperties(); 184 185 for (int i = 0; i < count; i++) 186 { 187 PlotValue pv = (PlotValue) plotValues.get(i); 188 189 String name = pv.getName(); 190 191 if (HiveMind.isBlank(name)) 192 name = "<New>"; 193 194 data[i] = new Double (pv.getValue()).doubleValue(); 195 labels[i] = new String (name); 196 } 197 198 Paint [] paints = TestDataGenerator.getRandomPaints(count); 199 200 try 201 { 202 return new PieChartDataSet("Pie Chart", data, labels, paints, properties); 203 } 204 catch (ChartDataException e) 205 { 206 return null; 207 } 208 } 209 210 } | Popular Tags |