Skip to content

Commit 2eed51b

Browse files
committed
Refactored AxisValueLabelFormatter and associated methods in XYGraphWidget to be NumberLabelFormatter.
1 parent 9c248fe commit 2eed51b

3 files changed

Lines changed: 132 additions & 72 deletions

File tree

androidplot-core/src/main/java/com/androidplot/xy/AxisValueLabelFormatter.java renamed to androidplot-core/src/main/java/com/androidplot/NumberLabelFormatter.java

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
1-
/*
2-
* Copyright 2015 AndroidPlot.com
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
17-
package com.androidplot.xy;
18-
19-
public class AxisValueLabelFormatter {
20-
//private Paint textPaint;
21-
private int color;
22-
23-
public AxisValueLabelFormatter(int color) {
24-
this.color = color;
25-
}
26-
27-
public int getColor() {
28-
return color;
29-
}
30-
31-
public void setColor(int color) {
32-
this.color = color;
33-
}
34-
}
1+
/*
2+
* Copyright 2016 AndroidPlot.com
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.androidplot;
18+
19+
import android.graphics.Paint;
20+
21+
public interface NumberLabelFormatter {
22+
23+
24+
/**
25+
*
26+
* @param value The value being rendered by this formatter.
27+
* @return Paint instance that should be used to render the specified value.
28+
*/
29+
Paint getPaint(Number value);
30+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2016 AndroidPlot.com
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.androidplot;
18+
19+
import android.graphics.Color;
20+
import android.graphics.Paint;
21+
import com.androidplot.util.PixelUtils;
22+
23+
/**
24+
* A basic implementation of a {@link NumberLabelFormatter}.
25+
*/
26+
public class SimpleNumberLabelFormatter implements NumberLabelFormatter {
27+
28+
private static final int DEFAULT_TEXT_SIZE_SP = 12;
29+
private static final int DEFAULT_STROKE_SIZE_DP = 2;
30+
private Paint paint;
31+
32+
public SimpleNumberLabelFormatter() {
33+
this(new Paint());
34+
getPaint().setColor(Color.WHITE);
35+
getPaint().setTextSize(PixelUtils.spToPix(DEFAULT_TEXT_SIZE_SP));
36+
getPaint().setStrokeWidth(PixelUtils.dpToPix(DEFAULT_STROKE_SIZE_DP));
37+
}
38+
39+
public SimpleNumberLabelFormatter(int color) {
40+
this();
41+
getPaint().setColor(color);
42+
}
43+
44+
public SimpleNumberLabelFormatter(Paint paint) {
45+
this.paint = paint;
46+
}
47+
48+
public Paint getPaint() {
49+
return paint;
50+
}
51+
52+
public void setPaint(Paint paint) {
53+
this.paint = paint;
54+
}
55+
56+
@Override
57+
public Paint getPaint(Number value) {
58+
return getPaint();
59+
}
60+
}

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

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import android.graphics.*;
2020

