Skip to content
Prev Previous commit
Next Next commit
Labels at specific positions for x and y axis PhilJay#2692
(cherry picked from commit c85e212)
  • Loading branch information
Raji ewisor
Raji authored and ewisor committed Apr 3, 2025
commit 97afcbd54a92edecc7dfaac79179d741f349876d
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ public abstract class AxisBase extends ComponentBase {
*/
public float mAxisRange = 0f;

/**
* if true, then labels are displayed using specificLabelPositions instead of computed ones
*/
private boolean showSpecificLabelPositions = false;

/**
* specify to which values labels must be displayed. has no effect if not used showSpecificLabelPositions set to true
*/
private float[] specificLabelPositions = new float[]{};

/**
* default constructor
*/
Expand Down Expand Up @@ -779,4 +789,27 @@ public void setSpaceMax(float mSpaceMax)
{
this.mSpaceMax = mSpaceMax;
}

/**
* if set to true, labels will be displayed at the specific positions passed in via setSpecificLabelPositions
*/
public void setShowSpecificLabelPositions(boolean showSpecificLabelPositions)
{
this.showSpecificLabelPositions = showSpecificLabelPositions;
}

public boolean isShowSpecificLabelPositions()
{
return showSpecificLabelPositions;
}

public void setSpecificLabelPositions(float[] specificLabelPositions)
{
this.specificLabelPositions = specificLabelPositions;
}

public float[] getSpecificLabelPositions()
{
return specificLabelPositions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,20 @@ protected void drawLabels(Canvas c, float pos, MPPointF anchor) {

float[] positions = new float[mXAxis.mEntryCount * 2];

for (int i = 0; i < positions.length; i += 2) {

// only fill x values
if (centeringEnabled) {
positions[i] = mXAxis.mCenteredEntries[i / 2];
} else {
positions[i] = mXAxis.mEntries[i / 2];
if (mXAxis.isShowSpecificLabelPositions()) {
positions = new float[mXAxis.getSpecificLabelPositions().length * 2];
for (int i = 0; i < positions.length; i += 2) {
positions[i] = mXAxis.getSpecificLabelPositions()[i / 2];
}
} else {
for (int i = 0; i < positions.length; i += 2) {

// only fill x values
if (centeringEnabled) {
positions[i] = mXAxis.mCenteredEntries[i / 2];
} else {
positions[i] = mXAxis.mEntries[i / 2];
}
}
}

Expand All @@ -201,7 +208,9 @@ protected void drawLabels(Canvas c, float pos, MPPointF anchor) {

if (mViewPortHandler.isInBoundsX(x)) {

String label = mXAxis.getValueFormatter().getAxisLabel(mXAxis.mEntries[i / 2], mXAxis);
String label = mXAxis.isShowSpecificLabelPositions() ?
mXAxis.getValueFormatter().getAxisLabel(mXAxis.getSpecificLabelPositions()[i / 2], mXAxis)
: mXAxis.getValueFormatter().getAxisLabel(mXAxis.mEntries[i / 2], mXAxis);

if (mXAxis.isAvoidFirstLastClippingEnabled()) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,23 @@ public void renderAxisLine(Canvas c) {
*/
protected void drawYLabels(Canvas c, float fixedPosition, float[] positions, float offset) {

if (mYAxis.isShowSpecificLabelPositions()) {
float[] specificLabelsPositions = new float[mYAxis.getSpecificLabelPositions().length * 2];
for (int i = 0; i < mYAxis.getSpecificLabelPositions().length; i++) {
specificLabelsPositions[i * 2 + 1] = mYAxis.getSpecificLabelPositions()[i];
}
mTrans.pointValuesToPixel(specificLabelsPositions);

for (int i = 0; i < mYAxis.getSpecificLabelPositions().length; i++) {
float y = specificLabelsPositions[i * 2 + 1];
if (mViewPortHandler.isInBoundsY(y)) {
String text = mYAxis.getValueFormatter().getFormattedValue(mYAxis.getSpecificLabelPositions()[i], mYAxis);
c.drawText(text, fixedPosition, y + offset, mAxisLabelPaint);
}
}
return;
}

final int from = mYAxis.isDrawBottomYLabelEntryEnabled() ? 0 : 1;
final int to = mYAxis.isDrawTopYLabelEntryEnabled()
? mYAxis.mEntryCount
Expand Down