+/**\r
+ * Copyright (C) 2009 - 2012 SC 4ViewSoft SRL\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ * \r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.achartengine.chart;\r
+\r
+import org.achartengine.model.XYMultipleSeriesDataset;\r
+import org.achartengine.renderer.SimpleSeriesRenderer;\r
+import org.achartengine.renderer.XYMultipleSeriesRenderer;\r
+import org.achartengine.renderer.XYSeriesRenderer;\r
+\r
+import android.graphics.Canvas;\r
+import android.graphics.Paint;\r
+import android.graphics.Paint.Style;\r
+import android.graphics.RectF;\r
+\r
+/**\r
+ * The scatter chart rendering class.\r
+ */\r
+public class ScatterChart extends XYChart {\r
+ /** The constant to identify this chart type. */\r
+ public static final String TYPE = "Scatter";\r
+ /** The default point shape size. */\r
+ private static final float SIZE = 3;\r
+ /** The legend shape width. */\r
+ private static final int SHAPE_WIDTH = 10;\r
+ /** The point shape size. */\r
+ private float size = SIZE;\r
+\r
+ ScatterChart() {\r
+ }\r
+\r
+ /**\r
+ * Builds a new scatter chart instance.\r
+ * \r
+ * @param dataset the multiple series dataset\r
+ * @param renderer the multiple series renderer\r
+ */\r
+ public ScatterChart(XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer) {\r
+ super(dataset, renderer);\r
+ size = renderer.getPointSize();\r
+ }\r
+\r
+ // TODO: javadoc\r
+ protected void setDatasetRenderer(XYMultipleSeriesDataset dataset,\r
+ XYMultipleSeriesRenderer renderer) {\r
+ super.setDatasetRenderer(dataset, renderer);\r
+ size = renderer.getPointSize();\r
+ }\r
+\r
+ /**\r
+ * The graphical representation of a series.\r
+ * \r
+ * @param canvas the canvas to paint to\r
+ * @param paint the paint to be used for drawing\r
+ * @param points the array of points to be used for drawing the series\r
+ * @param seriesRenderer the series renderer\r
+ * @param yAxisValue the minimum value of the y axis\r
+ * @param seriesIndex the index of the series currently being drawn\r
+ * @param startIndex the start index of the rendering points\r
+ */\r
+ public void drawSeries(Canvas canvas, Paint paint, float[] points,\r
+ SimpleSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, int startIndex) {\r
+ XYSeriesRenderer renderer = (XYSeriesRenderer) seriesRenderer;\r
+ paint.setColor(renderer.getColor());\r
+ if (renderer.isFillPoints()) {\r
+ paint.setStyle(Style.FILL);\r
+ } else {\r
+ paint.setStyle(Style.STROKE);\r
+ }\r
+ int length = points.length;\r
+ switch (renderer.getPointStyle()) {\r
+ case X:\r
+ for (int i = 0; i < length; i += 2) {\r
+ drawX(canvas, paint, points[i], points[i + 1]);\r
+ }\r
+ break;\r
+ case CIRCLE:\r
+ for (int i = 0; i < length; i += 2) {\r
+ drawCircle(canvas, paint, points[i], points[i + 1]);\r
+ }\r
+ break;\r
+ case TRIANGLE:\r
+ float[] path = new float[6];\r
+ for (int i = 0; i < length; i += 2) {\r
+ drawTriangle(canvas, paint, path, points[i], points[i + 1]);\r
+ }\r
+ break;\r
+ case SQUARE:\r
+ for (int i = 0; i < length; i += 2) {\r
+ drawSquare(canvas, paint, points[i], points[i + 1]);\r
+ }\r
+ break;\r
+ case DIAMOND:\r
+ path = new float[8];\r
+ for (int i = 0; i < length; i += 2) {\r
+ drawDiamond(canvas, paint, path, points[i], points[i + 1]);\r
+ }\r
+ break;\r
+ case POINT:\r
+ canvas.drawPoints(points, paint);\r
+ break;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ protected ClickableArea[] clickableAreasForPoints(float[] points, double[] values,\r
+ float yAxisValue, int seriesIndex, int startIndex) {\r
+ int length = points.length;\r
+ ClickableArea[] ret = new ClickableArea[length / 2];\r
+ for (int i = 0; i < length; i += 2) {\r
+ int selectableBuffer = mRenderer.getSelectableBuffer();\r
+ ret[i / 2] = new ClickableArea(new RectF(points[i] - selectableBuffer, points[i + 1]\r
+ - selectableBuffer, points[i] + selectableBuffer, points[i + 1] + selectableBuffer),\r
+ values[i], values[i + 1]);\r
+ }\r
+ return ret;\r
+ }\r
+\r
+ /**\r
+ * Returns the legend shape width.\r
+ * \r
+ * @param seriesIndex the series index\r
+ * @return the legend shape width\r
+ */\r
+ public int getLegendShapeWidth(int seriesIndex) {\r
+ return SHAPE_WIDTH;\r
+ }\r
+\r
+ /**\r
+ * The graphical representation of the legend shape.\r
+ * \r
+ * @param canvas the canvas to paint to\r
+ * @param renderer the series renderer\r
+ * @param x the x value of the point the shape should be drawn at\r
+ * @param y the y value of the point the shape should be drawn at\r
+ * @param seriesIndex the series index\r
+ * @param paint the paint to be used for drawing\r
+ */\r
+ public void drawLegendShape(Canvas canvas, SimpleSeriesRenderer renderer, float x, float y,\r
+ int seriesIndex, Paint paint) {\r
+ if (((XYSeriesRenderer) renderer).isFillPoints()) {\r
+ paint.setStyle(Style.FILL);\r
+ } else {\r
+ paint.setStyle(Style.STROKE);\r
+ }\r
+ switch (((XYSeriesRenderer) renderer).getPointStyle()) {\r
+ case X:\r
+ drawX(canvas, paint, x + SHAPE_WIDTH, y);\r
+ break;\r
+ case CIRCLE:\r
+ drawCircle(canvas, paint, x + SHAPE_WIDTH, y);\r
+ break;\r
+ case TRIANGLE:\r
+ drawTriangle(canvas, paint, new float[6], x + SHAPE_WIDTH, y);\r
+ break;\r
+ case SQUARE:\r
+ drawSquare(canvas, paint, x + SHAPE_WIDTH, y);\r
+ break;\r
+ case DIAMOND:\r
+ drawDiamond(canvas, paint, new float[8], x + SHAPE_WIDTH, y);\r
+ break;\r
+ case POINT:\r
+ canvas.drawPoint(x + SHAPE_WIDTH, y, paint);\r
+ break;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * The graphical representation of an X point shape.\r
+ * \r
+ * @param canvas the canvas to paint to\r
+ * @param paint the paint to be used for drawing\r
+ * @param x the x value of the point the shape should be drawn at\r
+ * @param y the y value of the point the shape should be drawn at\r
+ */\r
+ private void drawX(Canvas canvas, Paint paint, float x, float y) {\r
+ canvas.drawLine(x - size, y - size, x + size, y + size, paint);\r
+ canvas.drawLine(x + size, y - size, x - size, y + size, paint);\r
+ }\r
+\r
+ /**\r
+ * The graphical representation of a circle point shape.\r
+ * \r
+ * @param canvas the canvas to paint to\r
+ * @param paint the paint to be used for drawing\r
+ * @param x the x value of the point the shape should be drawn at\r
+ * @param y the y value of the point the shape should be drawn at\r
+ */\r
+ private void drawCircle(Canvas canvas, Paint paint, float x, float y) {\r
+ canvas.drawCircle(x, y, size, paint);\r
+ }\r
+\r
+ /**\r
+ * The graphical representation of a triangle point shape.\r
+ * \r
+ * @param canvas the canvas to paint to\r
+ * @param paint the paint to be used for drawing\r
+ * @param path the triangle path\r
+ * @param x the x value of the point the shape should be drawn at\r
+ * @param y the y value of the point the shape should be drawn at\r
+ */\r
+ private void drawTriangle(Canvas canvas, Paint paint, float[] path, float x, float y) {\r
+ path[0] = x;\r
+ path[1] = y - size - size / 2;\r
+ path[2] = x - size;\r
+ path[3] = y + size;\r
+ path[4] = x + size;\r
+ path[5] = path[3];\r
+ drawPath(canvas, path, paint, true);\r
+ }\r
+\r
+ /**\r
+ * The graphical representation of a square point shape.\r
+ * \r
+ * @param canvas the canvas to paint to\r
+ * @param paint the paint to be used for drawing\r
+ * @param x the x value of the point the shape should be drawn at\r
+ * @param y the y value of the point the shape should be drawn at\r
+ */\r
+ private void drawSquare(Canvas canvas, Paint paint, float x, float y) {\r
+ canvas.drawRect(x - size, y - size, x + size, y + size, paint);\r
+ }\r
+\r
+ /**\r
+ * The graphical representation of a diamond point shape.\r
+ * \r
+ * @param canvas the canvas to paint to\r
+ * @param paint the paint to be used for drawing\r
+ * @param path the diamond path\r
+ * @param x the x value of the point the shape should be drawn at\r
+ * @param y the y value of the point the shape should be drawn at\r
+ */\r
+ private void drawDiamond(Canvas canvas, Paint paint, float[] path, float x, float y) {\r
+ path[0] = x;\r
+ path[1] = y - size;\r
+ path[2] = x - size;\r
+ path[3] = y;\r
+ path[4] = x;\r
+ path[5] = y + size;\r
+ path[6] = x + size;\r
+ path[7] = y;\r
+ drawPath(canvas, path, paint, true);\r
+ }\r
+\r
+ /**\r
+ * Returns the chart type identifier.\r
+ * \r
+ * @return the chart type\r
+ */\r
+ public String getChartType() {\r
+ return TYPE;\r
+ }\r
+\r
+}
\ No newline at end of file