Skip to content

Commit c85e212

Browse files
RajiJoe Morales
authored andcommitted
Labels at specific positions for x and y axis PhilJay#2692
1 parent 4b0fdfc commit c85e212

3 files changed

Lines changed: 67 additions & 8 deletions

File tree

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,16 @@ public abstract class AxisBase extends ComponentBase {
152152
*/
153153
public float mAxisRange = 0f;
154154

155+
/**
156+
* if true, then labels are displayed using specificLabelPositions instead of computed ones
157+
*/
158+
private boolean showSpecificLabelPositions = false;
159+
160+
/**
161+
* specify to which values labels must be displayed. has no effect if not used showSpecificLabelPositions set to true
162+
*/
163+
private float[] specificLabelPositions = new float[]{};
164+
155165
/**
156166
* default constructor
157167
*/
@@ -780,4 +790,27 @@ public void setSpaceMax(float mSpaceMax)
780790
{
781791
this.mSpaceMax = mSpaceMax;
782792
}
793+
794+
/**
795+
* if set to true, labels will be displayed at the specific positions passed in via setSpecificLabelPositions
796+
*/
797+
public void setShowSpecificLabelPositions(boolean showSpecificLabelPositions)
798+
{
799+
this.showSpecificLabelPositions = showSpecificLabelPositions;
800+
}
801+
802+
public boolean isShowSpecificLabelPositions()
803+
{
804+
return showSpecificLabelPositions;
805+
}
806+
807+
public void setSpecificLabelPositions(float[] specificLabelPositions)
808+
{
809+
this.specificLabelPositions = specificLabelPositions;
810+
}
811+
812+
public float[] getSpecificLabelPositions()
813+
{
814+
return specificLabelPositions;
815+
}
783816
}

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,20 @@ protected void drawLabels(Canvas c, float pos, MPPointF anchor) {
184184

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

187-
for (int i = 0; i < positions.length; i += 2) {
188-
189-
// only fill x values
190-
if (centeringEnabled) {
191-
positions[i] = mXAxis.mCenteredEntries[i / 2];
192-
} else {
193-
positions[i] = mXAxis.mEntries[i / 2];
187+
if (mXAxis.isShowSpecificLabelPositions()) {
188+
positions = new float[mXAxis.getSpecificLabelPositions().length * 2];
189+
for (int i = 0; i < positions.length; i += 2) {
190+
positions[i] = mXAxis.getSpecificLabelPositions()[i / 2];
191+
}
192+
} else {
193+
for (int i = 0; i < positions.length; i += 2) {
194+
195+
// only fill x values
196+
if (centeringEnabled) {
197+
positions[i] = mXAxis.mCenteredEntries[i / 2];
198+
} else {
199+
positions[i] = mXAxis.mEntries[i / 2];
200+
}
194201
}
195202
}
196203

@@ -202,7 +209,9 @@ protected void drawLabels(Canvas c, float pos, MPPointF anchor) {
202209

203210
if (mViewPortHandler.isInBoundsX(x)) {
204211

205-
String label = mXAxis.getValueFormatter().getFormattedValue(mXAxis.mEntries[i / 2], mXAxis);
212+
String label = mXAxis.isShowSpecificLabelPositions() ?
213+
mXAxis.getValueFormatter().getFormattedValue(mXAxis.getSpecificLabelPositions()[i / 2], mXAxis)
214+
: mXAxis.getValueFormatter().getFormattedValue(mXAxis.mEntries[i / 2], mXAxis);
206215

207216
if (mXAxis.isAvoidFirstLastClippingEnabled()) {
208217

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,23 @@ public void renderAxisLine(Canvas c) {
114114
*/
115115
protected void drawYLabels(Canvas c, float fixedPosition, float[] positions, float offset) {
116116

117+
if (mYAxis.isShowSpecificLabelPositions()) {
118+
float[] specificLabelsPositions = new float[mYAxis.getSpecificLabelPositions().length * 2];
119+
for (int i = 0; i < mYAxis.getSpecificLabelPositions().length; i++) {
120+
specificLabelsPositions[i * 2 + 1] = mYAxis.getSpecificLabelPositions()[i];
121+
}
122+
mTrans.pointValuesToPixel(specificLabelsPositions);
123+
124+
for (int i = 0; i < mYAxis.getSpecificLabelPositions().length; i++) {
125+
float y = specificLabelsPositions[i * 2 + 1];
126+
if (mViewPortHandler.isInBoundsY(y)) {
127+
String text = mYAxis.getValueFormatter().getFormattedValue(mYAxis.getSpecificLabelPositions()[i], mYAxis);
128+
c.drawText(text, fixedPosition, y + offset, mAxisLabelPaint);
129+
}
130+
}
131+
return;
132+
}
133+
117134
final int from = mYAxis.isDrawBottomYLabelEntryEnabled() ? 0 : 1;
118135
final int to = mYAxis.isDrawTopYLabelEntryEnabled()
119136
? mYAxis.mEntryCount

0 commit comments

Comments
 (0)