@granite-elements/granite-timeline 2.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,739 +1,285 @@
1
- import { PolymerElement } from '@polymer/polymer/polymer-element.js';
2
- import '@granite-elements/granite-js-dependencies-grabber/granite-js-dependencies-grabber.js';
3
- import { html } from '@polymer/polymer/lib/utils/html-tag.js';
1
+ import { LitElement, html, css } from 'lit';
2
+ import { renderTimeline } from './src/timeline-renderer.js';
3
+
4
+ /**
5
+ * Converter for the `beginning`/`ending` attributes: accepts a ms-epoch
6
+ * number, an ISO date string, or (as a property) a Date.
7
+ */
8
+ const dateConverter = {
9
+ fromAttribute(value) {
10
+ if (value === null || value === '') {
11
+ return undefined;
12
+ }
13
+ const asNumber = Number(value);
14
+ return new Date(Number.isNaN(asNumber) ? value : asNumber);
15
+ },
16
+ toAttribute(value) {
17
+ return value instanceof Date ? value.toISOString() : value;
18
+ },
19
+ };
20
+
21
+ /** Normalizes a Date | ms-number | string property value to a Date. */
22
+ const toDate = (value) => {
23
+ if (value === undefined || value === null) {
24
+ return undefined;
25
+ }
26
+ return value instanceof Date ? value : new Date(value);
27
+ };
4
28
 
5
- /* globals d3 */
6
29
  /**
7
30
  * `granite-timeline`
8
- * A timeline rendering element using d3 and d3-timelines plugin
9
31
  *
10
- * @customElement
11
- * @polymer
32
+ * A timeline rendering web component using Lit and d3.
33
+ *
34
+ * ```html
35
+ * <granite-timeline
36
+ * data='[{"times":[{"starting_time":1355752800000,"ending_time":1355759900000}]}]'
37
+ * show-time-axis></granite-timeline>
38
+ * ```
39
+ *
40
+ * @element granite-timeline
41
+ *
42
+ * @fires click - Fired on click on a timeline bar. detail: {d, index, datum, mouse, evt}
43
+ * @fires mouseover - Fired on mouseover of a timeline bar. detail: {d, index, datum, mouse, evt}
44
+ * @fires mouseout - Fired on mouseout of a timeline bar. detail: {d, index, datum, mouse, evt}
45
+ * @fires hover - Fired on mouse move over a timeline bar. detail: {d, index, datum, mouse, evt}
46
+ * @fires zoom - Fired when the visible domain changes by zooming/panning. detail: {start, end, transform}
47
+ *
48
+ * @cssprop --granite-timeline-label-color - Color of the series and bar labels
49
+ * @cssprop --granite-timeline-axis-color - Color of the time axis line and ticks
50
+ *
12
51
  * @demo demo/index.html
13
52
  */
