/
WordElement.ts
133 lines (120 loc) · 3.34 KB
/
WordElement.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { Element, IFontSpec, IVisualElement, ScriptableAndArrayOptions, toFont } from '@sgratzl/chartjs-esm-facade';
export interface IWordElementOptions extends IFontSpec {
/**
* rotation of the word
* @default undefined then it will be randomly derived given the other constraints
*/
rotate: number;
/**
* number of rotation steps between min and max rotation
* @default 2
*/
rotationSteps: number;
/**
* angle in degree for the min rotation
* @default -90
*/
minRotation: number;
/**
* angle in degree for the max rotation
* @default 0
*/
maxRotation: number;
/**
* padding around each word while doing the layout
* @default 1
*/
padding: number;
}
export interface IWordElementHoverOptions {
hoverColor: string;
hoverSize: number;
hoverStyle: string;
hoverWeight: string;
}
export interface IWordElementProps {
x: number;
y: number;
width: number;
height: number;
scale: number;
text: string;
}
export class WordElement extends Element<IWordElementProps, IWordElementOptions> implements IVisualElement {
static readonly id = 'word';
static readonly defaults = /* #__PURE__ */ {
// rotate: 0,
minRotation: -90,
maxRotation: 0,
rotationSteps: 2,
padding: 1,
weight: 'normal',
size: (ctx) => {
const v = ctx.dataPoint.y;
return v;
},
hoverColor: '#ababab',
} as Partial<ScriptableAndArrayOptions<IWordElementOptions>>;
static readonly defaultRoutes = {
color: 'font.color',
family: 'font.family',
};
static computeRotation(o: IWordElementOptions, rnd: () => number) {
if (o.rotationSteps <= 1) {
return 0;
}
if (o.minRotation === o.maxRotation) {
return o.minRotation;
}
const base = Math.min(o.rotationSteps, Math.floor(rnd() * o.rotationSteps)) / (o.rotationSteps - 1);
const range = o.maxRotation - o.minRotation;
return o.minRotation + base * range;
}
inRange(mouseX: number, mouseY: number) {
const p = this.getProps(['x', 'y', 'width', 'height', 'scale']);
if (p.scale <= 0) {
return false;
}
const x = Number.isNaN(mouseX) ? p.x : mouseX;
const y = Number.isNaN(mouseY) ? p.y : mouseY;
return x >= p.x - p.width / 2 && x <= p.x + p.width / 2 && y >= p.y - p.height / 2 && y <= p.y + p.height / 2;
}
inXRange(mouseX: number) {
return this.inRange(mouseX, Number.NaN);
}
inYRange(mouseY: number) {
return this.inRange(Number.NaN, mouseY);
}
getCenterPoint() {
return this.getProps(['x', 'y']);
}
tooltipPosition() {
return this.getCenterPoint();
}
draw(ctx: CanvasRenderingContext2D) {
const options = this.options;
const props = this.getProps(['x', 'y', 'width', 'height', 'text', 'scale']);
if (props.scale <= 0) {
return;
}
ctx.save();
const f = toFont(
Object.assign({}, options, {
size: options.size * props.scale,
})
);
ctx.font = f.string;
ctx.fillStyle = f.color;
ctx.textAlign = 'center';
// ctx.textBaseline = 'top';
ctx.translate(props.x, props.y);
// ctx.strokeRect(-props.width / 2, -props.height / 2, props.width, props.height);
ctx.rotate((options.rotate / 180) * Math.PI);
if (f.strokeStyle) {
ctx.strokeStyle = f.strokeStyle;
ctx.strokeText(props.text, 0, 0);
}
ctx.fillText(props.text, 0, 0);
ctx.restore();
}
}