Skip to content

Commit 18e3923

Browse files
committed
Added feature for drawing bar borders
1 parent ccced3c commit 18e3923

5 files changed

Lines changed: 132 additions & 4 deletions

File tree

‎MPChartLib/src/com/github/mikephil/charting/data/BarDataSet.java‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public class BarDataSet extends BarLineScatterCandleBubbleDataSet<BarEntry> impl
2626
*/
2727
private int mBarShadowColor = Color.rgb(215, 215, 215);
2828

29+
private float mBarBorderWidth = 0.0f;
30+
31+
private int mBarBorderColor = Color.BLACK;
32+
2933
/**
3034
* the alpha value used to draw the highlight indicator bar
3135
*/
@@ -217,6 +221,46 @@ public int getBarShadowColor() {
217221
return mBarShadowColor;
218222
}
219223

224+
/**
225+
* Sets the width used for drawing borders around the bars.
226+
* If borderWidth == 0, no border will be drawn.
227+
*
228+
* @return
229+
*/
230+
public void setBarBorderWidth(float width) {
231+
mBarBorderWidth = width;
232+
}
233+
234+
/**
235+
* Returns the width used for drawing borders around the bars.
236+
* If borderWidth == 0, no border will be drawn.
237+
*
238+
* @return
239+
*/
240+
@Override
241+
public float getBarBorderWidth() {
242+
return mBarBorderWidth;
243+
}
244+
245+
/**
246+
* Sets the color drawing borders around the bars.
247+
*
248+
* @return
249+
*/
250+
public void setBarBorderColor(int color) {
251+
mBarBorderColor = color;
252+
}
253+
254+
/**
255+
* Returns the color drawing borders around the bars.
256+
*
257+
* @return
258+
*/
259+
@Override
260+
public int getBarBorderColor() {
261+
return mBarBorderColor;
262+
}
263+
220264
/**
221265
* Set the alpha value (transparency) that is used for drawing the highlight
222266
* indicator bar. min = 0 (fully transparent), max = 255 (fully opaque)

‎MPChartLib/src/com/github/mikephil/charting/data/realm/implementation/RealmBarDataSet.java‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public class RealmBarDataSet<T extends RealmObject> extends RealmBarLineScatterC
3434
*/
3535
private int mBarShadowColor = Color.rgb(215, 215, 215);
3636

37+
private float mBarBorderWidth = 0.0f;
38+
39+
private int mBarBorderColor = Color.BLACK;
40+
3741
/**
3842
* the alpha value used to draw the highlight indicator bar
3943
*/
@@ -212,6 +216,46 @@ public int getBarShadowColor() {
212216
return mBarShadowColor;
213217
}
214218

219+
/**
220+
* Sets the width used for drawing borders around the bars.
221+
* If borderWidth == 0, no border will be drawn.
222+
*
223+
* @return
224+
*/
225+
public void setBarBorderWidth(float width) {
226+
mBarBorderWidth = width;
227+
}
228+
229+
/**
230+
* Returns the width used for drawing borders around the bars.
231+
* If borderWidth == 0, no border will be drawn.
232+
*
233+
* @return
234+
*/
235+
@Override
236+
public float getBarBorderWidth() {
237+
return mBarBorderWidth;
238+
}
239+
240+
/**
241+
* Sets the color drawing borders around the bars.
242+
*
243+
* @return
244+
*/
245+
public void setBarBorderColor(int color) {
246+
mBarBorderColor = color;
247+
}
248+
249+
/**
250+
* Returns the color drawing borders around the bars.
251+
*
252+
* @return
253+
*/
254+
@Override
255+
public int getBarBorderColor() {
256+
return mBarBorderColor;
257+
}
258+
215259
/**
216260
* Set the alpha value (transparency) that is used for drawing the highlight
217261
* indicator bar. min = 0 (fully transparent), max = 255 (fully opaque)

‎MPChartLib/src/com/github/mikephil/charting/interfaces/datasets/IBarDataSet.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ public interface IBarDataSet extends IBarLineScatterCandleBubbleDataSet<BarEntry
3737
*/
3838
int getBarShadowColor();
3939

