X Tutup
Skip to content

Commit 08dfebc

Browse files
committed
Final fixes and improvements concerning zero line
1 parent 661ed7d commit 08dfebc

File tree

6 files changed

+54
-3
lines changed

6 files changed

+54
-3
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ protected void onCreate(Bundle savedInstanceState) {
6767
left.setDrawAxisLine(false);
6868
left.setDrawGridLines(false);
6969
left.setDrawZeroLine(true); // draw a zero line
70+
left.setZeroLineColor(Color.GRAY);
71+
left.setZeroLineWidth(0.7f);
7072
mChart.getAxisRight().setEnabled(false);
7173
mChart.getLegend().setEnabled(false);
7274

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ protected void onCreate(Bundle savedInstanceState) {
133133
leftAxis.setStartAtZero(false);
134134
//leftAxis.setYOffset(20f);
135135
leftAxis.enableGridDashedLine(10f, 10f, 0f);
136+
leftAxis.setDrawZeroLine(false);
136137

137138
// limit lines are drawn behind data (and not on top)
138139
leftAxis.setDrawLimitLinesBehindData(true);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ protected void onCreate(Bundle savedInstanceState) {
123123
rightAxis.setStartAtZero(false);
124124
rightAxis.setAxisMinValue(-200);
125125
rightAxis.setDrawGridLines(false);
126+
rightAxis.setDrawZeroLine(false);
126127
}
127128

128129
@Override

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.mikephil.charting.components;
22

3+
import android.graphics.Color;
34
import android.graphics.Paint;
45

56
import com.github.mikephil.charting.formatter.DefaultValueFormatter;
@@ -72,6 +73,16 @@ public class YAxis extends AxisBase {
7273
*/
7374
protected boolean mDrawZeroLine = true;
7475

76+
/**
77+
* Color of the zero line
78+
*/
79+
protected int mZeroLineColor = Color.GRAY;
80+
81+
/**
82+
* Width of the zero line in pixels
83+
*/
84+
protected float mZeroLineWidth = 1f;
85+
7586
/**
7687
* custom minimum value this axis represents
7788
*/
@@ -365,6 +376,32 @@ public void setDrawZeroLine(boolean mDrawZeroLine) {
365376
this.mDrawZeroLine = mDrawZeroLine;
366377
}
367378

379+
public int getZeroLineColor() {
380+
return mZeroLineColor;
381+
}
382+
383+
/**
384+
* Sets the color of the zero line
385+
*
386+
* @param color
387+
*/
388+
public void setZeroLineColor(int color) {
389+
mZeroLineColor = color;
390+
}
391+
392+
public float getZeroLineWidth() {
393+
return mZeroLineWidth;
394+
}
395+
396+
/**
397+
* Sets the width of the zero line in dp
398+
*
399+
* @param width
400+
*/
401+
public void setZeroLineWidth(float width) {
402+
this.mZeroLineWidth = Utils.convertDpToPixel(width);
403+
}
404+
368405
/**
369406
* This is for normal (not horizontal) charts horizontal spacing.
370407
*

MPChartLib/src/com/github/mikephil/charting/renderer/YAxisRenderer.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@ public class YAxisRenderer extends AxisRenderer {
2121

2222
protected YAxis mYAxis;
2323

24+
protected Paint mZeroLinePaint;
25+
2426
public YAxisRenderer(ViewPortHandler viewPortHandler, YAxis yAxis, Transformer trans) {
2527
super(viewPortHandler, trans);
2628

2729
this.mYAxis = yAxis;
2830

2931
mAxisLabelPaint.setColor(Color.BLACK);
3032
mAxisLabelPaint.setTextSize(Utils.convertDpToPixel(10f));
33+
34+
mZeroLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
35+
mZeroLinePaint.setColor(Color.GRAY);
36+
mZeroLinePaint.setStrokeWidth(1f);
37+
mZeroLinePaint.setStyle(Paint.Style.STROKE);
3138
}
3239

3340
/**
@@ -283,7 +290,7 @@ public void renderGridLines(Canvas c) {
283290
position[1] = 0f;
284291
mTrans.pointValuesToPixel(position);
285292

286-
drawZeroLine(c, mViewPortHandler.offsetLeft(), mViewPortHandler.contentRight(), position[1], position[1]);
293+
drawZeroLine(c, mViewPortHandler.offsetLeft(), mViewPortHandler.contentRight(), position[1]-1, position[1]-1);
287294
}
288295
}
289296

@@ -298,13 +305,16 @@ public void renderGridLines(Canvas c) {
298305
*/
299306
protected void drawZeroLine(Canvas c, float x1, float x2, float y1, float y2) {
300307

308+
mZeroLinePaint.setColor(mYAxis.getZeroLineColor());
309+
mZeroLinePaint.setStrokeWidth(mYAxis.getZeroLineWidth());
310+
301311
Path zeroLinePath = new Path();
302312

303313
zeroLinePath.moveTo(x1, y1);
304314
zeroLinePath.lineTo(x2, y2);
305315

306316
// draw a path because lines don't support dashing on lower android versions
307-
c.drawPath(zeroLinePath, mGridPaint);
317+
c.drawPath(zeroLinePath, mZeroLinePaint);
308318
}
309319

310320
/**

MPChartLib/src/com/github/mikephil/charting/renderer/YAxisRendererHorizontalBarChart.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public void renderGridLines(Canvas c) {
185185
position[0] = 0f;
186186
mTrans.pointValuesToPixel(position);
187187

188-
drawZeroLine(c, position[0], position[0], mViewPortHandler.contentTop(), mViewPortHandler.contentBottom());
188+
drawZeroLine(c, position[0]+1, position[0]+1, mViewPortHandler.contentTop(), mViewPortHandler.contentBottom());
189189
}
190190
}
191191

0 commit comments

Comments
 (0)
X Tutup