Skip to content

Commit 7792f5e

Browse files
committed
* Added new attrs for domain/range step.
1 parent 40d95e7 commit 7792f5e

5 files changed

Lines changed: 144 additions & 36 deletions

File tree

androidplot-core/src/main/java/com/androidplot/util/AttrUtils.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import android.util.TypedValue;
2222
import com.androidplot.ui.*;
2323
import com.androidplot.ui.widget.Widget;
24+
import com.androidplot.xy.XYStepMode;
25+
import com.androidplot.xy.XYStepModel;
2426

2527
/**
2628
* Methods for applying styleable attributes.
@@ -112,7 +114,7 @@ public static void configureSize(TypedArray attrs, Size model, int heightSizeLay
112114

113115
private static void configureSizeMetric(TypedArray attrs, SizeMetric model, int typeAttr, int valueAttr) {
114116

115-
final float value = getFloatOrDimenValue(attrs, valueAttr, model.getValue());
117+
final float value = getIntFloatDimenValue(attrs, valueAttr, model.getValue()).floatValue();
116118
final SizeLayoutType sizeLayoutType =
117119
getSizeLayoutType(attrs, typeAttr, model.getLayoutType());
118120

@@ -146,35 +148,38 @@ public static void configureWidget(TypedArray attrs, Widget widget, int heightSi
146148
* @param yLayoutValueAttr
147149
* @param anchorPositionAttr
148150
*/
149-
public static void configurePositionMetrics(TypedArray attrs, PositionMetrics metrics, int xLayoutStyleAttr, int xLayoutValueAttr,
150-
int yLayoutStyleAttr, int yLayoutValueAttr, int anchorPositionAttr) {
151+
public static void configurePositionMetrics(TypedArray attrs, PositionMetrics metrics, int xLayoutStyleAttr,
152+
int xLayoutValueAttr, int yLayoutStyleAttr, int yLayoutValueAttr,
153+
int anchorPositionAttr) {
151154
if(attrs != null) {
152155
metrics.getXPositionMetric().set(
153-
getFloatOrDimenValue(attrs, xLayoutValueAttr, metrics.getXPositionMetric().getValue()),
156+
getIntFloatDimenValue(attrs, xLayoutValueAttr, metrics.getXPositionMetric().getValue()).floatValue(),
154157
getXLayoutStyle(attrs, xLayoutStyleAttr, metrics.getXPositionMetric().getLayoutType()));
155158

156159
metrics.getYPositionMetric().set(
157-
getFloatOrDimenValue(attrs, yLayoutValueAttr, metrics.getYPositionMetric().getValue()),
160+
getIntFloatDimenValue(attrs, yLayoutValueAttr, metrics.getYPositionMetric().getValue()).floatValue(),
158161
getYLayoutStyle(attrs, yLayoutStyleAttr, metrics.getYPositionMetric().getLayoutType()));
159162
metrics.setAnchor(getAnchorPosition(attrs, anchorPositionAttr, metrics.getAnchor()));
160163
}
161164
}
162165

163166
/**
164-
* Convenience method to retrieve float values from xml that can be entered as either a float or a dimen.
167+
* Convenience method to retrieve values from xml that can be entered as a int, float or dimen.
165168
* @param attrs
166169
* @param valueAttr
167170
* @param defaultValue
168171
* @return
169172
*/
170-
private static float getFloatOrDimenValue(TypedArray attrs, int valueAttr, float defaultValue) {
171-
float result = defaultValue;
173+
private static Number getIntFloatDimenValue(TypedArray attrs, int valueAttr, Number defaultValue) {
174+
Number result = defaultValue;
172175
if(attrs != null && attrs.hasValue(valueAttr)) {
173176
final int valueType = attrs.peekValue(valueAttr).type;
174177
if (valueType == TypedValue.TYPE_DIMENSION) {
175-
result = attrs.getDimension(valueAttr, defaultValue);
178+
result = attrs.getDimension(valueAttr, defaultValue.floatValue());
179+
} else if(valueType == TypedValue.TYPE_INT_DEC) {
180+
result = attrs.getInt(valueAttr, defaultValue.intValue());
176181
} else if (valueType == TypedValue.TYPE_FLOAT) {
177-
result = attrs.getFloat(valueAttr, defaultValue);
182+
result = attrs.getFloat(valueAttr, defaultValue.floatValue());
178183
} else {
179184
throw new IllegalArgumentException("Invalid value type - must be float or dimension.");
180185
}
@@ -193,4 +198,11 @@ private static YLayoutStyle getYLayoutStyle(TypedArray attrs, int attr, YLayoutS
193198
private static AnchorPosition getAnchorPosition(TypedArray attrs, int attr, AnchorPosition defaultValue) {
194199
return AnchorPosition.values()[attrs.getInt(attr, defaultValue.ordinal())];
195200
}
201+
202+
public static void configureStep(TypedArray attrs, XYStepModel model, int stepModeAttr, int stepValueAttr) {
203+
if(attrs != null) {
204+
model.setMode(XYStepMode.values()[attrs.getInt(stepModeAttr, model.getMode().ordinal())]);
205+
model.setValue(getIntFloatDimenValue(attrs, stepValueAttr, model.getValue()).doubleValue());
206+
}
207+
}
196208
}

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

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,13 @@ public class XYPlot extends Plot<XYSeries, XYSeriesFormatter, XYSeriesRenderer>
8181
private TextLabelWidget domainLabelWidget;
8282
private TextLabelWidget rangeLabelWidget;
8383

84-
private XYStepMode domainStepMode = XYStepMode.SUBDIVIDE;
85-
private double domainStepValue = 10;
84+
//private XYStepMode domainStepMode = XYStepMode.SUBDIVIDE;
85+
//private double domainStepValue = 10;
8686

87-
private XYStepMode rangeStepMode = XYStepMode.SUBDIVIDE;
88-
private double rangeStepValue = 10;
87+
//private XYStepMode rangeStepMode = XYStepMode.SUBDIVIDE;
88+
//private double rangeStepValue = 10;
89+
private XYStepModel domainStepModel;
90+
private XYStepModel rangeStepModel;
8991

9092
// user settable min/max values
9193
private Number userMinX;
@@ -267,6 +269,9 @@ protected void onPreInit() {
267269
addSeries(new SimpleXYSeries(Arrays.asList(3, 3, 2, 3, 3), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Blue"),
268270
new LineAndPointFormatter(Color.BLUE, null, null, null));
269271
}
272+
273+
domainStepModel = new XYStepModel(XYStepMode.SUBDIVIDE, 10);
274+
rangeStepModel = new XYStepModel(XYStepMode.SUBDIVIDE, 10);
270275
}
271276

272277
@Override
@@ -281,6 +286,12 @@ protected void processAttrs(TypedArray attrs) {
281286
setRangeLabel(rangeLabelAttr);
282287
}
283288

289+
AttrUtils.configureStep(attrs, getDomainStepModel(),
290+
R.styleable.xy_XYPlot_domainStepMode, R.styleable.xy_XYPlot_domainStep);
291+
292+
AttrUtils.configureStep(attrs, getRangeStepModel(),
293+
R.styleable.xy_XYPlot_rangeStepMode, R.styleable.xy_XYPlot_rangeStep);
294+
284295

285296
// domainLabel size & position
286297
AttrUtils.configureWidget(attrs, getDomainLabelWidget(),
@@ -810,19 +821,19 @@ public void setTicksPerDomainLabel(int ticksPerDomainLabel) {
810821
}
811822

812823
public XYStepMode getDomainStepMode() {
813-
return domainStepMode;
824+
return domainStepModel.getMode();
814825
}
815826

816827
public void setDomainStepMode(XYStepMode domainStepMode) {
817-
this.domainStepMode = domainStepMode;
828+
domainStepModel.setMode(domainStepMode);
818829
}
819830

820831
public double getDomainStepValue() {
821-
return domainStepValue;
832+
return domainStepModel.getValue();
822833
}
823834

824835
public void setDomainStepValue(double domainStepValue) {
825-
this.domainStepValue = domainStepValue;
836+
domainStepModel.setValue(domainStepValue);
826837
}
827838

828839
public void setDomainStep(XYStepMode mode, double value) {
@@ -831,19 +842,19 @@ public void setDomainStep(XYStepMode mode, double value) {
831842
}
832843

833844
public XYStepMode getRangeStepMode() {
834-
return rangeStepMode;
845+
return rangeStepModel.getMode();
835846
}
836847

837848
public void setRangeStepMode(XYStepMode rangeStepMode) {
838-
this.rangeStepMode = rangeStepMode;
849+
rangeStepModel.setMode(rangeStepMode);
839850
}
840851

841852
public double getRangeStepValue() {
842-
return rangeStepValue;
853+
return rangeStepModel.getValue();
843854
}
844855

845856
public void setRangeStepValue(double rangeStepValue) {
846-
this.rangeStepValue = rangeStepValue;
857+
rangeStepModel.setValue(rangeStepValue);
847858
}
848859

849860
public void setRangeStep(XYStepMode mode, double value) {
@@ -1404,4 +1415,20 @@ public Number getDomainRightMax() {
14041415
public synchronized void setDomainRightMax(Number domainRightMax) {
14051416
this.domainRightMax = domainRightMax;
14061417
}
1418+
1419+
public XYStepModel getDomainStepModel() {
1420+
return domainStepModel;
1421+
}
1422+
1423+
public void setDomainStepModel(XYStepModel domainStepModel) {
1424+
this.domainStepModel = domainStepModel;
1425+
}
1426+
1427+
public XYStepModel getRangeStepModel() {
1428+
return rangeStepModel;
1429+
}
1430+
1431+
public void setRangeStepModel(XYStepModel rangeStepModel) {
1432+
this.rangeStepModel = rangeStepModel;
1433+
}
14071434
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
/**
20+
* Encapsulates a set of stepping parameters for a single axis.
21+
*/
22+
public class XYStepModel {
23+
24+
public XYStepModel(XYStepMode mode, double value) {
25+
setMode(mode);
26+
setValue(value);
27+
}
28+
29+
private XYStepMode mode;
30+
private double value;
31+
32+
public XYStepMode getMode() {
33+
return mode;
34+
}
35+
36+
public void setMode(XYStepMode mode) {
37+
this.mode = mode;
38+
}
39+
40+
public double getValue() {
41+
return value;
42+
}
43+
44+
public void setValue(double value) {
45+
this.value = value;
46+
}
47+
}

androidplot-core/src/main/res/values/attrs.xml

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@
2222
<enum name="use_main_thread" value="0"/>
2323
</attr>
2424

25+
<attr name="domainStepMode" format="enum">
26+
<enum name="subdivide" value="0"/>
27+
<enum name="increment_by_val" value="1"/>
28+
<enum name="increment_by_pixels" value="2"/>
29+
</attr>
30+
31+
<attr name="rangeStepMode" format="enum">
32+
<enum name="subdivide" value="0"/>
33+
<enum name="increment_by_val" value="1"/>
34+
<enum name="increment_by_pixels" value="2"/>
35+
</attr>
36+
2537
<!-- values must match the ordinal of its corresponding
2638
element in the AnchorPosition enum. -->
2739
<attr name="domainLabelAnchorPosition" format="enum">
@@ -176,18 +188,24 @@
176188
<!-- XYPlot -->
177189
<declare-styleable name="xy.XYPlot" parent="@style/Plot">
178190

191+
<attr name="domainStepMode"/>
192+
<attr name="domainStep" format="dimension|float|integer"/>
193+
194+
<attr name="rangeStepMode"/>
195+
<attr name="rangeStep" format="dimension|float|integer"/>
196+
179197
<!-- domain label -->
180198
<attr name="domainLabel" format="string"/>
181199
<attr name="domainLabelTextColor" format="color"/>
182200
<attr name="domainLabelTextSize" format="dimension"/>
183201
<attr name="domainLabelHeightSizeLayoutType"/>
184202
<attr name="domainLabelWidthSizeLayoutType"/>
185-
<attr name="domainLabelHeight" format="dimension|float"/>
186-
<attr name="domainLabelWidth" format="dimension|float"/>
203+
<attr name="domainLabelHeight" format="dimension|float|integer"/>
204+
<attr name="domainLabelWidth" format="dimension|float|integer"/>
187205
<attr name="domainLabelLayoutStyleX"/>
188206
<attr name="domainLabelLayoutStyleY"/>
189-
<attr name="domainLabelPositionX" format="dimension|float"/>
190-
<attr name="domainLabelPositionY" format="dimension|float"/>
207+
<attr name="domainLabelPositionX" format="dimension|float|integer"/>
208+
<attr name="domainLabelPositionY" format="dimension|float|integer"/>
191209
<attr name="domainLabelAnchorPosition"/>
192210
<attr name="domainLabelVisible" format="boolean"/>
193211

@@ -197,12 +215,12 @@
197215
<attr name="rangeLabelTextSize" format="dimension"/>
198216
<attr name="rangeLabelHeightSizeLayoutType"/>
199217
<attr name="rangeLabelWidthSizeLayoutType"/>
200-
<attr name="rangeLabelHeight" format="dimension|float"/>
201-
<attr name="rangeLabelWidth" format="dimension|float"/>
218+
<attr name="rangeLabelHeight" format="dimension|float|integer"/>
219+
<attr name="rangeLabelWidth" format="dimension|float|integer"/>
202220
<attr name="rangeLabelLayoutStyleX"/>
203221
<attr name="rangeLabelLayoutStyleY"/>
204-
<attr name="rangeLabelPositionX" format="dimension|float"/>
205-
<attr name="rangeLabelPositionY" format="dimension|float"/>
222+
<attr name="rangeLabelPositionX" format="dimension|float|integer"/>
223+
<attr name="rangeLabelPositionY" format="dimension|float|integer"/>
206224
<attr name="rangeLabelAnchorPosition"/>
207225
<attr name="rangeLabelVisible" format="boolean"/>
208226

@@ -244,19 +262,19 @@
244262
<!-- legend widget -->
245263
<attr name="legendHeightSizeLayoutType"/>
246264
<attr name="legendWidthSizeLayoutType"/>
247-
<attr name="legendHeight" format="dimension|float"/>
248-
<attr name="legendWidth" format="dimension|float"/>
265+
<attr name="legendHeight" format="dimension|float|integer"/>
266+
<attr name="legendWidth" format="dimension|float|integer"/>
249267
<attr name="legendLayoutStyleX"/>
250268
<attr name="legendLayoutStyleY"/>
251-
<attr name="legendPositionX" format="dimension|float"/>
252-
<attr name="legendPositionY" format="dimension|float"/>
269+
<attr name="legendPositionX" format="dimension|float|integer"/>
270+
<attr name="legendPositionY" format="dimension|float|integer"/>
253271
<attr name="legendAnchorPosition"/>
254272
<attr name="legendTextSize" format="dimension"/>
255273
<attr name="legendTextColor" format="color"/>
256274
<attr name="legendIconHeightSizeLayoutType"/>
257275
<attr name="legendIconWidthSizeLayoutType"/>
258-
<attr name="legendIconHeight" format="dimension|float"/>
259-
<attr name="legendIconWidth" format="dimension|float"/>
276+
<attr name="legendIconHeight" format="dimension|float|integer"/>
277+
<attr name="legendIconWidth" format="dimension|float|integer"/>
260278
<attr name="legendVisible" format="boolean"/>
261279
</declare-styleable>
262280

demoapp-wearable/src/main/res/layout/activity_main.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
ap:label="Wearable"
2929
ap:rangeLabel="range"
3030
ap:domainLabel="domain"
31+
ap:domainStepMode="subdivide"
32+
ap:domainStep="2"
33+
ap:rangeStepMode="subdivide"
34+
ap:rangeStep="3"
3135
ap:rangeLabelTextSize="13sp"
3236
ap:domainLabelTextSize="13sp"
3337
ap:domainLabelLayoutStyleX="relative_from_center"

0 commit comments

Comments
 (0)