Skip to content

Commit 7db6b7e

Browse files
committed
Work on barchart rendering
1 parent ec67525 commit 7db6b7e

6 files changed

Lines changed: 69 additions & 76 deletions

File tree

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivity.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.github.mikephil.charting.highlight.Highlight;
2525
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
2626
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
27+
import com.github.mikephil.charting.utils.ColorTemplate;
2728
import com.xxmassdeveloper.mpchartexample.custom.MyValueFormatter;
2829
import com.xxmassdeveloper.mpchartexample.custom.MyYAxisValueFormatter;
2930
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
@@ -59,7 +60,7 @@ protected void onCreate(Bundle savedInstanceState) {
5960

6061
// if more than 60 entries are displayed in the chart, no values will be
6162
// drawn
62-
mChart.setMaxVisibleValueCount(60);
63+
mChart.setMaxVisibleValueCount(40);
6364

6465
// scaling can now only be done on x- and y-axis separately
6566
mChart.setPinchZoom(false);
@@ -261,6 +262,10 @@ private int[] getColors() {
261262
// have as many colors as stack-values per entry
262263
int[] colors = new int[stacksize];
263264

265+
for(int i = 0; i < colors.length; i++) {
266+
colors[i] = ColorTemplate.MATERIAL_COLORS[i];
267+
}
268+
264269
return colors;
265270
}
266271
}

