Skip to content

Commit 0dbfcf5

Browse files
authored
Merge pull request #345 from AppDevNext/limit-range
Enable vertical limit range shading
2 parents 90b1b49 + 80080ef commit 0dbfcf5

17 files changed

+504
-42
lines changed

MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ public abstract class AxisBase extends ComponentBase {
108108
*/
109109
protected List<LimitLine> mLimitLines;
110110

111+
/**
112+
* array of limit ranges that can be set for the axis
113+
*/
114+
protected List<LimitRange> mLimitRanges;
115+
111116
/**
112117
* flag indicating the limit lines layer depth
113118
*/
@@ -204,6 +209,7 @@ public AxisBase() {
204209
this.mXOffset = Utils.convertDpToPixel(5f);
205210
this.mYOffset = Utils.convertDpToPixel(5f);
206211
this.mLimitLines = new ArrayList<LimitLine>();
212+
this.mLimitRanges = new ArrayList<LimitRange>();
207213
}
208214

209215
/**
@@ -453,6 +459,18 @@ public void addLimitLine(LimitLine l) {
453459
}
454460
}
455461

462+
/**
463+
* Adds a new LimitLine to this axis.
464+
*/
465+
public void addLimitRange(LimitRange l) {
466+
mLimitRanges.add(l);
467+
468+
if (mLimitRanges.size() > 6) {
469+
Log.e("MPAndroiChart",
470+
"Warning! You have more than 6 LimitLines on your axis, do you really want that?");
471+
}
472+
}
473+
456474
/**
457475
* Removes the specified LimitLine from the axis.
458476
*
@@ -469,6 +487,22 @@ public void removeAllLimitLines() {
469487
mLimitLines.clear();
470488
}
471489

490+
/**
491+
* Removes the specified LimitRange from the axis.
492+
*
493+
* @param l
494+
*/
495+
public void removeLimitRange(LimitRange l) {
496+
mLimitRanges.remove(l);
497+
}
498+
499+
/**
500+
* Removes all LimitLines from the axis.
501+
*/
502+
public void removeAllLimitRanges() {
503+
mLimitRanges.clear();
504+
}
505+
472506
/**
473507
* Returns the LimitLines of this axis.
474508
*
@@ -478,6 +512,15 @@ public List<LimitLine> getLimitLines() {
478512
return mLimitLines;
479513
}
480514

515+
/**
516+
* Returns the LimitRanges of this axis.
517+
*
518+
* @return
519+
*/
520+
public List<LimitRange> getLimitRanges() {
521+
return mLimitRanges;
522+
}
523+
481524
/**
482525
* If this is set to true, the LimitLines are drawn behind the actual data,
483526
* otherwise on top. Default: false
Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
2+
package com.github.mikephil.charting.components;
3+
4+
import android.graphics.Color;
5+
import android.graphics.DashPathEffect;
6+
import android.graphics.Paint;
7+
8+
import com.github.mikephil.charting.utils.Utils;
9+
10+
/**
11+
* The limit line is an additional feature for all Line-, Bar- and
12+
* ScatterCharts. It allows the displaying of an additional line in the chart
13+
* that marks a certain maximum / limit on the specified axis (x- or y-axis).
14+
*/
15+
public class LimitRange extends ComponentBase {
16+
17+
public static class Range {
18+
private final float mLow;
19+
private final float mHigh;
20+
21+
Range(float r1, float r2) {
22+
if (r1 < r2) {
23+
mLow = r1;
24+
mHigh = r2;
25+
} else {
26+
mLow = r2;
27+
mHigh = r1;
28+
}
29+
}
30+
31+
public float getLow() {
32+
return mLow;
33+
}
34+
35+
public float getHigh() {
36+
return mHigh;
37+
}
38+
}
39+
40+
/**
41+
* limit / maximum (the y-value or xIndex)
42+
*/
43+
private Range mLimit;
44+
45+
/**
46+
* the width of the limit line
47+
*/
48+
private float mLineWidth = 0f;
49+
50+
/**
51+
* the color of the limit line
52+
*/
53+
private int mLineColor = Color.rgb(237, 91, 91);
54+
55+
/**
56+
* the color of the Range
57+
*/
58+
private int mRangeColor = Color.rgb(128, 128, 128);
59+
60+
/**
61+
* the style of the label text
62+
*/
63+
private Paint.Style mTextStyle = Paint.Style.FILL;
64+
65+
/**
66+
* label string that is drawn next to the limit line
67+
*/
68+
private String mLabel = "";
69+
70+
/**
71+
* the path effect of this LimitLine that makes dashed lines possible
72+
*/
73+
private DashPathEffect mDashPathEffect = null;
74+
75+
/**
76+
* indicates the position of the LimitLine label
77+
*/
78+
private LimitLine.LimitLabelPosition mLabelPosition = LimitLine.LimitLabelPosition.RIGHT_TOP;
79+
80+
/**
81+
* Constructor with limit.
82+
*
83+
* @param firstLimit - the position (the value) on the y-axis (y-value) or x-axis
84+
* (xIndex) where this line should appear
85+
* @param secondLimit - the position (the value) on the y-axis (y-value) or x-axis
86+
* (xIndex) where this line should appear
87+
*/
88+
public LimitRange(float firstLimit, float secondLimit) {
89+
mLimit = new Range(firstLimit, secondLimit);
90+
}
91+
92+
/**
93+
* Constructor with limit and label.
94+
*
95+
* @param firstLimit - the position (the value) on the y-axis (y-value) or x-axis
96+
* (xIndex) where this line should appear
97+
* @param secondLimit - the position (the value) on the y-axis (y-value) or x-axis
98+
* (xIndex) where this line should appear
99+
* @param label - provide "" if no label is required
100+
*/
101+
public LimitRange(float firstLimit, float secondLimit, String label) {
102+
mLimit = new Range(firstLimit, secondLimit);
103+
mLabel = label;
104+
}
105+
106+
/**
107+
* Returns the limit that is set for this line.
108+
*
109+
* @return
110+
*/
111+
public Range getLimit() {
112+
return mLimit;
113+
}
114+
115+
/**
116+
* set the line width of the chart (min = 0.2f, max = 12f); default 2f NOTE:
117+
* thinner line == better performance, thicker line == worse performance
118+
*
119+
* @param width
120+
*/
121+
public void setLineWidth(float width) {
122+
if (width > 12.0f) {
123+
width = 12.0f;
124+
}
125+
mLineWidth = Utils.convertDpToPixel(width);
126+
}
127+
128+
/**
129+
* returns the width of limit line
130+
*
131+
* @return
132+
*/
133+
public float getLineWidth() {
134+
return mLineWidth;
135+
}
136+
137+
/**
138+
* Sets the linecolor for this LimitLine. Make sure to use
139+
* getResources().getColor(...)
140+
*
141+
* @param color
142+
*/
143+
public void setLineColor(int color) {
144+
mLineColor = color;
145+
}
146+
147+
/**
148+
* Sets the range color for this LimitRange. Make sure to use
149+
* getResources().getColor(...)
150+
*
151+
* @param color
152+
*/
153+
public void setRangeColor(int color) {
154+
mRangeColor = color;
155+
}
156+
157+
/**
158+
* Returns the color that is used for this LimitLine
159+
*
160+
* @return
161+
*/
162+
public int getLineColor() {
163+
return mLineColor;
164+
}
165+
166+
/**
167+
* Returns the color that is used for this LimitRange
168+
*
169+
* @return
170+
*/
171+
public int getRangeColor() {
172+
return mRangeColor;
173+
}
174+
175+
/**
176+
* Enables the line to be drawn in dashed mode, e.g. like this "- - - - - -"
177+
*
178+
* @param lineLength the length of the line pieces
179+
* @param spaceLength the length of space inbetween the pieces
180+
* @param phase offset, in degrees (normally, use 0)
181+
*/
182+
public void enableDashedLine(float lineLength, float spaceLength, float phase) {
183+
mDashPathEffect = new DashPathEffect(new float[]{
184+
lineLength, spaceLength
185+
}, phase);
186+
}
187+
188+
/**
189+
* Disables the line to be drawn in dashed mode.
190+
*/
191+
public void disableDashedLine() {
192+
mDashPathEffect = null;
193+
}
194+
195+
/**
196+
* Returns true if the dashed-line effect is enabled, false if not. Default:
197+
* disabled
198+
*
199+
* @return
200+
*/
201+
public boolean isDashedLineEnabled() {
202+
return mDashPathEffect == null ? false : true;
203+
}
204+
205+
/**
206+
* returns the DashPathEffect that is set for this LimitLine
207+
*
208+
* @return
209+
*/
210+
public DashPathEffect getDashPathEffect() {
211+
return mDashPathEffect;
212+
}
213+
214+
/**
215+
* Sets the color of the value-text that is drawn next to the LimitLine.
216+
* Default: Paint.Style.FILL_AND_STROKE
217+
*
218+
* @param style
219+
*/
220+
public void setTextStyle(Paint.Style style) {
221+
this.mTextStyle = style;
222+
}
223+
224+
/**
225+
* Returns the color of the value-text that is drawn next to the LimitLine.
226+
*
227+
* @return
228+
*/
229+
public Paint.Style getTextStyle() {
230+
return mTextStyle;
231+
}
232+
233+
/**
234+
* Sets the position of the LimitLine value label (either on the right or on
235+
* the left edge of the chart). Not supported for RadarChart.
236+
*
237+
* @param pos
238+
*/
239+
public void setLabelPosition(LimitLine.LimitLabelPosition pos) {
240+
mLabelPosition = pos;
241+
}
242+
243+
/**
244+
* Returns the position of the LimitLine label (value).
245+
*
246+
* @return
247+
*/
248+
public LimitLine.LimitLabelPosition getLabelPosition() {
249+
return mLabelPosition;
250+
}
251+
252+
/**
253+
* Sets the label that is drawn next to the limit line. Provide "" if no
254+
* label is required.
255+
*
256+
* @param label
257+
*/
258+
public void setLabel(String label) {
259+
mLabel = label;
260+
}
261+
262+
/**
263+
* Returns the label that is drawn next to the limit line.
264+
*
265+
* @return
266+
*/
267+
public String getLabel() {
268+
return mLabel;
269+
}
270+
}

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/AxisRenderer.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ abstract class AxisRenderer(
6666
style = Paint.Style.STROKE
6767
}
6868

69+
/**
70+
* paint used for the limit ranges
71+
*/
72+
@JvmField
73+
protected var limitRangePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
74+
style = Paint.Style.STROKE
75+
}
76+
77+
/**
78+
* paint used for the limit range fill
79+
*/
80+
@JvmField
81+
protected var limitRangePaintFill = Paint(Paint.ANTI_ALIAS_FLAG).apply {
82+
style = Paint.Style.FILL
83+
}
84+
6985
/**
7086
* Computes the axis values.
7187
*

0 commit comments

Comments
 (0)