40+
/**
41+
* Returns the width used for drawing borders around the bars.
42+
* If borderWidth == 0, no border will be drawn.
43+
*
44+
* @return
45+
*/
46+
float getBarBorderWidth();
47+
48+
/**
49+
* Returns the color drawing borders around the bars.
50+
*
51+
* @return
52+
*/
53+
int getBarBorderColor();
54+
4055
/**
4156
* Returns the alpha value (transparency) that is used for drawing the
4257
* highlight indicator.

‎MPChartLib/src/com/github/mikephil/charting/renderer/BarChartRenderer.java‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class BarChartRenderer extends DataRenderer {
3131
protected BarBuffer[] mBarBuffers;
3232

3333
protected Paint mShadowPaint;
34+
protected Paint mBarBorderPaint;
3435

3536
public BarChartRenderer(BarDataProvider chart, ChartAnimator animator,
3637
ViewPortHandler viewPortHandler) {
@@ -45,6 +46,9 @@ public BarChartRenderer(BarDataProvider chart, ChartAnimator animator,
4546

4647
mShadowPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
4748
mShadowPaint.setStyle(Paint.Style.FILL);
49+
50+
mBarBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
51+
mBarBorderPaint.setStyle(Paint.Style.STROKE);
4852
}
4953

5054
@Override
@@ -81,6 +85,10 @@ protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
8185
Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
8286

8387
mShadowPaint.setColor(dataSet.getBarShadowColor());
88+
mBarBorderPaint.setColor(dataSet.getBarBorderColor());
89+
mBarBorderPaint.setStrokeWidth(dataSet.getBarBorderWidth());
90+
91+
final boolean drawBorder = dataSet.getBarBorderWidth() > 0.f;
8492

8593
float phaseX = mAnimator.getPhaseX();
8694
float phaseY = mAnimator.getPhaseY();
@@ -125,11 +133,15 @@ protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
125133
break;
126134

127135
// Set the color for the currently drawn value. If the index
128-
// is
129-
// out of bounds, reuse colors.
136+
// is out of bounds, reuse colors.
130137
mRenderPaint.setColor(dataSet.getColor(j / 4));
131138
c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
132139
buffer.buffer[j + 3], mRenderPaint);
140+
141+
if (drawBorder) {
142+
c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
143+
buffer.buffer[j + 3], mBarBorderPaint);
144+
}
133145
}
134146
} else {
135147

@@ -145,6 +157,11 @@ protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
145157

146158
c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
147159
buffer.buffer[j + 3], mRenderPaint);
160+
161+
if (drawBorder) {
162+
c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
163+
buffer.buffer[j + 3], mBarBorderPaint);
164+
}
148165
}
149166
}
150167
}

‎MPChartLib/src/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
5252
Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
5353

5454
mShadowPaint.setColor(dataSet.getBarShadowColor());
55+
mBarBorderPaint.setColor(dataSet.getBarBorderColor());
56+
mBarBorderPaint.setStrokeWidth(dataSet.getBarBorderWidth());
57+
58+
final boolean drawBorder = dataSet.getBarBorderWidth() > 0.f;
5559

5660
float phaseX = mAnimator.getPhaseX();
5761
float phaseY = mAnimator.getPhaseY();
@@ -82,11 +86,15 @@ protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
8286
}
8387

8488
// Set the color for the currently drawn value. If the index
85-
// is
86-
// out of bounds, reuse colors.
89+
// is out of bounds, reuse colors.
8790
mRenderPaint.setColor(dataSet.getColor(j / 4));
8891
c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
8992
buffer.buffer[j + 3], mRenderPaint);
93+
94+
if (drawBorder) {
95+
c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
96+
buffer.buffer[j + 3], mBarBorderPaint);
97+
}
9098
}
9199
}
92100

0 commit comments

Comments
 (0)