‎MPChartLib/src/com/github/mikephil/charting/buffer/BarBuffer.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ public class BarBuffer extends AbstractBuffer<IBarDataSet> {
1313
protected boolean mContainsStacks = false;
1414
protected boolean mInverted = false;
1515

16-
/** interval on the x-axis per group */
16+
/**
17+
* interval on the x-axis per group
18+
*/
1719
protected float mInterval = 0f;
1820

1921
public BarBuffer(int size, float groupspace, int dataSetCount, boolean containsStacks) {
@@ -73,13 +75,14 @@ public void feed(IBarDataSet data) {
7375
float x = mInterval * i + dataSetSpace;
7476

7577
float y = e.getY();
76-
float [] vals = e.getYVals();
78+
float[] vals = e.getYVals();
7779

7880
if (!mContainsStacks || vals == null) {
7981

8082
float left = x + groupSpaceWidthHalf + barSpaceWidthHalf;
8183
float right = left + newBarWidth - barSpaceWidth;
8284
float bottom, top;
85+
8386
if (mInverted) {
8487
bottom = y >= 0 ? y : 0;
8588
top = y <= 0 ? y : 0;
@@ -107,7 +110,7 @@ public void feed(IBarDataSet data) {
107110

108111
float value = vals[k];
109112

110-
if(value >= 0f) {
113+
if (value >= 0f) {
111114
y = posY;
112115
yStart = posY + value;
113116
posY = yStart;
@@ -120,6 +123,7 @@ public void feed(IBarDataSet data) {
120123
float left = x + groupSpaceWidthHalf + barSpaceWidthHalf;
121124
float right = left + newBarWidth - barSpaceWidth;
122125
float bottom, top;
126+
123127
if (mInverted) {
124128
bottom = y >= yStart ? y : yStart;
125129
top = y <= yStart ? y : yStart;

‎MPChartLib/src/com/github/mikephil/charting/charts/Chart.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,11 @@ public float getXChartMin() {
10181018
return mXAxis.mAxisMinimum;
10191019
}
10201020

1021+
@Override
1022+
public float getXRange() {
1023+
return mXAxis.mAxisRange;
1024+
}
1025+
10211026
@Override
10221027
public int getXValCount() {
10231028
return mData.getXValCount();

‎MPChartLib/src/com/github/mikephil/charting/interfaces/dataprovider/ChartInterface.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public interface ChartInterface {
2828
*/
2929
float getXChartMax();
3030

31+
float getXRange();
32+
3133
/**
3234
* Returns the minimum y-value of the chart, regardless of zoom or translation.
3335
*

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

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public class BarChartRenderer extends DataRenderer {
2525

2626
protected BarDataProvider mChart;
2727

28-
/** the rect object that is used for drawing the bars */
28+
/**
29+
* the rect object that is used for drawing the bars
30+
*/
2931
protected RectF mBarRect = new RectF();
3032

3133
protected BarBuffer[] mBarBuffers;
@@ -34,7 +36,7 @@ public class BarChartRenderer extends DataRenderer {
3436
protected Paint mBarBorderPaint;
3537

3638
public BarChartRenderer(BarDataProvider chart, ChartAnimator animator,
37-
ViewPortHandler viewPortHandler) {
39+
ViewPortHandler viewPortHandler) {
3840
super(animator, viewPortHandler);
3941
this.mChart = chart;
4042

@@ -99,7 +101,7 @@ protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
99101
buffer.setBarSpace(dataSet.getBarSpace());
100102
buffer.setDataSet(index);
101103
buffer.setInverted(mChart.isInverted(dataSet.getAxisDependency()));
102-
buffer.setInterval(mChart.getXChartMax() / dataSet.getEntryCount());
104+
buffer.setInterval(mChart.getXRange() / dataSet.getEntryCount());
103105

104106
buffer.feed(dataSet);
105107

@@ -169,15 +171,15 @@ protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
169171

170172
/**
171173
* Prepares a bar for being highlighted.
172-
*
173-
* @param x the x-position
174-
* @param y1 the y1-position
175-
* @param y2 the y2-position
174+
*
175+
* @param x the x-position
176+
* @param y1 the y1-position
177+
* @param y2 the y2-position
176178
* @param barspaceHalf the space between bars
177179
* @param trans
178180
*/
179181
protected void prepareBarHighlight(float x, float y1, float y2, float barspaceHalf,
180-
Transformer trans) {
182+
Transformer trans) {
181183

182184
float barWidth = 0.5f;
183185

@@ -227,58 +229,66 @@ public void drawValues(Canvas c) {
227229
negOffset = -negOffset - valueTextHeight;
228230
}
229231

230-
Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
231-
232-
float[] valuePoints = getTransformedValues(trans, dataSet, i);
232+
// get the buffer
233+
BarBuffer buffer = mBarBuffers[i];
233234

234235
// if only single values are drawn (sum)
235236
if (!dataSet.isStacked()) {
236237

237-
for (int j = 0; j < valuePoints.length * mAnimator.getPhaseX(); j += 2) {
238+
for (int j = 0; j < buffer.buffer.length * mAnimator.getPhaseX(); j += 4) {
238239

239-
if (!mViewPortHandler.isInBoundsRight(valuePoints[j]))
240+
float x = (buffer.buffer[j] + buffer.buffer[j + 2]) / 2f;
241+
242+
if (!mViewPortHandler.isInBoundsRight(x))
240243
break;
241244

242-
if (!mViewPortHandler.isInBoundsY(valuePoints[j + 1])
243-
|| !mViewPortHandler.isInBoundsLeft(valuePoints[j]))
245+
if (!mViewPortHandler.isInBoundsY(buffer.buffer[j + 1])
246+
|| !mViewPortHandler.isInBoundsLeft(x))
244247
continue;
245248

246-
BarEntry entry = dataSet.getEntryForIndex(j / 2);
249+
BarEntry entry = dataSet.getEntryForIndex(j / 4);
247250
float val = entry.getY();
248251

249-
drawValue(c, dataSet.getValueFormatter(), val, entry, i, valuePoints[j],
250-
valuePoints[j + 1] + (val >= 0 ? posOffset : negOffset), dataSet.getValueTextColor(j / 2));
252+
drawValue(c, dataSet.getValueFormatter(), val, entry, i, x,
253+
buffer.buffer[j + 1] + (val >= 0 ? posOffset : negOffset), dataSet.getValueTextColor
254+
(j / 4));
251255
}
252256

253257
// if we have stacks
254258
} else {
255259

256-
for (int j = 0; j < (valuePoints.length - 1) * mAnimator.getPhaseX(); j += 2) {
260+
Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
261+
262+
int bufferIndex = 0;
263+
int index = 0;
257264

258-
BarEntry entry = dataSet.getEntryForIndex(j / 2);
265+
while (index < dataSet.getEntryCount() * mAnimator.getPhaseX()) {
266+
267+
BarEntry entry = dataSet.getEntryForIndex(index);
259268

260269
float[] vals = entry.getYVals();
270+
float x = (buffer.buffer[bufferIndex] + buffer.buffer[bufferIndex + 2]) / 2f;
271+
272+
int color = dataSet.getValueTextColor(index);
261273

262274
// we still draw stacked bars, but there is one
263275
// non-stacked
264276
// in between
265277
if (vals == null) {
266278

267-
if (!mViewPortHandler.isInBoundsRight(valuePoints[j]))
279+
if (!mViewPortHandler.isInBoundsRight(x))
268280
break;
269281

270-
if (!mViewPortHandler.isInBoundsY(valuePoints[j + 1])
271-
|| !mViewPortHandler.isInBoundsLeft(valuePoints[j]))
282+
if (!mViewPortHandler.isInBoundsY(buffer.buffer[bufferIndex + 1])
283+
|| !mViewPortHandler.isInBoundsLeft(x))
272284
continue;
273285

274-
drawValue(c, dataSet.getValueFormatter(), entry.getY(), entry, i, valuePoints[j],
275-
valuePoints[j + 1] + (entry.getY() >= 0 ? posOffset : negOffset), dataSet.getValueTextColor(j / 2));
286+
drawValue(c, dataSet.getValueFormatter(), entry.getY(), entry, i, x,
287+
buffer.buffer[bufferIndex + 1] + (entry.getY() >= 0 ? posOffset : negOffset), color);
276288

277289
// draw stack values
278290
} else {
279291

280-
int color = dataSet.getValueTextColor(j / 2);
281-
282292
float[] transformed = new float[vals.length * 2];
283293

284294
float posY = 0f;
@@ -304,7 +314,6 @@ public void drawValues(Canvas c) {
304314

305315
for (int k = 0; k < transformed.length; k += 2) {
306316

307-
float x = valuePoints[j];
308317
float y = transformed[k + 1]
309318
+ (vals[k / 2] >= 0 ? posOffset : negOffset);
310319

@@ -318,6 +327,9 @@ public void drawValues(Canvas c) {
318327
drawValue(c, dataSet.getValueFormatter(), vals[k / 2], entry, i, x, y, color);
319328
}
320329
}
330+
331+
bufferIndex = vals == null ? bufferIndex + 4 : bufferIndex + 4 * vals.length;
332+
index++;
321333
}
322334
}
323335
}
@@ -341,8 +353,8 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {
341353
if (maxDataSetIndex - minDataSetIndex < 1) continue;
342354

343355
for (int dataSetIndex = minDataSetIndex;
344-
dataSetIndex < maxDataSetIndex;
345-
dataSetIndex++) {
356+
dataSetIndex < maxDataSetIndex;
357+
dataSetIndex++) {
346358

347359
IBarDataSet set = barData.getDataSetByIndex(dataSetIndex);
348360

@@ -420,10 +432,11 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {
420432
}
421433

422434
public float[] getTransformedValues(Transformer trans, IBarDataSet data,
423-
int dataSetIndex) {
424-
return trans.generateTransformedValuesBarChart(data, dataSetIndex,
425-
mChart.getBarData(),
426-
mAnimator.getPhaseY());
435+
int dataSetIndex) {
436+
// return trans.generateTransformedValuesBarChart(data, dataSetIndex,
437+
// mChart.getBarData(),
438+
// mAnimator.getPhaseY());
439+
return null;
427440
}
428441

429442
protected boolean passesCheck() {
@@ -432,5 +445,6 @@ protected boolean passesCheck() {
432445
}
433446

434447
@Override
435-
public void drawExtras(Canvas c) { }
448+
public void drawExtras(Canvas c) {
449+
}
436450
}

‎MPChartLib/src/com/github/mikephil/charting/utils/Transformer.java‎

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -211,43 +211,6 @@ public float[] generateTransformedValuesCandle(ICandleDataSet data,
211211
return valuePoints;
212212
}
213213

214-
/**
215-
* Transforms an List of Entry into a float array containing the x and
216-
* y values transformed with all matrices for the BARCHART.
217-
*
218-
* @param data
219-
* @param dataSetIndex the dataset index
220-
* @param bd
221-
* @param phaseY
222-
* @return
223-
*/
224-
public float[] generateTransformedValuesBarChart(IBarDataSet data,
225-
int dataSetIndex, BarData bd, float phaseY) {
226-
227-
float[] valuePoints = new float[data.getEntryCount() * 2];
228-
229-
int setCount = bd.getDataSetCount();
230-
float space = bd.getGroupSpace();
231-
232-
for (int j = 0; j < valuePoints.length; j += 2) {
233-
234-
Entry e = data.getEntryForIndex(j / 2);
235-
float i = e.getX();
236-
237-
// calculate the x-position, depending on datasetcount
238-
float x = e.getX() + i * (setCount - 1) + dataSetIndex + space * i
239-
+ space / 2f;
240-
float y = e.getY();
241-
242-
valuePoints[j] = x;
243-
valuePoints[j + 1] = y * phaseY;
244-
}
245-
246-
getValueToPixelMatrix().mapPoints(valuePoints);
247-
248-
return valuePoints;
249-
}
250-
251214
/**
252215
* Transforms an List of Entry into a float array containing the x and
253216
* y values transformed with all matrices for the BARCHART.

0 commit comments

Comments
 (0)