Skip to content

Commit 56eee31

Browse files
committed
more unit tests.
1 parent 9af3c32 commit 56eee31

20 files changed

Lines changed: 502 additions & 159 deletions

androidplot-core/src/main/java/com/androidplot/Plot.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public Plot(Context context, String title) {
254254
public Plot(Context context, String title, RenderMode mode) {
255255
super(context);
256256
this.renderMode = mode;
257-
init(null, null, 0);
257+
init(context, null, 0);
258258
getTitle().setText(title);
259259
}
260260

@@ -337,9 +337,8 @@ protected void onAfterConfig() {
337337
// nothing to do by default
338338
}
339339

340-
341-
private void init(Context context, AttributeSet attrs, int defStyle) {
342-
PixelUtils.init(getContext());
340+
protected final void init(Context context, AttributeSet attrs, int defStyle) {
341+
PixelUtils.init(context);
343342
layoutManager = new LayoutManager();
344343
title = new TextLabelWidget(layoutManager, new Size(25,
345344
SizeMode.ABSOLUTE, 100,

androidplot-core/src/main/java/com/androidplot/Region.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
package com.androidplot;
1919

20-
import android.graphics.*;
21-
2220
/**
2321
* A one dimensional region represented by a starting and ending value.
2422
*/
@@ -125,6 +123,8 @@ public double ratio(double min, double max) {
125123
* Compares the input bounds min/max against this instance's current min/max.
126124
* If the input.min is less than this.min then this.min will be set to input.min.
127125
* If the input.max is greater than this.max then this.max will be set to input.max
126+
*
127+
* The result of a union will always be an equal or larger size region.
128128
* @param input
129129
*/
130130
public void union(Region input) {
@@ -138,6 +138,20 @@ public void union(Region input) {
138138
}
139139
}
140140

141+
/**
142+
* The result of an intersect will always be an equal or smaller size region.
143+
* @param input
144+
*/
145+
public void intersect(Region input) {
146+
if(getMin().doubleValue() < input.getMin().doubleValue()) {
147+
setMin(input.getMin());
148+
}
149+
150+
if(getMax().doubleValue() > input.getMax().doubleValue()) {
151+
setMax(input.getMax());
152+
}
153+
}
154+
141155
/**
142156
* Tests whether this segment intersects another
143157
* @param line2Min

androidplot-core/src/main/java/com/androidplot/xy/BubbleFormatter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
public class BubbleFormatter extends XYSeriesFormatter<XYRegionFormatter> {
3030

3131
private static final float DEFAULT_STROKE_PIX = 1;
32+
private static final int DEFAULT_STROKE_COLOR = Color.BLACK;
33+
private static final int DEFAULT_FILL_COLOR = Color.YELLOW;
34+
3235

3336
private Paint strokePaint;
3437
private Paint fillPaint;
@@ -38,9 +41,11 @@ public class BubbleFormatter extends XYSeriesFormatter<XYRegionFormatter> {
3841
strokePaint.setAntiAlias(true);
3942
strokePaint.setStrokeWidth(PixelUtils.dpToPix(DEFAULT_STROKE_PIX));
4043
strokePaint.setStyle(Paint.Style.STROKE);
44+
strokePaint.setColor(DEFAULT_STROKE_COLOR);
4145

4246
fillPaint = new Paint();
4347
fillPaint.setAntiAlias(true);
48+
fillPaint.setColor(DEFAULT_FILL_COLOR);
4449

4550
// default point labeler should draw z for bubbles:
4651
setPointLabeler(new PointLabeler<BubbleSeries>() {
@@ -51,7 +56,10 @@ public String getLabel(BubbleSeries series, int index) {
5156
});
5257
}
5358

59+
public BubbleFormatter() {}
60+
5461
public BubbleFormatter(Context context, int xmlCfgId) {
62+
this();
5563
configure(context, xmlCfgId);
5664
}
5765

androidplot-core/src/main/java/com/androidplot/xy/BubbleSeries.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ public class BubbleSeries implements XYSeries {
1212
private List<Number> zVals;
1313
private String title;
1414

15+
/**
16+
*
17+
* @param interleavedValues Interleaved values ordered as x, y, z; total size must be a multiple of 3.
18+
*/
19+
public BubbleSeries(Number... interleavedValues) {
20+
if(interleavedValues == null || interleavedValues.length % 3 > 0) {
21+
throw new RuntimeException("BubbleSeries interleave array length must be a non-zero multiple of 3.");
22+
}
23+
24+
xVals = new ArrayList<>();
25+
yVals = new ArrayList<>();
26+
zVals = new ArrayList<>();
27+
for(int i = 0; i < interleavedValues.length; i+=3) {
28+
xVals.add(interleavedValues[i]);
29+
yVals.add(interleavedValues[i+1]);
30+
zVals.add(interleavedValues[i+2]);
31+
}
32+
}
33+
1534
public BubbleSeries(List<Number> yVals, List<Number> zVals, String title) {
1635
this.yVals = yVals;
1736
this.zVals = zVals;

androidplot-core/src/main/java/com/androidplot/xy/LineAndPointRenderer.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,17 @@ protected void renderPath(Canvas canvas, RectF plotArea, Path path, PointF first
221221
XYRegionFormatter regionFormatter = formatter.getRegionFormatter(thisRegion);
222222
RectRegion thisRegionTransformed = bounds
223223
.transform(thisRegion, plotRegion, false, true);
224-
thisRegionTransformed.clip(plotRegion);
225-
RectF thisRegionRectF = thisRegionTransformed.asRectF();
226-
if (thisRegionRectF != null) {
227-
try {
228-
canvas.save(Canvas.ALL_SAVE_FLAG);
229-
canvas.clipPath(path);
230-
canvas.drawRect(thisRegionRectF, regionFormatter.getPaint());
231-
} finally {
232-
canvas.restore();
224+
thisRegionTransformed.intersect(plotRegion);
225+
if(thisRegion.isFullyDefined()) {
226+
RectF thisRegionRectF = thisRegionTransformed.asRectF();
227+
if (thisRegionRectF != null) {
228+
try {
229+
canvas.save(Canvas.ALL_SAVE_FLAG);
230+
canvas.clipPath(path);
231+
canvas.drawRect(thisRegionRectF, regionFormatter.getPaint());
232+
} finally {
233+
canvas.restore();
234+
}
233235
}
234236
}
235237
}

androidplot-core/src/main/java/com/androidplot/xy/PanZoom.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
public class PanZoom implements View.OnTouchListener {
1414

15-
private static final float MIN_DIST_2_FING = 5f;
15+
protected static final float MIN_DIST_2_FING = 5f;
1616

1717
private XYPlot plot;
1818
private Pan pan;
@@ -187,7 +187,7 @@ public boolean onTouch(final View view, final MotionEvent event) {
187187
* @param evt
188188
* @return
189189
*/
190-
private RectF getDistance(final MotionEvent evt) {
190+
protected RectF getDistance(final MotionEvent evt) {
191191
float left;
192192
float right;
193193
float top;

androidplot-core/src/main/java/com/androidplot/xy/RectRegion.java

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,6 @@ public RectRegion(Number minX, Number maxX, Number minY, Number maxY) {
7878
this(minX, maxX, minY, maxY, null);
7979
}
8080

81-
public boolean containsPoint(PointF point) {
82-
throw new UnsupportedOperationException("Not yet implemented.");
83-
}
84-
85-
public boolean containsValue(Number x, Number y) {
86-
throw new UnsupportedOperationException("Not yet implemented.");
87-
}
88-
89-
public boolean containsDomainValue(Number value) {
90-
return xRegion.contains(value);
91-
}
92-
93-
public boolean containsRangeValue(Number value) {
94-
return yRegion.contains(value);
95-
}
96-
9781
public XYCoords transform(Number x, Number y, RectRegion region2, boolean flipX, boolean flipY) {
9882
Number xx = xRegion.transform(x.doubleValue(), region2.xRegion, flipX);
9983
Number yy = yRegion.transform(y.doubleValue(), region2.yRegion, flipY);
@@ -161,6 +145,8 @@ public PointF transform(XYCoords value, RectF region2, boolean flipX, boolean fl
161145
* Compares the input bounds xy min/max vals against this instance's current xy min/max vals.
162146
* If the input.min is less than this.min then this.min will be set to input.min.
163147
* If the input.max is greater than this.max then this.max will be set to input.max
148+
*
149+
* The result will always have equal or greater area than the inputs.
164150
* @param input
165151
*/
166152
public void union(RectRegion input) {
@@ -192,41 +178,19 @@ public RectF asRectF() {
192178
getMaxX().floatValue(), getMaxY().floatValue());
193179
}
194180

195-
// public RectF getRectF(RectF plotRect, Number visMinX, Number visMaxX, Number visMinY,
196-
// Number visMaxY) {
197-
// PointF topLeftPoint = transform(new XYCoords(
198-
// xRegion.getMin().doubleValue() != Double.NEGATIVE_INFINITY
199-
// ? xRegion.getMin() : visMinX,
200-
// yRegion.getMax().doubleValue() != Double.POSITIVE_INFINITY
201-
// ? yRegion.getMax() : visMaxY), plotRect, false, true);
202-
//
203-
// PointF bottomRightPoint = transform(new XYCoords(
204-
// xRegion.getMin().doubleValue() != Double.NEGATIVE_INFINITY
205-
// ? xRegion.getMin() : visMaxX,
206-
// yRegion.getMax().doubleValue() != Double.POSITIVE_INFINITY
207-
// ? yRegion.getMax() : visMinY), plotRect, false, true);
208-
// return new RectF(
209-
// topLeftPoint.x,
210-
// topLeftPoint.y,
211-
// bottomRightPoint.x,
212-
// bottomRightPoint.y);
213-
// }
214-
215-
public void clip(RectRegion clippingBounds) {
216-
if(getMinX().doubleValue() < clippingBounds.getMinX().doubleValue()) {
217-
setMinX(clippingBounds.getMinX());
218-
}
219-
220-
if(getMinY().doubleValue() < clippingBounds.getMinY().doubleValue()) {
221-
setMinY(clippingBounds.getMinY());
222-
}
223-
224-
if(getMaxX().doubleValue() > clippingBounds.getMaxX().doubleValue()) {
225-
setMaxX(clippingBounds.getMaxX());
226-
}
227-
228-
if(getMaxY().doubleValue() > clippingBounds.getMaxY().doubleValue()) {
229-
setMaxY(clippingBounds.getMaxY());
181+
/**
182+
* The result of an intersect is always a RectRegion with an equal or smaller area.
183+
* @param clippingBounds
184+
*/
185+
public void intersect(RectRegion clippingBounds) {
186+
if(intersects(clippingBounds)) {
187+
xRegion.intersect(clippingBounds.xRegion);
188+
yRegion.intersect(clippingBounds.yRegion);
189+
} else {
190+
setMinY(null);
191+
setMaxY(null);
192+
setMinX(null);
193+
setMaxX(null);
230194
}
231195
}
232196

androidplot-core/src/main/java/com/androidplot/xy/XYGraphWidget.java

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -512,29 +512,31 @@ protected void drawGrid(Canvas canvas) {
512512
}
513513

514514

515-
double domainOrigin;
516-
if (plot.getDomainOrigin() != null) {
517-
domainOrigin = plot.getBounds().getxRegion().transform(
515+
Number domainOrigin = plot.getDomainOrigin();
516+
double domainOriginPix;
517+
if (domainOrigin != null) {
518+
domainOriginPix = plot.getBounds().getxRegion().transform(
518519
plot.getDomainOrigin().doubleValue(), gridRect.left, gridRect.right, false);
519520
} else {
520521
// if no domain origin is set, use the leftmost value visible on the grid:
521-
domainOrigin = gridRect.left;
522+
domainOriginPix = gridRect.left;
523+
domainOrigin=plot.getBounds().getMinX();
522524
}
523525

524526
Step domainStep = XYStepCalculator.getStep(plot, Axis.DOMAIN, gridRect);
525527

526528
// draw domain origin:
527-
if (domainOrigin >= gridRect.left
528-
&& domainOrigin <= gridRect.right) {
529-
drawDomainLine(canvas, (float) domainOrigin, plot.getDomainOrigin()
530-
.doubleValue(), domainOriginLinePaint, true);
529+
if (domainOriginPix >= gridRect.left
530+
&& domainOriginPix <= gridRect.right) {
531+
drawDomainLine(canvas, (float) domainOriginPix,
532+
domainOrigin, domainOriginLinePaint, true);
531533
}
532534

533535
// draw lines LEFT of origin:
534-
double xPix = domainOrigin - domainStep.getStepPix();
535-
for (int i = ONE; xPix >= gridRect.left - FUDGE; xPix = domainOrigin
536+
double xPix = domainOriginPix - domainStep.getStepPix();
537+
for (int i = ONE; xPix >= gridRect.left - FUDGE; xPix = domainOriginPix
536538
- (i * domainStep.getStepPix())) {
537-
double xVal = plot.getDomainOrigin().doubleValue() - i
539+
double xVal = domainOrigin.doubleValue() - i
538540
* domainStep.getStepVal();
539541

540542
if (xPix <= gridRect.right) {
@@ -546,10 +548,10 @@ protected void drawGrid(Canvas canvas) {
546548
}
547549

548550
// draw lines RIGHT of origin:
549-
xPix = domainOrigin + domainStep.getStepPix();
550-
for (int i = ONE; xPix <= gridRect.right + FUDGE; xPix = domainOrigin
551+
xPix = domainOriginPix + domainStep.getStepPix();
552+
for (int i = ONE; xPix <= gridRect.right + FUDGE; xPix = domainOriginPix
551553
+ (i * domainStep.getStepPix())) {
552-
double xVal = plot.getDomainOrigin().doubleValue() + i
554+
double xVal = domainOrigin.doubleValue() + i
553555
* domainStep.getStepVal();
554556

555557
if (xPix >= gridRect.left) {
@@ -560,29 +562,31 @@ protected void drawGrid(Canvas canvas) {
560562
i++;
561563
}
562564

563-
double rangeOrigin;
564-
if (plot.getRangeOrigin() != null) {
565-
rangeOrigin = plot.getBounds().getyRegion().transform(
566-
plot.getRangeOrigin().doubleValue(), gridRect.top, gridRect.bottom, true);
565+
Number rangeOrigin = plot.getRangeOrigin();
566+
double rangeOriginPix;
567+
if (rangeOrigin != null) {
568+
rangeOriginPix = plot.getBounds().getyRegion().transform(
569+
rangeOrigin.doubleValue(), gridRect.top, gridRect.bottom, true);
567570
} else {
568571
// if no range origin is set, use the bottom-most value visible on the grid:
569-
rangeOrigin = gridRect.bottom;
572+
rangeOriginPix = gridRect.bottom;
573+
rangeOrigin = plot.getBounds().getMinY();
570574
}
571575

572576
Step rangeStep = XYStepCalculator.getStep(plot, Axis.RANGE, gridRect);
573577

574578
// draw range origin:
575-
if (rangeOrigin >= gridRect.top && rangeOrigin <= gridRect.bottom) {
576-
drawRangeLine(canvas, (float) rangeOrigin, plot.getRangeOrigin()
577-
.doubleValue(), rangeOriginLinePaint, true);
579+
if (rangeOriginPix >= gridRect.top && rangeOriginPix <= gridRect.bottom) {
580+
drawRangeLine(canvas, (float) rangeOriginPix,
581+
rangeOrigin, rangeOriginLinePaint, true);
578582
}
579583

580584
final double rangeStepPix = rangeStep.getStepPix();
581585

582586
// draw lines ABOVE origin:
583-
double yPix = rangeOrigin - rangeStep.getStepPix();
584-
for (int i = ONE; yPix >= gridRect.top - FUDGE; yPix = rangeOrigin - (i * rangeStepPix)) {
585-
double yVal = plot.getRangeOrigin().doubleValue() + i
587+
double yPix = rangeOriginPix - rangeStep.getStepPix();
588+
for (int i = ONE; yPix >= gridRect.top - FUDGE; yPix = rangeOriginPix - (i * rangeStepPix)) {
589+
double yVal = rangeOrigin.doubleValue() + i
586590
* rangeStep.getStepVal();
587591

588592
if (yPix <= gridRect.bottom) {
@@ -594,9 +598,9 @@ protected void drawGrid(Canvas canvas) {
594598
}
595599

596600
// draw lines BENEATH origin:
597-
yPix = rangeOrigin + rangeStep.getStepPix();
598-
for (int i = ONE; yPix <= gridRect.bottom + FUDGE; yPix = rangeOrigin + (i * rangeStepPix)) {
599-
double yVal = plot.getRangeOrigin().doubleValue() - i
601+
yPix = rangeOriginPix + rangeStep.getStepPix();
602+
for (int i = ONE; yPix <= gridRect.bottom + FUDGE; yPix = rangeOriginPix + (i * rangeStepPix)) {
603+
double yVal = rangeOrigin.doubleValue() - i
600604
* rangeStep.getStepVal();
601605
if (yPix >= gridRect.top) {
602606
final boolean isRangeTick = i% getLinesPerRangeLabel() == ZERO;

0 commit comments

Comments
 (0)