@pioneer-code/pioneer-charts 22.2.2 → 22.2.3
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.
|
@@ -232,6 +232,7 @@ class PcacAxisBuilder {
|
|
|
232
232
|
drawYAxis(config) {
|
|
233
233
|
if (config.yAxis.hide)
|
|
234
234
|
return;
|
|
235
|
+
config.svg.selectAll('.pcac-y-axis').remove();
|
|
235
236
|
const yAxis = axisLeft(config.yScale).ticks(config.yAxis.ticks);
|
|
236
237
|
if (config.yAxis.tickSize !== undefined) {
|
|
237
238
|
yAxis.tickSizeInner(config.yAxis.tickSize);
|
|
@@ -352,8 +353,8 @@ function gridLines(scale, numberOfTicks) {
|
|
|
352
353
|
class PcacGridBuilder {
|
|
353
354
|
/**
|
|
354
355
|
* Vertical lines, one per x-axis tick (or category), running the plot's full height. The group
|
|
355
|
-
* is classed `pcac-grid-vertical` so a chart can
|
|
356
|
-
* line/area/plot charts do on zoom, when
|
|
356
|
+
* is classed `pcac-grid-vertical` (the horizontal one `pcac-grid-horizontal`) so a chart can
|
|
357
|
+
* find and redraw just one grid - the line/area/plot charts do on zoom, when an axis's ticks move.
|
|
357
358
|
*/
|
|
358
359
|
drawVerticalGrid(config) {
|
|
359
360
|
config.svg.append('g')
|
|
@@ -752,7 +753,7 @@ class PcacChart {
|
|
|
752
753
|
/**
|
|
753
754
|
* Draws whichever grids the resolved axes ask for: vertical lines from the x axis's ticks,
|
|
754
755
|
* horizontal from the y's (`PcacAxisConfig.showGrid`). `which` restricts it to one axis - the
|
|
755
|
-
* line/area/plot charts redraw just
|
|
756
|
+
* line/area/plot charts redraw just a zoomed axis's grid against its rescaled scale on zoom.
|
|
756
757
|
*/
|
|
757
758
|
drawGrids(xScale, yScale, which = 'both') {
|
|
758
759
|
const base = { svg: this.svg, width: this.width, height: this.height, xScale, yScale };
|
|
@@ -1784,7 +1785,13 @@ class PcacPointImageConfig {
|
|
|
1784
1785
|
*/
|
|
1785
1786
|
class PcacLineAreaChartConfig extends PcacAxisChartConfig {
|
|
1786
1787
|
enableEffects = true;
|
|
1787
|
-
|
|
1788
|
+
/**
|
|
1789
|
+
* Zoom and pan along the x / y axis (scroll / pinch to zoom 1x-10x, drag to pan, never leaving
|
|
1790
|
+
* the original domain). Either, or both, can be on. A gesture is always two-dimensional, so with
|
|
1791
|
+
* only one enabled the other axis simply stays put. Both off by default.
|
|
1792
|
+
*/
|
|
1793
|
+
enableZoomX = false;
|
|
1794
|
+
enableZoomY = false;
|
|
1788
1795
|
/**
|
|
1789
1796
|
* Hex color codes to override the default colors
|
|
1790
1797
|
*/
|
|
@@ -1827,6 +1834,16 @@ class PlaChartEffectsBuilder {
|
|
|
1827
1834
|
this.buildCollection();
|
|
1828
1835
|
this.buildCanvas();
|
|
1829
1836
|
}
|
|
1837
|
+
/**
|
|
1838
|
+
* Swaps in the scales a zoom has rescaled. The crosshair finds its point by walking the line
|
|
1839
|
+
* geometry, which the chart already redraws on zoom, but the value it labels comes from
|
|
1840
|
+
* inverting `y` - so a y-axis zoom has to hand the zoomed scale over here or the label reads
|
|
1841
|
+
* the pixel against the original domain.
|
|
1842
|
+
*/
|
|
1843
|
+
updateScales(x, y) {
|
|
1844
|
+
this.config.x = x;
|
|
1845
|
+
this.config.y = y;
|
|
1846
|
+
}
|
|
1830
1847
|
buildCollection() {
|
|
1831
1848
|
const collection = this.config.svg.append('g')
|
|
1832
1849
|
.attr('class', 'effects');
|
|
@@ -2211,18 +2228,32 @@ class PlaChartBuilder extends PcacChart {
|
|
|
2211
2228
|
this.scales = new PlaChartScalesBuilder().build(this.xAxis, this.yAxis, this.config.data, this.width, this.height);
|
|
2212
2229
|
this.lineGenerator = buildLineGenerator(this.xAxis.format, this.scales);
|
|
2213
2230
|
this.areaGenerator = buildAreaGenerator(this.xAxis.format, this.scales, this.height);
|
|
2214
|
-
if (this.
|
|
2231
|
+
if (this.zoomEnabled) {
|
|
2215
2232
|
this.zoomBehavior = buildZoomBehavior(this.width, this.height, (event) => {
|
|
2216
|
-
//
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
this.
|
|
2220
|
-
|
|
2221
|
-
//
|
|
2222
|
-
//
|
|
2223
|
-
//
|
|
2224
|
-
|
|
2225
|
-
|
|
2233
|
+
// A d3 zoom transform is always two-dimensional; only the enabled axes follow it and the
|
|
2234
|
+
// other keeps its original scale, so that component of the gesture is simply ignored.
|
|
2235
|
+
const newX = this.config.enableZoomX ? event.transform.rescaleX(this.scales.x) : this.scales.x;
|
|
2236
|
+
const newY = this.config.enableZoomY ? event.transform.rescaleY(this.scales.y) : this.scales.y;
|
|
2237
|
+
const zoomedScales = { x: newX, y: newY };
|
|
2238
|
+
// Update the zoomed axes, each with its grid: the grid hangs off the axis's ticks, so it
|
|
2239
|
+
// is redrawn against the rescaled scale and dropped back underneath everything (append
|
|
2240
|
+
// puts it on top).
|
|
2241
|
+
if (this.config.enableZoomX) {
|
|
2242
|
+
this.axisBuilder.drawXAxis(this.axisBuilderConfig(newX, newY));
|
|
2243
|
+
this.svg.selectAll('.pcac-grid-vertical').remove();
|
|
2244
|
+
this.drawGrids(newX, newY, 'x');
|
|
2245
|
+
}
|
|
2246
|
+
if (this.config.enableZoomY) {
|
|
2247
|
+
this.axisBuilder.drawYAxis(this.axisBuilderConfig(newX, newY));
|
|
2248
|
+
this.svg.selectAll('.pcac-grid-horizontal').remove();
|
|
2249
|
+
this.drawGrids(newX, newY, 'y');
|
|
2250
|
+
}
|
|
2251
|
+
this.svg.selectAll('.pcac-grid').lower();
|
|
2252
|
+
// Update lines/areas. Fresh generators against the rescaled scales, so they go through
|
|
2253
|
+
// the same getXFormat() as the dots below - positioning by bare index here (which this
|
|
2254
|
+
// used to do, and which also overwrote the original generators' x accessor for good) is
|
|
2255
|
+
// only right for the default DatasetLength format; a DateTime/Decimal chart's lines
|
|
2256
|
+
// drifted away from its dots as soon as it was zoomed.
|
|
2226
2257
|
const zoomedLine = buildLineGenerator(this.xAxis.format, zoomedScales);
|
|
2227
2258
|
const zoomedArea = buildAreaGenerator(this.xAxis.format, zoomedScales, this.height);
|
|
2228
2259
|
this.svg.selectAll('.line').attr('d', (d) => zoomedLine(d));
|
|
@@ -2232,16 +2263,16 @@ class PlaChartBuilder extends PcacChart {
|
|
|
2232
2263
|
// (DatasetLength) positions by index, and a flat selection would number every series'
|
|
2233
2264
|
// points consecutively, shoving the second series' points off to the right on zoom.
|
|
2234
2265
|
this.svg.selectAll('.dots').selectAll('.point')
|
|
2235
|
-
.attr('transform', (d, i) => this.pointTransform(d, i,
|
|
2266
|
+
.attr('transform', (d, i) => this.pointTransform(d, i, zoomedScales));
|
|
2236
2267
|
// A fan-out's anchor is positioned like the points it belongs to, so it moves the same way.
|
|
2237
2268
|
this.svg.selectAll('.fan-out')
|
|
2238
|
-
.attr('transform', (g) => this.fanOutTransform(g,
|
|
2239
|
-
// The
|
|
2240
|
-
//
|
|
2241
|
-
this.
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
//
|
|
2269
|
+
.attr('transform', (g) => this.fanOutTransform(g, zoomedScales));
|
|
2270
|
+
// The hover crosshair walks the (already updated) line geometry, but reads its value back
|
|
2271
|
+
// off the y scale - which must be the zoomed one or the label is wrong.
|
|
2272
|
+
if (this.config.enableEffects) {
|
|
2273
|
+
this.effectsBuilder.updateScales(newX, newY);
|
|
2274
|
+
}
|
|
2275
|
+
// Redrawing an axis appends it at the end of the group, above the dots; put them back
|
|
2245
2276
|
// on top so a point on the baseline isn't covered (see drawChart's draw order).
|
|
2246
2277
|
this.svg.selectAll('.fan-outs').raise();
|
|
2247
2278
|
this.svg.selectAll('.dots').raise();
|
|
@@ -2338,8 +2369,11 @@ class PlaChartBuilder extends PcacChart {
|
|
|
2338
2369
|
.attr('width', this.width + this.clipBuffer * 2)
|
|
2339
2370
|
.attr('height', this.height + this.clipBuffer * 2);
|
|
2340
2371
|
}
|
|
2372
|
+
get zoomEnabled() {
|
|
2373
|
+
return !!(this.config.enableZoomX || this.config.enableZoomY);
|
|
2374
|
+
}
|
|
2341
2375
|
attachZoomBehavior() {
|
|
2342
|
-
if (!this.
|
|
2376
|
+
if (!this.zoomEnabled)
|
|
2343
2377
|
return;
|
|
2344
2378
|
// Add a transparent rect to capture zoom events
|
|
2345
2379
|
//
|
|
@@ -2409,14 +2443,14 @@ class PlaChartBuilder extends PcacChart {
|
|
|
2409
2443
|
* Draws one `<g class="fan-outs">` holding a `<g class="fan-out">` per group of coincident
|
|
2410
2444
|
* points: a spoke from the shared coordinate to where each member is drawn, and an anchor dot
|
|
2411
2445
|
* on the coordinate itself. Only when the fan-out is on and asks for anchors. The group is
|
|
2412
|
-
* positioned by the same
|
|
2413
|
-
* way
|
|
2446
|
+
* positioned by the same translate as a point (see `drawDots`), so zoom moves it the same
|
|
2447
|
+
* way and everything inside is relative to the coordinate; it fades in over the points' own
|
|
2448
|
+
* entry transition.
|
|
2414
2449
|
*/
|
|
2415
2450
|
drawFanOuts() {
|
|
2416
2451
|
if (!this.fanOut?.showAnchor || this.coincidentGroups.length === 0) {
|
|
2417
2452
|
return;
|
|
2418
2453
|
}
|
|
2419
|
-
const y = (group) => this.scales.y(group.members[0].data.value);
|
|
2420
2454
|
const groups = this.svg.append('g')
|
|
2421
2455
|
.attr('class', 'fan-outs')
|
|
2422
2456
|
.attr('clip-path', `url(#${this.clipPathId})`)
|
|
@@ -2424,18 +2458,17 @@ class PlaChartBuilder extends PcacChart {
|
|
|
2424
2458
|
.data(this.coincidentGroups)
|
|
2425
2459
|
.enter().append('g')
|
|
2426
2460
|
.attr('class', 'fan-out')
|
|
2427
|
-
.attr('transform', (group) => this.fanOutTransform(group, this.scales
|
|
2461
|
+
.attr('transform', (group) => this.fanOutTransform(group, this.scales));
|
|
2428
2462
|
groups.selectAll('.fan-out-spoke')
|
|
2429
|
-
.data((group) => group.members.map((member) =>
|
|
2463
|
+
.data((group) => group.members.map((member) => this.offsetOf(member.data)))
|
|
2430
2464
|
.enter().append('line')
|
|
2431
2465
|
.attr('class', 'fan-out-spoke')
|
|
2432
2466
|
.attr('x1', 0)
|
|
2433
|
-
.attr('y1',
|
|
2467
|
+
.attr('y1', 0)
|
|
2434
2468
|
.attr('x2', (spoke) => spoke.dx)
|
|
2435
|
-
.attr('y2', (spoke) => spoke.
|
|
2469
|
+
.attr('y2', (spoke) => spoke.dy);
|
|
2436
2470
|
groups.append('circle')
|
|
2437
2471
|
.attr('class', 'fan-out-anchor')
|
|
2438
|
-
.attr('cy', y)
|
|
2439
2472
|
.attr('r', 3);
|
|
2440
2473
|
groups.attr('opacity', 0)
|
|
2441
2474
|
.transition()
|
|
@@ -2444,17 +2477,20 @@ class PlaChartBuilder extends PcacChart {
|
|
|
2444
2477
|
}
|
|
2445
2478
|
/**
|
|
2446
2479
|
* Draws one `<g class="dots">` per series holding a `<g class="point">` per data point. The
|
|
2447
|
-
* point group carries the
|
|
2448
|
-
* attribute regardless of what's inside; the child is either the regular
|
|
2449
|
-
* or, when the point has a `PcacData.image`, an `<image class="dot-image">`
|
|
2450
|
-
*
|
|
2451
|
-
*
|
|
2452
|
-
*
|
|
2480
|
+
* point group carries the position (as a translate) so that zoom, on either axis, only has to
|
|
2481
|
+
* touch that one attribute regardless of what's inside; the child is either the regular
|
|
2482
|
+
* `<circle class="dot">` or, when the point has a `PcacData.image`, an `<image class="dot-image">`
|
|
2483
|
+
* in its place, and sits at the group's origin. Both animate in from the baseline the same way
|
|
2484
|
+
* - the start is the baseline expressed relative to the point, since the group is already on
|
|
2485
|
+
* the point - and the tooltip / click handlers sit on the group so they behave identically for
|
|
2486
|
+
* either. A fanned-out point's offset is applied to the child, inside the group, so it too is
|
|
2487
|
+
* untouched by zoom.
|
|
2453
2488
|
*/
|
|
2454
2489
|
drawDots(config) {
|
|
2455
2490
|
const self = this;
|
|
2456
2491
|
const duration = this.transitionService.getTransitionDuration();
|
|
2457
2492
|
const { maxWidth, maxHeight } = this.pointImage;
|
|
2493
|
+
const rise = (d) => this.scales.y(0) - this.scales.y(d.value);
|
|
2458
2494
|
for (let index = 0; index < config.data.length; index++) {
|
|
2459
2495
|
const series = config.data[index];
|
|
2460
2496
|
const points = this.svg.append('g')
|
|
@@ -2465,7 +2501,7 @@ class PlaChartBuilder extends PcacChart {
|
|
|
2465
2501
|
.data(series.data)
|
|
2466
2502
|
.enter().append('g')
|
|
2467
2503
|
.attr('class', 'point')
|
|
2468
|
-
.attr('transform', (d, i) => this.pointTransform(d, i, this.scales
|
|
2504
|
+
.attr('transform', (d, i) => this.pointTransform(d, i, this.scales))
|
|
2469
2505
|
.on('mouseover', function (event, d) {
|
|
2470
2506
|
// `d` is the very element bound from `series.data` above, so identity lookup is exact.
|
|
2471
2507
|
self.showTooltip(event, d, {
|
|
@@ -2499,16 +2535,17 @@ class PlaChartBuilder extends PcacChart {
|
|
|
2499
2535
|
.attr('class', 'dot')
|
|
2500
2536
|
.attr('stroke', this.colors[index])
|
|
2501
2537
|
.attr('cx', (d) => this.offsetOf(d).dx)
|
|
2502
|
-
.attr('cy',
|
|
2538
|
+
.attr('cy', rise)
|
|
2503
2539
|
.attr('fill', '#fff')
|
|
2504
2540
|
.transition()
|
|
2505
2541
|
.duration(duration)
|
|
2506
|
-
.attr('cy', (d) => this.
|
|
2542
|
+
.attr('cy', (d) => this.offsetOf(d).dy)
|
|
2507
2543
|
.attr('r', 4);
|
|
2508
2544
|
// `preserveAspectRatio="xMidYMid meet"` is what does the "resize to fit" - the image is
|
|
2509
2545
|
// scaled uniformly to fit inside the maxWidth x maxHeight box and centered within it, so
|
|
2510
2546
|
// a non-square image still lands centered on the point. x/y offset by half the box so the
|
|
2511
|
-
// box (not its top-left corner) is centered on the
|
|
2547
|
+
// box (not its top-left corner) is centered on the point group's origin, matching where a
|
|
2548
|
+
// dot sits.
|
|
2512
2549
|
points.filter((d) => !!d.image)
|
|
2513
2550
|
.append('image')
|
|
2514
2551
|
.attr('class', 'dot-image')
|
|
@@ -2517,19 +2554,19 @@ class PlaChartBuilder extends PcacChart {
|
|
|
2517
2554
|
.attr('height', maxHeight)
|
|
2518
2555
|
.attr('preserveAspectRatio', 'xMidYMid meet')
|
|
2519
2556
|
.attr('x', (d) => -maxWidth / 2 + this.offsetOf(d).dx)
|
|
2520
|
-
.attr('y',
|
|
2557
|
+
.attr('y', (d) => rise(d) - maxHeight / 2)
|
|
2521
2558
|
.transition()
|
|
2522
2559
|
.duration(duration)
|
|
2523
|
-
.attr('y', (d) =>
|
|
2560
|
+
.attr('y', (d) => -maxHeight / 2 + this.offsetOf(d).dy);
|
|
2524
2561
|
}
|
|
2525
2562
|
}
|
|
2526
|
-
pointTransform(d, i,
|
|
2527
|
-
return `translate(${getXFormat(this.xAxis.format, d, i,
|
|
2563
|
+
pointTransform(d, i, scales) {
|
|
2564
|
+
return `translate(${getXFormat(this.xAxis.format, d, i, scales.x)}, ${scales.y(d.value)})`;
|
|
2528
2565
|
}
|
|
2529
2566
|
/** A fan-out sits where its members would have been drawn; any member locates it. */
|
|
2530
|
-
fanOutTransform(group,
|
|
2567
|
+
fanOutTransform(group, scales) {
|
|
2531
2568
|
const { data, index } = group.members[0];
|
|
2532
|
-
return this.pointTransform(data, index,
|
|
2569
|
+
return this.pointTransform(data, index, scales);
|
|
2533
2570
|
}
|
|
2534
2571
|
offsetOf(d) {
|
|
2535
2572
|
return this.fanOutOffsets.get(d) ?? { dx: 0, dy: 0 };
|