create changelog entry
[debian/openrocket] / android-libraries / achartengine / src / org / achartengine / chart / BubbleChart.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.model.XYValueSeries;\r
20 import org.achartengine.renderer.SimpleSeriesRenderer;\r
21 import org.achartengine.renderer.XYMultipleSeriesRenderer;\r
22 import org.achartengine.renderer.XYSeriesRenderer;\r
23 \r
24 import android.graphics.Canvas;\r
25 import android.graphics.Paint;\r
26 import android.graphics.Paint.Style;\r
27 import android.graphics.RectF;\r
28 \r
29 /**\r
30  * The bubble chart rendering class.\r
31  */\r
32 public class BubbleChart extends XYChart {\r
33   /** The constant to identify this chart type. */\r
34   public static final String TYPE = "Bubble";\r
35   /** The legend shape width. */\r
36   private static final int SHAPE_WIDTH = 10;\r
37   /** The minimum bubble size. */\r
38   private static final int MIN_BUBBLE_SIZE = 2;\r
39   /** The maximum bubble size. */\r
40   private static final int MAX_BUBBLE_SIZE = 20;\r
41 \r
42   BubbleChart() {\r
43   }\r
44 \r
45   /**\r
46    * Builds a new bubble chart instance.\r
47    * \r
48    * @param dataset the multiple series dataset\r
49    * @param renderer the multiple series renderer\r
50    */\r
51   public BubbleChart(XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer) {\r
52     super(dataset, renderer);\r
53   }\r
54 \r
55   /**\r
56    * The graphical representation of a series.\r
57    * \r
58    * @param canvas the canvas to paint to\r
59    * @param paint the paint to be used for drawing\r
60    * @param points the array of points to be used for drawing the series\r
61    * @param seriesRenderer the series renderer\r
62    * @param yAxisValue the minimum value of the y axis\r
63    * @param seriesIndex the index of the series currently being drawn\r
64    * @param startIndex the start index of the rendering points\r
65    */\r
66   public void drawSeries(Canvas canvas, Paint paint, float[] points,\r
67       SimpleSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, int startIndex) {\r
68     XYSeriesRenderer renderer = (XYSeriesRenderer) seriesRenderer;\r
69     paint.setColor(renderer.getColor());\r
70     paint.setStyle(Style.FILL);\r
71     int length = points.length;\r
72     XYValueSeries series = (XYValueSeries) mDataset.getSeriesAt(seriesIndex);\r
73     double max = series.getMaxValue();\r
74     double coef = MAX_BUBBLE_SIZE / max;\r
75     for (int i = 0; i < length; i += 2) {\r
76       double size = series.getValue(startIndex + i / 2) * coef + MIN_BUBBLE_SIZE;\r
77       drawCircle(canvas, paint, points[i], points[i + 1], (float) size);\r
78     }\r
79   }\r
80 \r
81   @Override\r
82   protected ClickableArea[] clickableAreasForPoints(float[] points, double[] values,\r
83       float yAxisValue, int seriesIndex, int startIndex) {\r
84     int length = points.length;\r
85     XYValueSeries series = (XYValueSeries) mDataset.getSeriesAt(seriesIndex);\r
86     double max = series.getMaxValue();\r
87     double coef = MAX_BUBBLE_SIZE / max;\r
88     ClickableArea[] ret = new ClickableArea[length / 2];\r
89     for (int i = 0; i < length; i += 2) {\r
90       double size = series.getValue(startIndex + i / 2) * coef + MIN_BUBBLE_SIZE;\r
91       ret[i / 2] = new ClickableArea(new RectF(points[i] - (float) size, points[i + 1]\r
92           - (float) size, points[i] + (float) size, points[i + 1] + (float) size), values[i],\r
93           values[i + 1]);\r
94     }\r
95     return ret;\r
96   }\r
97 \r
98   /**\r
99    * Returns the legend shape width.\r
100    * \r
101    * @param seriesIndex the series index\r
102    * @return the legend shape width\r
103    */\r
104   public int getLegendShapeWidth(int seriesIndex) {\r
105     return SHAPE_WIDTH;\r
106   }\r
107 \r
108   /**\r
109    * The graphical representation of the legend shape.\r
110    * \r
111    * @param canvas the canvas to paint to\r
112    * @param renderer the series renderer\r
113    * @param x the x value of the point the shape should be drawn at\r
114    * @param y the y value of the point the shape should be drawn at\r
115    * @param seriesIndex the series index\r
116    * @param paint the paint to be used for drawing\r
117    */\r
118   public void drawLegendShape(Canvas canvas, SimpleSeriesRenderer renderer, float x, float y,\r
119       int seriesIndex, Paint paint) {\r
120     paint.setStyle(Style.FILL);\r
121     drawCircle(canvas, paint, x + SHAPE_WIDTH, y, 3);\r
122   }\r
123 \r
124   /**\r
125    * The graphical representation of a circle point shape.\r
126    * \r
127    * @param canvas the canvas to paint to\r
128    * @param paint the paint to be used for drawing\r
129    * @param x the x value of the point the shape should be drawn at\r
130    * @param y the y value of the point the shape should be drawn at\r
131    * @param radius the bubble radius\r
132    */\r
133   private void drawCircle(Canvas canvas, Paint paint, float x, float y, float radius) {\r
134     canvas.drawCircle(x, y, radius, paint);\r
135   }\r
136 \r
137   /**\r
138    * Returns the chart type identifier.\r
139    * \r
140    * @return the chart type\r
141    */\r
142   public String getChartType() {\r
143     return TYPE;\r
144   }\r
145 \r
146 }