Skip to content

Commit 5c4e8dd

Browse files
Change and reorder the types definitions (#759)
* Reorder the types definitions * Update types/label.d.ts Co-authored-by: Jacco van den Berg <[email protected]> * applied review Co-authored-by: Jacco van den Berg <[email protected]>
1 parent 8352c68 commit 5c4e8dd

File tree

5 files changed

+49
-51
lines changed

5 files changed

+49
-51
lines changed

docs/guide/migrationV2.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,7 @@ The following diagram is showing the element properties about a `'polygon'` anno
102102
* When [scatter charts](https://www.chartjs.org/docs/latest/charts/scatter.html) are used, the interaction default `mode` in Chart.js is `point`, while, in the previous plugin version, the default was `nearest`.
103103

104104
The `dblclick` event hook was removed from annotations options because, being executed asynchronously, it can not enable the chart re-rendering, automatically after processing the event completely. This is important when the user requires re-draws. It gets slow and messy if every event hook does the draw (or update!).
105+
106+
## Types
107+
108+
`chartjs-plugin-annotation` plugin version 2 removes the compatibility with TypeScript versions less than 4.1 which is the minimum supported one.

test/integration/ts/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
"dependencies": {
88
"chart.js": "^3.1.0",
99
"chartjs-plugin-annotation": "file:../plugin.tgz",
10-
"typescript-3.8": "npm:[email protected]",
11-
"typescript-3.9": "npm:[email protected]",
12-
"typescript-4.0": "npm:[email protected]",
1310
"typescript-4.1": "npm:[email protected]",
1411
"typescript-4.2": "npm:[email protected]",
1512
"typescript-4.3": "npm:[email protected]",

types/label.d.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@ import { Color, FontSpec, BorderRadius } from 'chart.js';
22
import { PartialEventContext } from './events';
33
import { DrawTime, Scriptable, ShadowOptions } from './options';
44

5+
type percentString = `${number}%`;
6+
export type LabelPosition = 'start' | 'center' | 'end' | percentString;
7+
8+
export type LabelTextAlign = 'left' | 'start' | 'center' | 'right' | 'end';
9+
10+
export interface LabelPositionObject {
11+
x?: LabelPosition,
12+
y?: LabelPosition
13+
}
14+
15+
export interface LabelPadding {
16+
top?: number,
17+
left?: number,
18+
right?: number,
19+
bottom?: number,
20+
x?: number,
21+
y?: number
22+
}
23+
524
export interface CoreLabelOptions {
625
drawTime?: Scriptable<DrawTime, PartialEventContext>,
726
font?: FontSpec
@@ -107,22 +126,3 @@ export interface BoxLabelOptions extends CoreLabelOptions {
107126
export interface LabelTypeOptions extends ContainedLabelOptions {
108127
position?: Scriptable<LabelPosition | LabelPositionObject, PartialEventContext>,
109128
}
110-
111-
type percentString = string;
112-
export type LabelPosition = 'start' | 'center' | 'end' | percentString;
113-
114-
export type LabelTextAlign = 'left' | 'start' | 'center' | 'right' | 'end';
115-
116-
interface LabelPositionObject {
117-
x?: LabelPosition,
118-
y?: LabelPosition
119-
}
120-
121-
interface LabelPadding {
122-
top?: number,
123-
left?: number,
124-
right?: number,
125-
bottom?: number,
126-
x?: number,
127-
y?: number
128-
}

types/options.d.ts

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { LabelOptions, BoxLabelOptions, LabelTypeOptions } from './label';
44

55
export type DrawTime = 'afterDraw' | 'afterDatasetsDraw' | 'beforeDraw' | 'beforeDatasetsDraw';
66

7+
export type CalloutPosition = 'left' | 'top' | 'bottom' | 'right' | 'auto';
8+
79
export interface AnnotationTypeRegistry {
810
box: BoxAnnotationOptions
911
ellipse: EllipseAnnotationOptions
@@ -14,10 +16,12 @@ export interface AnnotationTypeRegistry {
1416
}
1517

1618
export type AnnotationType = keyof AnnotationTypeRegistry;
17-
1819
export type AnnotationOptions<TYPE extends AnnotationType = AnnotationType> =
1920
{ [key in TYPE]: { type: key } & AnnotationTypeRegistry[key] }[TYPE]
2021

22+
export type Scriptable<T, TContext> = T | ((ctx: TContext, options: AnnotationOptions) => T);
23+
export type ScaleValue = number | string;
24+
2125
interface ShadowOptions {
2226
backgroundShadowColor?: Scriptable<Color, PartialEventContext>,
2327
borderShadowColor?: Scriptable<Color, PartialEventContext>,
@@ -26,33 +30,25 @@ interface ShadowOptions {
2630
shadowOffsetY?: Scriptable<number, PartialEventContext>
2731
}
2832

29-
export interface CoreAnnotationOptions extends AnnotationEvents, ShadowOptions {
30-
id?: string,
31-
display?: Scriptable<boolean, PartialEventContext>,
33+
export interface CoreAnnotationOptions extends AnnotationEvents, ShadowOptions{
3234
adjustScaleRange?: Scriptable<boolean, PartialEventContext>,
3335
borderColor?: Scriptable<Color, PartialEventContext>,
34-
borderWidth?: Scriptable<number, PartialEventContext>,
3536
borderDash?: Scriptable<number[], PartialEventContext>,
3637
borderDashOffset?: Scriptable<number, PartialEventContext>,
38+
borderWidth?: Scriptable<number, PartialEventContext>,
39+
display?: Scriptable<boolean, PartialEventContext>,
3740
drawTime?: Scriptable<DrawTime, PartialEventContext>,
38-
endValue?: Scriptable<number|string, PartialEventContext>,
39-
scaleID?: Scriptable<string, PartialEventContext>,
40-
value?: Scriptable<number|string, PartialEventContext>,
41-
xScaleID?: Scriptable<string, PartialEventContext>,
42-
yScaleID?: Scriptable<string, PartialEventContext>,
43-
z?: Scriptable<number, PartialEventContext>
44-
}
45-
46-
export type Scriptable<T, TContext> = T | ((ctx: TContext, options: AnnotationOptions) => T);
47-
export type ScaleValue = number | string;
48-
interface AnnotationCoordinates {
41+
id?: string,
4942
xMax?: Scriptable<ScaleValue, PartialEventContext>,
5043
xMin?: Scriptable<ScaleValue, PartialEventContext>,
44+
xScaleID?: Scriptable<string, PartialEventContext>,
5145
yMax?: Scriptable<ScaleValue, PartialEventContext>,
5246
yMin?: Scriptable<ScaleValue, PartialEventContext>,
47+
yScaleID?: Scriptable<string, PartialEventContext>,
48+
z?: Scriptable<number, PartialEventContext>
5349
}
5450

55-
interface AnnotationPointCoordinates extends AnnotationCoordinates {
51+
interface AnnotationPointCoordinates {
5652
xValue?: Scriptable<ScaleValue, PartialEventContext>,
5753
yValue?: Scriptable<ScaleValue, PartialEventContext>,
5854
}
@@ -74,12 +70,15 @@ export interface ArrowHeadsOptions extends ArrowHeadOptions{
7470
start?: ArrowHeadOptions,
7571
}
7672

77-
export interface LineAnnotationOptions extends CoreAnnotationOptions, AnnotationCoordinates {
73+
export interface LineAnnotationOptions extends CoreAnnotationOptions {
7874
arrowHeads?: ArrowHeadsOptions,
79-
label?: LabelOptions
75+
endValue?: Scriptable<number|string, PartialEventContext>,
76+
label?: LabelOptions,
77+
scaleID?: Scriptable<string, PartialEventContext>,
78+
value?: Scriptable<number|string, PartialEventContext>
8079
}
8180

82-
export interface BoxAnnotationOptions extends CoreAnnotationOptions, AnnotationCoordinates {
81+
export interface BoxAnnotationOptions extends CoreAnnotationOptions {
8382
backgroundColor?: Scriptable<Color, PartialEventContext>,
8483
/**
8584
* Border line cap style. See MDN.
@@ -106,7 +105,7 @@ export interface BoxAnnotationOptions extends CoreAnnotationOptions, AnnotationC
106105
rotation?: Scriptable<number, PartialEventContext>
107106
}
108107

109-
export interface EllipseAnnotationOptions extends CoreAnnotationOptions, AnnotationCoordinates {
108+
export interface EllipseAnnotationOptions extends CoreAnnotationOptions {
110109
backgroundColor?: Scriptable<Color, PartialEventContext>,
111110
rotation?: Scriptable<number, PartialEventContext>
112111
}
@@ -120,8 +119,6 @@ export interface PointAnnotationOptions extends CoreAnnotationOptions, Annotatio
120119
yAdjust?: Scriptable<number, PartialEventContext>,
121120
}
122121

123-
export type CalloutPosition = 'left' | 'top' | 'bottom' | 'right' | 'auto';
124-
125122
export interface CalloutOptions {
126123
borderCapStyle?: Scriptable<CanvasLineCap, PartialEventContext>,
127124
borderColor?: Scriptable<Color, PartialEventContext>,
@@ -153,14 +150,10 @@ interface PolygonAnnotationOptions extends CoreAnnotationOptions, AnnotationPoin
153150
yAdjust?: Scriptable<number, PartialEventContext>,
154151
}
155152

156-
export interface AnnotationPluginCommonOptions {
157-
drawTime?: Scriptable<DrawTime, PartialEventContext>
158-
}
159-
160153
export interface AnnotationPluginOptions extends AnnotationEvents {
161154
animations?: Record<string, unknown>,
162155
annotations: AnnotationOptions[] | Record<string, AnnotationOptions>,
163156
clip?: boolean,
164-
common?: AnnotationPluginCommonOptions,
157+
common?: BoxAnnotationOptions | EllipseAnnotationOptions | LabelAnnotationOptions | LineAnnotationOptions | PointAnnotationOptions | PolygonAnnotationOptions,
165158
interaction?: CoreInteractionOptions
166159
}

types/tests/exports.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ const chart = new Chart('id', {
2222
intersect: true
2323
},
2424
common: {
25-
drawTime: 'afterDraw'
25+
drawTime: 'afterDraw',
26+
borderColor: 'red',
27+
label: {
28+
display: true
29+
}
2630
},
2731
annotations: [{
2832
type: 'line',

0 commit comments

Comments
 (0)