Skip to content

Commit a26e537

Browse files
authored
1.5.4 Changes (halfhp#74)
* halfhp#69 Fixes resize etc. for Y_VALS_ONLY format for SimpleXYSeries. * halfhp#73 Fixes issue with XYGraphWidget not laying out on size change. Also adds Widget.onResize to efficiently detect when new layout dimensions may need recalculation. * Adds license and issue template for Github display
1 parent 636768a commit a26e537

14 files changed

Lines changed: 283 additions & 87 deletions

File tree

.github/ISSUE_TEMPLATE.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Thanks for taking the time to help make Androidplot better! Please note
2+
that issues should be submitted on Github for bugs only.
3+
4+
How-to questions may be posted under the [Androidplot tag on Stack Overflow](https://stackoverflow.com/questions/tagged/androidplot)
5+
and feature requests etc. may be posted to the [Google Group Forum](https://groups.google.com/forum/#!forum/androidplot).
6+
7+
When possible, please include the following in your bug report:
8+
9+
* Description of the problem
10+
* Steps to reproduce the issue
11+
* Version(s) of Androidplot being used
12+
* A stacktrace if the issue is causing a crash
13+
14+

LICENSE.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2018 Androidplot.com
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Technical questions should be posted using the [androidplot tag](http://stackove
4343
# License
4444
Androidplot has been made available under the Apache 2.0 license:
4545

46-
Copyright 2016 Androidplot.com
46+
Copyright 2018 Androidplot.com
4747

4848
Licensed under the Apache License, Version 2.0 (the "License");
4949
you may not use this file except in compliance with the License.

androidplot-core/src/main/java/com/androidplot/ui/LayoutManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void draw(Canvas canvas) throws PlotRenderException {
9696
PositionMetrics metrics = widget.getPositionMetrics();
9797
float elementWidth = widget.getWidthPix(displayDims.paddedRect.width());
9898
float elementHeight = widget.getHeightPix(displayDims.paddedRect.height());
99-
PointF coords = widget.getElementCoordinates(elementHeight,
99+
PointF coords = Widget.calculateCoordinates(elementHeight,
100100
elementWidth, displayDims.paddedRect, metrics);
101101

102102
DisplayDimensions dims = widget.getWidgetDimensions();

androidplot-core/src/main/java/com/androidplot/ui/widget/Widget.java

Lines changed: 79 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package com.androidplot.ui.widget;
1818

1919
import android.graphics.*;
20+
import android.support.annotation.NonNull;
21+
import android.support.annotation.Nullable;
22+
2023
import com.androidplot.exception.PlotRenderException;
2124
import com.androidplot.ui.*;
2225
import com.androidplot.util.DisplayDimensions;
@@ -42,6 +45,7 @@ public abstract class Widget implements BoxModelable, Resizable {
4245
private LayoutManager layoutManager;
4346

4447
private Rotation rotation = Rotation.NONE;
48+
private RectF lastWidgetRect = null;
4549

4650
public enum Rotation {
4751
NINETY_DEGREES,
@@ -77,6 +81,7 @@ public void setAnchor(Anchor anchor) {
7781
/**
7882
* Same as {@link #position(float, HorizontalPositioning, float, VerticalPositioning, Anchor)}
7983
* but with the anchor parameter defaulted to the upper left corner.
84+
*
8085
* @param x
8186
* @param horizontalPositioning
8287
* @param y
@@ -87,11 +92,11 @@ public void position(float x, HorizontalPositioning horizontalPositioning, float
8792
}
8893

8994
/**
90-
* @param x X-Coordinate of the top left corner of element. When using RELATIVE, must be a value between 0 and 1.
95+
* @param x X-Coordinate of the top left corner of element. When using RELATIVE, must be a value between 0 and 1.
9196
* @param horizontalPositioning LayoutType to use when orienting this element's X-Coordinate.
92-
* @param y Y_VALS_ONLY-Coordinate of the top-left corner of element. When using RELATIVE, must be a value between 0 and 1.
93-
* @param verticalPositioning LayoutType to use when orienting this element's Y_VALS_ONLY-Coordinate.
94-
* @param anchor The point of reference used by this positioning call.
97+
* @param y Y_VALS_ONLY-Coordinate of the top-left corner of element. When using RELATIVE, must be a value between 0 and 1.
98+
* @param verticalPositioning LayoutType to use when orienting this element's Y_VALS_ONLY-Coordinate.
99+
* @param anchor The point of reference used by this positioning call.
95100
*/
96101
public void position(float x, HorizontalPositioning horizontalPositioning, float y,
97102
VerticalPositioning verticalPositioning, Anchor anchor) {
@@ -271,15 +276,15 @@ public float getMarginRight() {
271276
* into this Widget's size or position is altered.
272277
*/
273278
public synchronized void refreshLayout() {
274-
if(positionMetrics == null) {
279+
if (positionMetrics == null) {
275280
// make sure positionMetrics have been set. this method can be
276281
// automatically called during xml configuration of certain params
277282
// before the widget is fully configured.
278283
return;
279284
}
280285
float elementWidth = getWidthPix(plotDimensions.paddedRect.width());
281286
float elementHeight = getHeightPix(plotDimensions.paddedRect.height());
282-
PointF coords = getElementCoordinates(elementHeight,
287+
PointF coords = calculateCoordinates(elementHeight,
283288
elementWidth, plotDimensions.paddedRect, positionMetrics);
284289

285290
RectF widgetRect = new RectF(coords.x, coords.y,
@@ -296,69 +301,90 @@ public synchronized void layout(final DisplayDimensions plotDimensions) {
296301
refreshLayout();
297302
}
298303

299-
public PointF getElementCoordinates(float height, float width, RectF viewRect, PositionMetrics metrics) {
300-
float x = metrics.getXPositionMetric().getPixelValue(viewRect.width()) + viewRect.left;
301-
float y = metrics.getYPositionMetric().getPixelValue(viewRect.height()) + viewRect.top;
302-
PointF point = new PointF(x, y);
303-
return PixelUtils.sub(point, getAnchorOffset(width, height, metrics.getAnchor()));
304-
}
304+
305+
public static PointF calculateCoordinates(float height, float width, RectF viewRect, PositionMetrics metrics) {
306+
float x = metrics.getXPositionMetric().getPixelValue(viewRect.width()) + viewRect.left;
307+
float y = metrics.getYPositionMetric().getPixelValue(viewRect.height()) + viewRect.top;
308+
PointF point = new PointF(x, y);
309+
return PixelUtils.sub(point, getAnchorOffset(width, height, metrics.getAnchor()));
310+
}
305311

306312
public static PointF getAnchorOffset(float width, float height, Anchor anchor) {
307-
PointF point = new PointF();
308-
switch (anchor) {
309-
case LEFT_TOP:
310-
break;
311-
case LEFT_MIDDLE:
312-
point.set(0, height / 2);
313-
break;
314-
case LEFT_BOTTOM:
315-
point.set(0, height);
316-
break;
317-
case RIGHT_TOP:
318-
point.set(width, 0);
319-
break;
320-
case RIGHT_BOTTOM:
321-
point.set(width, height);
322-
break;
323-
case RIGHT_MIDDLE:
324-
point.set(width, height / 2);
325-
break;
326-
case TOP_MIDDLE:
327-
point.set(width / 2, 0);
328-
break;
329-
case BOTTOM_MIDDLE:
330-
point.set(width / 2, height);
331-
break;
332-
case CENTER:
333-
point.set(width / 2, height / 2);
334-
break;
335-
default:
336-
throw new IllegalArgumentException("Unsupported anchor location: " + anchor);
337-
}
338-
return point;
313+
PointF point = new PointF();
314+
switch (anchor) {
315+
case LEFT_TOP:
316+
break;
317+
case LEFT_MIDDLE:
318+
point.set(0, height / 2);
319+
break;
320+
case LEFT_BOTTOM:
321+
point.set(0, height);
322+
break;
323+
case RIGHT_TOP:
324+
point.set(width, 0);
325+
break;
326+
case RIGHT_BOTTOM:
327+
point.set(width, height);
328+
break;
329+
case RIGHT_MIDDLE:
330+
point.set(width, height / 2);
331+
break;
332+
case TOP_MIDDLE:
333+
point.set(width / 2, 0);
334+
break;
335+
case BOTTOM_MIDDLE:
336+
point.set(width / 2, height);
337+
break;
338+
case CENTER:
339+
point.set(width / 2, height / 2);
340+
break;
341+
default:
342+
throw new IllegalArgumentException("Unsupported anchor location: " + anchor);
339343
}
344+
return point;
345+
}
340346

341347
public static PointF getAnchorCoordinates(RectF widgetRect, Anchor anchor) {
342-
return PixelUtils.add(new PointF(widgetRect.left, widgetRect.top),
343-
getAnchorOffset(widgetRect.width(), widgetRect.height(), anchor));
344-
}
348+
return PixelUtils.add(new PointF(widgetRect.left, widgetRect.top),
349+
getAnchorOffset(widgetRect.width(), widgetRect.height(), anchor));
350+
}
351+
352+
public static PointF getAnchorCoordinates(float x, float y, float width, float height, Anchor anchor) {
353+
return getAnchorCoordinates(new RectF(x, y, x + width, y + height), anchor);
354+
}
345355

346-
public static PointF getAnchorCoordinates(float x, float y, float width, float height, Anchor anchor) {
347-
return getAnchorCoordinates(new RectF(x, y, x+width, y+height), anchor);
356+
private void checkSize(@NonNull RectF widgetRect) {
357+
if (lastWidgetRect == null || !lastWidgetRect.equals(widgetRect)) {
358+
onResize(lastWidgetRect, widgetRect);
348359
}
360+
lastWidgetRect = widgetRect;
361+
}
362+
363+
/**
364+
* Called whenever the height or width of the Widget's reserved space has changed,
365+
* immediately before {@link #doOnDraw(Canvas, RectF)}.
366+
* May be used to efficiently carry out expensive operations only when necessary.
367+
*
368+
* @param oldRect
369+
* @param newRect
370+
*/
371+
protected void onResize(@Nullable RectF oldRect, @NonNull RectF newRect) {
372+
// do nothing by default
373+
}
349374

350375
public void draw(Canvas canvas) throws PlotRenderException {
351376
if (isVisible()) {
352377
if (backgroundPaint != null) {
353378
drawBackground(canvas, widgetDimensions.canvasRect);
354379
}
355380
canvas.save();
356-
final RectF paddedRect = applyRotation(canvas, widgetDimensions.paddedRect);
357-
doOnDraw(canvas, paddedRect);
381+
final RectF widgetRect = applyRotation(canvas, widgetDimensions.paddedRect);
382+
checkSize(widgetRect);
383+
doOnDraw(canvas, widgetRect);
358384
canvas.restore();
359385

360386
if (borderPaint != null) {
361-
drawBorder(canvas, paddedRect);
387+
drawBorder(canvas, widgetRect);
362388
}
363389
}
364390
}
@@ -395,7 +421,7 @@ protected RectF applyRotation(Canvas canvas, RectF rect) {
395421
throw new UnsupportedOperationException("Not yet implemented.");
396422

397423
}
398-
if(rotation != Rotation.NONE) {
424+
if (rotation != Rotation.NONE) {
399425
canvas.rotate(rotationDegs, cx, cy);
400426
}
401427
return rect;

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@
3333
* A convenience class used to create instances of XYPlot generated from Lists of Numbers.
3434
*/
3535
public class SimpleXYSeries implements EditableXYSeries, OrderedXYSeries, PlotListener {
36-
37-
private static final String TAG = SimpleXYSeries.class.getName();
38-
3936
private volatile LinkedList<Number> xVals = new LinkedList<>();
4037
private volatile LinkedList<Number> yVals = new LinkedList<>();
4138
private volatile String title = null;
@@ -144,7 +141,7 @@ public void setModel(List<? extends Number> model, ArrayFormat format) {
144141
lock.writeLock().lock();
145142
try {
146143
// empty the current values:
147-
xVals = null;
144+
xVals.clear();
148145
yVals.clear();
149146

150147
// make sure the new model has data:
@@ -156,15 +153,16 @@ public void setModel(List<? extends Number> model, ArrayFormat format) {
156153

157154
// array containing only y-vals. assume x = index:
158155
case Y_VALS_ONLY:
159-
for(Number n : model) {
160-
yVals.add(n);
156+
yVals.addAll(model);
157+
for(int i = 0; i < yVals.size(); i++) {
158+
xVals.add(i);
161159
}
162160
break;
163161

164162
// xy interleaved array:
165163
case XY_VALS_INTERLEAVED:
166164
if (xVals == null) {
167-
xVals = new LinkedList<Number>();
165+
xVals = new LinkedList<>();
168166
}
169167
if (model.size() % 2 != 0) {
170168
throw new IndexOutOfBoundsException("Cannot auto-generate series from odd-sized xy List.");
@@ -217,7 +215,6 @@ public void resize(int size) {
217215
try {
218216
lock.writeLock().lock();
219217
if (xVals.size() < size) {
220-
221218
for (int i = xVals.size(); i < size; i++) {
222219
xVals.add(null);
223220
yVals.add(null);

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import android.graphics.Paint;
2323
import android.graphics.PointF;
2424
import android.graphics.RectF;
25+
import android.support.annotation.NonNull;
26+
import android.support.annotation.Nullable;
2527

2628
import com.androidplot.R;
2729
import com.androidplot.Region;
@@ -492,18 +494,16 @@ protected float seriesToScreenY(Number y) {
492494
transform(y.doubleValue(), gridRect.bottom, gridRect.top, true);
493495
}
494496

497+
@Override
498+
protected void onResize(@Nullable RectF oldRect, @NonNull RectF newRect) {
499+
gridRect = RectFUtils.applyInsets(newRect, gridInsets);
500+
labelRect = RectFUtils.applyInsets(newRect, lineLabelInsets);
501+
}
502+
495503
@Override
496504
protected void doOnDraw(Canvas canvas, RectF widgetRect)
497505
throws PlotRenderException {
498506

499-
if(gridRect == null) {
500-
gridRect = RectFUtils.applyInsets(widgetRect, gridInsets);
501-
}
502-
503-
if(labelRect == null) {
504-
labelRect = RectFUtils.applyInsets(widgetRect, lineLabelInsets);
505-
}
506-
507507
// don't draw if we have no space to draw into
508508
if (gridRect.height() > ZERO && gridRect.width() > ZERO) {
509509
final RectRegion bounds = plot.getBounds();

0 commit comments

Comments
 (0)