Skip to content

Commit aba44b3

Browse files
committed
Fixed bug in 3D BarChart.
1 parent 245648f commit aba44b3

7 files changed

Lines changed: 113 additions & 150 deletions

File tree

‎MPChartExample/src/com/example/mpchartexample/BarChartActivityMultiDataset.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ protected void onCreate(Bundle savedInstanceState) {
6161
// mChart.setSpacePercent(20, 10);
6262
mChart.setYLegendCount(5);
6363
mChart.setTouchEnabled(true);
64+
mChart.setDescription("");
6465

6566
mSeekBarX.setProgress(45);
6667
mSeekBarY.setProgress(100);

‎MPChartExample/src/com/example/mpchartexample/PieChartActivity.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected void onCreate(Bundle savedInstanceState) {
4747

4848
ColorTemplate ct = new ColorTemplate();
4949
ct.addDataSetColors(ColorTemplate.COLORFUL_COLORS, this);
50-
ct.addDataSetColors(ColorTemplate.LIBERTY_COLORS, this);
50+
ct.addDataSetColors(ColorTemplate.FRESH_COLORS, this);
5151
mChart.setColorTemplate(ct);
5252

5353
mChart.setDrawYValues(true);
@@ -148,7 +148,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
148148
}
149149

150150
tvX.setText("" + (mSeekBarX.getProgress()));
151-
tvY.setText("" + (mSeekBarY.getProgress()));
151+
tvY.setText("" + (mSeekBarY.getProgress()));
152152

153153
ArrayList<String> xVals = new ArrayList<String>();
154154

‎MPChartLib/src/com/github/mikephil/charting/BarChart.java‎

Lines changed: 110 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import android.graphics.Color;
66
import android.graphics.Matrix;
77
import android.graphics.Paint;
8-
import android.graphics.Paint.Style;
98
import android.graphics.Path;
109
import android.graphics.RectF;
1110
import android.util.AttributeSet;
@@ -51,62 +50,74 @@ protected void init() {
5150
// set alpha after color
5251
mHighlightPaint.setAlpha(120);
5352
}
53+
54+
@Override
55+
public void setColorTemplate(ColorTemplate ct) {
56+
super.setColorTemplate(ct);
57+
58+
calculate3DColors();
59+
}
5460

5561
/** array that holds all the colors for the top 3D effect */
56-
private int[] mTopColors;
62+
private ArrayList<ArrayList<Integer>> mTopColors;
5763

5864
/** array that holds all the colors for the side 3D effect */
59-
private int[] mSideColors;
65+
private ArrayList<ArrayList<Integer>> mSideColors;
6066

61-
@Override
62-
protected void prepareDataPaints(ColorTemplate ct) {
63-
64-
// // prepare the paints
65-
// mDrawPaints = new Paint[ct.getColors().size()];
66-
//
67-
// for (int i = 0; i < ct.getColors().size(); i++) {
68-
// mDrawPaints[i] = new Paint(Paint.ANTI_ALIAS_FLAG);
69-
// mDrawPaints[i].setStyle(Style.FILL);
70-
// mDrawPaints[i].setColor(Color.BLACK);
71-
// }
72-
73-
// // generate the colors for the 3D effect
74-
// mTopColors = new int[mDrawPaints.length];
75-
// mSideColors = new int[mDrawPaints.length];
76-
//
77-
// float[] hsv = new float[3];
78-
//
79-
// for (int i = 0; i < mSideColors.length; i++) {
80-
//
81-
// // extract the color
82-
// int c = mDrawPaints[i].getColor();
83-
// Color.colorToHSV(c, hsv); // convert to hsv
84-
//
85-
// // make brighter
86-
// hsv[1] = hsv[1] - 0.1f; // less saturation
87-
// hsv[2] = hsv[2] + 0.1f; // more brightness
88-
//
89-
// // convert back
90-
// c = Color.HSVToColor(hsv);
91-
//
92-
// // assign
93-
// mTopColors[i] = c;
94-
//
95-
// // get color again
96-
// c = mDrawPaints[i].getColor();
97-
//
98-
// // convert
99-
// Color.colorToHSV(c, hsv);
100-
//
101-
// // make darker
102-
// hsv[1] = hsv[1] + 0.1f; // more saturation
103-
// hsv[2] = hsv[2] - 0.1f; // less brightness
104-
//
105-
// // reassing
106-
// c = Color.HSVToColor(hsv);
107-
//
108-
// mSideColors[i] = c;
109-
// }
67+
/**
68+
* calculates the 3D color arrays
69+
*/
70+
protected void calculate3DColors() {
71+
72+
// generate the colors for the 3D effect
73+
mTopColors = new ArrayList<ArrayList<Integer>>();
74+
mSideColors = new ArrayList<ArrayList<Integer>>();
75+
76+
float[] hsv = new float[3];
77+
78+
for (int i = 0; i < mCt.getColors().size(); i++) {
79+
80+
// Get the colors for the DataSet at the current index. If the index
81+
// is out of bounds, reuse DataSet colors.
82+
ArrayList<Integer> colors = mCt.getDataSetColors(i);
83+
ArrayList<Integer> topColors = new ArrayList<Integer>();
84+
ArrayList<Integer> sideColors = new ArrayList<Integer>();
85+
86+
for (int j = 0; j < colors.size(); j++) {
87+
88+
// extract the color
89+
int c = colors.get(j);
90+
Color.colorToHSV(c, hsv); // convert to hsv
91+
92+
// make brighter
93+
hsv[1] = hsv[1] - 0.1f; // less saturation
94+
hsv[2] = hsv[2] + 0.1f; // more brightness
95+
96+
// convert back
97+
c = Color.HSVToColor(hsv);
98+
99+
// assign
100+
topColors.add(c);
101+
102+
// get color again
103+
c = colors.get(j);
104+
105+
// convert
106+
Color.colorToHSV(c, hsv);
107+
108+
// make darker
109+
hsv[1] = hsv[1] + 0.1f; // more saturation
110+
hsv[2] = hsv[2] - 0.1f; // less brightness
111+
112+
// reassing
113+
c = Color.HSVToColor(hsv);
114+
115+
sideColors.add(c);
116+
}
117+
118+
mTopColors.add(topColors);
119+
mSideColors.add(sideColors);
120+
}
110121
}
111122

112123
@Override
@@ -159,15 +170,6 @@ protected void drawHighlights() {
159170
transformPath(arrow);
160171
mDrawCanvas.drawPath(arrow, mHighlightPaint);
161172
}
162-
163-
// float[] pts = new float[] {
164-
// index + 0.5f, mYChartMax, index + 0.5f, mYChartMin,
165-
// 0, mYVals.get(index), mDeltaX, mYVals.get(index)
166-
// };
167-
//
168-
// transformPointArray(pts);
169-
// // draw the highlight lines
170-
// mDrawCanvas.drawLines(pts, mHighlightPaint);
171173
}
172174
}
173175
}
@@ -181,6 +183,9 @@ protected void drawData() {
181183
ArrayList<Path> topPaths = new ArrayList<Path>();
182184
ArrayList<Path> sidePaths = new ArrayList<Path>();
183185

186+
ArrayList<DataSet> dataSets = mData.getDataSets();
187+
188+
// 3D drawing
184189
if (m3DEnabled) {
185190

186191
float[] pts = new float[] {
@@ -209,37 +214,45 @@ protected void drawData() {
209214

210215
float depth = Math.abs(pts[3] - pts[1]) * mDepth;
211216

212-
for (int i = 0; i < mData.getYValCount(); i++) {
217+
for (int i = 0; i < mData.getDataSetCount(); i++) {
213218

214-
float y = getYValue(i);
215-
float left = i + mBarSpace / 2f;
216-
float right = i + 1f - mBarSpace / 2f;
217-
float top = y >= 0 ? y : 0;
219+
DataSet dataSet = dataSets.get(i);
220+
ArrayList<Series> series = dataSet.getYVals();
221+
222+
for (int j = 0; j < series.size(); j++) {
223+
224+
float x = series.get(j).getXIndex();
225+
float y = series.get(j).getVal();
226+
float left = x + mBarSpace / 2f;
227+
float right = x + 1f - mBarSpace / 2f;
228+
float top = y >= 0 ? y : 0;
218229

219-
// create the 3D effect paths for the top and side
220-
Path topPath = new Path();
221-
topPath.moveTo(left, top);
222-
topPath.lineTo(left + mSkew, top + depth);
223-
topPath.lineTo(right + mSkew, top + depth);
224-
topPath.lineTo(right, top);
230+
// create the 3D effect paths for the top and side
231+
Path topPath = new Path();
232+
topPath.moveTo(left, top);
233+
topPath.lineTo(left + mSkew, top + depth);
234+
topPath.lineTo(right + mSkew, top + depth);
235+
topPath.lineTo(right, top);
225236

226-
topPaths.add(topPath);
237+
topPaths.add(topPath);
227238

228-
Path sidePath = new Path();
229-
sidePath.moveTo(right, top);
230-
sidePath.lineTo(right + mSkew, top + depth);
231-
sidePath.lineTo(right + mSkew, depth);
232-
sidePath.lineTo(right, 0);
239+
Path sidePath = new Path();
240+
sidePath.moveTo(right, top);
241+
sidePath.lineTo(right + mSkew, top + depth);
242+
sidePath.lineTo(right + mSkew, depth);
243+
sidePath.lineTo(right, 0);
233244

234-
sidePaths.add(sidePath);
245+
sidePaths.add(sidePath);
246+
}
235247
}
236248

237249
transformPaths(topPaths);
238250
transformPaths(sidePaths);
239251
}
252+
253+
int cnt = 0;
240254

241-
ArrayList<DataSet> dataSets = mData.getDataSets();
242-
255+
// 2D drawing
243256
for (int i = 0; i < mData.getDataSetCount(); i++) {
244257

245258
DataSet dataSet = dataSets.get(i);
@@ -248,6 +261,8 @@ protected void drawData() {
248261
// Get the colors for the DataSet at the current index. If the index
249262
// is out of bounds, reuse DataSet colors.
250263
ArrayList<Integer> colors = mCt.getDataSetColors(i % mCt.getColors().size());
264+
ArrayList<Integer> colors3DTop = mTopColors.get(i % mCt.getColors().size());
265+
ArrayList<Integer> colors3DSide = mSideColors.get(i % mCt.getColors().size());
251266

252267
// do the drawing
253268
for (int j = 0; j < dataSet.getSeriesCount(); j++) {
@@ -266,21 +281,27 @@ protected void drawData() {
266281
mBarRect.set(left, top, right, bottom);
267282

268283
transformRect(mBarRect);
284+
285+
// avoid drawing outofbounds values
286+
if(isOffContentRight(mBarRect.left)) break;
287+
288+
if(isOffContentLeft(mBarRect.right)) {
289+
cnt++;
290+
continue;
291+
}
269292

270293
mDrawCanvas.drawRect(mBarRect, mRenderPaint);
271294

272295
if (m3DEnabled) {
273296

274-
int c = mRenderPaint.getColor();
275-
276-
mRenderPaint.setColor(mTopColors[j % mTopColors.length]);
277-
mDrawCanvas.drawPath(topPaths.get(j), mRenderPaint);
278-
279-
mRenderPaint.setColor(mSideColors[j % mSideColors.length]);
280-
mDrawCanvas.drawPath(sidePaths.get(j), mRenderPaint);
297+
mRenderPaint.setColor(colors3DTop.get(j % colors3DTop.size()));
298+
mDrawCanvas.drawPath(topPaths.get(cnt), mRenderPaint);
281299

282-
mRenderPaint.setColor(c);
300+
mRenderPaint.setColor(colors3DSide.get(j % colors3DSide.size()));
301+
mDrawCanvas.drawPath(sidePaths.get(cnt), mRenderPaint);
283302
}
303+
304+
cnt++;
284305
}
285306
}
286307
}
@@ -399,24 +420,6 @@ public boolean is3DEnabled() {
399420
return m3DEnabled;
400421
}
401422

402-
/**
403-
* returns the top colors that define the color of the top 3D effect path
404-
*
405-
* @return
406-
*/
407-
public int[] getTopColors() {
408-
return mTopColors;
409-
}
410-
411-
/**
412-
* returns the side colors that define the color of the side 3D effect path
413-
*
414-
* @return
415-
*/
416-
public int[] getSideColors() {
417-
return mSideColors;
418-
}
419-
420423
/**
421424
* set this to true to draw the highlightning arrow
422425
*

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -254,16 +254,6 @@ public void setData(ArrayList<String> xVals, ArrayList<Float> yVals) {
254254
setData(data);
255255
}
256256

257-
/**
258-
* Prepares all the paint objects that are used for drawing the lines, bars
259-
* or pie-slices. The number of paint objects used depends on the number of
260-
* colors in the colortemplate. If more values than colors need to be drawn,
261-
* colors are repeated.
262-
*
263-
* @param ct
264-
*/
265-
protected abstract void prepareDataPaints(ColorTemplate ct);
266-
267257
/**
268258
* does needed preparations for drawing
269259
*/
@@ -965,8 +955,6 @@ public void setYStartEnd(float start, float end) {
965955
*/
966956
public void setColorTemplate(ColorTemplate ct) {
967957
this.mCt = ct;
968-
969-
prepareDataPaints(ct);
970958
}
971959

972960
/**

‎MPChartLib/src/com/github/mikephil/charting/LineChart.java‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,6 @@ protected void init() {
6565
mHighlightPaint.setColor(Color.rgb(255, 187, 115));
6666
}
6767

68-
@Override
69-
protected void prepareDataPaints(ColorTemplate ct) {
70-
//
71-
// if (ct == null)
72-
// return;
73-
//
74-
// mDrawPaints = new Paint[ct.getColors().size()];
75-
//
76-
// for (int i = 0; i < ct.getColors().size(); i++) {
77-
// mDrawPaints[i] = new Paint(Paint.ANTI_ALIAS_FLAG);
78-
// mDrawPaints[i].setStrokeWidth(mLineWidth);
79-
// mDrawPaints[i].setStyle(Style.FILL);
80-
// // mDrawPaints[i].setColor(ct.getColors().get(i));
81-
// }
82-
}
83-
8468
@Override
8569
protected void drawHighlights() {
8670

‎MPChartLib/src/com/github/mikephil/charting/PieChart.java‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -251,19 +251,6 @@ protected void prepareContentRect() {
251251
- mShift);
252252
}
253253

254-
@Override
255-
protected void prepareDataPaints(ColorTemplate ct) {
256-
257-
// mDrawPaints = new Paint[ct.getColors().size()];
258-
//
259-
// // setup all paint objects
260-
// for (int i = 0; i < ct.getColors().size(); i++) {
261-
// mDrawPaints[i] = new Paint(Paint.ANTI_ALIAS_FLAG);
262-
// mDrawPaints[i].setStyle(Style.FILL);
263-
// // mDrawPaints[i].setColor(ct.getColors().get(i));
264-
// }
265-
}
266-
267254
@Override
268255
protected void calcMinMax() {
269256
super.calcMinMax();
13.3 KB
Loading

0 commit comments

Comments
 (0)