Fixing a few of the issues in chart drawing.

This commit is contained in:
Rodrigo Damazio
2011-04-30 04:37:59 -03:00
parent 84d7281002
commit f125a798bf
4 changed files with 188 additions and 210 deletions
@@ -259,13 +259,13 @@ public class ChartActivity extends Activity implements TrackDataListener {
// TODO: Account for segment splits? // TODO: Account for segment splits?
switch (mode) { switch (mode) {
case BY_DISTANCE: case BY_DISTANCE:
timeOrDistance = profileLength; timeOrDistance = profileLength / 1000.0;
if (lastLocation != null) { if (lastLocation != null) {
double d = lastLocation.distanceTo(location); double d = lastLocation.distanceTo(location);
if (metricUnits) { if (metricUnits) {
profileLength += d / 1000.0; profileLength += d;
} else { } else {
profileLength += d * UnitConversions.KM_TO_MI / 1000.0; profileLength += d * UnitConversions.KM_TO_MI;
} }
} }
break; break;
@@ -361,9 +361,16 @@ public class ChartActivity extends Activity implements TrackDataListener {
lastLocation = null; lastLocation = null;
startTime = -1; startTime = -1;
elevationBuffer.reset(); elevationBuffer.reset();
speedBuffer.reset();
chartView.reset(); chartView.reset();
speedBuffer.reset();
pendingPoints.clear(); pendingPoints.clear();
runOnUiThread(new Runnable() {
@Override
public void run() {
chartView.resetScroll();
}
});
} }
@Override @Override
@@ -28,9 +28,9 @@ import android.graphics.Canvas;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.DashPathEffect; import android.graphics.DashPathEffect;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Paint.Align; import android.graphics.Paint.Align;
import android.graphics.Paint.Style; import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.VelocityTracker; import android.view.VelocityTracker;
@@ -93,9 +93,11 @@ public class ChartView extends View {
private int bottomBorder; private int bottomBorder;
private static final int RIGHT_BORDER = 17; private static final int RIGHT_BORDER = 17;
/** Space to leave for drawing the unit labels */ /** Space to leave for drawing the unit labels */
private static final int UNIT_BORDER = 15; private static final int UNIT_BORDER = 15;
private static final int FONT_HEIGHT = 10; private static final int FONT_HEIGHT = 10;
private int w = 0; private int w = 0;
private int h = 0; private int h = 0;
private int effectiveWidth = 0; private int effectiveWidth = 0;
@@ -118,11 +120,11 @@ public class ChartView extends View {
private ChartValueSeries[] series; private ChartValueSeries[] series;
private final ExtremityMonitor xMonitor = new ExtremityMonitor(); private final ExtremityMonitor xMonitor = new ExtremityMonitor();
private final NumberFormat xFormat = new DecimalFormat("###,###"); private static final NumberFormat X_FORMAT = new DecimalFormat("###,###");
private final NumberFormat xShortFormat = new DecimalFormat("#.0"); private static final NumberFormat X_SHORT_FORMAT = new DecimalFormat("#.0");
/* /*
* Paints etc. used when drawing the histogram: * Paints etc. used when drawing the chart:
*/ */
private final Paint borderPaint = new Paint(); private final Paint borderPaint = new Paint();
private final Paint labelPaint = new Paint(); private final Paint labelPaint = new Paint();
@@ -271,49 +273,12 @@ public class ChartView extends View {
: R.string.pace_label)); : R.string.pace_label));
} }
/**
* Gets the data that is displayed by the chart.
*
* @return an array list with data points
*/
public ArrayList<double[]> getData() {
return data;
}
/**
* Sets the data that is to be displayed by the chart.
*
* @param theData an array list of data points
*/
public synchronized void setDataPoints(ArrayList<double[]> theData) {
scrollTo(0, 0);
zoomLevel = 1;
data.clear();
xMonitor.reset();
for (ChartValueSeries cvs : series) {
cvs.reset();
}
addDataPoints(theData);
}
/**
* Adds a new data point to the chart.
*
* @param theData a data point
*/
public synchronized void addDataPoint(double[] theData) {
data.add(theData);
addDataPointInternal(theData);
updateDimensions();
setUpPath();
}
private void addDataPointInternal(double[] theData) { private void addDataPointInternal(double[] theData) {
xMonitor.update(theData[0]); xMonitor.update(theData[0]);
int min = Math.min(series.length, theData.length - 1); int min = Math.min(series.length, theData.length - 1);
for (int i = 0; i < min; i++) { for (int i = 1; i <= min; i++) {
if (!Double.isNaN(theData[i + 1])) { if (!Double.isNaN(theData[i])) {
series[i].update(theData[i + 1]); series[i - 1].update(theData[i]);
} }
} }
// Fill in the extra's if needed. // Fill in the extra's if needed.
@@ -329,7 +294,8 @@ public class ChartView extends View {
* *
* @param theData an array list of data points to be added * @param theData an array list of data points to be added
*/ */
public synchronized void addDataPoints(ArrayList<double[]> theData) { public void addDataPoints(ArrayList<double[]> theData) {
synchronized (data) {
data.addAll(theData); data.addAll(theData);
for (int i = 0; i < theData.size(); i++) { for (int i = 0; i < theData.size(); i++) {
double d[] = theData.get(i); double d[] = theData.get(i);
@@ -338,17 +304,23 @@ public class ChartView extends View {
updateDimensions(); updateDimensions();
setUpPath(); setUpPath();
} }
}
/** /**
* Clears all data. * Clears all data.
* Call this only from the UI thread!
*/ */
public synchronized void reset() { public void reset() {
synchronized (data) {
data.clear(); data.clear();
xMonitor.reset();
zoomLevel = 1; zoomLevel = 1;
scrollTo(0, 0);
updateDimensions(); updateDimensions();
} }
}
public void resetScroll() {
scrollTo(0, 0);
}
/** /**
* @return true if the chart can be zoomed into. * @return true if the chart can be zoomed into.
@@ -392,13 +364,6 @@ public class ChartView extends View {
} }
} }
/**
* @return the current zoom level (1 equals to showing all data points)
*/
public int getZoomLevel() {
return zoomLevel;
}
/** /**
* Initiates flinging. * Initiates flinging.
* *
@@ -427,14 +392,6 @@ public class ChartView extends View {
scrollTo(scrollX, 0); scrollTo(scrollX, 0);
} }
/**
* Sets the scroll position of the chart. This will trigger a redraw.
*/
@Override
public void scrollTo(int x, int y) {
super.scrollTo(x, y);
}
/** /**
* @return the current display mode (by distance, by time) * @return the current display mode (by distance, by time)
*/ */
@@ -551,7 +508,8 @@ public class ChartView extends View {
} }
@Override @Override
protected synchronized void onDraw(Canvas c) { protected void onDraw(Canvas c) {
synchronized (data) {
if (w != c.getWidth() || h != c.getHeight()) { if (w != c.getWidth() || h != c.getHeight()) {
// Dimensions have changed (for example due to orientation change). // Dimensions have changed (for example due to orientation change).
w = c.getWidth(); w = c.getWidth();
@@ -626,6 +584,7 @@ public class ChartView extends View {
pointer.draw(c); pointer.draw(c);
} }
} }
}
private void drawSeriesTitles(Canvas c) { private void drawSeriesTitles(Canvas c) {
int sections = 1; int sections = 1;
@@ -647,7 +606,8 @@ public class ChartView extends View {
* Sets up the path that is used to draw the histogram in onDraw(). The path * Sets up the path that is used to draw the histogram in onDraw(). The path
* needs to be updated any time after the data or histogram dimensions change. * needs to be updated any time after the data or histogram dimensions change.
*/ */
private synchronized void setUpPath() { private void setUpPath() {
synchronized (data) {
for (ChartValueSeries cvs : series) { for (ChartValueSeries cvs : series) {
cvs.getPath().reset(); cvs.getPath().reset();
} }
@@ -692,6 +652,7 @@ public class ChartView extends View {
} }
} }
} }
}
/** /**
* Find the index of the first point which has a series populated. * Find the index of the first point which has a series populated.
@@ -709,7 +670,7 @@ public class ChartView extends View {
} }
/** /**
* Update the histogram dimensions. * Update the chart dimensions.
*/ */
private void updateDimensions() { private void updateDimensions() {
maxX = xMonitor.getMax(); maxX = xMonitor.getMax();
@@ -785,7 +746,7 @@ public class ChartView extends View {
} }
String s = String s =
(mode == Mode.BY_DISTANCE) (mode == Mode.BY_DISTANCE)
? (shortFormat ? xShortFormat.format(x) : xFormat.format(x)) ? (shortFormat ? X_SHORT_FORMAT.format(x) : X_FORMAT.format(x))
: StringUtils.formatTime((long) x); : StringUtils.formatTime((long) x);
c.drawText(s, c.drawText(s,
getX(x), getX(x),
@@ -833,7 +794,7 @@ public class ChartView extends View {
canvas.drawText(s, leftBorder - UNIT_BORDER * .2f, UNIT_BORDER * .8f + topBorder, labelPaint); canvas.drawText(s, leftBorder - UNIT_BORDER * .2f, UNIT_BORDER * .8f + topBorder, labelPaint);
} }
private synchronized void drawGrid(Canvas c) { private void drawGrid(Canvas c) {
if (data.isEmpty()) { if (data.isEmpty()) {
return; return;
} }
@@ -253,10 +253,15 @@ public class StatsActivity extends Activity implements TrackDataListener {
} }
@Override @Override
public void onCurrentLocationChanged(Location loc) { public void onCurrentLocationChanged(final Location loc) {
if (dataHub.isRecordingSelected()) { if (dataHub.isRecordingSelected()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
showLocation(loc); showLocation(loc);
} }
});
}
} }
@Override @Override
@@ -269,7 +274,12 @@ public class StatsActivity extends Activity implements TrackDataListener {
switch (state) { switch (state) {
case DISABLED: case DISABLED:
case NO_FIX: case NO_FIX:
runOnUiThread(new Runnable() {
@Override
public void run() {
showUnknownLocation(); showUnknownLocation();
}
});
break; break;
} }
} }