| 1 33 34 package com.icesoft.faces.component.outputchart; 35 36 import org.krysalis.jcharts.chartData.AxisChartDataSet; 37 import org.krysalis.jcharts.chartData.DataSeries; 38 import org.krysalis.jcharts.properties.AreaChartProperties; 39 import org.krysalis.jcharts.properties.AxisProperties; 40 import org.krysalis.jcharts.properties.BarChartProperties; 41 import org.krysalis.jcharts.properties.ChartProperties; 42 import org.krysalis.jcharts.properties.ChartTypeProperties; 43 import org.krysalis.jcharts.properties.ClusteredBarChartProperties; 44 import org.krysalis.jcharts.properties.LegendProperties; 45 import org.krysalis.jcharts.properties.LineChartProperties; 46 import org.krysalis.jcharts.properties.PointChartProperties; 47 import org.krysalis.jcharts.properties.StackedAreaChartProperties; 48 import org.krysalis.jcharts.properties.StackedBarChartProperties; 49 import org.krysalis.jcharts.test.TestDataGenerator; 50 import org.krysalis.jcharts.types.ChartType; 51 52 import javax.faces.component.UIComponent; 53 import java.awt.*; 54 55 public class AxisChart extends AbstractChart { 56 57 public AxisChart(UIComponent uiComponent) throws Throwable { 58 super(uiComponent); 59 } 60 61 protected void buildChart() throws Throwable { 62 if (data == null) { 63 getData(outputChart.getData()); 64 } 65 if (type.equalsIgnoreCase(OutputChart.AREA_CHART_TYPE)) { 66 buildAreaChart(); 67 } else if (type.equalsIgnoreCase(OutputChart.AREA_STACKED_CHART_TYPE)) { 68 buildAreaStackedChart(); 69 } else if (type.equalsIgnoreCase(OutputChart.BAR_CHART_TYPE)) { 70 buildBarChart(); 71 } else if (type.equalsIgnoreCase(OutputChart.BAR_STACKED_CHART_TYPE)) { 72 buildBarStackedChart(); 73 } else 74 if (type.equalsIgnoreCase(OutputChart.BAR_CLUSTERED_CHART_TYPE)) { 75 buildBarClusteredChart(); 76 } else if (type.equalsIgnoreCase(OutputChart.LINE_CHART_TYPE)) { 77 buildLineChart(); 78 } else if (type.equalsIgnoreCase(OutputChart.POINT_CHART_TYPE)) { 79 buildPointChart(); 80 } 81 } 82 83 private void buildAreaChart() throws Throwable { 84 AreaChartProperties areaChartProperties = new AreaChartProperties(); 85 buildAxisChart(ChartType.AREA, areaChartProperties); 86 } 87 88 private void buildAreaStackedChart() throws Throwable { 89 StackedAreaChartProperties areaChartProperties = 90 new StackedAreaChartProperties(); 91 buildAxisChart(ChartType.AREA_STACKED, areaChartProperties); 92 } 93 94 private void buildBarChart() throws Throwable { 95 BarChartProperties barChartProperties = new BarChartProperties(); 96 buildAxisChart(ChartType.BAR, barChartProperties); 97 } 98 99 private void buildBarStackedChart() throws Throwable { 100 StackedBarChartProperties barChartProperties = 101 new StackedBarChartProperties(); 102 buildAxisChart(ChartType.BAR_STACKED, barChartProperties); 103 } 104 105 private void buildBarClusteredChart() throws Throwable { 106 ClusteredBarChartProperties barChartProperties = 107 new ClusteredBarChartProperties(); 108 buildAxisChart(ChartType.BAR_CLUSTERED, barChartProperties); 109 } 110 111 private void buildLineChart() throws Throwable { 112 Stroke[] strokes = new Stroke[data.length]; 113 for (int i = 0; i < data.length; i++) { 114 strokes[i] = LineChartProperties.DEFAULT_LINE_STROKE; 115 } 116 LineChartProperties lineChartProperties = new LineChartProperties( 117 strokes, getShapes(outputChart.getShapes())); 118 buildAxisChart(ChartType.LINE, lineChartProperties); 119 } 120 121 private void buildPointChart() throws Throwable { 122 Paint[] outlinePaints = TestDataGenerator.getRandomPaints(data.length); 123 boolean[] fillPointFlags = new boolean[data.length]; 124 for (int i = 0; i < data.length; i++) { 125 fillPointFlags[i] = true; 126 } 127 PointChartProperties pointChartProperties = new PointChartProperties( 128 getShapes(outputChart.getShapes()), fillPointFlags, 129 outlinePaints); 130 buildAxisChart(ChartType.POINT, pointChartProperties); 131 } 132 133 void buildAxisChart(ChartType chartType, 134 ChartTypeProperties chartTypeProperties) 135 throws Throwable { 136 DataSeries dataSeries = new DataSeries( 137 getAsXaxisLabelsArray(outputChart.getXaxisLabels()), 138 outputChart.getXaxisTitle(), 139 outputChart.getYaxisTitle(), 140 outputChart.getChartTitle()); 141 142 AxisChartDataSet axisChartDataSet = new AxisChartDataSet( 143 getAs2dDoubleArray(outputChart.getData()), 144 getAsLabelsArray(outputChart.getLabels()), 145 getPaints(outputChart.getColors()), 146 chartType, 147 chartTypeProperties); 148 149 AxisProperties axisProperties = ((chartType.equals(ChartType.BAR) || 150 chartType.equals(ChartType.BAR_CLUSTERED)) && 151 outputChart.isHorizontal())? 152 new AxisProperties(true): new AxisProperties(); 153 154 dataSeries.addIAxisPlotDataSet(axisChartDataSet); 155 chart = new org.krysalis.jcharts.axisChart.AxisChart(dataSeries, 156 new ChartProperties(), 157 axisProperties, 158 getLegendProperties(), 159 new Integer ( 160 outputChart.getWidth()).intValue(), 161 new Integer ( 162 outputChart.getHeight()).intValue()); 163 } 164 165 private Shape[] shapes; 166 167 private Shape[] getShapes(Object obj) { 168 if (obj == null && shapes == null) { 169 return shapes = getGeneratedShapes(data.length); 170 } else if (obj == null && shapes != null) { 171 return shapes; 172 } else { 173 return shapes = getAsShapeArray(obj); 174 } 175 } 176 177 String [] xaxisLabels = null; 178 179 public String [] getAsXaxisLabelsArray(Object obj) { 180 if (obj == null && xaxisLabels == null) { 181 return xaxisLabels = getGeneratedLabels("Xlabel", data[0].length); 182 } else if (obj == null && xaxisLabels != null) { 183 return xaxisLabels; 184 } else { 185 return getAsStringArray(obj); 186 } 187 } 188 189 String [] labels = null; 190 191 public String [] getAsLabelsArray(Object obj) { 192 if (obj == null && labels == null) { 193 return labels = getGeneratedLabels("Label", data.length); 194 } else if (obj == null && labels != null) { 195 return labels; 196 } else { 197 return getAsStringArray(obj); 198 } 199 } 200 201 private double[][] data = null; 202 203 public double[][] getData(Object obj) { 204 if (obj instanceof String && data != null) { 205 return data; 206 } else { 207 return data = getAs2dDoubleArray(obj); 208 } 209 } 210 211 public Paint[] getPaints(Object obj) { 212 return getPaints(obj, data.length); 213 } 214 } 215 | Popular Tags |