Skip to content

Commit aee1ed0

Browse files
committed
Worked on Horizontal-BarChart.
1 parent de83223 commit aee1ed0

10 files changed

Lines changed: 225 additions & 189 deletions

File tree

‎MPChartExample/AndroidManifest.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.xxmassdeveloper.mpchartexample"
4-
android:versionCode="29"
5-
android:versionName="1.7.4" >
4+
android:versionCode="30"
5+
android:versionName="2.0.0" >
66

77
<uses-sdk
88
android:minSdkVersion="8"

‎MPChartExample/build.gradle‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ android {
66
defaultConfig {
77
minSdkVersion 16
88
targetSdkVersion 19
9-
versionCode 29
10-
versionName '1.7.4'
9+
versionCode 30
10+
versionName '2.0.0'
1111

1212
sourceSets {
1313
main {

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,17 @@ protected void onCreate(Bundle savedInstanceState) {
8080

8181
XAxis xl = mChart.getXAxis();
8282
xl.setPosition(XAxisPosition.BOTTOM);
83-
xl.setCenterXLabelText(true);
8483
xl.setTypeface(tf);
84+
xl.setEnabled(false);
8585

8686
YAxis yl = mChart.getAxisLeft();
8787
yl.setTypeface(tf);
88-
yl.setLabelCount(8);
89-
// yl.setPosition(YLabelPosition.BOTH_SIDED);
88+
yl.setDrawAxisLine(true);
9089

90+
YAxis yr = mChart.getAxisRight();
91+
yr.setTypeface(tf);
92+
yr.setDrawAxisLine(true);
93+
9194
mChart.setValueTypeface(tf);
9295

9396
setData(12, 50);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ protected void onDraw(Canvas canvas) {
222222
Log.i(LOG_TAG, "DrawTime: " + (System.currentTimeMillis() - starttime) + " ms");
223223
}
224224

225-
private void prepareValuePxMatrix() {
225+
protected void prepareValuePxMatrix() {
226226

227227
if (mLogEnabled)
228228
Log.i(LOG_TAG, "Preparing Value-Px Matrix, xmin: " + mXChartMin + ", xmax: "

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

Lines changed: 19 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
import android.util.AttributeSet;
66

77
import com.github.mikephil.charting.components.YAxis;
8+
import com.github.mikephil.charting.renderer.HorizontalBarChartRenderer;
9+
import com.github.mikephil.charting.renderer.YAxisRendererHorizontalBarChart;
810

911
/**
1012
* BarChart with horizontal bar orientation. In this implementation, x- and
1113
* y-axis are switched.
14+
*
15+
* @author Philipp Jahoda
1216
*/
13-
public class HorizontalBarChart extends BarChart
14-
{
17+
public class HorizontalBarChart extends BarChart {
18+
1519
public HorizontalBarChart(Context context)
1620
{
1721
super(context);
@@ -31,115 +35,21 @@ public HorizontalBarChart(Context context, AttributeSet attrs,
3135
@Override
3236
protected void init() {
3337
super.init();
34-
// setDrawYLabels(true);
35-
// mTrans = new HorizontalBarChartTransformer();
38+
39+
mRenderer = new HorizontalBarChartRenderer(this, mAnimator, mViewPortHandler);
40+
mAxisRendererLeft = new YAxisRendererHorizontalBarChart(mViewPortHandler, mAxisLeft,
41+
mLeftAxisTransformer);
42+
mAxisRendererRight = new YAxisRendererHorizontalBarChart(mViewPortHandler, mAxisRight,
43+
mRightAxisTransformer);
3644
}
3745

38-
/**
39-
* Prepares a bar for drawing on the specified x-index and y-position. Also
40-
* prepares the shadow-bar if enabled.
41-
*
42-
* @param x the x-position
43-
* @param y the y-position
44-
* @param barspace the space between bars
45-
*/
46-
// @Override
47-
// protected void prepareBar(float x, float y, float barspace) {
48-
// super.prepareBar(x, y, barspace);
49-
//
50-
// float spaceHalf = barspace / 2f;
51-
//
52-
// float top = x + spaceHalf;
53-
// float bottom = x + 1f - spaceHalf;
54-
//
55-
// mBarRect.set(0, top, y, bottom);
56-
//
57-
// mTrans.rectValueToPixel(mBarRect, mPhaseY);
58-
//
59-
// // if a shadow is drawn, prepare it too
60-
// if (mDrawBarShadow) {
61-
// mBarShadow.set(mBarRect.left, mOffsetTop, mBarRect.right, getHeight() - mOffsetBottom);
62-
// }
63-
// }
64-
65-
// @Override
66-
// protected void calcModulus() {
67-
//
68-
// float[] values = new float[9];
69-
// mTrans.getTouchMatrix().getValues(values);
70-
//
71-
// mXLabels.mYAxisLabelModulus = (int) Math
72-
// .ceil((mData.getXValCount() * mXLabels.mLabelHeight)
73-
// / (mContentRect.height() * values[Matrix.MSCALE_Y]));
74-
// }
75-
//
76-
// @Override
77-
// protected void drawXLabels(float yPos)
78-
// {
79-
// if (!mDrawXLabels)
80-
// return;
81-
//
82-
// mXLabelPaint.setTextAlign(Paint.Align.RIGHT);
83-
// // pre allocate to save performance (dont allocate in loop)
84-
// float[] position = new float[] {
85-
// 0f, 0f
86-
// };
87-
//
88-
// float offset = Utils.calcTextHeight(mXLabelPaint, mData.getXVals().get(0)) / 2f;
89-
//
90-
// int step = mData.getDataSetCount();
91-
//
92-
// for (int i = 0; i < mData.getXValCount(); i += mXLabels.mYAxisLabelModulus) {
93-
//
94-
// position[1] = i * step + i * mData.getGroupSpace()
95-
// + mData.getGroupSpace() / 2f;
96-
//
97-
// // center the text
98-
// if (mXLabels.isCenterXLabelsEnabled())
99-
// position[1] += (step / 2f);
100-
//
101-
// mTrans.pointValuesToPixel(position);
102-
//
103-
// if (position[1] >= mOffsetTop && position[1] <= getHeight() - mOffsetBottom)
104-
// {
105-
// String label = mData.getXVals().get(i);
106-
//
107-
// mDrawCanvas.drawText(label, mOffsetLeft - 10, position[1] + offset, mXLabelPaint);
108-
// }
109-
// }
110-
// }
111-
//
112-
// private static String TALL_VALUE = "100%";
113-
//
114-
// @Override
115-
// protected float getPositiveYOffset(boolean drawAboveValueBar)
116-
// {
117-
// Rect bounds = new Rect();
118-
// mValuePaint.getTextBounds(TALL_VALUE, 0, TALL_VALUE.length(), bounds);
119-
//
120-
// return bounds.height() / 2;
121-
// }
122-
//
123-
// @Override
124-
// protected float getNegativeYOffset(boolean drawAboveValueBar)
125-
// {
126-
// return getPositiveYOffset(drawAboveValueBar);
127-
// }
128-
129-
// /**
130-
// * Sets up the y-axis labels. Computes the desired number of labels between
131-
// * the two given extremes. Unlike the papareXLabels() method, this method
132-
// * needs to be called upon every refresh of the view.
133-
// *
134-
// * @return
135-
// */
136-
// @Override
137-
// protected void prepareYLabels()
138-
// {
139-
// mYLabels = new XLabelsAsYLabels();
140-
// // super.prepareYLabels();
141-
//
142-
// }
46+
@Override
47+
protected void prepareValuePxMatrix() {
48+
mRightAxisTransformer.prepareMatrixValuePx(mXChartMin, mAxisRight.mAxisRange, mDeltaX,
49+
mAxisRight.mAxisMinimum);
50+
mLeftAxisTransformer.prepareMatrixValuePx(mXChartMin, mAxisLeft.mAxisRange, mDeltaX,
51+
mAxisLeft.mAxisMinimum);
52+
}
14353

14454
private class XLabelsAsYLabels extends YAxis
14555
{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void drawData(Canvas c) {
5656
}
5757
}
5858

59-
private void drawDataSet(Canvas c, BarDataSet dataSet, int index) {
59+
protected void drawDataSet(Canvas c, BarDataSet dataSet, int index) {
6060

6161
Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
6262

@@ -291,7 +291,7 @@ public void drawValues(Canvas c) {
291291
* @param xPos
292292
* @param yPos
293293
*/
294-
private void drawValue(Canvas c, float val, float xPos, float yPos) {
294+
protected void drawValue(Canvas c, float val, float xPos, float yPos) {
295295

296296
String value = mChart.getValueFormatter().getFormattedValue(val);
297297
c.drawText(value, xPos, yPos,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
package com.github.mikephil.charting.renderer;
3+
4+
import com.github.mikephil.charting.animation.ChartAnimator;
5+
import com.github.mikephil.charting.interfaces.BarDataProvider;
6+
import com.github.mikephil.charting.utils.Transformer;
7+
8+
/**
9+
* Renderer for the HorizontalBarChart.
10+
*
11+
* @author Philipp Jahoda
12+
*/
13+
public class HorizontalBarChartRenderer extends BarChartRenderer {
14+
15+
public HorizontalBarChartRenderer(BarDataProvider chart, ChartAnimator animator,
16+
ViewPortHandler viewPortHandler) {
17+
super(chart, animator, viewPortHandler);
18+
}
19+
20+
@Override
21+
protected void prepareBar(float x, float y, float barspace, Transformer trans) {
22+
23+
float spaceHalf = barspace / 2f;
24+
25+
float top = x - 0.5f + spaceHalf;
26+
float bottom = x + 0.5f - spaceHalf;
27+
float left = y >= 0 ? y : 0;
28+
float right = y <= 0 ? y : 0;
29+
30+
mBarRect.set(left, top, right, bottom);
31+
32+
trans.rectValueToPixel(mBarRect, mAnimator.getPhaseY());
33+
34+
// if a shadow is drawn, prepare it too
35+
if (mChart.isDrawBarShadowEnabled()) {
36+
mBarShadow.set(mBarRect.left, mViewPortHandler.offsetTop(), mBarRect.right,
37+
mViewPortHandler.contentBottom());
38+
}
39+
}
40+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void computeAxis(float yMin, float yMax) {
9696
*
9797
* @return
9898
*/
99-
private void computeAxisValues(float min, float max) {
99+
protected void computeAxisValues(float min, float max) {
100100

101101
float yMin = min;
102102
float yMax = max;
@@ -240,10 +240,10 @@ protected void drawAxisLine(Canvas c) {
240240
/**
241241
* draws the y-labels on the specified x-position
242242
*
243-
* @param xPos
243+
* @param fixedPosition
244244
* @param positions
245245
*/
246-
private void drawYLabels(Canvas c, float xPos, float[] positions, float yOffset) {
246+
protected void drawYLabels(Canvas c, float fixedPosition, float[] positions, float offset) {
247247

248248
// draw
249249
for (int i = 0; i < mYAxis.mEntryCount; i++) {
@@ -253,7 +253,7 @@ private void drawYLabels(Canvas c, float xPos, float[] positions, float yOffset)
253253
if (!mYAxis.isDrawTopYLabelEntryEnabled() && i >= mYAxis.mEntryCount - 1)
254254
return;
255255

256-
c.drawText(text, xPos, positions[i * 2 + 1] + yOffset, mAxisPaint);
256+
c.drawText(text, fixedPosition, positions[i * 2 + 1] + offset, mAxisPaint);
257257
}
258258
}
259259

0 commit comments

Comments
 (0)