]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Convert the PointElement to TS (#10730)
authorEvert Timberg <evert.timberg@gmail.com>
Fri, 7 Oct 2022 12:28:03 +0000 (08:28 -0400)
committerGitHub <noreply@github.com>
Fri, 7 Oct 2022 12:28:03 +0000 (08:28 -0400)
Co-authored-by: Chart.js <>
src/core/core.element.ts
src/elements/element.arc.js
src/elements/element.point.ts [moved from src/elements/element.point.js with 67% similarity]
types/index.d.ts

index 93006b58ed4fa0ee5c2d3ad3db26963fa0943b18..9bfca13b3c9e3335b631b9e944e47e0544fa93ad 100644 (file)
@@ -28,8 +28,8 @@ export default class Element<T = AnyObject, O = AnyObject> {
    * @param props - properties to get
    * @param [final] - get the final value (animation target)
    */
-  getProps<P extends string>(props: P[], final?: boolean): Partial<Record<P, unknown>>;
   getProps<P extends (keyof T)[]>(props: P, final?: boolean): Pick<T, P[number]>;
+  getProps<P extends string>(props: P[], final?: boolean): Partial<Record<P, unknown>>;
   getProps(props: string[], final?: boolean): Partial<Record<string, unknown>> {
     const anims = this.$animations;
     if (!final || !anims) {
index 7996330872a58f541b2c112896080062cb823d81..6ed0dbfdeab7654be3c6ec179f049a8aae32071b 100644 (file)
@@ -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([
similarity index 67%
rename from src/elements/element.point.js
rename to src/elements/element.point.ts
index b651d65b19884ebfda9c1a8ecf430077db5d3781..780a412991ff638d0e128ca45ba1a25730def73c 100644 (file)
@@ -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<PointProps, PointOptions & PointHoverOptions> {
 
   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<PointOptions & PointHoverOptions>) {
     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;
   }
 }
index 35420b1b74f3b13feb1162fd29d9244c170b0e07..4e56d2f1e5bd2104754e0a7887336d0cd80d0ee8 100644 (file)
@@ -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<T extends PointProps = PointProps, O extends PointOptions = PointOptions>
-  extends Element<T, O>,
-  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