Skip to content
This repository has been archived by the owner on Dec 14, 2020. It is now read-only.

Update TimeChart.java #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 114 additions & 1 deletion achartengine/src/org/achartengine/chart/TimeChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@
import java.util.Date;
import java.util.List;

import org.achartengine.model.Point;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;

/**
* The time chart rendering class.
Expand All @@ -40,8 +44,20 @@ public class TimeChart extends LineChart {
private String mDateFormat;
/** The starting point for labels. */
private Double mStartPoint;

private float mFirstMultiplier;

private float mSecondMultiplier;

/** A path measure for retrieving the points on the path. */
private PathMeasure mPathMeasure;


TimeChart() {
// default is to have first control point at about 33% of the distance,
mFirstMultiplier = 0.33f;
// and the next at 66% of the distance.
mSecondMultiplier = 1 - mFirstMultiplier;
}

/**
Expand All @@ -50,8 +66,104 @@ public class TimeChart extends LineChart {
* @param dataset the multiple series dataset
* @param renderer the multiple series renderer
*/
public TimeChart(XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer) {
public TimeChart(XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, float smoothness) {
super(dataset, renderer);
mFirstMultiplier = smoothness;
mSecondMultiplier = 1 - mFirstMultiplier;
}

@Override
protected void drawPath(Canvas canvas, List<Float> points, Paint paint, boolean circular) {

if (mFirstMultiplier > 0)
{
Path p = new Path();
float x = points.get(0);
float y = points.get(1);
p.moveTo(x, y);

int length = points.size();
if (circular) {
length -= 4;
}

Point p1 = new Point();
Point p2 = new Point();
Point p3 = new Point();
for (int i = 0; i < length; i += 2) {
int nextIndex = i + 2 < length ? i + 2 : i;
int nextNextIndex = i + 4 < length ? i + 4 : nextIndex;
calc(points, p1, i, nextIndex, mSecondMultiplier);
p2.setX(points.get(nextIndex));
p2.setY(points.get(nextIndex + 1));
calc(points, p3, nextIndex, nextNextIndex, mFirstMultiplier);
// From last point, approaching x1/y1 and x2/y2 and ends up at x3/y3
p.cubicTo(p1.getX(), p1.getY(), p2.getX(), p2.getY(), p3.getX(), p3.getY());
}
mPathMeasure = new PathMeasure(p, false);
if (circular) {
for (int i = length; i < length + 4; i += 2) {
p.lineTo(points.get(i), points.get(i + 1));
}
p.lineTo(points.get(0), points.get(1));
}
canvas.drawPath(p, paint);
}
else
super.drawPath(canvas, points, paint, circular);
}

private void calc(List<Float> points, Point result, int index1, int index2, final float multiplier) {
float p1x = points.get(index1);
float p1y = points.get(index1 + 1);
float p2x = points.get(index2);
float p2y = points.get(index2 + 1);

float diffX = p2x - p1x; // p2.x - p1.x;
float diffY = p2y - p1y; // p2.y - p1.y;
result.setX(p1x + (diffX * multiplier));
result.setY(p1y + (diffY * multiplier));
}

/**
* Draws the series points.
*
* @param canvas the canvas
* @param paint the paint object
* @param pointsList the points to be rendered
* @param seriesRenderer the series renderer
* @param yAxisValue the y axis value in pixels
* @param seriesIndex the series index
* @param startIndex the start index of the rendering points
*/
protected void drawPoints(Canvas canvas, Paint paint, List<Float> pointsList,
XYSeriesRenderer seriesRenderer, float yAxisValue, int seriesIndex, int startIndex) {
if (isRenderPoints(seriesRenderer)) {
ScatterChart pointsChart = getPointsChart();
if (pointsChart != null) {
if (mFirstMultiplier > 0)
{
int length = (int) mPathMeasure.getLength();
int pointsLength = pointsList.size();
float[] coords = new float[2];
for (int i = 0; i < length; i++) {
mPathMeasure.getPosTan(i, coords, null);
double prevDiff = Double.MAX_VALUE;
boolean ok = true;
for (int j = 0; j < pointsLength && ok; j += 2) {
double diff = Math.abs(pointsList.get(j) - coords[0]);
if (diff < 1) {
pointsList.set(j + 1, coords[1]);
prevDiff = diff;
}
ok = prevDiff > diff;
}
}
}
pointsChart.drawSeries(canvas, paint, pointsList, seriesRenderer, yAxisValue, seriesIndex,
startIndex);
}
}
}

/**
Expand Down Expand Up @@ -225,3 +337,4 @@ protected List<Double> getXLabels(double min, double max, int count) {
return result;
}
}