KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > jcharts > demo > simpleservlet > DualYAxis


1
2 /***********************************************************************************************
3  * File Info: $Id: DualYAxis.java,v 1.2 2003/08/28 01:18:02 nathaniel_auvil Exp $
4  * Copyright (C) 2002
5  * Author: Nathaniel G. Auvil
6  * Contributor(s):
7  *
8  * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
9  *
10  * Redistribution and use of this software and associated documentation ("Software"), with or
11  * without modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain copyright statements and notices.
14  * Redistributions must also contain a copy of this document.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
17  * conditions and the following disclaimer in the documentation and/or other materials
18  * provided with the distribution.
19  *
20  * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to endorse or promote
21  * products derived from this Software without prior written permission of Nathaniel G.
22  * Auvil. For written permission, please contact nathaniel_auvil@users.sourceforge.net
23  *
24  * 4. Products derived from this Software may not be called "jCharts" nor may "jCharts" appear
25  * in their names without prior written permission of Nathaniel G. Auvil. jCharts is a
26  * registered trademark of Nathaniel G. Auvil.
27  *
28  * 5. Due credit should be given to the jCharts Project (http://jcharts.sourceforge.net/).
29  *
30  * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS ``AS IS'' AND ANY
31  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
32  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
33  * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
34  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
38  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
39  ************************************************************************************************/

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 JavaDoc;
60 import javax.servlet.http.HttpServletRequest JavaDoc;
61 import javax.servlet.http.HttpServletResponse JavaDoc;
62 import java.awt.*;
63 import java.io.IOException JavaDoc;
64
65 // Dual Y axis changes courtesy of St�phane NGUYEN integrated by CMC 25Aug03
66
// French translation courtesy of my good wife Jacki ...
67
public class DualYAxis extends ChartServlet {
68
69
70     //---all of the charts will have some properties.
71
private LineChartProperties lineChartProperties;
72     private ClusteredBarChartProperties clusteredBarChartProperties;
73
74     protected LegendProperties legendProperties;
75     protected ChartProperties chartProperties;
76     protected AxisProperties axisProperties;
77
78     //---all of the charts have the same size
79
protected int width = 750;
80     protected int height = 430;
81
82
83   public void init() {
84
85
86     //---------------------Font & Police----------------------------------------
87

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     //On fixe la marge avec le cadre
104
// Set the margin within the frame
105
chartProperties.setEdgePadding(2f);
106
107     //On met le graphe sur un fond gris clair
108
// Set the chart against a plain grey background
109
axisProperties.setBackgroundPaint(new Color (245, 245, 245));
110
111     //Param�trage de la bo�te de l�gende
112
//Parameters for the legend box
113
//legendProperties.setPlacement(LegendAreaProperties.RIGHT);
114
//legendProperties.setNumColumns(1);
115
//legendProperties.setFont( new Font("Arial Narrow", Font.PLAIN, 13) );
116
//legendProperties.setFontPaint(Color.black);
117
ChartFont legendFont = new ChartFont(new Font("Arial Narrow", Font.PLAIN, 13), Color.black);
118     legendProperties.setChartFont(legendFont);
119     legendProperties.setChartPadding(10);
120
121     //Arrondi � la puissance 10
122
//Round up to the power of 10
123
DataAxisProperties dataAxisProperties= (DataAxisProperties) axisProperties.getYAxisProperties();
124     dataAxisProperties.setRoundToNearest(1);
125
126     //Parametrage du nombre de graduation sur l'axe des ordonn�es
127
//Set the number of ticks(?) for the ordinal axis
128
dataAxisProperties.setNumItems(6);
129
130     //utilisation de la virgule s'il y a lieu (normalement non)
131
// Set the use of the comma if there are any (usually not)
132
dataAxisProperties.setUseCommas(false);
133
134     //Ne pas afficher les bordures sup�rieur et droite du chart
135
// Don't attach upper borders and the right of the chart
136
axisProperties.getYAxisProperties().setShowEndBorder(false);
137     axisProperties.getXAxisProperties().setShowEndBorder(false);
138
139
140     Stroke[] strokes = {LineChartProperties.DEFAULT_LINE_STROKE};
141     //Shape[] shapes = {null};
142
Shape[] shapes= { PointChartProperties.SHAPE_TRIANGLE };
143     lineChartProperties = new LineChartProperties(strokes, shapes);
144
145     clusteredBarChartProperties = new ClusteredBarChartProperties();
146
147     //Param�trage de la largeur des barres de l'histogramme et non affichage des contours
148
//Set the width of the bars and don't show the outline
149
clusteredBarChartProperties.setWidthPercentage( 0.70f );
150     clusteredBarChartProperties.setShowOutlinesFlag( false );
151   }
152
153     /**********************************************************************************************
154      *
155      **********************************************************************************************/

156   public void service( HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc httpServletResponse ) throws ServletException JavaDoc, IOException JavaDoc
157   {
158     //---Some charts will need a second scale to render some values and then will
159
//---need to render an axis at the right of the charts with it
160
long secondScale=1;
161
162     //---specific variable use to render negative value in all case
163
double maxDataClustAbs = 0;
164     double maxDataLineAbs = 0;
165     int numberOfNegativ=0;
166     int numberOfZero=0;
167
168     try {
169
170       String JavaDoc[] 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 JavaDoc title = "Chart Title";
182       String JavaDoc legendLabel1 = "Legend Label 1";
183       String JavaDoc legendLabel2 = "Legend Label 2";
184       String JavaDoc legendLabel3 = "Legend Label 3";
185
186       //IAxisDataSeries dataSeries = new DataSeries( xAxisLabels, null, null, title );
187
IAxisDataSeries dataSeries = new DataSeries( xAxisLabels, "X Axis title", "Left Y Axis title", title );
188
189       String JavaDoc[] legendLabelsClust = {legendLabel1, legendLabel2};
190       Paint[] clustPaints = new Paint[]{Color.pink, Color.blue};
191
192       String JavaDoc[] legendLabelsLine = {legendLabel3};
193       Paint[] linePaints = new Paint[]{Color.green};
194
195       //------------On r�cup�re une 2�me �chelle "secondScale"------------------
196
//------------et on fait un traitement pour les nombres n�gatifs----------
197
//------------We get back the 2nd scale "secondScale"---------------------
198
//-------------and we handle the negative numbers---------------------
199
//On identifie la valeur maximum absolue des data Clustered Bar
200
//et on compte le nombre de z�ro et le nombre de chiffres n�gatifs
201
// We identify the absolute maximum value of the data in the Clustered Bar
202
// and we count the number of zero and negative digits
203
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       //On identifie la valeur maximum absolue des data Line
219
//et on compte le nombre de z�ro et le nombre de chiffres n�gatifs
220
// We identify the absolute maximum value of the data lines
221
// and we count the number of zero and negative digits
222
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       //On d�finit la valeur de la seconde �chelle si les ordres de grandeurs
235
//sont tr�s diff�rent (de l'ordre de 10).
236
//We define the value of the second scale if the increment is an order of
237
//magnitude greater
238
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       //On v�rifie s'il y a des valeurs n�gatives
247
// Check if there are negative numbers
248
if (numberOfNegativ>0) {
249         if ((maxDataLineAbs*secondScale)>maxDataClustAbs) {
250           axisProperties.getYAxisProperties().setMinRightAxis((double) (-maxDataLineAbs*secondScale));
251           //Si les valeurs ne sont pas toutes n�gatives on affiche l'axe des ordonn�es
252
//de mani�re a ce que le z�ro soit centr�
253
// If the numbers are not all negative we center the ordinal axis around zero
254
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 JavaDoc throwable ) {
272       //HACK do your error handling here...
273
throwable.printStackTrace();
274     }
275   }
276
277 }
Popular Tags