Skip to content

Commit fe489d7

Browse files
daniel-marcus-krogerhannesa2
authored andcommitted
Enable vertical shading
1 parent 1e51cbf commit fe489d7

File tree

3 files changed

+316
-0
lines changed

3 files changed

+316
-0
lines changed

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

Lines changed: 46 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
/**
@@ -454,6 +460,21 @@ public void addLimitLine(LimitLine l) {
454460
}
455461
}
456462

463+
/**
464+
* Adds a new LimitLine to this axis.
465+
*
466+
* @param l
467+
*/
468+
public void addLimitRange(LimitRange l) {
469+
mLimitRanges.add(l);
470+
471+
if (mLimitRanges.size() > 6) {
472+
Log.e("MPAndroiChart",
473+
"Warning! You have more than 6 LimitLines on your axis, do you really want " +
474+
"that?");
475+
}
476+
}
477+
457478
/**
458479
* Removes the specified LimitLine from the axis.
459480
*
@@ -470,6 +491,22 @@ public void removeAllLimitLines() {
470491
mLimitLines.clear();
471492
}
472493

494+
/**
495+
* Removes the specified LimitRange from the axis.
496+
*
497+
* @param l
498+
*/
499+
public void removeLimitRange(LimitRange l) {
500+
mLimitRanges.remove(l);
501+
}
502+
503+
/**
504+
* Removes all LimitLines from the axis.
505+
*/
506+
public void removeAllLimitRanges() {
507+
mLimitRanges.clear();
508+
}
509+
473510
/**
474511
* Returns the LimitLines of this axis.
475512
*
@@ -479,6 +516,15 @@ public List<LimitLine> getLimitLines() {
479516
return mLimitLines;
480517
}
481518

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

app/src/main/java/info/appdev/chartexample/LineChartActivity.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import com.github.mikephil.charting.animation.Easing
1717
import com.github.mikephil.charting.components.Legend.LegendForm
1818
import com.github.mikephil.charting.components.LimitLine
1919
import com.github.mikephil.charting.components.LimitLine.LimitLabelPosition
20+
import com.github.mikephil.charting.components.LimitRange
2021
import com.github.mikephil.charting.data.Entry
2122
import com.github.mikephil.charting.data.LineDataSet
2223
import com.github.mikephil.charting.highlight.Highlight
@@ -108,6 +109,19 @@ class LineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelec
108109
lineColor = Color.GREEN
109110
}
110111

112+
val limitRange = LimitRange(45f, 90f, "Middle Range").apply {
113+
lineWidth = 2f
114+
labelPosition = LimitLabelPosition.RIGHT_TOP
115+
textSize = 10f
116+
typeface = tfRegular
117+
lineColor = Color.RED
118+
rangeColor = Color.argb(30, 255, 235, 0)
119+
}
120+
121+
val limitRangeLower = LimitRange(45f, 52f).apply {
122+
rangeColor = Color.argb(30, 230, 0, 0)
123+
}
124+
111125
// draw limit lines behind data instead of on top
112126
binding.chart1.axisLeft.setDrawLimitLinesBehindData(true)
113127
binding.chart1.xAxis.setDrawLimitLinesBehindData(true)
@@ -117,6 +131,10 @@ class LineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelec
117131
binding.chart1.axisLeft.addLimitLine(limitLineLower)
118132
// binding.chart1.axisLeft.addLimitLine(llXAxis10)
119133

134+
// add limit range
135+
binding.chart1.axisLeft.addLimitRange(limitRange)
136+
binding.chart1.axisLeft.addLimitRange(limitRangeLower)
137+
120138
// add data
121139
binding.seekBarX.progress = 45
122140
binding.seekBarY.progress = 180

0 commit comments

Comments
 (0)