deno.land / x / billboardjs@3.6.0 / Plugin / stanford / Elements.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/** * Copyright (c) 2017 ~ present NAVER Corp. * billboard.js project is licensed under the MIT license */// @ts-nocheckimport CLASS from "./classes";import {getCentroid, isString, parseDate} from "./util";
/** * Stanford diagram plugin element class * @class ColorScale * @param {Stanford} owner Stanford instance * @private */export default class Elements { private owner;
constructor(owner) { this.owner = owner;
// MEMO: Avoid blocking eventRect const elements = owner.$$.$el.main.select(".bb-chart") .append("g") .attr("class", CLASS.stanfordElements);
elements.append("g").attr("class", CLASS.stanfordLines); elements.append("g").attr("class", CLASS.stanfordRegions); }
updateStanfordLines(duration: number): void { const {$$} = this.owner; const {config, $el: {main}} = $$; const isRotated = config.axis_rotated; const xvCustom = this.xvCustom.bind($$); const yvCustom = this.yvCustom.bind($$);
// Stanford-Lines const stanfordLine = main.select(`.${CLASS.stanfordLines}`) .style("shape-rendering", "geometricprecision") .selectAll(`.${CLASS.stanfordLine}`) .data(this.owner.config.lines);
// exit stanfordLine.exit().transition() .duration(duration) .style("opacity", "0") .remove();
// enter const stanfordLineEnter = stanfordLine.enter().append("g");
stanfordLineEnter.append("line") .style("opacity", "0");
stanfordLineEnter .merge(stanfordLine) .attr("class", d => CLASS.stanfordLine + (d.class ? ` ${d.class}` : "")) .select("line") .transition() .duration(duration) .attr("x1", d => (isRotated ? yvCustom(d, "y1") : xvCustom(d, "x1"))) .attr("x2", d => (isRotated ? yvCustom(d, "y2") : xvCustom(d, "x2"))) .attr("y1", d => (isRotated ? xvCustom(d, "x1") : yvCustom(d, "y1"))) .attr("y2", d => (isRotated ? xvCustom(d, "x2") : yvCustom(d, "y2"))) .transition() .style("opacity", null); }
updateStanfordRegions(duration: number): void { const {$$} = this.owner; const {config, $el: {main}} = $$; const isRotated = config.axis_rotated; const xvCustom = this.xvCustom.bind($$); const yvCustom = this.yvCustom.bind($$); const countPointsInRegion = this.owner.countEpochsInRegion.bind($$);
// Stanford-Regions let stanfordRegion = main.select(`.${CLASS.stanfordRegions}`) .selectAll(`.${CLASS.stanfordRegion}`) .data(this.owner.config.regions);
// exit stanfordRegion.exit().transition() .duration(duration) .style("opacity", "0") .remove();
// enter const stanfordRegionEnter = stanfordRegion.enter().append("g");
stanfordRegionEnter.append("polygon") .style("opacity", "0");
stanfordRegionEnter.append("text") .attr("transform", isRotated ? "rotate(-90)" : "") .style("opacity", "0");
stanfordRegion = stanfordRegionEnter.merge(stanfordRegion);
// update stanfordRegion .attr("class", d => CLASS.stanfordRegion + (d.class ? ` ${d.class}` : "")) .select("polygon") .transition() .duration(duration) .attr("points", d => d.points.map(value => [ isRotated ? yvCustom(value, "y") : xvCustom(value, "x"), isRotated ? xvCustom(value, "x") : yvCustom(value, "y") ].join(",")).join(" ")) .transition() .style("opacity", d => String(d.opacity ? d.opacity : 0.2));
stanfordRegion.select("text") .transition() .duration(duration) .attr("x", d => (isRotated ? yvCustom(getCentroid(d.points), "y") : xvCustom(getCentroid(d.points), "x"))) .attr("y", d => (isRotated ? xvCustom(getCentroid(d.points), "x") : yvCustom(getCentroid(d.points), "y"))) .text(d => { if (d.text) { const {value, percentage} = countPointsInRegion(d.points);
return d.text(value, percentage); }
return ""; }) .attr("text-anchor", "middle") .attr("dominant-baseline", "middle") .transition() .style("opacity", null); }
updateStanfordElements(duration = 0): void { this.updateStanfordLines(duration); this.updateStanfordRegions(duration); }
xvCustom(d, xyValue): number { const $$ = this; const {axis, config} = $$; let value = xyValue ? d[xyValue] : $$.getBaseValue(d);
if (axis.isTimeSeries()) { value = parseDate.call($$, value); } else if (axis.isCategorized() && isString(value)) { value = config.axis_x_categories.indexOf(d.value); }
return Math.ceil($$.scale.x(value)); }
yvCustom(d, xyValue): number { const $$ = this; const yScale = d.axis && d.axis === "y2" ? $$.scale.y2 : $$.scale.y; const value = xyValue ? d[xyValue] : $$.getBaseValue(d);
return Math.ceil(yScale(value)); }}
billboardjs

Version Info

Tagged at
a year ago