Skip to content

Commit 8bea117

Browse files
authored
Merge pull request #212 from weilanxiao/fix_y_label_width
Solve the problem of insufficient width of Y-axis label display
2 parents a45f5c6 + 8bb809b commit 8bea117

9 files changed

+30
-2
lines changed

MPChartLib/src/main/java/com/github/mikephil/charting/components/AxisBase.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import android.graphics.Color;
55
import android.graphics.DashPathEffect;
6+
import android.graphics.Paint;
67
import android.util.Log;
78

89
import com.github.mikephil.charting.formatter.DefaultAxisValueFormatter;
@@ -524,6 +525,33 @@ public String getLongestLabel() {
524525
return longest;
525526
}
526527

528+
/**
529+
* Returns the longest formatted label (in terms of px), this axis
530+
* contains.
531+
* If paint is null, then returns the longest formatted label (in terms of characters), this axis contains.
532+
*
533+
* @return
534+
*/
535+
public String getLongestLabel(Paint p) {
536+
if (p == null) {
537+
return getLongestLabel();
538+
}
539+
String longest = "";
540+
float max = 0f;
541+
542+
for (int i = 0; i < mEntries.length; i++) {
543+
String text = getFormattedLabel(i);
544+
if (text != null) {
545+
float width = p.measureText(text);
546+
if (max < width) {
547+
longest = text;
548+
}
549+
}
550+
}
551+
552+
return longest;
553+
}
554+
527555
public String getFormattedLabel(int index) {
528556

529557
if (index < 0 || index >= mEntries.length)

MPChartLib/src/main/java/com/github/mikephil/charting/components/YAxis.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public float getRequiredWidthSpace(Paint p) {
344344

345345
p.setTextSize(mTextSize);
346346

347-
String label = getLongestLabel();
347+
String label = getLongestLabel(p);
348348
float width = (float) Utils.calcTextWidth(p, label) + getXOffset() * 2f;
349349

350350
float minWidth = getMinWidth();
@@ -371,7 +371,7 @@ public float getRequiredHeightSpace(Paint p) {
371371

372372
p.setTextSize(mTextSize);
373373

374-
String label = getLongestLabel();
374+
String label = getLongestLabel(p);
375375
return (float) Utils.calcTextHeight(p, label) + getYOffset() * 2f;
376376
}
377377

Loading
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)