Skip to content

Commit 4c0e3dd

Browse files
committed
Added getters and setters, included value drawing for BarChart. Did refactoring.
1 parent f306343 commit 4c0e3dd

9 files changed

Lines changed: 352 additions & 133 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected void onCreate(Bundle savedInstanceState) {
4343
// mChart.setDrawFilled(true);
4444
// mChart.setRoundedYLegend(false);
4545
// mChart.setStartAtZero(true);
46-
mChart.setDrawValues(false);
46+
mChart.setDrawYValues(false);
4747
mChart.set3DEnabled(false);
4848
// mChart.setSpacePercent(20, 10);
4949
mChart.setYLegendCount(5);
@@ -72,10 +72,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
7272
break;
7373
}
7474
case R.id.actionToggleValues: {
75-
if (mChart.isDrawValuesEnabled())
76-
mChart.setDrawValues(false);
75+
if (mChart.isDrawYValuesEnabled())
76+
mChart.setDrawYValues(false);
7777
else
78-
mChart.setDrawValues(true);
78+
mChart.setDrawYValues(true);
7979
mChart.invalidate();
8080
break;
8181
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected void onCreate(Bundle savedInstanceState) {
4141
// mChart.setDrawFilled(true);
4242
// mChart.setRoundedYLegend(false);
4343
// mChart.setStartAtZero(true);
44-
mChart.setDrawValues(false);
44+
mChart.setDrawYValues(false);
4545
mChart.setLineWidth(5f);
4646
mChart.setCircleSize(5f);
4747
// mChart.setSpacePercent(20, 10);
@@ -71,10 +71,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
7171
break;
7272
}
7373
case R.id.actionToggleValues: {
74-
if (mChart.isDrawValuesEnabled())
75-
mChart.setDrawValues(false);
74+
if (mChart.isDrawYValuesEnabled())
75+
mChart.setDrawYValues(false);
7676
else
77-
mChart.setDrawValues(true);
77+
mChart.setDrawYValues(true);
7878
mChart.invalidate();
7979
break;
8080
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ protected void onCreate(Bundle savedInstanceState) {
6868
mPieChart.setData(xvalsSmall, small);
6969
mPieChart.setColorTemplate(new ColorTemplate(ColorTemplate.getColors(this, ColorTemplate.FRESH_COLORS)));
7070
// mChart3.highlightValues(new int[] {0, 1, 2, 3, 4} );
71-
mPieChart.setDrawValues(true);
72-
mPieChart.setDrawXVals(false);
71+
mPieChart.setDrawYValues(true);
72+
mPieChart.setDrawXValues(false);
7373

7474
mBarChart3D.setData(xvalsLarge, large);
7575
mBarChart3D.setColorTemplate(new ColorTemplate(ColorTemplate.getColors(this, ColorTemplate.LIBERTY_COLORS)));

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ protected void onCreate(Bundle savedInstanceState) {
4141
mChart = (PieChart) findViewById(R.id.chart1);
4242
mChart.setColorTemplate(new ColorTemplate(ColorTemplate.getColors(this, ColorTemplate.COLORFUL_COLORS)));
4343

44-
mChart.setDrawValues(true);
44+
mChart.setDrawYValues(true);
4545
mChart.setDrawCenterText(true);
4646

4747
mChart.setDescription("This is a description.");
4848
mChart.setDrawHoleEnabled(true);
49-
mChart.setDrawXVals(true);
49+
mChart.setDrawXValues(true);
5050
mChart.setTouchEnabled(true);
5151
mChart.setUsePercentValues(false);
5252
mChart.setOnChartValueSelectedListener(this);
@@ -71,10 +71,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
7171

7272
switch (item.getItemId()) {
7373
case R.id.actionToggleValues: {
74-
if (mChart.isDrawValuesEnabled())
75-
mChart.setDrawValues(false);
74+
if (mChart.isDrawYValuesEnabled())
75+
mChart.setDrawYValues(false);
7676
else
77-
mChart.setDrawValues(true);
77+
mChart.setDrawYValues(true);
7878
mChart.invalidate();
7979
break;
8080
}
@@ -103,10 +103,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
103103
break;
104104
}
105105
case R.id.actionToggleXVals: {
106-
if (mChart.isDrawXValsEnabled())
107-
mChart.setDrawXVals(false);
106+
if (mChart.isDrawXValuesEnabled())
107+
mChart.setDrawXValues(false);
108108
else
109-
mChart.setDrawXVals(true);
109+
mChart.setDrawXValues(true);
110110
mChart.invalidate();
111111
break;
112112
}

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

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ protected void calcMinMax() {
102102
// increase deltax by 1 because the bars have a width of 1
103103
mDeltaX++;
104104
}
105-
105+
106106
private RectF mBarRect = new RectF();
107107

108108
@Override
109109
protected void drawData() {
110-
110+
111111
ArrayList<Path> topPaths = new ArrayList<Path>();
112112
ArrayList<Path> sidePaths = new ArrayList<Path>();
113113

@@ -200,11 +200,83 @@ protected void drawData() {
200200
}
201201
}
202202

203+
@Override
204+
protected void drawValues() {
205+
206+
// if values are drawn
207+
if (mDrawYValues && mYVals.size() < mMaxVisibleCount * mScaleX) {
208+
209+
float[] valuePoints = new float[mYVals.size() * 2];
210+
211+
for (int i = 0; i < valuePoints.length; i += 2) {
212+
valuePoints[i] = i / 2 + 0.5f; // add 0.5f too keep the values
213+
// centered on top of the bars
214+
valuePoints[i + 1] = mYVals.get(i / 2);
215+
}
216+
217+
transformPointArray(valuePoints);
218+
219+
for (int i = 0; i < valuePoints.length; i += 2) {
220+
mDrawCanvas.drawText(
221+
mFormatValue.format(mYVals.get(i / 2)),
222+
valuePoints[i], valuePoints[i + 1] - 12, mValuePaint);
223+
}
224+
}
225+
}
226+
203227
@Override
204228
public void highlightValues(int[] indices) {
205229
super.highlightValues(indices);
206230
}
207231

232+
/**
233+
* sets the skew (default 0.3f), the skew indicates how much the 3D effect
234+
* of the chart is turned to the right
235+
*
236+
* @param skew
237+
*/
238+
public void setSkew(float skew) {
239+
this.mSkew = skew;
240+
}
241+
242+
/**
243+
* returns the skew value that indicates how much the 3D effect is turned to
244+
* the right
245+
*
246+
* @return
247+
*/
248+
public float getSkew() {
249+
return mSkew;
250+
}
251+
252+
/**
253+
* set the depth of the chart (default 0.3f), the depth indicates how much
254+
* the 3D effect of the chart goes back
255+
*
256+
* @param depth
257+
*/
258+
public void setDepth(float depth) {
259+
this.mDepth = depth;
260+
}
261+
262+
/**
263+
* returhs the depth, which indicates how much the 3D effect goes back
264+
*
265+
* @return
266+
*/
267+
public float getDepth() {
268+
return mDepth;
269+
}
270+
271+
/**
272+
* returns the space between bars in percent of the whole width of one value
273+
*
274+
* @return
275+
*/
276+
public float getBarSpace() {
277+
return mBarSpace * 100f;
278+
}
279+
208280
/**
209281
* sets the space between the bars in percent of the total bar width
210282
*
@@ -232,8 +304,22 @@ public boolean is3DEnabled() {
232304
return m3DEnabled;
233305
}
234306

235-
@Override
236-
protected void drawValues() {
307+
/**
308+
* returns the top colors that define the color of the top 3D effect path
309+
*
310+
* @return
311+
*/
312+
public int[] getTopColors() {
313+
return mTopColors;
314+
}
315+
316+
/**
317+
* returns the side colors that define the color of the side 3D effect path
318+
*
319+
* @return
320+
*/
321+
public int[] getSideColors() {
322+
return mSideColors;
237323
}
238324

239325
@Override

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

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,19 @@ public abstract class BarLineChartBase extends Chart {
6161
/** if true, the y-legend will always start at zero */
6262
protected boolean mStartAtZero = true;
6363

64+
/** paint object for the grid lines */
6465
protected Paint mGridPaint;
66+
67+
/** paint object for the (by default) lightgrey background of the grid */
6568
protected Paint mGridBackgroundPaint;
69+
70+
/** paint for the line surrounding the chart */
6671
protected Paint mOutLinePaint;
72+
73+
/** paint for the x-legend values */
6774
protected Paint mXLegendPaint;
75+
76+
/** paint for the y-legend values */
6877
protected Paint mYLegendPaint;
6978

7079
public BarLineChartBase(Context context, AttributeSet attrs, int defStyle) {
@@ -116,8 +125,9 @@ protected void init() {
116125
@Override
117126
protected void onDraw(Canvas canvas) {
118127
super.onDraw(canvas);
119-
120-
if(mDataNotSet) return;
128+
129+
if (mDataNotSet)
130+
return;
121131

122132
long starttime = System.currentTimeMillis();
123133

@@ -244,7 +254,7 @@ protected void calcFormats() {
244254
@Override
245255
protected void calcMinMax() {
246256
super.calcMinMax(); // calc min and max in the super class
247-
257+
248258
// additional handling for space (default 10% space)
249259

250260
float spaceTop = (mYMax - mYChartMin) / 100f * 10f;
@@ -486,29 +496,6 @@ protected void drawVerticalGrid() {
486496
}
487497
}
488498

489-
@Override
490-
protected void drawValues() {
491-
492-
// if values are drawn
493-
if (mDrawValues && mYVals.size() < mMaxVisibleCount * mScaleX) {
494-
495-
float[] valuePoints = new float[mYVals.size() * 2];
496-
497-
for (int i = 0; i < valuePoints.length; i += 2) {
498-
valuePoints[i] = i / 2;
499-
valuePoints[i + 1] = mYVals.get(i / 2);
500-
}
501-
502-
transformPointArray(valuePoints);
503-
504-
for (int i = 0; i < valuePoints.length; i += 2) {
505-
mDrawCanvas.drawText(
506-
mFormatValue.format(mYVals.get(i / 2)),
507-
valuePoints[i], valuePoints[i + 1] - 12, mValuePaint);
508-
}
509-
}
510-
}
511-
512499
/**
513500
* determines how much space (in percent of the total range) is left between
514501
* the loweset value of the chart and its bottom (bottomSpace) and the
@@ -518,22 +505,23 @@ protected void drawValues() {
518505
* @param bottomSpace
519506
* @param topSpace
520507
*/
521-
// public void setSpacePercent(int bottomSpace, int topSpace) {
522-
// mYSpacePercentBottom = bottomSpace;
523-
// mYSpacePercentTop = topSpace;
524-
// }
525-
508+
// public void setSpacePercent(int bottomSpace, int topSpace) {
509+
// mYSpacePercentBottom = bottomSpace;
510+
// mYSpacePercentTop = topSpace;
511+
// }
512+
526513
/**
527514
* sets the effective range of y-values the chart can display
515+
*
528516
* @param minY
529517
* @param maxY
530518
*/
531519
public void setYRange(float minY, float maxY) {
532-
520+
533521
mYChartMin = minY;
534522
mYChartMax = maxY;
535523
mDeltaY = mYChartMax - mYChartMin;
536-
524+
537525
prepareMatrix();
538526
prepareYLegend();
539527
}
@@ -694,9 +682,32 @@ public void setGridWidth(float width) {
694682
width = 3.0f;
695683
mGridWidth = width;
696684
}
697-
685+
698686
@Override
699687
protected void highlightValues(int[] indices) {
700688
super.highlightValues(indices);
701689
}
690+
691+
@Override
692+
public void setPaint(Paint p, int which) {
693+
super.setPaint(p, which);
694+
695+
switch (which) {
696+
case PAINT_GRID:
697+
mGridPaint = p;
698+
break;
699+
case PAINT_GRID_BACKGROUND:
700+
mGridBackgroundPaint = p;
701+
break;
702+
case PAINT_OUTLINE:
703+
mOutLinePaint = p;
704+
break;
705+
case PAINT_XLEGEND:
706+
mXLegendPaint = p;
707+
break;
708+
case PAINT_YLEGEND:
709+
mYLegendPaint = p;
710+
break;
711+
}
712+
}
702713
}

0 commit comments

Comments
 (0)