Skip to content

Commit c320127

Browse files
committed
feat: added axis.ensureLastLabel and axis.allowLastLabelAboveMax for custom rendering
1 parent a30476f commit c320127

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

src/charting/components/AxisBase.ts

+4
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ export abstract class AxisBase extends ComponentBase {
9090

9191
protected mCenterAxisLabels = false;
9292

93+
public ensureLastLabel = false;
94+
95+
public allowLastLabelAboveMax = false;
96+
9397
/**
9498
* the path effect of the axis line that makes dashed lines possible
9599
*/

src/charting/components/YAxis.ts

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export class YAxis extends AxisBase {
109109
super();
110110
this.mAxisDependency = position;
111111
this.mYOffset = 0;
112+
this.allowLastLabelAboveMax = true;
112113
}
113114

114115
public getAxisDependency() {

src/charting/renderer/AxisRenderer.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ export abstract class AxisRenderer extends Renderer {
204204
}
205205
// use Math.floor(yMax / interval) + 1 instead of
206206
// Math.floor(yMax / interval) to make sure the axis is showed "above" the higghest value
207-
const last = interval === 0 ? 0 : Utils.nextUp(Math.floor(yMax / interval) * interval);
207+
let last = interval === 0 ? 0 : Utils.nextUp(Math.floor(yMax / interval) * interval);
208+
if (axis.ensureLastLabel && last < max) {
209+
last = Math.min(max, last + interval);
210+
}
208211
let f;
209212
let i;
210213

@@ -213,6 +216,9 @@ export abstract class AxisRenderer extends Renderer {
213216
++n;
214217
}
215218
}
219+
if (axis.ensureLastLabel && (n - 1) * interval < last) {
220+
n++;
221+
}
216222

217223
axis.mEntryCount = n;
218224

@@ -222,10 +228,12 @@ export abstract class AxisRenderer extends Renderer {
222228
axis.mLabels = [];
223229
}
224230

225-
for (f = first, i = 0; i < n; f += interval, ++i) {
231+
for (f = first, i = 0; i <= n; f += interval, ++i) {
226232
if (f === 0.0) {
227233
// Fix for negative zero case (Where value == -0.0, and 0.0 == -0.0)
228234
f = 0.0;
235+
} else if (!axis.allowLastLabelAboveMax && f > max) {
236+
f = max;
229237
}
230238

231239
axis.mEntries[i] = f;

0 commit comments

Comments
 (0)