From: Evert Timberg Date: Fri, 7 Oct 2022 12:28:03 +0000 (-0400) Subject: Convert the PointElement to TS (#10730) X-Git-Tag: v4.0.0~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=06bbadb00f0a79ff1d4f44a3b76c702d090b485d;p=thirdparty%2FChart.js.git Convert the PointElement to TS (#10730) Co-authored-by: Chart.js <> --- diff --git a/src/core/core.element.ts b/src/core/core.element.ts index 93006b58e..9bfca13b3 100644 --- a/src/core/core.element.ts +++ b/src/core/core.element.ts @@ -28,8 +28,8 @@ export default class Element { * @param props - properties to get * @param [final] - get the final value (animation target) */ - getProps

(props: P[], final?: boolean): Partial>; getProps

(props: P, final?: boolean): Pick; + getProps

(props: P[], final?: boolean): Partial>; getProps(props: string[], final?: boolean): Partial> { const anims = this.$animations; if (!final || !anims) { diff --git a/src/elements/element.arc.js b/src/elements/element.arc.js index 799633087..6ed0dbfde 100644 --- a/src/elements/element.arc.js +++ b/src/elements/element.arc.js @@ -311,6 +311,7 @@ export default class ArcElement extends Element { * @param {boolean} [useFinalPosition] */ inRange(chartX, chartY, useFinalPosition) { + // @ts-ignore This will be fixed when the arc element is converted to TS const point = /** @type {Point} */ (this.getProps(['x', 'y'], useFinalPosition)); const {angle, distance} = getAngleFromPoint(point, {x: chartX, y: chartY}); const {startAngle, endAngle, innerRadius, outerRadius, circumference} = /** @type {ArcProps} */ (this.getProps([ diff --git a/src/elements/element.point.js b/src/elements/element.point.ts similarity index 67% rename from src/elements/element.point.js rename to src/elements/element.point.ts index b651d65b1..780a41299 100644 --- a/src/elements/element.point.js +++ b/src/elements/element.point.ts @@ -1,17 +1,30 @@ import Element from '../core/core.element'; import {drawPoint, _isPointInArea} from '../helpers/helpers.canvas'; - -function inRange(el, pos, axis, useFinalPosition) { +import { + type CartesianParsedData, + type ChartArea, + type Point, + type PointHoverOptions, + type PointOptions, +} from '../../types'; + +function inRange(el: PointElement, pos: number, axis: 'x' | 'y', useFinalPosition?: boolean) { const options = el.options; const {[axis]: value} = el.getProps([axis], useFinalPosition); return (Math.abs(pos - value) < options.radius + options.hitRadius); } -export default class PointElement extends Element { +export type PointProps = Point + +export default class PointElement extends Element { static id = 'point'; + parsed: CartesianParsedData; + skip?: boolean; + stop?: boolean; + /** * @type {any} */ @@ -46,26 +59,26 @@ export default class PointElement extends Element { } } - inRange(mouseX, mouseY, useFinalPosition) { + inRange(mouseX: number, mouseY: number, useFinalPosition?: boolean) { const options = this.options; - const {x, y} = /** @type {{ x: number, y: number }} */ (this.getProps(['x', 'y'], useFinalPosition)); + const {x, y} = this.getProps(['x', 'y'], useFinalPosition); return ((Math.pow(mouseX - x, 2) + Math.pow(mouseY - y, 2)) < Math.pow(options.hitRadius + options.radius, 2)); } - inXRange(mouseX, useFinalPosition) { + inXRange(mouseX: number, useFinalPosition?: boolean) { return inRange(this, mouseX, 'x', useFinalPosition); } - inYRange(mouseY, useFinalPosition) { + inYRange(mouseY: number, useFinalPosition?: boolean) { return inRange(this, mouseY, 'y', useFinalPosition); } - getCenterPoint(useFinalPosition) { + getCenterPoint(useFinalPosition?: boolean) { const {x, y} = this.getProps(['x', 'y'], useFinalPosition); return {x, y}; } - size(options) { + size(options?: Partial) { options = options || this.options || {}; let radius = options.radius || 0; radius = Math.max(radius, radius && options.hoverRadius || 0); @@ -73,7 +86,7 @@ export default class PointElement extends Element { return (radius + borderWidth) * 2; } - draw(ctx, area) { + draw(ctx: CanvasRenderingContext2D, area: ChartArea) { const options = this.options; if (this.skip || options.radius < 0.1 || !_isPointInArea(this, area, this.size(options) / 2)) { @@ -88,6 +101,7 @@ export default class PointElement extends Element { getRange() { const options = this.options || {}; + // @ts-expect-error Fallbacks should never be hit in practice return options.radius + options.hitRadius; } } diff --git a/types/index.d.ts b/types/index.d.ts index 35420b1b7..4e56d2f1e 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,6 +1,7 @@ import { DeepPartial, DistributiveArray, UnionToIntersection } from './utils'; import { TimeUnit } from '../src/core/core.adapters'; +import PointElement from '../src/elements/element.point'; import { EasingFunction } from '../src/helpers/helpers.easing'; import { AnimationEvent } from './animation'; import { AnyObject, EmptyObject } from './basic'; @@ -10,6 +11,7 @@ import { ChartArea, Padding, Point } from './geometric'; import { LayoutItem, LayoutPosition } from './layout'; export { EasingFunction } from '../src/helpers/helpers.easing'; +export { default as PointElement, PointProps } from '../src/elements/element.point'; export { Animation, Animations, Animator, AnimationEvent } from './animation'; export { Color } from './color'; export { ChartArea, Point } from './geometric'; @@ -1821,8 +1823,6 @@ export declare const LineElement: ChartComponent & { new (cfg: AnyObject): LineElement; }; -export interface PointProps extends Point {} - export type PointStyle = | 'circle' | 'cross' @@ -1923,18 +1923,6 @@ export interface PointPrefixedHoverOptions { pointHoverRadius: number; } -export interface PointElement - extends Element, - VisualElement { - readonly skip: boolean; - readonly parsed: CartesianParsedData; -} - -export declare const PointElement: ChartComponent & { - prototype: PointElement; - new (cfg: AnyObject): PointElement; -}; - export interface BarProps extends Point { base: number; horizontal: boolean; @@ -3477,7 +3465,7 @@ export interface ScaleTypeRegistry extends CartesianScaleTypeRegistry, RadialSca export type ScaleType = keyof ScaleTypeRegistry; -interface CartesianParsedData extends Point { +export interface CartesianParsedData extends Point { // Only specified when stacked bars are enabled _stacks?: { // Key is the stack ID which is generally the axis ID