Skip to content

Commit f58cd6b

Browse files
authored
1.5.5 changes (halfhp#79)
* remove unused button from simple xy plot * halfhp#78 Fixes issue where setting XYGraphWidget insets has no effect. * XYGraphWidget.drawMarkerText is now protected instead of private. * 1.5.5 documentation updates
1 parent c53ac24 commit f58cd6b

File tree

6 files changed

+107
-76
lines changed

6 files changed

+107
-76
lines changed

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

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,15 @@ protected float seriesToScreenY(Number y) {
530530

531531
@Override
532532
protected void onResize(@Nullable RectF oldRect, @NonNull RectF newRect) {
533-
gridRect = RectFUtils.applyInsets(newRect, gridInsets);
534-
labelRect = RectFUtils.applyInsets(newRect, lineLabelInsets);
533+
recalculateSizes(newRect);
534+
}
535+
536+
protected void recalculateSizes(@Nullable RectF rect) {
537+
if(rect == null) {
538+
rect = getWidgetDimensions().paddedRect;
539+
}
540+
gridRect = RectFUtils.applyInsets(rect, gridInsets);
541+
labelRect = RectFUtils.applyInsets(rect, lineLabelInsets);
535542
}
536543

537544
@Override
@@ -694,27 +701,29 @@ protected void drawGrid(Canvas canvas) {
694701
* @param x
695702
* @param y
696703
*/
697-
private void drawMarkerText(Canvas canvas, String text, ValueMarker marker,
704+
protected void drawMarkerText(Canvas canvas, String text, ValueMarker marker,
698705
float x, float y) {
699-
x += MARKER_LABEL_SPACING;
700-
y -= MARKER_LABEL_SPACING;
701-
RectF textRect = new RectF(FontUtils.getStringDimensions(
702-
text,
703-
marker.getTextPaint()
704-
));
705-
textRect.offsetTo(x, y - textRect.height());
706+
if (marker.getText() != null) {
707+
x += MARKER_LABEL_SPACING;
708+
y -= MARKER_LABEL_SPACING;
709+
RectF textRect = new RectF(FontUtils.getStringDimensions(
710+
text,
711+
marker.getTextPaint()
712+
));
713+
textRect.offsetTo(x, y - textRect.height());
714+
715+
if (textRect.right > gridRect.right) {
716+
textRect.offset(-(textRect.right - gridRect.right), ZERO);
717+
}
706718

707-
if (textRect.right > gridRect.right) {
708-
textRect.offset(-(textRect.right - gridRect.right), ZERO);
709-
}
719+
if (textRect.top < gridRect.top) {
720+
textRect.offset(0, gridRect.top - textRect.top);
721+
}
710722

711-
if (textRect.top < gridRect.top) {
712-
textRect.offset(0, gridRect.top - textRect.top);
723+
canvas.drawText(text, textRect.left, textRect.bottom,
724+
marker.getTextPaint()
725+
);
713726
}
714-
715-
canvas.drawText(text, textRect.left, textRect.bottom,
716-
marker.getTextPaint()
717-
);
718727
}
719728

720729
protected void drawMarkers(Canvas canvas) {
@@ -731,10 +740,7 @@ protected void drawMarkers(Canvas canvas) {
731740
float xPix = marker.getTextPosition().getPixelValue(
732741
gridRect.width());
733742
xPix += gridRect.left;
734-
735-
if (marker.getText() != null) {
736-
drawMarkerText(canvas, marker.getText(), marker, xPix, yPix);
737-
}
743+
drawMarkerText(canvas, marker.getText(), marker, xPix, yPix);
738744
}
739745
}
740746
}
@@ -1149,6 +1155,7 @@ public Insets getGridInsets() {
11491155

11501156
public void setGridInsets(Insets gridInsets) {
11511157
this.gridInsets = gridInsets;
1158+
recalculateSizes(null);
11521159
}
11531160

11541161
/**
@@ -1160,6 +1167,7 @@ public Insets getLineLabelInsets() {
11601167

11611168
public void setLineLabelInsets(Insets lineLabelInsets) {
11621169
this.lineLabelInsets = lineLabelInsets;
1170+
recalculateSizes(null);
11631171
}
11641172

11651173
public RectF getGridRect() {

androidplot-core/src/test/java/com/androidplot/xy/XYGraphWidgetTest.java

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.androidplot.test.*;
2323
import com.androidplot.ui.*;
24+
import com.androidplot.util.DisplayDimensions;
2425

2526
import org.junit.*;
2627
import org.mockito.*;
@@ -67,7 +68,7 @@ public class XYGraphWidgetTest extends AndroidplotTest {
6768

6869

6970
@Before
70-
public void setUp() throws Exception {
71+
public void setUp() {
7172
size = spy(new Size(100, SizeMode.ABSOLUTE, 100, SizeMode.ABSOLUTE));
7273
xyPlot = spy(new XYPlot(getContext(), "XYPlot"));
7374
when(xyPlot.getRegistry()).thenReturn(seriesRegistry);
@@ -83,21 +84,16 @@ public void setUp() throws Exception {
8384
graphWidget.setLabelRect(new RectF(0, 0, 100, 100));
8485
}
8586

86-
@After
87-
public void tearDown() throws Exception {
88-
89-
}
90-
9187
@Test
92-
public void testProcessAttrs() throws Exception {
88+
public void processAttrs_withDefaults_disablesGridClipping() {
9389
XYGraphWidget graphWidget = spy(new XYGraphWidget(layoutManager, xyPlot, size));
9490

9591
graphWidget.processAttrs(typedArray);
9692
verify(graphWidget, times(1)).setGridClippingEnabled(false);
9793
}
9894

9995
@Test
100-
public void testDoOnDraw_drawGridOnTopFalse() throws Exception {
96+
public void doOnDraw_drawGridOnTopFalse_drawsGridAfterData() throws Exception {
10197
XYGraphWidget graphWidget = spy(new XYGraphWidget(layoutManager, xyPlot, size));
10298
graphWidget.setDrawGridOnTop(false);
10399
doNothing().when(graphWidget).drawGrid(canvas);
@@ -113,7 +109,7 @@ public void testDoOnDraw_drawGridOnTopFalse() throws Exception {
113109
}
114110

115111
@Test
116-
public void testDoOnDraw_drawGridOnTopTrue() throws Exception {
112+
public void doOnDraw_drawGridOnTopTrue_drawsGridBeforeData() throws Exception {
117113
XYGraphWidget graphWidget = spy(new XYGraphWidget(layoutManager, xyPlot, size));
118114
graphWidget.setDrawGridOnTop(true);
119115
doNothing().when(graphWidget).drawGrid(canvas);
@@ -129,7 +125,7 @@ public void testDoOnDraw_drawGridOnTopTrue() throws Exception {
129125
}
130126

131127
@Test
132-
public void testDrawMarkers() throws Exception {
128+
public void drawMarkers_drawsLineForEachMarker() {
133129
xyPlot.addMarker(new XValueMarker(1, "x"));
134130
xyPlot.addMarker(new YValueMarker(-1, "y"));
135131

@@ -140,42 +136,22 @@ public void testDrawMarkers() throws Exception {
140136
.drawLine(anyFloat(), anyFloat(), anyFloat(), anyFloat(), any(Paint.class));
141137
}
142138

143-
protected void runDrawGridTest() {
144-
doNothing().when(graphWidget).
145-
drawDomainLine(any(Canvas.class), anyFloat(), any(Number.class), any(Paint.class), anyBoolean());
146-
147-
doNothing().when(graphWidget).
148-
drawRangeLine(any(Canvas.class), anyFloat(), any(Number.class), any(Paint.class), anyBoolean());
149-
150-
xyPlot.setRangeBoundaries(0, 100, BoundaryMode.FIXED);
151-
xyPlot.setDomainBoundaries(0, 100, BoundaryMode.FIXED);
152-
153-
graphWidget.drawGrid(canvas);
154-
155-
// expecting a 100x100 grid to be drawn:
156-
verify(graphWidget, times(100))
157-
.drawDomainLine(eq(canvas), anyFloat(), anyFloat(), any(Paint.class), eq(false));
158-
159-
verify(graphWidget, times(100))
160-
.drawRangeLine(eq(canvas), anyFloat(), anyFloat(), any(Paint.class), eq(false));
161-
}
162-
163139
@Test
164-
public void testDrawGrid_nullOrigin() throws Exception {
140+
public void drawGrid_nullOrigin_drawsGrid() {
165141
when(xyPlot.getDomainOrigin()).thenReturn(null);
166142
when(xyPlot.getRangeOrigin()).thenReturn(null);
167143
runDrawGridTest();
168144
}
169145

170146
@Test
171-
public void testDrawGrid_zeroOrigin() throws Exception {
147+
public void drawGrid_zeroOrigin_drawsGrid() {
172148
when(xyPlot.getDomainOrigin()).thenReturn(0);
173149
when(xyPlot.getRangeOrigin()).thenReturn(0);
174150
runDrawGridTest();
175151
}
176152

177153
@Test
178-
public void testDrawGrid_centeredOrigin() throws Exception {
154+
public void drawGrid_centeredOrigin_drawsGrid() {
179155

180156
// set origin to midpoint so we exercise
181157
// code to draw lines on both sides of the origin:
@@ -185,7 +161,7 @@ public void testDrawGrid_centeredOrigin() throws Exception {
185161
}
186162

187163
@Test
188-
public void testDrawCursors_ifCursorPaintAndPositionAreSet() throws Exception {
164+
public void drawCursors_withCursorPaintAndPosition_drawsCursorLines() {
189165
final Paint domainCursorPaint = new Paint();
190166
graphWidget.setDomainCursorPaint(domainCursorPaint);
191167

@@ -201,7 +177,7 @@ public void testDrawCursors_ifCursorPaintAndPositionAreSet() throws Exception {
201177
}
202178

203179
@Test
204-
public void testDrawCursors_ifCursorPaintAndPositionAreNotSet() throws Exception {
180+
public void testDrawCursors_noCursorPaintOrPosition_drawsNoCursorLines() {
205181
graphWidget.setDomainCursorPaint(null);
206182
graphWidget.setRangeCursorPaint(null);
207183

@@ -212,7 +188,7 @@ public void testDrawCursors_ifCursorPaintAndPositionAreNotSet() throws Exception
212188
}
213189

214190
@Test
215-
public void testDrawCursorLabel() throws Exception {
191+
public void drawCursorLabel_drawsText() {
216192
graphWidget.setDomainCursorPosition(0f);
217193
graphWidget.setRangeCursorPosition(0f);
218194
XYGraphWidget.CursorLabelFormatter clf = mock(XYGraphWidget.CursorLabelFormatter.class);
@@ -224,7 +200,7 @@ public void testDrawCursorLabel() throws Exception {
224200
}
225201

226202
@Test
227-
public void testSetLineLabelEdges() throws Exception {
203+
public void setLineLabelEdges_setsEdges() {
228204

229205
graphWidget.setLineLabelEdges(XYGraphWidget.Edge.LEFT, XYGraphWidget.Edge.BOTTOM);
230206
assertTrue(graphWidget.isLineLabelEnabled(XYGraphWidget.Edge.LEFT));
@@ -238,7 +214,7 @@ public void testSetLineLabelEdges() throws Exception {
238214
}
239215

240216
@Test
241-
public void testSetLineLabelEdges_bitfield() throws Exception {
217+
public void setLineLabelEdges_bitfield_setsEdges() {
242218

243219
graphWidget.setLineLabelEdges(
244220
XYGraphWidget.Edge.TOP.getValue() | XYGraphWidget.Edge.RIGHT.getValue());
@@ -250,7 +226,7 @@ public void testSetLineLabelEdges_bitfield() throws Exception {
250226
}
251227

252228
@Test
253-
public void testScreenToSeries() throws Exception {
229+
public void screenToSeries_returnsSeriesCoords() {
254230
when(xyPlot.getBounds()).thenReturn(new RectRegion(-100, 100, -100, 100));
255231

256232
XYCoords coords = graphWidget.screenToSeries(new PointF(0, 0));
@@ -267,7 +243,7 @@ public void testScreenToSeries() throws Exception {
267243
}
268244

269245
@Test
270-
public void testSeriesToScreen() throws Exception {
246+
public void seriesToScreen_returnsScreenPoint() {
271247
when(xyPlot.getBounds()).thenReturn(new RectRegion(-100, 100, -100, 100));
272248

273249
PointF point = graphWidget.seriesToScreen(new XYCoords(-100, 100));
@@ -284,7 +260,7 @@ public void testSeriesToScreen() throws Exception {
284260
}
285261

286262
@Test
287-
public void testScreenToSeriesX() throws Exception {
263+
public void screenToSeriesX_returnsSeriesValue() {
288264
when(xyPlot.getBounds()).thenReturn(new RectRegion(-100, 100, -100, 100));
289265

290266
assertEquals(-100, graphWidget.screenToSeriesX(new PointF(0, 0)).intValue());
@@ -293,7 +269,7 @@ public void testScreenToSeriesX() throws Exception {
293269
}
294270

295271
@Test
296-
public void testScreenToSeriesY() throws Exception {
272+
public void screenToSeriesY_returnsSeriesValue() {
297273
when(xyPlot.getBounds()).thenReturn(new RectRegion(-100, 100, -100, 100));
298274

299275
assertEquals(100, graphWidget.screenToSeriesY(new PointF(0, 0)).intValue());
@@ -302,7 +278,7 @@ public void testScreenToSeriesY() throws Exception {
302278
}
303279

304280
@Test
305-
public void testSeriesToScreenX() throws Exception {
281+
public void seriesToScreenX_returnsScreenValue() {
306282
when(xyPlot.getBounds()).thenReturn(new RectRegion(-100, 100, -100, 100));
307283

308284
assertEquals(0f, graphWidget.seriesToScreenX(-100));
@@ -311,11 +287,45 @@ public void testSeriesToScreenX() throws Exception {
311287
}
312288

313289
@Test
314-
public void testSeriesToScreenY() throws Exception {
290+
public void seriesToScreenY_returnsScreenValue() {
315291
when(xyPlot.getBounds()).thenReturn(new RectRegion(-100, 100, -100, 100));
316292

317293
assertEquals(100f, graphWidget.seriesToScreenY(100));
318294
assertEquals(0f, graphWidget.seriesToScreenY(-100));
319295
assertEquals(50f, graphWidget.seriesToScreenY(0));
320296
}
297+
298+
@Test
299+
public void setGridInsets_updatesGridRect() {
300+
graphWidget.setGridInsets(new Insets(0, 0, 0, 0));
301+
final RectF oldRect = graphWidget.getGridRect();
302+
303+
graphWidget.setGridInsets(new Insets(2, 2, 2, 2));
304+
final RectF newRect = graphWidget.getGridRect();
305+
306+
assertEquals(oldRect.left + 2, newRect.left);
307+
assertEquals(oldRect.top + 2, newRect.top);
308+
assertEquals(oldRect.right - 2, newRect.right);
309+
assertEquals(oldRect.bottom -2, newRect.bottom);
310+
}
311+
312+
private void runDrawGridTest() {
313+
doNothing().when(graphWidget).
314+
drawDomainLine(any(Canvas.class), anyFloat(), any(Number.class), any(Paint.class), anyBoolean());
315+
316+
doNothing().when(graphWidget).
317+
drawRangeLine(any(Canvas.class), anyFloat(), any(Number.class), any(Paint.class), anyBoolean());
318+
319+
xyPlot.setRangeBoundaries(0, 100, BoundaryMode.FIXED);
320+
xyPlot.setDomainBoundaries(0, 100, BoundaryMode.FIXED);
321+
322+
graphWidget.drawGrid(canvas);
323+
324+
// expecting a 100x100 grid to be drawn:
325+
verify(graphWidget, times(100))
326+
.drawDomainLine(eq(canvas), anyFloat(), anyFloat(), any(Paint.class), eq(false));
327+
328+
verify(graphWidget, times(100))
329+
.drawRangeLine(eq(canvas), anyFloat(), anyFloat(), any(Paint.class), eq(false));
330+
}
321331
}

demoapp/src/main/java/com/androidplot/demos/SimpleXYPlotActivity.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
import android.os.Bundle;
2222
import android.support.annotation.NonNull;
2323

24-
import com.androidplot.ui.Size;
25-
import com.androidplot.ui.SizeMode;
24+
import com.androidplot.ui.Insets;
2625
import com.androidplot.util.PixelUtils;
2726
import com.androidplot.xy.CatmullRomInterpolator;
2827
import com.androidplot.xy.LineAndPointFormatter;
@@ -102,5 +101,19 @@ public Object parseObject(String source, @NonNull ParsePosition pos) {
102101
return null;
103102
}
104103
});
104+
105+
new Thread(new Runnable() {
106+
107+
@Override
108+
public void run() {
109+
try {
110+
Thread.sleep(2000);
111+
} catch (InterruptedException e) {
112+
throw new RuntimeException(e);
113+
}
114+
plot.getGraph().setGridInsets(new Insets(120, 120, 120, 120));
115+
plot.redraw();
116+
}
117+
}).start();
105118
}
106119
}

demoapp/src/main/res/layout/simple_xy_plot_example.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,4 @@
3333
ap:lineLabelRotationBottom="-45"
3434
android:layout_weight="1"/>
3535

36-
<Button
37-
android:id="@+id/hide_button"
38-
android:layout_width="match_parent"
39-
android:layout_height="300dp"
40-
android:text="HIDE"/>
41-
4236
</LinearLayout>

docs/quickstart.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ To use the library in your gradle project add the following to your build.gradle
1414

1515
```groovy
1616
dependencies {
17-
compile "com.androidplot:androidplot-core:1.5.4"
17+
compile "com.androidplot:androidplot-core:1.5.5"
1818
}
1919
```
2020

docs/release_notes.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
For details on what to expect in general when updating to a new version of Androiplot, check out the
33
[versioning doc](versioning.md).
44

5+
# 1.5.5
6+
7+
* (#76) Fixed a bug that could cause a deadlock when grid steps are much larger than actual plot range.
8+
* (#78) Fixed a bug where setting insets on XYGraphWidget would have no effect after the plot was drawn.
9+
* XYGraphWidget.drawMarkerText is now marked `protected`.
10+
511
# 1.5.4
612

713
* (#69) Fixed a bug in `SimpleXYPlot` preventing the resizing of `Y_VALS_ONLY` formatted series.

0 commit comments

Comments
 (0)