Skip to content

Commit 2877575

Browse files
committed
Introducing zero line which can be drawn regardless of grid lines
1 parent ce84fe2 commit 2877575

6 files changed

Lines changed: 202 additions & 122 deletions

File tree

MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartPositiveNegative.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ protected void onCreate(Bundle savedInstanceState) {
6666
left.setSpaceBottom(25f);
6767
left.setDrawAxisLine(false);
6868
left.setDrawGridLines(false);
69+
left.setDrawZeroLine(true); // draw a zero line
6970
mChart.getAxisRight().setEnabled(false);
7071
mChart.getLegend().setEnabled(false);
7172

MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivityNegative.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ protected void onCreate(Bundle savedInstanceState) {
6262
mChart.getAxisRight().setStartAtZero(false);
6363
mChart.getAxisRight().setAxisMaxValue(25f);
6464
mChart.getAxisRight().setAxisMinValue(-25f);
65+
mChart.getAxisRight().setDrawGridLines(false);
66+
mChart.getAxisRight().setDrawZeroLine(true);
6567
mChart.getAxisRight().setLabelCount(7, false);
6668
mChart.getAxisRight().setValueFormatter(new CustomFormatter());
6769
mChart.getAxisRight().setTextSize(9f);

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

Lines changed: 99 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,62 @@
1212

1313
/**
1414
* Baseclass of all labels.
15-
*
15+
*
1616
* @author Philipp Jahoda
1717
*/
1818
public abstract class AxisBase extends ComponentBase {
1919

20-
private int mGridColor = Color.GRAY;
20+
private int mGridColor = Color.GRAY;
2121

2222
private float mGridLineWidth = 1f;
2323

2424
private int mAxisLineColor = Color.GRAY;
2525

2626
private float mAxisLineWidth = 1f;
2727

28-
/** flag indicating if the grid lines for this axis should be drawn */
28+
/**
29+
* flag indicating if the grid lines for this axis should be drawn
30+
*/
2931
protected boolean mDrawGridLines = true;
3032

31-
/** flag that indicates if the line alongside the axis is drawn or not */
33+
/**
34+
* flag that indicates if the line alongside the axis is drawn or not
35+
*/
3236
protected boolean mDrawAxisLine = true;
3337

34-
/** flag that indicates of the labels of this axis should be drawn or not */
38+
/**
39+
* flag that indicates of the labels of this axis should be drawn or not
40+
*/
3541
protected boolean mDrawLabels = true;
3642

37-
/** the path effect of the grid lines that makes dashed lines possible */
43+
/**
44+
* the path effect of the grid lines that makes dashed lines possible
45+
*/
3846
private DashPathEffect mGridDashPathEffect = null;
3947

40-
/** array of limit lines that can be set for the axis */
41-
protected List<LimitLine> mLimitLines;
48+
/**
49+
* array of limit lines that can be set for the axis
50+
*/
51+
protected List<LimitLine> mLimitLines;
4252

43-
/** flag indicating the limit lines layer depth */
44-
protected boolean mDrawLimitLineBehindData = false;
53+
/**
54+
* flag indicating the limit lines layer depth
55+
*/
56+
protected boolean mDrawLimitLineBehindData = false;
4557

46-
/** default constructor */
58+
/**
59+
* default constructor
60+
*/
4761
public AxisBase() {
4862
this.mTextSize = Utils.convertDpToPixel(10f);
4963
this.mXOffset = Utils.convertDpToPixel(5f);
5064
this.mYOffset = Utils.convertDpToPixel(5f);
51-
this.mLimitLines = new ArrayList<LimitLine>();
52-
}
65+
this.mLimitLines = new ArrayList<LimitLine>();
66+
}
5367

5468
/**
5569
* Set this to true to enable drawing the grid lines for this axis.
56-
*
70+
*
5771
* @param enabled
5872
*/
5973
public void setDrawGridLines(boolean enabled) {
@@ -62,7 +76,7 @@ public void setDrawGridLines(boolean enabled) {
6276

6377
/**
6478
* Returns true if drawing grid lines is enabled for this axis.
65-
*
79+
*
6680
* @return
6781
*/
6882
public boolean isDrawGridLinesEnabled() {
@@ -71,7 +85,7 @@ public boolean isDrawGridLinesEnabled() {
7185

7286
/**
7387
* Set this to true if the line alongside the axis should be drawn or not.
74-
*
88+
*
7589
* @param enabled
7690
*/
7791
public void setDrawAxisLine(boolean enabled) {
@@ -80,7 +94,7 @@ public void setDrawAxisLine(boolean enabled) {
8094

8195
/**
8296
* Returns true if the line alongside the axis should be drawn.
83-
*
97+
*
8498
* @return
8599
*/
86100
public boolean isDrawAxisLineEnabled() {
@@ -90,7 +104,7 @@ public boolean isDrawAxisLineEnabled() {
90104
/**
91105
* Sets the color of the grid lines for this axis (the horizontal lines
92106
* coming from each label).
93-
*
107+
*
94108
* @param color
95109
*/
96110
public void setGridColor(int color) {
@@ -100,7 +114,7 @@ public void setGridColor(int color) {
100114
/**
101115
* Returns the color of the grid lines for this axis (the horizontal lines
102116
* coming from each label).
103-
*
117+
*
104118
* @return
105119
*/
106120
public int getGridColor() {
@@ -109,7 +123,7 @@ public int getGridColor() {
109123

110124
/**
111125
* Sets the width of the border surrounding the chart in dp.
112-
*
126+
*
113127
* @param width
114128
*/
115129
public void setAxisLineWidth(float width) {
@@ -118,7 +132,7 @@ public void setAxisLineWidth(float width) {
118132

119133
/**
120134
* Returns the width of the axis line (line alongside the axis).
121-
*
135+
*
122136
* @return
123137
*/
124138
public float getAxisLineWidth() {
@@ -128,7 +142,7 @@ public float getAxisLineWidth() {
128142
/**
129143
* Sets the width of the grid lines that are drawn away from each axis
130144
* label.
131-
*
145+
*
132146
* @param width
133147
*/
134148
public void setGridLineWidth(float width) {
@@ -138,7 +152,7 @@ public void setGridLineWidth(float width) {
138152
/**
139153
* Returns the width of the grid lines that are drawn away from each axis
140154
* label.
141-
*
155+
*
142156
* @return
143157
*/
144158
public float getGridLineWidth() {
@@ -147,7 +161,7 @@ public float getGridLineWidth() {
147161

148162
/**
149163
* Sets the color of the border surrounding the chart.
150-
*
164+
*
151165
* @param color
152166
*/
153167
public void setAxisLineColor(int color) {
@@ -156,7 +170,7 @@ public void setAxisLineColor(int color) {
156170

157171
/**
158172
* Returns the color of the axis line (line alongside the axis).
159-
*
173+
*
160174
* @return
161175
*/
162176
public int getAxisLineColor() {
@@ -166,7 +180,7 @@ public int getAxisLineColor() {
166180
/**
167181
* Set this to true to enable drawing the labels of this axis (this will not
168182
* affect drawing the grid lines or axis lines).
169-
*
183+
*
170184
* @param enabled
171185
*/
172186
public void setDrawLabels(boolean enabled) {
@@ -175,70 +189,70 @@ public void setDrawLabels(boolean enabled) {
175189

176190
/**
177191
* Returns true if drawing the labels is enabled for this axis.
178-
*
192+
*
179193
* @return
180194
*/
181195
public boolean isDrawLabelsEnabled() {
182196
return mDrawLabels;
183197
}
184198

185-
/**
186-
* Adds a new LimitLine to this axis.
187-
*
188-
* @param l
189-
*/
190-
public void addLimitLine(LimitLine l) {
191-
mLimitLines.add(l);
192-
193-
if (mLimitLines.size() > 6) {
194-
Log.e("MPAndroiChart",
195-
"Warning! You have more than 6 LimitLines on your axis, do you really want that?");
196-
}
197-
}
198-
199-
/**
200-
* Removes the specified LimitLine from the axis.
201-
*
202-
* @param l
203-
*/
204-
public void removeLimitLine(LimitLine l) {
205-
mLimitLines.remove(l);
206-
}
207-
208-
/**
209-
* Removes all LimitLines from the axis.
210-
*/
211-
public void removeAllLimitLines() {
212-
mLimitLines.clear();
213-
}
214-
215-
/**
216-
* Returns the LimitLines of this axis.
217-
*
218-
* @return
219-
*/
220-
public List<LimitLine> getLimitLines() {
221-
return mLimitLines;
222-
}
223-
224-
/**
225-
* If this is set to true, the LimitLines are drawn behind the actual data,
226-
* otherwise on top. Default: false
227-
*
228-
* @param enabled
229-
*/
230-
public void setDrawLimitLinesBehindData(boolean enabled) {
231-
mDrawLimitLineBehindData = enabled;
232-
}
233-
234-
public boolean isDrawLimitLinesBehindDataEnabled() {
235-
return mDrawLimitLineBehindData;
236-
}
237-
238-
/**
199+
/**
200+
* Adds a new LimitLine to this axis.
201+
*
202+
* @param l
203+
*/
204+
public void addLimitLine(LimitLine l) {
205+
mLimitLines.add(l);
206+
207+
if (mLimitLines.size() > 6) {
208+
Log.e("MPAndroiChart",
209+
"Warning! You have more than 6 LimitLines on your axis, do you really want that?");
210+
}
211+
}
212+
213+
/**
214+
* Removes the specified LimitLine from the axis.
215+
*
216+
* @param l
217+
*/
218+
public void removeLimitLine(LimitLine l) {
219+
mLimitLines.remove(l);
220+
}
221+
222+
/**
223+
* Removes all LimitLines from the axis.
224+
*/
225+
public void removeAllLimitLines() {
226+
mLimitLines.clear();
227+
}
228+
229+
/**
230+
* Returns the LimitLines of this axis.
231+
*
232+
* @return
233+
*/
234+
public List<LimitLine> getLimitLines() {
235+
return mLimitLines;
236+
}
237+
238+
/**
239+
* If this is set to true, the LimitLines are drawn behind the actual data,
240+
* otherwise on top. Default: false
241+
*
242+
* @param enabled
243+
*/
244+
public void setDrawLimitLinesBehindData(boolean enabled) {
245+
mDrawLimitLineBehindData = enabled;
246+
}
247+
248+
public boolean isDrawLimitLinesBehindDataEnabled() {
249+
return mDrawLimitLineBehindData;
250+
}
251+
252+
/**
239253
* Returns the longest formatted label (in terms of characters), this axis
240254
* contains.
241-
*
255+
*
242256
* @return
243257
*/
244258
public abstract String getLongestLabel();
@@ -248,12 +262,12 @@ public boolean isDrawLimitLinesBehindDataEnabled() {
248262
* "- - - - - -". THIS ONLY WORKS IF HARDWARE-ACCELERATION IS TURNED OFF.
249263
* Keep in mind that hardware acceleration boosts performance.
250264
*
251-
* @param lineLength the length of the line pieces
265+
* @param lineLength the length of the line pieces
252266
* @param spaceLength the length of space in between the pieces
253-
* @param phase offset, in degrees (normally, use 0)
267+
* @param phase offset, in degrees (normally, use 0)
254268
*/
255269
public void enableGridDashedLine(float lineLength, float spaceLength, float phase) {
256-
mGridDashPathEffect = new DashPathEffect(new float[] {
270+
mGridDashPathEffect = new DashPathEffect(new float[]{
257271
lineLength, spaceLength
258272
}, phase);
259273
}

MPChartLib/src/com/github/mikephil/charting/components/YAxis.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public class YAxis extends AxisBase {
6767
*/
6868
protected boolean mForceLabels = false;
6969

70+
/**
71+
* flag that indicates if the zero-line should be drawn regardless of other grid lines
72+
*/
73+
protected boolean mDrawZeroLine = true;
74+
7075
/**
7176
* custom minimum value this axis represents
7277
*/
@@ -346,6 +351,20 @@ public float getSpaceBottom() {
346351
return mSpacePercentBottom;
347352
}
348353

354+
public boolean isDrawZeroLineEnabled() {
355+
return mDrawZeroLine;
356+
}
357+
358+
/**
359+
* Set this to true to draw the zero-line regardless of weather other
360+
* grid-lines are enabled or not.
361+
*
362+
* @param mDrawZeroLine
363+
*/
364+
public void setDrawZeroLine(boolean mDrawZeroLine) {
365+
this.mDrawZeroLine = mDrawZeroLine;
366+
}
367+
349368
/**
350369
* This is for normal (not horizontal) charts horizontal spacing.
351370
*

0 commit comments

Comments
 (0)