create changelog entry
[debian/openrocket] / android-libraries / achartengine / src / org / achartengine / chart / ScatterChart.java
1 /**\r
2  * Copyright (C) 2009 - 2012 SC 4ViewSoft SRL\r
3  *  \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *  \r
8  *      http://www.apache.org/licenses/LICENSE-2.0\r
9  *  \r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 package org.achartengine.chart;\r
17 \r
18 import org.achartengine.model.XYMultipleSeriesDataset;\r
19 import org.achartengine.renderer.SimpleSeriesRenderer;\r
20 import org.achartengine.renderer.XYMultipleSeriesRenderer;\r
21 import org.achartengine.renderer.XYSeriesRenderer;\r
22 \r
23 import android.graphics.Canvas;\r
24 import android.graphics.Paint;\r
25 import android.graphics.Paint.Style;\r
26 import android.graphics.RectF;\r
27 \r
28 /**\r
29  * The scatter chart rendering class.\r
30  */\r
31 public class ScatterChart extends XYChart {\r
32   /** The constant to identify this chart type. */\r
33   public static final String TYPE = "Scatter";\r
34   /** The default point shape size. */\r
35   private static final float SIZE = 3;\r
36   /** The legend shape width. */\r
37   private static final int SHAPE_WIDTH = 10;\r
38   /** The point shape size. */\r
39   private float size = SIZE;\r
40 \r
41   ScatterChart() {\r
42   }\r
43 \r
44   /**\r
45    * Builds a new scatter chart instance.\r
46    * \r
47    * @param dataset the multiple series dataset\r
48    * @param renderer the multiple series renderer\r
49    */\r
50   public ScatterChart(XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer) {\r
51     super(dataset, renderer);\r
52     size = renderer.getPointSize();\r
53   }\r
54 \r
55   // TODO: javadoc\r
56   protected void setDatasetRenderer(XYMultipleSeriesDataset dataset,\r
57       XYMultipleSeriesRenderer renderer) {\r
58     super.setDatasetRenderer(dataset, renderer);\r
59     size = renderer.getPointSize();\r
60   }\r
61 \r
62   /**\r
63    * The graphical representation of a series.\r
64    * \r
65    * @param canvas the canvas to paint to\r
66    * @param paint the paint to be used for drawing\r
67    * @param points the array of points to be used for drawing the series\r
68    * @param seriesRenderer the series renderer\r
69    * @param yAxisValue the minimum value of the y axis\r
70    * @param seriesIndex the index of the series currently being drawn\r
71    * @param startIndex the start index of the rendering points\r
72    */\r
73   public void drawSeries(Canvas canvas, Paint paint, float[] points,\r
74       SimpleSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, int startIndex) {\r
75     XYSeriesRenderer renderer = (XYSeriesRenderer) seriesRenderer;\r
76     paint.setColor(renderer.getColor());\r
77     if (renderer.isFillPoints()) {\r
78       paint.setStyle(Style.FILL);\r
79     } else {\r
80       paint.setStyle(Style.STROKE);\r
81     }\r
82     int length = points.length;\r
83     switch (renderer.getPointStyle()) {\r
84     case X:\r
85       for (int i = 0; i < length; i += 2) {\r
86         drawX(canvas, paint, points[i], points[i + 1]);\r
87       }\r
88       break;\r
89     case CIRCLE:\r
90       for (int i = 0; i < length; i += 2) {\r
91         drawCircle(canvas, paint, points[i], points[i + 1]);\r
92       }\r
93       break;\r
94     case TRIANGLE:\r
95       float[] path = new float[6];\r
96       for (int i = 0; i < length; i += 2) {\r
97         drawTriangle(canvas, paint, path, points[i], points[i + 1]);\r
98       }\r
99       break;\r
100     case SQUARE:\r
101       for (int i = 0; i < length; i += 2) {\r
102         drawSquare(canvas, paint, points[i], points[i + 1]);\r
103       }\r
104       break;\r
105     case DIAMOND:\r
106       path = new float[8];\r
107       for (int i = 0; i < length; i += 2) {\r
108         drawDiamond(canvas, paint, path, points[i], points[i + 1]);\r
109       }\r
110       break;\r
111     case POINT:\r
112       canvas.drawPoints(points, paint);\r
113       break;\r
114     }\r
115   }\r
116 \r
117   @Override\r
118   protected ClickableArea[] clickableAreasForPoints(float[] points, double[] values,\r
119       float yAxisValue, int seriesIndex, int startIndex) {\r
120     int length = points.length;\r
121     ClickableArea[] ret = new ClickableArea[length / 2];\r
122     for (int i = 0; i < length; i += 2) {\r
123       int selectableBuffer = mRenderer.getSelectableBuffer();\r
124       ret[i / 2] = new ClickableArea(new RectF(points[i] - selectableBuffer, points[i + 1]\r
125           - selectableBuffer, points[i] + selectableBuffer, points[i + 1] + selectableBuffer),\r
126           values[i], values[i + 1]);\r
127     }\r
128     return ret;\r
129   }\r
130 \r
131   /**\r
132    * Returns the legend shape width.\r
133    * \r
134    * @param seriesIndex the series index\r
135    * @return the legend shape width\r
136    */\r
137   public int getLegendShapeWidth(int seriesIndex) {\r
138     return SHAPE_WIDTH;\r
139   }\r
140 \r
141   /**\r
142    * The graphical representation of the legend shape.\r
143    * \r
144    * @param canvas the canvas to paint to\r
145    * @param renderer the series renderer\r
146    * @param x the x value of the point the shape should be drawn at\r
147    * @param y the y value of the point the shape should be drawn at\r
148    * @param seriesIndex the series index\r
149    * @param paint the paint to be used for drawing\r
150    */\r
151   public void drawLegendShape(Canvas canvas, SimpleSeriesRenderer renderer, float x, float y,\r
152       int seriesIndex, Paint paint) {\r
153     if (((XYSeriesRenderer) renderer).isFillPoints()) {\r
154       paint.setStyle(Style.FILL);\r
155     } else {\r
156       paint.setStyle(Style.STROKE);\r
157     }\r
158     switch (((XYSeriesRenderer) renderer).getPointStyle()) {\r
159     case X:\r
160       drawX(canvas, paint, x + SHAPE_WIDTH, y);\r
161       break;\r
162     case CIRCLE:\r
163       drawCircle(canvas, paint, x + SHAPE_WIDTH, y);\r
164       break;\r
165     case TRIANGLE:\r
166       drawTriangle(canvas, paint, new float[6], x + SHAPE_WIDTH, y);\r
167       break;\r
168     case SQUARE:\r
169       drawSquare(canvas, paint, x + SHAPE_WIDTH, y);\r
170       break;\r
171     case DIAMOND:\r
172       drawDiamond(canvas, paint, new float[8], x + SHAPE_WIDTH, y);\r
173       break;\r
174     case POINT:\r
175       canvas.drawPoint(x + SHAPE_WIDTH, y, paint);\r
176       break;\r
177     }\r
178   }\r
179 \r
180   /**\r
181    * The graphical representation of an X point shape.\r
182    * \r
183    * @param canvas the canvas to paint to\r
184    * @param paint the paint to be used for drawing\r
185    * @param x the x value of the point the shape should be drawn at\r
186    * @param y the y value of the point the shape should be drawn at\r
187    */\r
188   private void drawX(Canvas canvas, Paint paint, float x, float y) {\r
189     canvas.drawLine(x - size, y - size, x + size, y + size, paint);\r
190     canvas.drawLine(x + size, y - size, x - size, y + size, paint);\r
191   }\r
192 \r
193   /**\r
194    * The graphical representation of a circle point shape.\r
195    * \r
196    * @param canvas the canvas to paint to\r
197    * @param paint the paint to be used for drawing\r
198    * @param x the x value of the point the shape should be drawn at\r
199    * @param y the y value of the point the shape should be drawn at\r
200    */\r
201   private void drawCircle(Canvas canvas, Paint paint, float x, float y) {\r
202     canvas.drawCircle(x, y, size, paint);\r
203   }\r
204 \r
205   /**\r
206    * The graphical representation of a triangle point shape.\r
207    * \r
208    * @param canvas the canvas to paint to\r
209    * @param paint the paint to be used for drawing\r
210    * @param path the triangle path\r
211    * @param x the x value of the point the shape should be drawn at\r
212    * @param y the y value of the point the shape should be drawn at\r
213    */\r
214   private void drawTriangle(Canvas canvas, Paint paint, float[] path, float x, float y) {\r
215     path[0] = x;\r
216     path[1] = y - size - size / 2;\r
217     path[2] = x - size;\r
218     path[3] = y + size;\r
219     path[4] = x + size;\r
220     path[5] = path[3];\r
221     drawPath(canvas, path, paint, true);\r
222   }\r
223 \r
224   /**\r
225    * The graphical representation of a square point shape.\r
226    * \r
227    * @param canvas the canvas to paint to\r
228    * @param paint the paint to be used for drawing\r
229    * @param x the x value of the point the shape should be drawn at\r
230    * @param y the y value of the point the shape should be drawn at\r
231    */\r
232   private void drawSquare(Canvas canvas, Paint paint, float x, float y) {\r
233     canvas.drawRect(x - size, y - size, x + size, y + size, paint);\r
234   }\r
235 \r
236   /**\r
237    * The graphical representation of a diamond point shape.\r
238    * \r
239    * @param canvas the canvas to paint to\r
240    * @param paint the paint to be used for drawing\r
241    * @param path the diamond path\r
242    * @param x the x value of the point the shape should be drawn at\r
243    * @param y the y value of the point the shape should be drawn at\r
244    */\r
245   private void drawDiamond(Canvas canvas, Paint paint, float[] path, float x, float y) {\r
246     path[0] = x;\r
247     path[1] = y - size;\r
248     path[2] = x - size;\r
249     path[3] = y;\r
250     path[4] = x;\r
251     path[5] = y + size;\r
252     path[6] = x + size;\r
253     path[7] = y;\r
254     drawPath(canvas, path, paint, true);\r
255   }\r
256 \r
257   /**\r
258    * Returns the chart type identifier.\r
259    * \r
260    * @return the chart type\r
261    */\r
262   public String getChartType() {\r
263     return TYPE;\r
264   }\r
265 \r
266 }