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.Point;
\r
19 import org.achartengine.model.XYMultipleSeriesDataset;
\r
20 import org.achartengine.renderer.XYMultipleSeriesRenderer;
\r
22 import android.graphics.Canvas;
\r
23 import android.graphics.Paint;
\r
24 import android.graphics.Path;
\r
27 * The interpolated (cubic) line chart rendering class.
\r
29 public class CubicLineChart extends LineChart {
\r
30 /** The chart type. */
\r
31 public static final String TYPE = "Cubic";
\r
33 private float firstMultiplier;
\r
35 private float secondMultiplier;
\r
37 private Point p1 = new Point();
\r
39 private Point p2 = new Point();
\r
41 private Point p3 = new Point();
\r
43 public CubicLineChart() {
\r
44 // default is to have first control point at about 33% of the distance,
\r
45 firstMultiplier = 0.33f;
\r
46 // and the next at 66% of the distance.
\r
47 secondMultiplier = 1 - firstMultiplier;
\r
51 * Builds a cubic line chart.
\r
53 * @param dataset the dataset
\r
54 * @param renderer the renderer
\r
55 * @param smoothness smoothness determines how smooth the curve should be,
\r
56 * range [0->0.5] super smooth, 0.5, means that it might not get
\r
57 * close to control points if you have random data // less smooth,
\r
58 * (close to 0) means that it will most likely touch all control //
\r
61 public CubicLineChart(XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer,
\r
63 super(dataset, renderer);
\r
64 firstMultiplier = smoothness;
\r
65 secondMultiplier = 1 - firstMultiplier;
\r
69 protected void drawPath(Canvas canvas, float[] points, Paint paint, boolean circular) {
\r
70 Path p = new Path();
\r
71 float x = points[0];
\r
72 float y = points[1];
\r
75 int length = points.length;
\r
80 for (int i = 0; i < length; i += 2) {
\r
81 int nextIndex = i + 2 < length ? i + 2 : i;
\r
82 int nextNextIndex = i + 4 < length ? i + 4 : nextIndex;
\r
83 calc(points, p1, i, nextIndex, secondMultiplier);
\r
84 p2.setX(points[nextIndex]);
\r
85 p2.setY(points[nextIndex + 1]);
\r
86 calc(points, p3, nextIndex, nextNextIndex, firstMultiplier);
\r
87 // From last point, approaching x1/y1 and x2/y2 and ends up at x3/y3
\r
88 p.cubicTo(p1.getX(), p1.getY(), p2.getX(), p2.getY(), p3.getX(), p3.getY());
\r
91 for (int i = length; i < length + 4; i += 2) {
\r
92 p.lineTo(points[i], points[i + 1]);
\r
94 p.lineTo(points[0], points[1]);
\r
96 canvas.drawPath(p, paint);
\r
99 private void calc(float[] points, Point result, int index1, int index2, final float multiplier) {
\r
100 float p1x = points[index1];
\r
101 float p1y = points[index1 + 1];
\r
102 float p2x = points[index2];
\r
103 float p2y = points[index2 + 1];
\r
105 float diffX = p2x - p1x; // p2.x - p1.x;
\r
106 float diffY = p2y - p1y; // p2.y - p1.y;
\r
107 result.setX(p1x + (diffX * multiplier));
\r
108 result.setY(p1y + (diffY * multiplier));
\r
112 * Returns the chart type identifier.
\r
114 * @return the chart type
\r
116 public String getChartType() {
\r