Skip to content

Commit 8062018

Browse files
Rajipapjul
authored andcommitted
Labels at specific positions for x and y axis #2692
1 parent db20125 commit 8062018

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
@@ -185,6 +185,16 @@ public void setAxisMaxLabels(int labels) {
185185
mAxisMaxLabels = labels;
186186
}
187187

188+
/**
189+
* if true, then labels are displayed using specificLabelPositions instead of computed ones
190+
*/
191+
private boolean showSpecificLabelPositions = false;
192+
193+
/**
194+
* specify to which values labels must be displayed. has no effect if not used showSpecificLabelPositions set to true
195+
*/
196+
private float[] specificLabelPositions = new float[]{};
197+
188198
/**
189199
* default constructor
190200
*/
@@ -813,4 +823,27 @@ public void setSpaceMax(float mSpaceMax)
813823
{
814824
this.mSpaceMax = mSpaceMax;
815825
}
826+
827+
/**
828+
* if set to true, labels will be displayed at the specific positions passed in via setSpecificLabelPositions
829+
*/
830+
public void setShowSpecificLabelPositions(boolean showSpecificLabelPositions)
831+
{
832+
this.showSpecificLabelPositions = showSpecificLabelPositions;
833+
}
834+
835+
public boolean isShowSpecificLabelPositions()
836+
{
837+
return showSpecificLabelPositions;
838+
}
839+
840+
public void setSpecificLabelPositions(float[] specificLabelPositions)
841+
{
842+
this.specificLabelPositions = specificLabelPositions;
843+
}
844+
845+
public float[] getSpecificLabelPositions()
846+
{
847+
return specificLabelPositions;
848+
}
816849
}

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
@@ -180,13 +180,20 @@ protected void drawLabels(Canvas c, float pos, MPPointF anchor) {
180180

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

183-
for (int i = 0; i < positions.length; i += 2) {
184-
185-
// only fill x values
186-
if (centeringEnabled) {
187-
positions[i] = mXAxis.mCenteredEntries[i / 2];
188-
} else {
189-
positions[i] = mXAxis.mEntries[i / 2];
183+
if (mXAxis.isShowSpecificLabelPositions()) {
184+
positions = new float[mXAxis.getSpecificLabelPositions().length * 2];
185+
for (int i = 0; i < positions.length; i += 2) {
186+
positions[i] = mXAxis.getSpecificLabelPositions()[i / 2];
187+
}
188+
} else {
189+
for (int i = 0; i < positions.length; i += 2) {
190+
191+
// only fill x values
192+
if (centeringEnabled) {
193+
positions[i] = mXAxis.mCenteredEntries[i / 2];
194+
} else {
195+
positions[i] = mXAxis.mEntries[i / 2];
196+
}
190197
}
191198
}
192199

@@ -198,7 +205,9 @@ protected void drawLabels(Canvas c, float pos, MPPointF anchor) {
198205

199206
if (mViewPortHandler.isInBoundsX(x)) {
200207

201-
String label = mXAxis.getValueFormatter().getFormattedValue(mXAxis.mEntries[i / 2], mXAxis);
208+
String label = mXAxis.isShowSpecificLabelPositions() ?
209+
mXAxis.getValueFormatter().getFormattedValue(mXAxis.getSpecificLabelPositions()[i / 2], mXAxis)
210+
: mXAxis.getValueFormatter().getFormattedValue(mXAxis.mEntries[i / 2], mXAxis);
202211

203212
if (mXAxis.isAvoidFirstLastClippingEnabled()) {
204213

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
@@ -120,6 +120,23 @@ public void renderAxisLine(Canvas c) {
120120
*/
121121
protected void drawYLabels(Canvas c, float fixedPosition, float[] positions, float offset) {
122122

123+
if (mYAxis.isShowSpecificLabelPositions()) {
124+
float[] specificLabelsPositions = new float[mYAxis.getSpecificLabelPositions().length * 2];
125+
for (int i = 0; i < mYAxis.getSpecificLabelPositions().length; i++) {
126+
specificLabelsPositions[i * 2 + 1] = mYAxis.getSpecificLabelPositions()[i];
127+
}
128+
mTrans.pointValuesToPixel(specificLabelsPositions);
129+
130+
for (int i = 0; i < mYAxis.getSpecificLabelPositions().length; i++) {
131+
float y = specificLabelsPositions[i * 2 + 1];
132+
if (mViewPortHandler.isInBoundsY(y)) {
133+
String text = mYAxis.getValueFormatter().getFormattedValue(mYAxis.getSpecificLabelPositions()[i], mYAxis);
134+
c.drawText(text, fixedPosition, y + offset, mAxisLabelPaint);
135+
}
136+
}
137+
return;
138+
}
139+
123140
final int from = mYAxis.isDrawBottomYLabelEntryEnabled() ? 0 : 1;
124141
final int to = mYAxis.isDrawTopYLabelEntryEnabled()
125142
? mYAxis.mEntryCount

0 commit comments

Comments
 (0)