forked from PhilJay/MPAndroidChart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXAxisRendererBarChart.java
More file actions
111 lines (79 loc) · 3.35 KB
/
XAxisRendererBarChart.java
File metadata and controls
111 lines (79 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.github.mikephil.charting.renderer;
import android.graphics.Canvas;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.utils.Transformer;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;
public class XAxisRendererBarChart extends XAxisRenderer {
protected BarChart mChart;
public XAxisRendererBarChart(ViewPortHandler viewPortHandler, XAxis xAxis, Transformer trans,
BarChart chart) {
super(viewPortHandler, xAxis, trans);
this.mChart = chart;
}
/**
* draws the x-labels on the specified y-position
*
* @param pos
*/
@Override
protected void drawLabels(Canvas c, float pos) {
// pre allocate to save performance (dont allocate in loop)
float[] position = new float[] {
0f, 0f
};
BarData bd = mChart.getData();
int step = bd.getDataSetCount();
for (int i = mMinX; i <= mMaxX; i += mXAxis.mAxisLabelModulus) {
position[0] = i * step + i * bd.getGroupSpace()
+ bd.getGroupSpace() / 2f;
// consider groups (center label for each group)
if (step > 1) {
position[0] += ((float) step - 1f) / 2f;
}
mTrans.pointValuesToPixel(position);
if (mViewPortHandler.isInBoundsX(position[0]) && i >= 0
&& i < mXAxis.getValues().size()) {
String label = mXAxis.getValues().get(i);
if (mXAxis.isAvoidFirstLastClippingEnabled()) {
// avoid clipping of the last
if (i == mXAxis.getValues().size() - 1) {
float width = Utils.calcTextWidth(mAxisLabelPaint, label);
if (width > mViewPortHandler.offsetRight() * 2
&& position[0] + width > mViewPortHandler.getChartWidth())
position[0] -= width / 2;
// avoid clipping of the first
} else if (i == 0) {
float width = Utils.calcTextWidth(mAxisLabelPaint, label);
position[0] += width / 2;
}
}
c.drawText(label, position[0],
pos,
mAxisLabelPaint);
}
}
}
@Override
public void renderGridLines(Canvas c) {
if (!mXAxis.isDrawGridLinesEnabled() || !mXAxis.isEnabled())
return;
float[] position = new float[] {
0f, 0f
};
mGridPaint.setColor(mXAxis.getGridColor());
mGridPaint.setStrokeWidth(mXAxis.getGridLineWidth());
BarData bd = mChart.getData();
int step = bd.getDataSetCount();
for (int i = mMinX; i < mMaxX; i += mXAxis.mAxisLabelModulus) {
position[0] = i * step + i * bd.getGroupSpace() - 0.5f;
mTrans.pointValuesToPixel(position);
if (mViewPortHandler.isInBoundsX(position[0])) {
c.drawLine(position[0], mViewPortHandler.offsetTop(), position[0],
mViewPortHandler.contentBottom(), mGridPaint);
}
}
}
}