1 2 40 41 package org.krysalis.jcharts.demo.simpleservlet; 42 43 44 import org.krysalis.jcharts.axisChart.AxisChart; 45 import org.krysalis.jcharts.chartData.AxisChartDataSet; 46 import org.krysalis.jcharts.chartData.DataSeries; 47 import org.krysalis.jcharts.chartData.interfaces.IAxisDataSeries; 48 import org.krysalis.jcharts.encoders.ServletEncoderHelper; 49 import org.krysalis.jcharts.properties.AxisProperties; 50 import org.krysalis.jcharts.properties.ChartProperties; 51 import org.krysalis.jcharts.properties.ClusteredBarChartProperties; 52 import org.krysalis.jcharts.properties.DataAxisProperties; 53 import org.krysalis.jcharts.properties.LegendProperties; 54 import org.krysalis.jcharts.properties.LineChartProperties; 55 import org.krysalis.jcharts.properties.PointChartProperties; 56 import org.krysalis.jcharts.properties.util.ChartFont; 57 import org.krysalis.jcharts.types.ChartType; 58 59 import javax.servlet.ServletException ; 60 import javax.servlet.http.HttpServletRequest ; 61 import javax.servlet.http.HttpServletResponse ; 62 import java.awt.*; 63 import java.io.IOException ; 64 65 public class DualYAxis extends ChartServlet { 68 69 70 private LineChartProperties lineChartProperties; 72 private ClusteredBarChartProperties clusteredBarChartProperties; 73 74 protected LegendProperties legendProperties; 75 protected ChartProperties chartProperties; 76 protected AxisProperties axisProperties; 77 78 protected int width = 750; 80 protected int height = 430; 81 82 83 public void init() { 84 85 86 88 legendProperties = new LegendProperties(); 89 chartProperties = new ChartProperties(); 90 axisProperties = new AxisProperties(false); 91 92 ChartFont axisScaleFont = new ChartFont(new Font("Arial Narrow", Font.PLAIN, 10), Color.black); 93 axisProperties.getXAxisProperties().setScaleChartFont(axisScaleFont); 94 axisProperties.getYAxisProperties().setScaleChartFont(axisScaleFont); 95 96 ChartFont axisTitleFont = new ChartFont(new Font("Arial Narrow", Font.PLAIN, 13), Color.black); 97 axisProperties.getXAxisProperties().setTitleChartFont(axisTitleFont); 98 axisProperties.getYAxisProperties().setTitleChartFont(axisTitleFont); 99 100 ChartFont titleFont = new ChartFont(new Font("Georgia Negreta cursiva", Font.PLAIN, 15), Color.black); 101 chartProperties.setTitleFont(titleFont); 102 103 chartProperties.setEdgePadding(2f); 106 107 axisProperties.setBackgroundPaint(new Color (245, 245, 245)); 110 111 ChartFont legendFont = new ChartFont(new Font("Arial Narrow", Font.PLAIN, 13), Color.black); 118 legendProperties.setChartFont(legendFont); 119 legendProperties.setChartPadding(10); 120 121 DataAxisProperties dataAxisProperties= (DataAxisProperties) axisProperties.getYAxisProperties(); 124 dataAxisProperties.setRoundToNearest(1); 125 126 dataAxisProperties.setNumItems(6); 129 130 dataAxisProperties.setUseCommas(false); 133 134 axisProperties.getYAxisProperties().setShowEndBorder(false); 137 axisProperties.getXAxisProperties().setShowEndBorder(false); 138 139 140 Stroke[] strokes = {LineChartProperties.DEFAULT_LINE_STROKE}; 141 Shape[] shapes= { PointChartProperties.SHAPE_TRIANGLE }; 143 lineChartProperties = new LineChartProperties(strokes, shapes); 144 145 clusteredBarChartProperties = new ClusteredBarChartProperties(); 146 147 clusteredBarChartProperties.setWidthPercentage( 0.70f ); 150 clusteredBarChartProperties.setShowOutlinesFlag( false ); 151 } 152 153 156 public void service( HttpServletRequest req, HttpServletResponse httpServletResponse ) throws ServletException , IOException 157 { 158 long secondScale=1; 161 162 double maxDataClustAbs = 0; 164 double maxDataLineAbs = 0; 165 int numberOfNegativ=0; 166 int numberOfZero=0; 167 168 try { 169 170 String [] xAxisLabels = {"MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC", "JAN", "FEB"}; 171 172 173 double[][] dataClust = new double[][]{ 174 {3533, 101265, 76076, 86664, 135567, 87729, 85309, 143423, 18732, 111842, 39534, 11980} 175 , {0, 103400, 65000, 88500, 143000, 80000, 88500, 143500, 99000, 118500, 38500, 18000} 176 }; 177 double[][] dataLine = new double[][]{ 178 {21, 4, 1, 2, 3, 2, 3, 2, 93, 18, 54, 0} 179 }; 180 181 String title = "Chart Title"; 182 String legendLabel1 = "Legend Label 1"; 183 String legendLabel2 = "Legend Label 2"; 184 String legendLabel3 = "Legend Label 3"; 185 186 IAxisDataSeries dataSeries = new DataSeries( xAxisLabels, "X Axis title", "Left Y Axis title", title ); 188 189 String [] legendLabelsClust = {legendLabel1, legendLabel2}; 190 Paint[] clustPaints = new Paint[]{Color.pink, Color.blue}; 191 192 String [] legendLabelsLine = {legendLabel3}; 193 Paint[] linePaints = new Paint[]{Color.green}; 194 195 for (int k=0; k<2; k++) { 204 205 for (int j=0; j<xAxisLabels.length; j++) { 206 if (maxDataClustAbs < (Math.abs(dataClust[k][j])) && (dataClust[k][j]!=0) ) { 207 maxDataClustAbs=(Math.abs(dataClust[k][j])); 208 } 209 if (dataClust[k][j]<0) { 210 numberOfNegativ+=1; 211 } 212 if (dataClust[k][j]==0) { 213 numberOfZero+=1; 214 } 215 } 216 } 217 218 for (int j=0; j<xAxisLabels.length; j++) { 223 if (maxDataLineAbs < (Math.abs(dataLine[0][j])) && (dataLine[0][j]!=0) ) { 224 maxDataLineAbs=(Math.abs(dataLine[0][j])); 225 } 226 if (dataLine[0][j]<0) { 227 numberOfNegativ+=1; 228 } 229 if (dataLine[0][j]==0) { 230 numberOfZero+=1; 231 } 232 } 233 234 if (maxDataClustAbs>10*maxDataLineAbs) { 239 axisProperties.getYAxisProperties().setShowRightAxis(true); 240 secondScale=Math.round(maxDataClustAbs/(maxDataLineAbs)); 241 axisProperties.getYAxisProperties().setSecondScaleRight((float) (secondScale) ); 242 ChartFont axisScaleFontRight = new ChartFont( new Font("Arial Narrow", Font.PLAIN, 10), Color.green.darker() ); 243 axisProperties.getYAxisProperties().setScaleChartFontRight( axisScaleFontRight ); 244 } 245 246 if (numberOfNegativ>0) { 249 if ((maxDataLineAbs*secondScale)>maxDataClustAbs) { 250 axisProperties.getYAxisProperties().setMinRightAxis((double) (-maxDataLineAbs*secondScale)); 251 if (numberOfNegativ!=(3*xAxisLabels.length-numberOfZero)) { 255 axisProperties.getYAxisProperties().setMaxRightAxis((double) (maxDataLineAbs*secondScale)); 256 } 257 } else { 258 axisProperties.getYAxisProperties().setMinRightAxis((double) (-maxDataClustAbs)); 259 if (numberOfNegativ!=(3*xAxisLabels.length-numberOfZero)) { 260 axisProperties.getYAxisProperties().setMaxRightAxis((double) (maxDataClustAbs)); 261 } 262 } 263 } 264 265 dataSeries.addIAxisPlotDataSet( new AxisChartDataSet( dataClust, legendLabelsClust, clustPaints, ChartType.BAR_CLUSTERED, clusteredBarChartProperties ) ); 266 dataSeries.addIAxisPlotDataSet( new AxisChartDataSet( dataLine, legendLabelsLine, linePaints, ChartType.LINE, lineChartProperties ) ); 267 AxisChart axisChart = new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, width, height ); 268 269 ServletEncoderHelper.encodeJPEG13( axisChart, 1.0f, httpServletResponse ); 270 } 271 catch( Throwable throwable ) { 272 throwable.printStackTrace(); 274 } 275 } 276 277 } | Popular Tags |