Skip to content

Commit 99c87c4

Browse files
committed
Fix 1410
1 parent 9137956 commit 99c87c4

6 files changed

Lines changed: 33 additions & 28 deletions

File tree

MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ protected void onCreate(Bundle savedInstanceState) {
131131
leftAxis.setAxisMaxValue(220f);
132132
leftAxis.setAxisMinValue(-50f);
133133
leftAxis.setStartAtZero(false);
134+
leftAxis.setStartAtZero(false);
134135
//leftAxis.setYOffset(20f);
135136
leftAxis.enableGridDashedLine(10f, 10f, 0f);
136137

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ public void notifyDataSetChanged() {
341341
mAxisRendererLeft.computeAxis(mAxisLeft.mAxisMinimum, mAxisLeft.mAxisMaximum);
342342
mAxisRendererRight.computeAxis(mAxisRight.mAxisMinimum, mAxisRight.mAxisMaximum);
343343

344-
mXAxisRenderer.computeAxis(mData.getXValAverageLength(), mData.getXVals());
344+
mXAxisRenderer.computeAxis(mData.getXValMaximumLength(), mData.getXVals());
345345

346346
if (mLegend != null)
347347
mLegendRenderer.computeLegend(mData);

MPChartLib/src/com/github/mikephil/charting/charts/RadarChart.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public void notifyDataSetChanged() {
171171
// }
172172

173173
mYAxisRenderer.computeAxis(mYAxis.mAxisMinimum, mYAxis.mAxisMaximum);
174-
mXAxisRenderer.computeAxis(mData.getXValAverageLength(), mData.getXVals());
174+
mXAxisRenderer.computeAxis(mData.getXValMaximumLength(), mData.getXVals());
175175

176176
if (mLegend != null && !mLegend.isLegendCustom())
177177
mLegendRenderer.computeLegend(mData);

MPChartLib/src/com/github/mikephil/charting/components/XAxis.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class XAxis extends AxisBase {
4747
/**
4848
* This is the angle for drawing the X axis labels (in degrees)
4949
*/
50-
protected float mLabelRotationAngle = 0.f;
50+
protected float mLabelRotationAngle = 0f;
5151

5252
/**
5353
* the space that should be left out (in characters) between the x-axis
@@ -67,14 +67,6 @@ public class XAxis extends AxisBase {
6767
* it's auto, if true, then custom. default: false (automatic modulus)
6868
*/
6969
private boolean mIsAxisModulusCustom = false;
70-
71-
/**
72-
* the modulus that indicates if a value at a specified index in an
73-
* array(list) for the y-axis-labels is drawn or not. If index % modulus ==
74-
* 0 DRAW, else dont draw. THIS IS ONLY FOR HORIZONTAL BARCHART.
75-
*/
76-
public int mYAxisLabelModulus = 1;
77-
7870
/**
7971
* if set to true, the chart will avoid that the first and last label entry
8072
* in the chart "clip" off the edge of the chart

MPChartLib/src/com/github/mikephil/charting/data/ChartData.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ public abstract class ChartData<T extends IDataSet<? extends Entry>> {
4545
private int mYValCount = 0;
4646

4747
/**
48-
* contains the average length (in characters) an entry in the x-vals array
48+
* contains the maximum length (in characters) an entry in the x-vals array
4949
* has
5050
*/
51-
private float mXValAverageLength = 0;
51+
private float mXValMaximumLength = 0;
5252

5353
/**
5454
* holds all x-values the chart represents
@@ -139,26 +139,30 @@ protected void init() {
139139
calcYValueCount();
140140
calcMinMax(0, mYValCount);
141141

142-
calcXValAverageLength();
142+
calcXValMaximumLength();
143143
}
144144

145145
/**
146146
* calculates the average length (in characters) across all x-value strings
147147
*/
148-
private void calcXValAverageLength() {
148+
private void calcXValMaximumLength() {
149149

150150
if (mXVals.size() <= 0) {
151-
mXValAverageLength = 1;
151+
mXValMaximumLength = 1;
152152
return;
153153
}
154154

155-
float sum = 1f;
155+
int max = 1;
156156

157157
for (int i = 0; i < mXVals.size(); i++) {
158-
sum += mXVals.get(i).length();
158+
159+
int length = mXVals.get(i).length();
160+
161+
if(length > max)
162+
max = length;
159163
}
160164

161-
mXValAverageLength = sum / (float) mXVals.size();
165+
mXValMaximumLength = max;
162166
}
163167

164168
/**
@@ -343,13 +347,13 @@ public float getYMax(AxisDependency axis) {
343347
}
344348

345349
/**
346-
* returns the average length (in characters) across all values in the
350+
* returns the maximum length (in characters) across all values in the
347351
* x-vals array
348352
*
349353
* @return
350354
*/
351-
public float getXValAverageLength() {
352-
return mXValAverageLength;
355+
public float getXValMaximumLength() {
356+
return mXValMaximumLength;
353357
}
354358

355359
/**
@@ -378,7 +382,7 @@ public List<String> getXVals() {
378382
*/
379383
public void addXValue(String xVal) {
380384

381-
mXValAverageLength = (mXValAverageLength + xVal.length()) / 2f;
385+
mXValMaximumLength = (mXValMaximumLength + xVal.length()) / 2f;
382386
mXVals.add(xVal);
383387
}
384388

MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRenderer.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ public void computeAxis(float xValAverageLength, List<String> xValues) {
4040

4141
StringBuilder widthText = new StringBuilder();
4242

43-
int max = Math.round(xValAverageLength
44-
+ mXAxis.getSpaceBetweenLabels());
43+
int xValChars = Math.round(xValAverageLength);
4544

46-
for (int i = 0; i < max; i++) {
45+
for (int i = 0; i < xValChars; i++) {
4746
widthText.append('h');
4847
}
4948

@@ -57,9 +56,18 @@ public void computeAxis(float xValAverageLength, List<String> xValues) {
5756
labelHeight,
5857
mXAxis.getLabelRotationAngle());
5958

60-
mXAxis.mLabelWidth = Math.round(labelWidth);
59+
StringBuilder space = new StringBuilder();
60+
int xValSpaceChars = mXAxis.getSpaceBetweenLabels();
61+
62+
for (int i = 0; i < xValSpaceChars; i++) {
63+
space.append('h');
64+
}
65+
66+
final FSize spaceSize = Utils.calcTextSize(mAxisLabelPaint, space.toString());
67+
68+
mXAxis.mLabelWidth = Math.round(labelWidth + spaceSize.width);
6169
mXAxis.mLabelHeight = Math.round(labelHeight);
62-
mXAxis.mLabelRotatedWidth = Math.round(labelRotatedSize.width);
70+
mXAxis.mLabelRotatedWidth = Math.round(labelRotatedSize.width + spaceSize.width);
6371
mXAxis.mLabelRotatedHeight = Math.round(labelRotatedSize.height);
6472

6573
mXAxis.setValues(xValues);

0 commit comments

Comments
 (0)