Skip to content

Commit 1096719

Browse files
committed
drawBarShadowEnabled is working again
1 parent 0c00b09 commit 1096719

File tree

2 files changed

+75
-24
lines changed

2 files changed

+75
-24
lines changed

Diff for: MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.java

+37-17
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,12 @@ public void drawData(Canvas c) {
8080
}
8181
}
8282

83+
private RectF mBarShadowRectBuffer = new RectF();
84+
8385
protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
8486

8587
Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
8688

87-
mShadowPaint.setColor(dataSet.getBarShadowColor());
8889
mBarBorderPaint.setColor(dataSet.getBarBorderColor());
8990
mBarBorderPaint.setStrokeWidth(Utils.convertDpToPixel(dataSet.getBarBorderWidth()));
9091

@@ -93,34 +94,53 @@ protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
9394
float phaseX = mAnimator.getPhaseX();
9495
float phaseY = mAnimator.getPhaseY();
9596

96-
// initialize the buffer
97-
BarBuffer buffer = mBarBuffers[index];
98-
buffer.setPhases(phaseX, phaseY);
99-
buffer.setDataSet(index);
100-
buffer.setInverted(mChart.isInverted(dataSet.getAxisDependency()));
101-
buffer.setBarWidth(mChart.getBarData().getBarWidth());
97+
// draw the bar shadow before the values
98+
if (mChart.isDrawBarShadowEnabled()) {
99+
mShadowPaint.setColor(dataSet.getBarShadowColor());
102100

103-
buffer.feed(dataSet);
101+
BarData barData = mChart.getBarData();
104102

105-
trans.pointValuesToPixel(buffer.buffer);
103+
final float barWidth = barData.getBarWidth();
104+
final float barWidthHalf = barWidth / 2.0f;
105+
float x;
106106

107-
// draw the bar shadow before the values
108-
if (mChart.isDrawBarShadowEnabled()) {
107+
for (int i = 0, count = Math.min((int)(Math.ceil((float)(dataSet.getEntryCount()) * phaseX)), dataSet.getEntryCount());
108+
i < count;
109+
i++) {
110+
111+
BarEntry e = dataSet.getEntryForIndex(i);
109112

110-
for (int j = 0; j < buffer.size(); j += 4) {
113+
x = e.getX();
111114

112-
if (!mViewPortHandler.isInBoundsLeft(buffer.buffer[j + 2]))
115+
mBarShadowRectBuffer.left = x - barWidthHalf;
116+
mBarShadowRectBuffer.right = x + barWidthHalf;
117+
118+
trans.rectValueToPixel(mBarShadowRectBuffer);
119+
120+
if (!mViewPortHandler.isInBoundsLeft(mBarShadowRectBuffer.right))
113121
continue;
114122

115-
if (!mViewPortHandler.isInBoundsRight(buffer.buffer[j]))
123+
if (!mViewPortHandler.isInBoundsRight(mBarShadowRectBuffer.left))
116124
break;
117125

118-
c.drawRect(buffer.buffer[j], mViewPortHandler.contentTop(),
119-
buffer.buffer[j + 2],
120-
mViewPortHandler.contentBottom(), mShadowPaint);
126+
mBarShadowRectBuffer.top = mViewPortHandler.contentTop();
127+
mBarShadowRectBuffer.bottom = mViewPortHandler.contentBottom();
128+
129+
c.drawRect(mBarShadowRectBuffer, mShadowPaint);
121130
}
122131
}
123132

133+
// initialize the buffer
134+
BarBuffer buffer = mBarBuffers[index];
135+
buffer.setPhases(phaseX, phaseY);
136+
buffer.setDataSet(index);
137+
buffer.setInverted(mChart.isInverted(dataSet.getAxisDependency()));
138+
buffer.setBarWidth(mChart.getBarData().getBarWidth());
139+
140+
buffer.feed(dataSet);
141+
142+
trans.pointValuesToPixel(buffer.buffer);
143+
124144
final boolean isSingleColor = dataSet.getColors().size() == 1;
125145

126146
if (isSingleColor) {

Diff for: MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.java

+38-7
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ public void initBuffers() {
4848
}
4949
}
5050

51+
private RectF mBarShadowRectBuffer = new RectF();
52+
5153
@Override
5254
protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
5355

5456
Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
5557

56-
mShadowPaint.setColor(dataSet.getBarShadowColor());
5758
mBarBorderPaint.setColor(dataSet.getBarBorderColor());
5859
mBarBorderPaint.setStrokeWidth(Utils.convertDpToPixel(dataSet.getBarBorderWidth()));
5960

@@ -62,6 +63,42 @@ protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
6263
float phaseX = mAnimator.getPhaseX();
6364
float phaseY = mAnimator.getPhaseY();
6465

66+
// draw the bar shadow before the values
67+
if (mChart.isDrawBarShadowEnabled()) {
68+
mShadowPaint.setColor(dataSet.getBarShadowColor());
69+
70+
BarData barData = mChart.getBarData();
71+
72+
final float barWidth = barData.getBarWidth();
73+
final float barWidthHalf = barWidth / 2.0f;
74+
float x;
75+
76+
for (int i = 0, count = Math.min((int)(Math.ceil((float)(dataSet.getEntryCount()) * phaseX)), dataSet.getEntryCount());
77+
i < count;
78+
i++) {
79+
80+
BarEntry e = dataSet.getEntryForIndex(i);
81+
82+
x = e.getX();
83+
84+
mBarShadowRectBuffer.top = x - barWidthHalf;
85+
mBarShadowRectBuffer.bottom = x + barWidthHalf;
86+
87+
trans.rectValueToPixel(mBarShadowRectBuffer);
88+
89+
if (!mViewPortHandler.isInBoundsTop(mBarShadowRectBuffer.bottom))
90+
continue;
91+
92+
if (!mViewPortHandler.isInBoundsBottom(mBarShadowRectBuffer.top))
93+
break;
94+
95+
mBarShadowRectBuffer.left = mViewPortHandler.contentLeft();
96+
mBarShadowRectBuffer.right = mViewPortHandler.contentRight();
97+
98+
c.drawRect(mBarShadowRectBuffer, mShadowPaint);
99+
}
100+
}
101+
65102
// initialize the buffer
66103
BarBuffer buffer = mBarBuffers[index];
67104
buffer.setPhases(phaseX, phaseY);
@@ -87,12 +124,6 @@ protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
87124
if (!mViewPortHandler.isInBoundsBottom(buffer.buffer[j + 1]))
88125
continue;
89126

90-
if (mChart.isDrawBarShadowEnabled()) {
91-
c.drawRect(mViewPortHandler.contentLeft(), buffer.buffer[j + 1],
92-
mViewPortHandler.contentRight(),
93-
buffer.buffer[j + 3], mShadowPaint);
94-
}
95-
96127
if (!isSingleColor) {
97128
// Set the color for the currently drawn value. If the index
98129
// is out of bounds, reuse colors.

0 commit comments

Comments
 (0)