21+
import com.androidplot.NumberLabelFormatter;
2122
import com.androidplot.exception.PlotRenderException;
2223
import com.androidplot.ui.BoxModel;
2324
import com.androidplot.ui.LayoutManager;
@@ -106,11 +107,11 @@ public class XYGraphWidget extends Widget {
106107
private Mapping<Paint, Number> domainTickLabelPaintMap;
107108
private Mapping<Paint, Number> rangeTickLabelPaintMap;
108109

109-
private ZHash<RectRegion, AxisValueLabelFormatter> axisValueLabelRegions;
110+
private ZHash<RectRegion, NumberLabelFormatter> tickLabelRegionFormatters;
110111

111112
private RenderStack<? extends XYSeries, ? extends XYSeriesFormatter> renderStack;
112113

113-
private static final float DEFAULT_TICK_LABEL_TEXT_SIZE_PX = PixelUtils.spToPix(15); // 15sp
114+
private static final float DEFAULT_TICK_LABEL_TEXT_SIZE_PX = PixelUtils.spToPix(15);
114115

115116
public float getRangeLabelOrientation() {
116117
return rangeLabelOrientation;
@@ -253,7 +254,7 @@ public void setRangeCursorPaint(Paint rangeCursorPaint) {
253254
setMarginBottom(4);
254255
rangeValueFormat = new DecimalFormat("0.0");
255256
domainValueFormat = new DecimalFormat("0.0");
256-
axisValueLabelRegions = new ZHash<>();
257+
tickLabelRegionFormatters = new ZHash<>();
257258
setClippingEnabled(true);
258259
}
259260

@@ -263,26 +264,25 @@ public XYGraphWidget(LayoutManager layoutManager, XYPlot plot, Size size) {
263264
renderStack = new RenderStack(plot);
264265
}
265266

266-
public ZIndexable<RectRegion> getAxisValueLabelRegions() {
267-
return axisValueLabelRegions;
267+
public ZIndexable<RectRegion> getTickLabelRegionFormatters() {
268+
return tickLabelRegionFormatters;
268269
}
269270

270271
/**
271-
* Add a new Region used for rendering axis valuelabels. Note that it is
272-
* possible to add multiple Region instances which overlap, in which cast
272+
* Add a new Region used for rendering tick labels. Note that it is
273+
* possible to add multiple Region instances which overlap, in which case
273274
* the last region to be added will be used. It is up to the developer to
274-
* guard against this often undesireable situation.
275+
* guard against this often undesirable situation.
275276
*
276277
* @param region
277278
* @param formatter
278279
*/
279-
public void addAxisValueLabelRegion(RectRegion region,
280-
AxisValueLabelFormatter formatter) {
281-
axisValueLabelRegions.addToTop(region, formatter);
280+
public void addTickLabelFormatter(RectRegion region, NumberLabelFormatter formatter) {
281+
tickLabelRegionFormatters.addToTop(region, formatter);
282282
}
283283

284284
/**
285-
* Convenience method - wraps addAxisValueLabelRegion, using
285+
* Convenience method - wraps addDomainTickLabelFormatter, using
286286
* Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY to mask off range
287287
* axis value labels.
288288
*
@@ -291,25 +291,23 @@ public void addAxisValueLabelRegion(RectRegion region,
291291
* @param formatter
292292
*
293293
*/
294-
public void addDomainAxisValueLabelRegion(double min, double max,
295-
AxisValueLabelFormatter formatter) {
296-
addAxisValueLabelRegion(new RectRegion(min, max,
294+
public void addDomainTickLabelFormatter(double min, double max, NumberLabelFormatter formatter) {
295+
addTickLabelFormatter(new RectRegion(min, max,
297296
Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, null),
298297
formatter);
299298
}
300299

301300
/**
302-
* Convenience method - wraps addAxisValueLabelRegion, using
301+
* Convenience method - wraps addDomainTickLabelFormatter, using
303302
* Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY to mask off domain
304303
* axis value labels.
305304
*
306305
* @param min
307306
* @param max
308307
* @param formatter
309308
*/
310-
public void addRangeAxisValueLabelRegion(double min, double max,
311-
AxisValueLabelFormatter formatter) {
312-
addAxisValueLabelRegion(new RectRegion(Double.POSITIVE_INFINITY,
309+
public void addRangeTickLabelFormatter(double min, double max, NumberLabelFormatter formatter) {
310+
addTickLabelFormatter(new RectRegion(Double.POSITIVE_INFINITY,
313311
Double.NEGATIVE_INFINITY, min, max, null), formatter);
314312
}
315313

@@ -322,31 +320,40 @@ public void addRangeAxisValueLabelRegion(double min, double max,
322320
* @return the formatter associated with the first (bottom) region
323321
* containing x and y. null otherwise.
324322
*/
325-
public AxisValueLabelFormatter getAxisValueLabelFormatterForVal(double x,
326-
double y) {
327-
for (RectRegion r : axisValueLabelRegions.elements()) {
323+
public NumberLabelFormatter getTickLabelFormatter(double x, double y) {
324+
for (RectRegion r : tickLabelRegionFormatters.elements()) {
328325
if (r.containsValue(x, y)) {
329-
return axisValueLabelRegions.get(r);
326+
return tickLabelRegionFormatters.get(r);
330327
}
331328
}
332329
return null;
333330
}
334331

335-
public AxisValueLabelFormatter getAxisValueLabelFormatterForDomainVal(
332+
/**
333+
*
334+
* @param val domain value
335+
* @return
336+
*/
337+
public NumberLabelFormatter getDomainTickLabelFormatter(
336338
double val) {
337-
for (RectRegion r : axisValueLabelRegions.elements()) {
339+
for (RectRegion r : tickLabelRegionFormatters.elements()) {
338340
if (r.containsDomainValue(val)) {
339-
return axisValueLabelRegions.get(r);
341+
return tickLabelRegionFormatters.get(r);
340342
}
341343
}
342344
return null;
343345
}
344346

345-
public AxisValueLabelFormatter getAxisValueLabelFormatterForRangeVal(
347+
/**
348+
*
349+
* @param val range value
350+
* @return
351+
*/
352+
public NumberLabelFormatter getRangeTickLabelFormatter(
346353
double val) {
347-
for (RectRegion r : axisValueLabelRegions.elements()) {
354+
for (RectRegion r : tickLabelRegionFormatters.elements()) {
348355
if (r.containsRangeValue(val)) {
349-
return axisValueLabelRegions.get(r);
356+
return tickLabelRegionFormatters.get(r);
350357
}
351358
}
352359
return null;
@@ -451,20 +458,20 @@ private void calculateGridDimensions(RectF widgetRect) {
451458

452459
private void drawTickText(Canvas canvas, XYAxisType axis, Number value,
453460
float xPix, float yPix, Paint labelPaint) {
454-
AxisValueLabelFormatter rf = null;
455-
String txt = null;
461+
NumberLabelFormatter formatter;
462+
String txt;
456463
double v = value.doubleValue();
457464

458465
int canvasState = canvas.save();
459466
try {
460467
switch (axis) {
461468
case DOMAIN:
462-
rf = getAxisValueLabelFormatterForDomainVal(v);
469+
formatter = getDomainTickLabelFormatter(v);
463470
txt = getFormattedDomainValue(value);
464471
canvas.rotate(getDomainLabelOrientation(), xPix, yPix);
465472
break;
466473
case RANGE:
467-
rf = getAxisValueLabelFormatterForRangeVal(v);
474+
formatter = getRangeTickLabelFormatter(v);
468475
txt = getFormattedRangeValue(value);
469476
canvas.rotate(getRangeLabelOrientation(), xPix, yPix);
470477
break;
@@ -476,9 +483,8 @@ private void drawTickText(Canvas canvas, XYAxisType axis, Number value,
476483
// of labelPaint and use the formatter's color. Otherwise
477484
// just use labelPaint:
478485
Paint p;
479-
if (rf != null) {
480-
p = new Paint(labelPaint);
481-
p.setColor(rf.getColor());
486+
if (formatter != null) {
487+
p = formatter.getPaint(value);
482488
} else {
483489
p = labelPaint;
484490
}
@@ -591,7 +597,6 @@ protected void drawGrid(Canvas canvas) {
591597
paddedGridRect, plot.getCalculatedMinX().doubleValue(), plot
592598
.getCalculatedMaxX().doubleValue());
593599

594-
595600
// draw domain origin:
596601
if (domainOriginF >= paddedGridRect.left
597602
&& domainOriginF <= paddedGridRect.right) {
@@ -764,7 +769,6 @@ private void drawMarkerText(Canvas canvas, String text, ValueMarker marker,
764769

765770
canvas.drawText(text, textRect.left, textRect.bottom,
766771
marker.getTextPaint());
767-
768772
}
769773

770774
protected void drawMarkers(Canvas canvas) {

0 commit comments

Comments
 (0)