2 * Copyright (C) 2009 - 2012 SC 4ViewSoft SRL
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
16 package org.achartengine.chart;
\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
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
29 * The scatter chart rendering class.
\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
45 * Builds a new scatter chart instance.
\r
47 * @param dataset the multiple series dataset
\r
48 * @param renderer the multiple series renderer
\r
50 public ScatterChart(XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer) {
\r
51 super(dataset, renderer);
\r
52 size = renderer.getPointSize();
\r
56 protected void setDatasetRenderer(XYMultipleSeriesDataset dataset,
\r
57 XYMultipleSeriesRenderer renderer) {
\r
58 super.setDatasetRenderer(dataset, renderer);
\r
59 size = renderer.getPointSize();
\r
63 * The graphical representation of a series.
\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
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
80 paint.setStyle(Style.STROKE);
\r
82 int length = points.length;
\r
83 switch (renderer.getPointStyle()) {
\r
85 for (int i = 0; i < length; i += 2) {
\r
86 drawX(canvas, paint, points[i], points[i + 1]);
\r
90 for (int i = 0; i < length; i += 2) {
\r
91 drawCircle(canvas, paint, points[i], points[i + 1]);
\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
101 for (int i = 0; i < length; i += 2) {
\r
102 drawSquare(canvas, paint, points[i], points[i + 1]);
\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
112 canvas.drawPoints(points, paint);
\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
132 * Returns the legend shape width.
\r
134 * @param seriesIndex the series index
\r
135 * @return the legend shape width
\r
137 public int getLegendShapeWidth(int seriesIndex) {
\r
138 return SHAPE_WIDTH;
\r
142 * The graphical representation of the legend shape.
\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
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
156 paint.setStyle(Style.STROKE);
\r
158 switch (((XYSeriesRenderer) renderer).getPointStyle()) {
\r
160 drawX(canvas, paint, x + SHAPE_WIDTH, y);
\r
163 drawCircle(canvas, paint, x + SHAPE_WIDTH, y);
\r
166 drawTriangle(canvas, paint, new float[6], x + SHAPE_WIDTH, y);
\r
169 drawSquare(canvas, paint, x + SHAPE_WIDTH, y);
\r
172 drawDiamond(canvas, paint, new float[8], x + SHAPE_WIDTH, y);
\r
175 canvas.drawPoint(x + SHAPE_WIDTH, y, paint);
\r
181 * The graphical representation of an X point shape.
\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
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
194 * The graphical representation of a circle point shape.
\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
201 private void drawCircle(Canvas canvas, Paint paint, float x, float y) {
\r
202 canvas.drawCircle(x, y, size, paint);
\r
206 * The graphical representation of a triangle point shape.
\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
214 private void drawTriangle(Canvas canvas, Paint paint, float[] path, float x, float y) {
\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
221 drawPath(canvas, path, paint, true);
\r
225 * The graphical representation of a square point shape.
\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
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
237 * The graphical representation of a diamond point shape.
\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
245 private void drawDiamond(Canvas canvas, Paint paint, float[] path, float x, float y) {
\r
247 path[1] = y - size;
\r
248 path[2] = x - size;
\r
251 path[5] = y + size;
\r
252 path[6] = x + size;
\r
254 drawPath(canvas, path, paint, true);
\r
258 * Returns the chart type identifier.
\r
260 * @return the chart type
\r
262 public String getChartType() {
\r