Skip to content

Commit 42085e6

Browse files
committed
fix: axis new custom renderers
1 parent b17209f commit 42085e6

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

src/charting/renderer/AxisRenderer.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ import { Renderer } from './Renderer';
1010

1111
export type CustomRendererGridLineFunction = (c: Canvas, renderer: AxisRenderer, rect: RectF, x, y, axisValue, paint: Paint) => void;
1212
export type CustomRendererLimitLineFunction = (c: Canvas, renderer: AxisRenderer, limitLine: LimitLine, rect: RectF, x: number, paint: Paint) => void;
13+
export type CustomRendererLabelFunction = (c: Canvas, renderer: AxisRenderer, text, x, y, paint: Paint, anchor, angleDegrees) => void;
14+
export type CustomRendererTickFunction = (c: Canvas, renderer: AxisRenderer, startX: number, startY: number, stopX: number, stopY: number, paint: Paint) => void;
1315
export interface CustomRenderer extends BaseCustomRenderer {
16+
drawLabel?: CustomRendererLabelFunction;
1417
drawGridLine?: CustomRendererGridLineFunction;
1518
drawLimitLine?: CustomRendererLimitLineFunction;
19+
drawMarkTicket?: CustomRendererTickFunction;
1620
}
1721

1822
/**
@@ -162,8 +166,8 @@ export abstract class AxisRenderer extends Renderer {
162166
}
163167

164168
// Find out how much spacing (in y value space) between axis values
165-
const rawInterval = range / labelCount;
166-
let interval = axis.isForceIntervalEnabled() ? axis.getForcedInterval() : Utils.roundToNextSignificant(rawInterval);
169+
const rawInterval = range / (labelCount - 1);
170+
let interval = axis.isForceIntervalEnabled() ? axis.getForcedInterval() : rawInterval;
167171

168172
// If granularity is enabled, then do not allow the interval to go below specified granularity.
169173
// This is used to avoid repeated values when rounding values for display.
@@ -197,6 +201,9 @@ export abstract class AxisRenderer extends Renderer {
197201
let v = min;
198202

199203
for (let i = 0; i < labelCount; i++) {
204+
if (axis.ensureLastLabel && i === labelCount - 1) {
205+
v = max;
206+
}
200207
axis.mEntries[i] = v;
201208
axis.mLabels[i] = formatter.getAxisLabel(v, axis, this.mViewPortHandler);
202209
v += interval;

src/charting/renderer/XAxisRenderer.ts

+21-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { MPPointF } from '../utils/MPPointF';
66
import { Transformer } from '../utils/Transformer';
77
import { Utils } from '../utils/Utils';
88
import { ViewPortHandler } from '../utils/ViewPortHandler';
9-
import { AxisRenderer, CustomRendererGridLineFunction, CustomRendererLimitLineFunction } from './AxisRenderer';
9+
import { AxisRenderer, CustomRendererGridLineFunction, CustomRendererLabelFunction, CustomRendererLimitLineFunction } from './AxisRenderer';
1010

1111
export class XAxisRenderer extends AxisRenderer {
1212
protected mXAxis: XAxis;
@@ -162,6 +162,9 @@ export class XAxisRenderer extends AxisRenderer {
162162
if (entryCount === 0) {
163163
return;
164164
}
165+
166+
const customRender = axis.getCustomRenderer();
167+
const customRenderFunction = customRender && customRender.drawLabel;
165168
const length = entryCount * 2;
166169
if (!this.mLabelsPositionsBuffer || this.mLabelsPositionsBuffer.length !== length) {
167170
this.mLabelsPositionsBuffer = Utils.createArrayBuffer(length);
@@ -216,13 +219,17 @@ export class XAxisRenderer extends AxisRenderer {
216219
}
217220
}
218221
}
219-
this.drawLabel(c, label, x, pos, anchor, labelRotationAngleDegrees, paint);
222+
this.drawLabel(c, label, x, pos, anchor, labelRotationAngleDegrees, paint, customRenderFunction);
220223
}
221224
}
222225
}
223226

224-
protected drawLabel(c: Canvas, formattedLabel, x, y, anchor: MPPointF, angleDegrees, paint) {
225-
Utils.drawXAxisValue(c, formattedLabel, x, y, paint, anchor, angleDegrees);
227+
protected drawLabel(c: Canvas, formattedLabel, x, y, anchor: MPPointF, angleDegrees, paint, customRenderFunction?: CustomRendererLabelFunction) {
228+
if (customRenderFunction) {
229+
customRenderFunction(c, this, formattedLabel, x, y, paint, anchor, angleDegrees);
230+
} else {
231+
Utils.drawXAxisValue(c, formattedLabel, x, y, paint, anchor, angleDegrees);
232+
}
226233
}
227234

228235
/**
@@ -232,15 +239,18 @@ export class XAxisRenderer extends AxisRenderer {
232239
* @param length
233240
*/
234241
protected drawMarkTicket(c: Canvas, pos, ticklength) {
235-
if (!this.mXAxis.isDrawMarkTicksEnabled()) return;
242+
const axis = this.mXAxis;
243+
if (!axis.isDrawMarkTicksEnabled()) return;
236244

245+
const customRender = axis.getCustomRenderer();
246+
const customRenderFunction = customRender && customRender.drawMarkTicket;
237247
const length = this.mAxis.mEntryCount * 2;
238248
if (!this.mRenderGridLinesBuffer || this.mRenderGridLinesBuffer.length !== length) {
239249
this.mRenderGridLinesBuffer = Utils.createArrayBuffer(length);
240250
}
241251
const positionsBuffer = this.mRenderGridLinesBuffer;
242252
for (let i = 0; i < length; i += 2) {
243-
positionsBuffer[i] = this.mXAxis.mEntries[i / 2];
253+
positionsBuffer[i] = axis.mEntries[i / 2];
244254
if (i + 1 < length) {
245255
positionsBuffer[i + 1] = 0;
246256
}
@@ -251,7 +261,11 @@ export class XAxisRenderer extends AxisRenderer {
251261
const paint = this.axisLinePaint;
252262
for (let i = 0; i < length; i += 2) {
253263
const x = points[i];
254-
c.drawLine(x, pos, x, pos + ticklength, paint);
264+
if (customRenderFunction) {
265+
customRenderFunction(c, this, x, pos, x, pos + ticklength, paint);
266+
} else {
267+
c.drawLine(x, pos, x, pos + ticklength, paint);
268+
}
255269
}
256270
}
257271

@@ -288,7 +302,6 @@ export class XAxisRenderer extends AxisRenderer {
288302
const customRenderFunction = customRender && customRender.drawGridLine;
289303
const rect = this.mAxis.isIgnoringOffsets() ? this.mViewPortHandler.getChartRect() : this.mViewPortHandler.getContentRect();
290304
for (let i = 0; i < positions.length; i += 2) {
291-
const x = positions[i];
292305
this.drawGridLine(c, rect, positions[i], positions[i + 1], axis.mEntries[i / 2], paint, customRenderFunction);
293306
}
294307

0 commit comments

Comments
 (0)