deno.land / x / billboardjs@3.6.0 / Plugin / sparkline / index.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/** * Copyright (c) 2021 ~ present NAVER Corp. * billboard.js project is licensed under the MIT license */import {$COMMON} from "../../config/classes";import Plugin from "../Plugin";import Options from "./Options";import type {IData} from "../../ChartInternal/data/IData";import {loadConfig} from "../../config/config";
/** * Sparkline plugin.<br> * Generates sparkline charts * - **NOTE:** * - Plugins aren't built-in. Need to be loaded or imported to be used. * - Non required modules from billboard.js core, need to be installed separately. * * - **Bear in mind:** * - Use this plugin to visualize multiple tiny chart only and chart APIs won't work properly. * - Sparkline chart size will be based on the main chart element size. To control spakrline charts, is highly recommended to set `size` option. * - Bubble, scatter and Arc(pie, donut, ratdar) types aren't supported. * - Some options will be stricted to be: * - `resize.auto = false` * - `axis.x.show = false` * - `axis.y.show = false` * - `axis.y.padding = 10` * - `legend.show = false` * * @class plugin-sparkline * @param {object} options sparkline plugin options * @augments Plugin * @returns {Sparkline} * @example * // Plugin must be loaded before the use. * <script src="$YOUR_PATH/plugin/billboardjs-plugin-sparkline.js"></script> * * var chart = bb.generate({ * ... * plugins: [ * new bb.plugin.sparkline({ * selector: ".sparkline" * }), * ] * }); * @example * import {bb} from "billboard.js"; * import Sparkline from "billboard.js/dist/billboardjs-plugin-sparkline"; * * bb.generate({ * ... * plugins: [ * new Sparkline({ ... }) * ] * }) */export default class Sparkline extends Plugin { static version = `0.0.1`; private config; private element;
constructor(options) { super(options); this.config = new Options();
return this; }
$beforeInit(): void { loadConfig.call(this, this.options);
this.validate(); this.element = [].slice.call(document.querySelectorAll(this.config.selector));
// override internal methods this.overrideInternals();
// override options this.overrideOptions();
// bind event handlers's context this.overHandler = this.overHandler.bind(this); this.moveHandler = this.moveHandler.bind(this); this.outHandler = this.outHandler.bind(this); }
validate(): void { const {$$, config} = this; let msg = "";
if (!config.selector || !document.querySelector(config.selector)) { msg = "No holder elements found from given selector option."; }
if ($$.hasType("bubble") || $$.hasType("scatter") || $$.hasArcType($$.data.targets)) { msg = "Contains non supported chart types."; }
if (msg) { throw new Error(`[Sparkline plugin] ${msg}`); } }
overrideInternals(): void { const {$$} = this; const {getBarW, getIndices} = $$;
// override internal methods to positioning bars $$.getIndices = function(indices, d, caller) { return caller === "getShapeX" ? {} : getIndices.call(this, indices, d); };
$$.getBarW = function(type, axis) { return getBarW.call(this, type, axis, 1); }; }
overrideOptions(): void { const {config} = this.$$;
config.legend_show = false; config.resize_auto = false; config.axis_x_show = false;
// set default axes padding if (config.padding !== false) { const hasOption = o => Object.keys(o || {}).length > 0;
if (hasOption(config.axis_x_padding)) { config.axis_x_padding = { left: 15, right: 15, unit: "px" }; }
if (hasOption(config.axis_y_padding)) { config.axis_y_padding = 5; } }
config.axis_y_show = false;
if (!config.tooltip_position) { config.tooltip_position = function(data, width, height) { const {internal: {state: {event}}} = this; let top = event.pageY - (height * 1.35); let left = event.pageX - (width / 2);
if (top < 0) { top = 0; }
if (left < 0) { left = 0; }
return {top, left}; }; } }
$init(): void { const {$$: {$el}} = this;
// make disable-ish main chart element $el.chart .style("width", "0") .style("height", "0") .style("pointer-events", "none");
$el.tooltip?.node() && document.body.appendChild($el.tooltip.node()); }
$afterInit(): void { const {$$} = this;
$$.$el.svg.attr("style", null) .style("width", "0") .style("height", "0");
this.bindEvents(true); }
/** * Bind tooltip event handlers for each sparkline elements. * @param {boolean} bind or unbind * @private */ bindEvents(bind = true): void { const {$$: {config}} = this;
if (config.interaction_enabled && config.tooltip_show) { const method = `${bind ? "add" : "remove"}EventListener`;
this.element .forEach(el => { const svg = el.querySelector("svg");
svg[method]("mouseover", this.overHandler); svg[method]("mousemove", this.moveHandler); svg[method]("mouseout", this.outHandler); }); } }
overHandler(e): void { const {$$} = this; const {state: {eventReceiver}} = $$;
eventReceiver.rect = e.target.getBoundingClientRect(); }
moveHandler(e): void { const {$$} = this; const index = $$.getDataIndexFromEvent(e); const data = $$.api.data(e.target.__id)?.[0] as IData; const d = data?.values?.[index];
if (d && !d.name) { d.name = d.id; }
$$.state.event = e; $$.setExpand(index, data.id, true); $$.showTooltip([d], e.target); }
outHandler(e): void { const {$$} = this;
$$.state.event = e; $$.unexpandCircles(); $$.hideTooltip(); }
$redraw(): void { const {$$} = this; const {$el} = $$;
let el = this.element; const data = $$.api.data(); const svgWrapper = $el.chart.html().match(/<svg[^>]*>/)?.[0];
// append sparkline holder if is less than the data length if (el.length < data.length) { const chart = $el.chart.node();
for (let i = data.length - el.length; i > 0; i--) { chart.parentNode.insertBefore(el[0].cloneNode(), chart.nextSibling); }
this.element = document.querySelectorAll(this.config.selector); el = this.element; }
data.map(v => v.id) .forEach((id, i) => { const selector = `.${$COMMON.target}-${id}`; const shape = $el.main.selectAll(selector); let svg = el[i].querySelector("svg");
if (!svg) { el[i].innerHTML = `${svgWrapper}</svg>`; svg = el[i].querySelector("svg"); svg.__id = id; }
if (!svg.querySelector(selector)) { shape.style("opacity", null); }
shape .style("fill", "none") .style("opacity", null);
svg.innerHTML = ""; svg.appendChild(shape.node()); }); }
$willDestroy(): void { this.bindEvents(false); this.element .forEach(el => { el.innerHTML = ""; }); }}
billboardjs

Version Info

Tagged at
a year ago