14
- class GraniteTimeline extends PolymerElement {
15
- static get template() {
16
- return html`
17
- <style>
18
- :host {
19
- display: block;
20
- width: 100%;
21
- height: 100%;
22
- }
23
- #timeline {
24
- width: 100%;
25
- height: 100%;
26
- }
27
- </style>
28
-
29
- <granite-js-dependencies-grabber
30
- dependencies="[[_dependencies]]"
31
- on-dependency-is-ready="_onDependencyIsReady"
32
- debug="[[debug]]"></granite-js-dependencies-grabber>
33
- <div id="timeline"></div>
34
-
35
- `;
53
+ export class GraniteTimeline extends LitElement {
54
+ static properties = {
55
+ /**
56
+ * The timeline data:
57
+ * `[{label?, times: [{starting_time, ending_time, label?, color?}]}]`
58
+ */
59
+ data: { type: Array },
60
+ /** Width of the timeline in pixels. Defaults to the element width. */
61
+ width: { type: Number },
62
+ /** Height of the timeline in pixels. Computed from the rows if unset. */
63
+ height: { type: Number },
64
+ /** Height of a data series row in pixels. Default: 20 */
65
+ itemHeight: { type: Number, attribute: 'item-height' },
66
+ /** Margin between data series rows in pixels. Default: 5 */
67
+ itemMargin: { type: Number, attribute: 'item-margin' },
68
+ /** Top margin in pixels. Default: 30 */
69
+ marginTop: { type: Number, attribute: 'margin-top' },
70
+ /** Bottom margin in pixels. Default: 30 */
71
+ marginBottom: { type: Number, attribute: 'margin-bottom' },
72
+ /** Left margin in pixels. Default: 30 */
73
+ marginLeft: { type: Number, attribute: 'margin-left' },
74
+ /** Right margin in pixels. Default: 30 */
75
+ marginRight: { type: Number, attribute: 'margin-right' },
76
+ /**
77
+ * Tick label format: a d3 time-format specifier string (e.g. `%H:%M`)
78
+ * or, as a property, a `(date) => string` function. Default: `%I %p`
79
+ */
80
+ tickFormat: { attribute: 'tick-format' },
81
+ /**
82
+ * Time unit of the ticks: a d3-time interval, e.g.
83
+ * `import { timeHour } from 'd3-time'`. Property only.
84
+ */
85
+ tickTime: { attribute: false },
86
+ /** Tick interval, used together with `tickTime`. Default: 1 */
87
+ tickInterval: { type: Number, attribute: 'tick-interval' },
88
+ /** Number of ticks, used when `tickTime` is not set. */
89
+ numTicks: { type: Number, attribute: 'num-ticks' },
90
+ /** Tick size in pixels. Default: 6 */
91
+ tickSize: { type: Number, attribute: 'tick-size' },
92
+ /** Explicit tick values (array of Date or ms-epoch). Property only. */
93
+ tickValues: { attribute: false },
94
+ /** Degrees of rotation of the tick labels. Default: 0 */
95
+ rotateTicks: { type: Number, attribute: 'rotate-ticks' },
96
+ /** If set, the time axis is placed on top instead of the bottom. */
97
+ axisTop: { type: Boolean, attribute: 'axis-top' },
98
+ /**
99
+ * If set, the time axis can be zoomed with Ctrl/⌘ + mouse wheel (or
100
+ * trackpad pinch), panned by dragging, and zoomed in by double-clicking.
101
+ */
102
+ axisZoom: { type: Boolean, attribute: 'axis-zoom' },
103
+ /**
104
+ * A d3 ordinal color scale for the data series.
105
+ * Default: `scaleOrdinal(schemeCategory10)`. Property only.
106
+ */
107
+ colors: { attribute: false },
108
+ /**
109
+ * Data item property name that maps data items to the `colors` scale.
110
+ * Looked up on the time entry first, then on the series.
111
+ */
112
+ colorsProperty: { type: String, attribute: 'colors-property' },
113
+ /** Start of the timeline. Computed from the data if unset. */
114
+ beginning: { converter: dateConverter },
115
+ /** End of the timeline. Computed from the data if unset. */
116
+ ending: { converter: dateConverter },
117
+ /** If set, each data series is stacked on its own row. */
118
+ stack: { type: Boolean },
119
+ /** If set, a vertical line shows the current time. */
120
+ showToday: { type: Boolean, attribute: 'show-today' },
121
+ /** Top margin of the today line. Default: 25 */
122
+ todayMarginTop: { type: Number, attribute: 'today-margin-top' },
123
+ /** Bottom margin of the today line. Default: 0 */
124
+ todayMarginBottom: { type: Number, attribute: 'today-margin-bottom' },
125
+ /** Stroke width of the today line. Default: 2 */
126
+ todayWidth: { type: Number, attribute: 'today-width' },
127
+ /** Color of the today line. Default: rgb(245, 157, 0) */
128
+ todayColor: { type: String, attribute: 'today-color' },
129
+ /** If set, the time axis is shown. */
130
+ showTimeAxis: { type: Boolean, attribute: 'show-time-axis' },
131
+ /** Background color of the rows. */
132
+ background: { type: String },
133
+ /** Color of the horizontal separator lines between rows. */
134
+ rowSeparators: { type: String, attribute: 'row-separators' },
135
+ /**
136
+ * Callback generating the text of series and bar labels from the raw
137
+ * `label` value, e.g. `(label) => label[currentLocale]`. Property only.
138
+ */
139
+ labelFormat: { attribute: false },
140
+ /** If set, debug information is logged to the console. */
141
+ debug: { type: Boolean },
142
+ };
143
+
144
+ static styles = css`
145
+ :host {
146
+ display: block;
147
+ width: 100%;
148
+ }
149
+ #timeline {
150
+ width: 100%;
151
+ }
152
+ .timeline-label,
153
+ .timeline-bar-label {
154
+ font: 12px sans-serif;
155
+ fill: var(--granite-timeline-label-color, currentColor);
156
+ }
157
+ .timeline-axis {
158
+ color: var(--granite-timeline-axis-color, currentColor);
159
+ font: 10px sans-serif;
160
+ }
161
+ `;
162
+
163
+ constructor() {
164
+ super();
165
+ this.data = [];
166
+ this.axisTop = false;
167
+ this.axisZoom = false;
168
+ this.stack = false;
169
+ this.showToday = false;
170
+ this.showTimeAxis = false;
171
+ this.debug = false;
36
172
  }
37
173
 
38
- /**
39
- * In order to statically import non ES mudules resources, you need to use `importPath`.
40
- * But in order to use `importPath`, for elements defined in ES modules, users should implement
41
- * `static get importMeta() { return import.meta; }`, and the default
42
- * implementation of `importPath` will return `import.meta.url`'s path.
43
- * More info on @Polymer/lib/mixins/element-mixin.js`
44
- */
45
- static get importMeta() { return import.meta; }
46
-
47
- static get is() { return 'granite-timeline'; }
48
-
49
- /**
50
- * Fired on mouseover of the timeline data.
51
- *
52
- * @event mouseover
53
- * @param object d The current rendering object
54
- * @param number i The index during d3 rendering
55
- * @param object datum The data object
56
- */
57
-
58
- /**
59
- * Fired on mouseout of the timeline data.
60
- *
61
- * @event mouseout
62
- * @param object d The current rendering object
63
- * @param number i The index during d3 rendering
64
- * @param object datum The data object
65
- */
66
-
67
- /**
68
- * Fired on click of the timeline data.
69
- *
70
- * @event click
71
- * @param object d The current rendering object
72
- * @param number i The index during d3 rendering
73
- * @param object datum The data object
74
- */
75
-
76
- /**
77
- * Fired on hover of the timeline data.
78
- *
79
- * @event hover
80
- * @param object d The current rendering object
81
- * @param number i The index during d3 rendering
82
- * @param object datum The data object
83
- */
84
-
85
-
86
- /**
87
- * Fired on scroll of the timeline data.
88
- *
89
- * @event scroll
90
- * @param object d The current rendering object
91
- * @param number i The index during d3 rendering
92
- * @param object datum The data object
93
- */
94
-
95
- static get properties() {
96
- return {
97
- data: {
98
- type: Array,
99
- value: () => [],
100
- observer: '_onDataChanged',
101
- },
102
- chart: {
103
- type: Object,
104
- value: () => {},
105
- },
106
-
107
- /**
108
- * Sets the width of the timeline. If the width of the timeline is longer than
109
- * the width of the svg object, the timeline will automatically scroll. The width
110
- * of the timeline will default to the width of the svg if width is not set.
111
- */
112
- width: {
113
- type: Number,
114
- },
115
- /**
116
- * Sets the height of the timeline. The height of the timeline will be automatically
117
- * calculated from the height of each item if height is not set on the
118
- * timeline or the svg.
119
- */
120
- height: {
121
- type: Number,
122
- },
123
- /**
124
- * Sets the height of the data series in the timeline in pixels. Default: 20
125
- */
126
- itemHeight: {
127
- type: Number,
128
- },
129
- /**
130
- * Sets the margin between the data series in the timeline in pixels. Default: 5
131
- */
132
- itemMargin: {
133
- type: Number,
134
- },
135
- /**
136
- * Sets the top margin in pixel. Default: 30
137
- */
138
- marginTop: {
139
- type: Number,
140
- },
141
- /**
142
- * Sets the bottom margin in pixel. Default: 30
143
- */
144
- marginBottom: {
145
- type: Number,
146
- },
147
- /**
148
- * Sets the left margin in pixel. Default: 30
149
- */
150
- marginLeft: {
151
- type: Number,
152
- },
153
- /**
154
- * Sets the right margin in pixel. Default: 30
155
- */
156
- marginRight: {
157
- type: Number,
158
- },
159
-
160
- /**
161
- * By default the data is displayed as rectangles (`.display('rect')`).
162
- * If set, data series are displayed as circles (`.display('circle')`).
163
- */
164
- displayCircles: {
165
- type: Boolean,
166
- value: false,
167
- },
168
-
169
- /**
170
- * registers a function to be called when the text for the label needs to be
171
- * generated. Useful if your label looks like this:
172
- * ```
173
- * {
174
- * en: "my label",
175
- * fr: "mon étiquette"
176
- * }
177
- * The callback function is passed the whatever the datum.label returns, so in this
178
- * case it would be the object above. So the `labelFormat` might look something like
179
- * this:
180
- *
181
- * `(label) => label[currentLocale]`
182
- */
183
- labelFormat: {
184
- type: Object,
185
- },
186
-
187
- /**
188
- * sets the formatting of the ticks in the timeline. Default: d3.time.format("%I %p")
189
- */
190
- tickFormat: {
191
- type: Object,
192
- },
193
- /**
194
- * sets the time unit of the ticks in the timeline.
195
- *
196
- * Tick interval/values can be set with:
197
- *
198
- * `tickTime` and `tickInterval`
199
- * `numTicks` and `tickInterval`
200
- * `tickValues`
201
- *
202
- * Default: d3.time.hours
203
- */
204
- tickTime: {
205
- type: Object,
206
- },
207
- /**
208
- * sets the tick interval for the ticks in the timeline.
209
- *
210
- * Tick interval/values can be set with:
211
- *
212
- * `tickTime` and `tickInterval`
213
- * `numTicks` and `tickInterval`
214
- * `tickValues`
215
- *
216
- * Default: 1
217
- */
218
- tickInterval: {
219
- type: Number,
220
- },
221
- /**
222
- * sets the number of ticks in the timeline.
223
- *
224
- * Tick interval/values can be set with:
225
- *
226
- * `tickTime` and `tickInterval`
227
- * `numTicks` and `tickInterval`
228
- * `tickValues`
229
- *
230
- * Default: not set
231
- */
232
- numTicks: {
233
- type: Object,
234
- },
235
- /**
236
- * sets the tick size for the ticks in the timeline. Default: 6
237
- */
238
- tickSize: {
239
- type: Number,
240
- },
241
- /**
242
- * sets the values of the ticks in the timeline.
243
- *
244
- * Default: not set
245
- */
246
- tickValues: {
247
- type: Array,
248
- },
249
-
250
- /**
251
- * sets the degree of rotation of the tickmarks. Defaults to no rotation (0 degrees)
252
- */
253
- rotateTicks: {
254
- type: Number,
255
- },
256
-
257
- /**
258
- * By default the axis in at the bottom (`.orient('bottom')`).
259
- * If set, the axis is placed on top (`.orient('top')`).
260
- */
261
- axisTop: {
262
- type: Boolean,
263
- value: false,
264
- },
265
-
266
- /**
267
- * Callback setting the d3 color scale the data series in the timeline.
268
- * Default: `d3.scale.category20()`
269
- */
270
- colors: {
271
- type: Object,
272
- },
273
-
274
- /**
275
- * sets the data item property name that maps your data items to your color scale.
276
- * For example if you set your chart's `colors` and `colorsProperty` as follows:
277
- *
278
- * var colorScale = d3.scale.ordinal()
279
- * .range(['#6b0000','#ef9b0f','#ffee00'])
280
- * .domain(['apple','orange','lemon']);
281
- *
282
- * [...]
283
- *
284
- * <granite-timeline colors="{{colorScale}}" color-property="fruit">
285
- * </granite-timeline>
286
- *
287
- * And pass this dataset:
288
- *
289
- * var testData = [
290
- * {label: "fruit 1", fruit: "orange", times: [
291
- * {"starting_time": 1355759910000, "ending_time": 1355761900000}]},
292
- * {label: "fruit 2", fruit: "apple", times: [
293
- * {"starting_time": 1355752800000, "ending_time": 1355759900000},
294
- * {"starting_time": 1355767900000, "ending_time": 1355774400000}]},
295
- * {label: "fruit3", fruit: "lemon", times: [
296
- * {"starting_time": 1355761910000, "ending_time": 1355763910000}]}
297
- * ];
298
- *
299
- * Your chart's bar colors will be determined based on the value of the fruit property
300
- *
301
- * You can also set the color property for a specific time object:
302
- *
303
- * var testData = [
304
- * {label: "fruit 2", fruit: "apple", times: [
305
- * {fruit: "orange", "starting_time": 1355752800000, "ending_time": 1355759900000},
306
- * {"starting_time": 1355767900000, "ending_time": 1355774400000},
307
- * {fruit: "lemon", "starting_time": 1355774400000, "ending_time": 1355775500000}]}
308
- * ];
309
- *
310
- * Properties set in the time object will override the property set for the series
311
- */
312
- colorsProperty: {
313
- type: String,
314
- },
315
-
316
- /**
317
- * sets the time that the timeline should start. If beginning and ending are not set,
318
- * the timeline will calculate it based off of the smallest and largest times.
319
- */
320
- beginning: {
321
- type: Date,
322
- },
323
- /**
324
- * sets the time that the timeline should stop. If beginning and ending are not set,
325
- * the timeline will calculate it based off of the smallest and largest times.
326
- */
327
- ending: {
328
- type: Date,
329
- },
330
-
331
- /**
332
- * Toggles the stacking/unstacking of data series in the timeline.
333
- */
334
- stack: {
335
- type: Boolean,
336
- value: false,
337
- },
338
-
339
- /**
340
- * Toggles the calculation and use of relative timestamps.
341
- * The origin of the timeline will be set to 0 and the starting_time of the
342
- * first data dictionary in the data array will be subtracted from every
343
- * subsequent timestamp.
344
- */
345
- relativeTime: {
346
- type: Boolean,
347
- value: false,
348
- },
349
-
350
- /**
351
- * Toggles a vertical line showing the current `Date.now()` time.
352
- */
353
- showToday: {
354
- type: Boolean,
355
- value: false,
356
- },
357
- /**
358
- * Sets the formatting of the showToday line
359
- */
360
- todayMarginTop: {
361
- type: Number,
362
- },
363
- /**
364
- * Sets the formatting of the showToday line
365
- */
366
- todayMarginBottom: {
367
- type: Number,
368
- },
369
- /**
370
- * Sets the formatting of the showToday line
371
- */
372
- todayWidth: {
373
- type: Number,
374
- },
375
- /**
376
- * Sets the formatting of the showToday line
377
- */
378
- todayColor: {
379
- type: String,
380
- },
381
-
382
- /**
383
- * Toggles a vertical line showing the borders of one specific timeline.
384
- */
385
- showBorderLine: {
386
- type: Boolean,
387
- value: false,
388
- },
389
- /**
390
- * Sets the formatting of the showBorderLine line
391
- */
392
- borderLineMarginTop: {
393
- type: Number,
394
- },
395
- /**
396
- * Sets the formatting of the showBorderLine line
397
- */
398
- borderLineMarginBottom: {
399
- type: Number,
400
- },
401
- /**
402
- * Sets the formatting of the showBorderLine line
403
- */
404
- borderLineWidth: {
405
- type: Number,
406
- },
407
- /**
408
- * Sets the formatting of the showBorderLine line
409
- */
410
- borderLineColor: {
411
- type: String,
412
- },
413
-
414
- /**
415
- * Toggles the visibility of the time axis.
416
- */
417
- showTimeAxis: {
418
- type: Boolean,
419
- value: false,
420
- },
421
- /**
422
- * Shows tick marks along the X axis.
423
- * Useful for datasets with a lot of stacked elements.
424
- */
425
- showTimeAxisTick: {
426
- type: Boolean,
427
- value: false,
428
- },
429
- /**
430
- * Sets the formatting of the showTimeAxisTick lines. Default: 'stroke-dasharray'
431
- */
432
- timeAxisTickStroke: {
433
- type: String,
434
- },
435
- /**
436
- * Sets the formatting of the showTimeAxisTick lines. Default: '4 10'
437
- */
438
- timeAxisTickSpacing: {
439
- type: String,
440
- },
441
-
442
- /**
443
- * Sets the class for the x axis. Default: 'timeline-xAxis'
444
- */
445
- xAxisClass: {
446
- type: String,
447
- },
448
-
449
- /**
450
- * If set, zooming is allowed
451
- */
452
- axisZoom: {
453
- type: Boolean,
454
- default: false,
455
- },
456
-
457
- /**
458
- * Sets the background of the rows. Useful for creating a continuous effect
459
- * when there are gaps in your data.
460
- */
461
- background: {
462
- type: String,
463
- },
464
-
465
- /**
466
- * Sets the display of horizontal lines betweens rows.
467
- */
468
- rowSeparators: {
469
- type: String,
470
- },
471
-
472
- debug: {
473
- type: Boolean,
474
- value: false,
475
- },
476
- _libReady: {
477
- type: Boolean,
478
- value: false,
479
- },
480
- /**
481
- * The application dependencies
482
- */
483
- _dependencies: {
484
- type: Array,
485
- value: [
486
- {name: 'd3', url: `${this.importPath}../../d3/dist/d3.min.js`},
487
- {name: 'd3.timelines', url: `${this.importPath}../../d3-timelines/dist/d3-timelines.js`},
488
- ],
489
- },
490
- };
174
+ render() {
175
+ return html`<div id="timeline" part="timeline"></div>`;
491
176
  }
492
177
 
493
- // -----------------------------------------------------------------------
494
- // Livecycle
495
- // -----------------------------------------------------------------------
496
178
  connectedCallback() {
497
179
  super.connectedCallback();
498
- if (this.debug) {
499
- console.log('[granite-timeline] connectedCallback');
500
- }
180
+ // Re-observe on reconnection; initial observation happens in firstUpdated()
181
+ this._resizeObserver?.observe(this);
501
182
  }
502
183
 
503
- // -----------------------------------------------------------------------
504
- // Observers
505
- // -----------------------------------------------------------------------
506
- _onDataChanged() {
507
- this.draw();
184
+ firstUpdated() {
185
+ this._container = this.renderRoot.querySelector('#timeline');
186
+ this._resizeObserver = new ResizeObserver(() => this._requestDraw());
187
+ this._resizeObserver.observe(this);
508
188
  }
509
189
 
510
- // -----------------------------------------------------------------------
511
- // Event listeners
512
- // -----------------------------------------------------------------------
513
- _onDependencyIsReady(evt) {
514
- if (this.debug) {
515
- console.log('[granite-timeline] _onDependencyIsReady', evt.detail.name,this.$.timeline);
516
- }
517
- if (evt.detail.name !== 'd3.timelines') {
518
- return;
519
- }
520
- this._width = this.$.timeline.clientWidth || 300;
521
- this._libReady = true;
190
+ updated() {
191
+ this._requestDraw();
522
192
  }
523
193
 
524
- // -----------------------------------------------------------------------
525
- // Other methods
526
- // -----------------------------------------------------------------------
527
- _setProperties() {
528
- if (this.width) {
529
- this.chart.width(this.width);
530
- }
531
- if (this.height) {
532
- this.chart.height(this.height);
533
- }
534
- if (this.itemHeight) {
535
- this.chart.itemHeight(this.itemHeight);
536
- }
537
- if (this.itemMargin) {
538
- this.chart.itemMargin(this.itemMargin);
539
- }
540
-
541
- let margin = this.chart.margin();
542
- if (this.marginTop !== undefined) {
543
- margin.top = this.marginTop;
544
- }
545
- if (this.marginBottom !== undefined) {
546
- margin.bottom = this.marginBottom;
547
- }
548
- if (this.marginLeft !== undefined) {
549
- margin.left = this.marginLeft;
550
- }
551
- if (this.marginRight !== undefined) {
552
- margin.right = this.marginRight;
553
- }
554
- this.chart.margin(margin);
555
-
556
- if (this.displayCircles) {
557
- this.chart.display('circle');
558
- }
559
-
560
- if (this.labelFormat) {
561
- this.chart.labelFormat(this.labelFormat);
562
- }
563
-
564
- let tickFormat = this.chart.tickFormat();
565
- if (this.tickFormat) {
566
- tickFormat.format = this.tickFormat;
567
- }
568
- if (this.tickTime) {
569
- tickFormat.tickTime = this.tickTime;
570
- }
571
- if (this.tickInterval) {
572
- tickFormat.tickInterval = this.tickInterval;
573
- }
574
- if (this.tickSize) {
575
- tickFormat.tickSize = this.tickSize;
576
- }
577
- if (this.numTicks) {
578
- tickFormat.numTicks = this.numTicks;
579
- }
580
- if (this.tickValues) {
581
- tickFormat.tickValues = this.tickValues;
582
- }
583
- this.chart.tickFormat(tickFormat);
584
-
585
-
586
- if (this.rotateTicks) {
587
- this.chart.rotateTicks(this.rotateTicks);
588
- }
589
-
590
- if (this.axisTop) {
591
- this.chart.orient('top');
592
- }
593
-
594
- if (this.colors) {
595
- this.chart.colors(this.colors);
596
- }
597
-
598
- if (this.colorsProperty) {
599
- this.chart.colorsProperty(this.colorsProperty);
600
- }
601
-
602
- if (this.beginning) {
603
- this.chart.beginning(this.beginning);
604
- }
605
- if (this.ending) {
606
- this.chart.ending(this.ending);
607
- }
608
-
609
- if (this.stack) {
610
- this.chart.stack();
611
- }
612
-
613
- if (this.relativeTime) {
614
- this.chart.relativeTime();
615
- }
616
-
617
- if (this.showToday) {
618
- this.chart.showToday();
619
- }
620
-
621
- let showTodayFormat = this.chart.showTodayFormat();
622
- if (this.todayMarginTop) {
623
- showTodayFormat.marginTop = this.todayMarginTop;
624
- }
625
- if (this.todayMarginBottom) {
626
- showTodayFormat.marginBottom = this.todayMarginBottom;
627
- }
628
- if (this.todayWidth) {
629
- showTodayFormat.width = this.todayWidth;
630
- }
631
- if (this.todayColor) {
632
- showTodayFormat.color = this.todayColor;
633
- }
634
- this.chart.showTodayFormat(showTodayFormat);
635
-
636
- if (this.showBorderLine) {
637
- this.chart.showBorderLine();
638
- }
639
-
640
- let borderFormat = this.chart.showBorderFormat();
641
- if (this.borderLineMarginTop) {
642
- borderFormat.marginTop = this.borderLineMarginTop;
643
- }
644
- if (this.borderLineMarginBottom) {
645
- borderFormat.borderLineBottom = this.borderLineMarginBottom;
646
- }
647
- if (this.borderLineWidth) {
648
- borderFormat.width = this.borderLineWidth;
649
- }
650
- if (this.borderLineColor) {
651
- borderFormat.color = this.borderLineColor;
652
- }
653
- this.chart.showBorderFormat(borderFormat);
654
-
655
- if (this.showTimeAxis) {
656
- this.chart.showTimeAxis();
657
- }
658
- if (this.showTimeAxisTick) {
659
- this.chart.showTimeAxisTick();
660
- }
661
- let showTimeAxisTickFormat = this.chart.showTimeAxisTickFormat();
662
- if (this.timeAxisTickStroke) {
663
- showTimeAxisTickFormat.stroke = this.timeAxisTickStroke;
664
- }
665
- if (this.timeAxisTickSpacing) {
666
- showTimeAxisTickFormat.spacing = this.timeAxisTickSpacing;
667
- }
668
- this.chart.showTimeAxisTickFormat(showTimeAxisTickFormat);
194
+ disconnectedCallback() {
195
+ this._resizeObserver?.disconnect();
196
+ super.disconnectedCallback();
197
+ }
669
198
 
670
- if (this.xAxisClass) {
671
- this.chart.xAxisClass(this.xAxisClass);
672
- }
673
- if (this.axisZoom) {
674
- this.chart.axisZoom();
675
- }
199
+ /** Resets the axis zoom/pan to the initial full-domain view. */
200
+ resetZoom() {
201
+ this._zoomTransform = undefined;
202
+ this._requestDraw();
203
+ }
676
204
 
677
- if (this.background) {
678
- this.chart.background(this.background);
679
- }
680
- if (this.rowSeparators) {
681
- this.chart.rowSeparators(this.rowSeparators);
205
+ /** Coalesces draw requests (property updates + resizes) into one redraw. */
206
+ _requestDraw() {
207
+ if (this._drawQueued) {
208
+ return;
682
209
  }
210
+ this._drawQueued = true;
211
+ queueMicrotask(() => {
212
+ this._drawQueued = false;
213
+ this._draw();
214
+ });
683
215
  }
684
216
 
685
- _setListeners() {
686
- this.chart
687
- .mouseover((d, index, datum, i) => {
688
- let evt = d3.event;
689
- let mouse = d3.mouse(this);
690
- this.dispatchEvent(new CustomEvent('mouseover', {detail: {d, index, datum, i, evt, mouse}}));
691
- })
692
- .mouseout((d, index, datum, i) => {
693
- let evt = d3.event;
694
- let mouse = d3.mouse(this);
695
- this.dispatchEvent(new CustomEvent('mouseout', {detail: {d, index, datum, i, evt, mouse}}));
696
- })
697
- .hover((d, index, datum, i) => {
698
- let evt = d3.event;
699
- let mouse = d3.mouse(this);
700
- this.dispatchEvent(new CustomEvent('hover', {detail: {d, index, datum, i, evt, mouse}}));
701
- })
702
- .click((d, index, datum, i) => {
703
- let evt = d3.event;
704
- let mouse = d3.mouse(this);
705
- this.dispatchEvent(new CustomEvent('click', {detail: {d, index, datum, i, evt, mouse}}));
706
- })
707
- .scroll((d, index, datum, i) => {
708
- let evt = d3.event;
709
- let mouse = d3.mouse(this);
710
- this.dispatchEvent(new CustomEvent('scroll', {detail: {d, index, datum, i, evt, mouse}}));
711
- });
217
+ /** Builds the normalized options object for the renderer. */
218
+ _buildOptions(width) {
219
+ return {
220
+ width,
221
+ height: this.height,
222
+ itemHeight: this.itemHeight,
223
+ itemMargin: this.itemMargin,
224
+ margin: {
225
+ ...(this.marginTop !== undefined && { top: this.marginTop }),
226
+ ...(this.marginBottom !== undefined && { bottom: this.marginBottom }),
227
+ ...(this.marginLeft !== undefined && { left: this.marginLeft }),
228
+ ...(this.marginRight !== undefined && { right: this.marginRight }),
229
+ },
230
+ tickFormat: this.tickFormat,
231
+ tickTime: this.tickTime,
232
+ tickInterval: this.tickInterval,
233
+ numTicks: this.numTicks,
234
+ tickSize: this.tickSize,
235
+ tickValues: this.tickValues,
236
+ rotateTicks: this.rotateTicks,
237
+ axisTop: this.axisTop,
238
+ axisZoom: this.axisZoom,
239
+ zoomTransform: this._zoomTransform,
240
+ onZoom: (transform, [start, end]) => {
241
+ this._zoomTransform = transform;
242
+ this.dispatchEvent(new CustomEvent('zoom', {
243
+ detail: { start, end, transform },
244
+ bubbles: true,
245
+ composed: true,
246
+ }));
247
+ },
248
+ colors: this.colors,
249
+ colorsProperty: this.colorsProperty,
250
+ beginning: toDate(this.beginning),
251
+ ending: toDate(this.ending),
252
+ stack: this.stack,
253
+ showToday: this.showToday,
254
+ todayFormat: {
255
+ ...(this.todayMarginTop !== undefined && { marginTop: this.todayMarginTop }),
256
+ ...(this.todayMarginBottom !== undefined && { marginBottom: this.todayMarginBottom }),
257
+ ...(this.todayWidth !== undefined && { width: this.todayWidth }),
258
+ ...(this.todayColor !== undefined && { color: this.todayColor }),
259
+ },
260
+ showTimeAxis: this.showTimeAxis,
261
+ background: this.background,
262
+ rowSeparators: this.rowSeparators,
263
+ labelFormat: this.labelFormat,
264
+ onBarEvent: (type, detail) => {
265
+ this.dispatchEvent(new CustomEvent(type, { detail, bubbles: true, composed: true }));
266
+ },
267
+ debug: this.debug,
268
+ };
712
269
  }
713
270
 
714
- draw() {
715
- if (!this._libReady) {
716
- if (!this._interval) {
717
- this._interval = setInterval(() => this.draw(), 5);
718
- }
271
+ _draw() {
272
+ if (!this._container || !this.isConnected) {
719
273
  return;
720
274
  }
721
- if (this._interval) {
722
- clearInterval(this._interval);
723
- this._interval = null;
724
- }
275
+ const width = this.width || this._container.clientWidth || 300;
725
276
  if (this.debug) {
726
277
  console.log('[granite-timeline] draw - Drawing', this.data);
727
278
  }
728
-
729
- this.chart = d3.timelines();
730
- this._setProperties();
731
- this._setListeners();
732
-
733
- d3.select(this.$.timeline)
734
- .append('svg').attr('width', this._width)
735
- .datum(this.data).call(this.chart);
279
+ renderTimeline(this._container, this.data || [], this._buildOptions(width));
736
280
  }
737
281
  }
738
282
 
739
- window.customElements.define(GraniteTimeline.is, GraniteTimeline);
283
+ if (!customElements.get('granite-timeline')) {
284
+ customElements.define('granite-timeline', GraniteTimeline);
285
+ }