Merge commit '42b2e5ca519766e37ce6941ba4faecc9691cc403' into upstream
[debian/openrocket] / android-libraries / achartengine / src / org / achartengine / chart / CombinedXYChart.java
diff --git a/android-libraries/achartengine/src/org/achartengine/chart/CombinedXYChart.java b/android-libraries/achartengine/src/org/achartengine/chart/CombinedXYChart.java
new file mode 100644 (file)
index 0000000..d684c3a
--- /dev/null
@@ -0,0 +1,177 @@
+/**\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 java.util.List;\r
+\r
+import org.achartengine.model.XYMultipleSeriesDataset;\r
+import org.achartengine.model.XYSeries;\r
+import org.achartengine.renderer.SimpleSeriesRenderer;\r
+import org.achartengine.renderer.XYMultipleSeriesRenderer;\r
+import org.achartengine.renderer.XYMultipleSeriesRenderer.Orientation;\r
+\r
+import android.graphics.Canvas;\r
+import android.graphics.Paint;\r
+\r
+/**\r
+ * The combined XY chart rendering class.\r
+ */\r
+public class CombinedXYChart extends XYChart {\r
+  /** The embedded XY charts. */\r
+  private XYChart[] mCharts;\r
+  /** The supported charts for being combined. */\r
+  private Class[] xyChartTypes = new Class[] { TimeChart.class, LineChart.class,\r
+      CubicLineChart.class, BarChart.class, BubbleChart.class, ScatterChart.class,\r
+      RangeBarChart.class, RangeStackedBarChart.class };\r
+\r
+  /**\r
+   * Builds a new combined XY chart instance.\r
+   * \r
+   * @param dataset the multiple series dataset\r
+   * @param renderer the multiple series renderer\r
+   * @param types the XY chart types\r
+   */\r
+  public CombinedXYChart(XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer,\r
+      String[] types) {\r
+    super(dataset, renderer);\r
+    int length = types.length;\r
+    mCharts = new XYChart[length];\r
+    for (int i = 0; i < length; i++) {\r
+      try {\r
+        mCharts[i] = getXYChart(types[i]);\r
+      } catch (Exception e) {\r
+        // ignore\r
+      }\r
+      if (mCharts[i] == null) {\r
+        throw new IllegalArgumentException("Unknown chart type " + types[i]);\r
+      } else {\r
+        XYMultipleSeriesDataset newDataset = new XYMultipleSeriesDataset();\r
+        newDataset.addSeries(dataset.getSeriesAt(i));\r
+        XYMultipleSeriesRenderer newRenderer = new XYMultipleSeriesRenderer();\r
+        // TODO: copy other parameters here\r
+        newRenderer.setBarSpacing(renderer.getBarSpacing());\r
+        newRenderer.setPointSize(renderer.getPointSize());\r
+        int scale = dataset.getSeriesAt(i).getScaleNumber();\r
+        if (renderer.isMinXSet(scale)) {\r
+          newRenderer.setXAxisMin(renderer.getXAxisMin(scale));\r
+        }\r
+        if (renderer.isMaxXSet(scale)) {\r
+          newRenderer.setXAxisMax(renderer.getXAxisMax(scale));\r
+        }\r
+        if (renderer.isMinYSet(scale)) {\r
+          newRenderer.setYAxisMin(renderer.getYAxisMin(scale));\r
+        }\r
+        if (renderer.isMaxYSet(scale)) {\r
+          newRenderer.setYAxisMax(renderer.getYAxisMax(scale));\r
+        }\r
+        newRenderer.addSeriesRenderer(renderer.getSeriesRendererAt(i));\r
+        mCharts[i].setDatasetRenderer(newDataset, newRenderer);\r
+      }\r
+    }\r
+  }\r
+\r
+  /**\r
+   * Returns a chart instance based on the provided type.\r
+   * \r
+   * @param type the chart type\r
+   * @return an instance of a chart implementation\r
+   * @throws IllegalAccessException\r
+   * @throws InstantiationException\r
+   */\r
+  private XYChart getXYChart(String type) throws IllegalAccessException, InstantiationException {\r
+    XYChart chart = null;\r
+    int length = xyChartTypes.length;\r
+    for (int i = 0; i < length && chart == null; i++) {\r
+      XYChart newChart = (XYChart) xyChartTypes[i].newInstance();\r
+      if (type.equals(newChart.getChartType())) {\r
+        chart = newChart;\r
+      }\r
+    }\r
+    return chart;\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
+    mCharts[seriesIndex].setScreenR(getScreenR());\r
+    mCharts[seriesIndex].setCalcRange(getCalcRange(mDataset.getSeriesAt(seriesIndex)\r
+        .getScaleNumber()), 0);\r
+    mCharts[seriesIndex].drawSeries(canvas, paint, points, seriesRenderer, yAxisValue, 0,\r
+        startIndex);\r
+  }\r
+\r
+  @Override\r
+  protected ClickableArea[] clickableAreasForPoints(float[] points, double[] values,\r
+      float yAxisValue, int seriesIndex, int startIndex) {\r
+    return mCharts[seriesIndex].clickableAreasForPoints(points, values, yAxisValue, 0, startIndex);\r
+  }\r
+\r
+  @Override\r
+  protected void drawSeries(XYSeries series, Canvas canvas, Paint paint, List<Float> pointsList,\r
+      SimpleSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, Orientation or,\r
+      int startIndex) {\r
+    mCharts[seriesIndex].setScreenR(getScreenR());\r
+    mCharts[seriesIndex].setCalcRange(getCalcRange(mDataset.getSeriesAt(seriesIndex)\r
+        .getScaleNumber()), 0);\r
+    mCharts[seriesIndex].drawSeries(series, canvas, paint, pointsList, seriesRenderer, yAxisValue,\r
+        0, or, startIndex);\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 mCharts[seriesIndex].getLegendShapeWidth(0);\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
+    mCharts[seriesIndex].drawLegendShape(canvas, renderer, x, y, 0, paint);\r
+  }\r
+\r
+  /**\r
+   * Returns the chart type identifier.\r
+   * \r
+   * @return the chart type\r
+   */\r
+  public String getChartType() {\r
+    return "Combined";\r
+  }\r
+\r
+}\r