Skip to content

Commit 2277cd3

Browse files
committed
feat: customRenderer drawZeroLine
1 parent 8d18408 commit 2277cd3

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/ui-chart/renderer/AxisRenderer.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Align, Canvas, Paint, RectF } from '@nativescript-community/ui-canvas';
1+
import { Align, Canvas, Paint, Path, RectF } from '@nativescript-community/ui-canvas';
22
import { profile } from '@nativescript/core';
33
import { AxisBase } from '../components/AxisBase';
44
import { LimitLine } from '../components/LimitLine';
@@ -9,13 +9,15 @@ import { BaseCustomRenderer } from './DataRenderer';
99
import { Renderer } from './Renderer';
1010
import { MPPointF } from '../utils/MPPointF';
1111

12+
export type CustomRendererZeroLineFunction = (c: Canvas, axis: AxisBase, zeroPos: MPPointF, path: Path, paint: Paint) => void;
1213
export type CustomRendererGridLineFunction = (c: Canvas, axis: AxisBase, rect: RectF, x, y, axisValue, paint: Paint) => void;
1314
export type CustomRendererLimitLineFunction = (c: Canvas, axis: AxisBase, limitLine: LimitLine, rect: RectF, x: number, paint: Paint) => void;
1415
export type CustomRendererLabelFunction = (c: Canvas, axis: AxisBase, text: string, x: number, y: number, paint: Paint, anchor?: MPPointF, angleDegrees?: number) => void;
1516
export type CustomRendererTickFunction = (c: Canvas, renderer: AxisRenderer, startX: number, startY: number, stopX: number, stopY: number, paint: Paint) => void;
1617
export interface CustomRenderer extends BaseCustomRenderer {
1718
drawLabel?: CustomRendererLabelFunction;
1819
drawGridLine?: CustomRendererGridLineFunction;
20+
drawZeroLine?: CustomRendererZeroLineFunction;
1921
drawLimitLine?: CustomRendererLimitLineFunction;
2022
drawMarkTick?: CustomRendererTickFunction;
2123
}
@@ -140,7 +142,6 @@ export abstract class AxisRenderer extends Renderer {
140142
* @param min - the minimum value in the data object for this axis
141143
* @param max - the maximum value in the data object for this axis
142144
*/
143-
@profile
144145
public computeAxis(min, max, inverted) {
145146
const axis = this.mAxis;
146147
if (!axis.enabled) {

src/ui-chart/renderer/YAxisRenderer.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ export class YAxisRenderer extends AxisRenderer {
226226
*/
227227
protected drawZeroLine(c: Canvas) {
228228
const axis = this.mYAxis;
229+
230+
const customRender = axis.customRenderer;
231+
const customRendererFunc = customRender && customRender.drawZeroLine;
229232
const clipRestoreCount = c.save();
230233
const rect = this.mAxis.ignoreOffsets ? this.mViewPortHandler.chartRect : this.mViewPortHandler.contentRect;
231234
const zeroLineClippingRectl = Utils.getTempRectF();
@@ -246,7 +249,11 @@ export class YAxisRenderer extends AxisRenderer {
246249
zeroLinePath.lineTo(rect.right, pos.y);
247250

248251
// draw a path because lines don't support dashing on lower android versions
249-
c.drawPath(zeroLinePath, paint);
252+
if (customRendererFunc) {
253+
customRendererFunc(c, axis, pos, zeroLinePath, paint);
254+
} else {
255+
c.drawPath(zeroLinePath, paint);
256+
}
250257

251258
c.restoreToCount(clipRestoreCount);
252259
}

src/ui-chart/renderer/YAxisRendererHorizontalBarChart.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ export class YAxisRendererHorizontalBarChart extends YAxisRenderer {
142142

143143
protected drawZeroLine(c: Canvas) {
144144
const axis = this.mYAxis;
145+
const customRender = axis.customRenderer;
146+
const customRendererFunc = customRender && customRender.drawZeroLine;
145147
const clipRestoreCount = c.save();
146148
const rect = this.mAxis.ignoreOffsets ? this.mViewPortHandler.chartRect : this.mViewPortHandler.contentRect;
147149
const zeroLineClippingRect = Utils.getTempRectF();
@@ -163,7 +165,12 @@ export class YAxisRendererHorizontalBarChart extends YAxisRenderer {
163165
zeroLinePath.lineTo(pos.x - 1, rect.bottom);
164166

165167
// draw a path because lines don't support dashing on lower android versions
166-
c.drawPath(zeroLinePath, paint);
168+
if (customRendererFunc) {
169+
customRendererFunc(c, axis, pos, zeroLinePath, paint);
170+
} else {
171+
// draw a path because lines don't support dashing on lower android versions
172+
c.drawPath(zeroLinePath, paint);
173+
}
167174

168175
c.restoreToCount(clipRestoreCount);
169176
}

0 commit comments

Comments
 (0)