@pioneer-code/pioneer-charts 22.1.5 → 22.2.1

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.
@@ -40,6 +40,145 @@ class PcacChartConfig {
40
40
  */
41
41
  heightFull = false;
42
42
  }
43
+ /**
44
+ * Up to three short labels placed along an axis by position rather than by value: `min` at its
45
+ * start, `mid` at its center, `max` at its end. Any subset can be given.
46
+ */
47
+ class PcacAxisSubLabels {
48
+ min;
49
+ mid;
50
+ max;
51
+ }
52
+ /**
53
+ * Everything configurable about one axis. Used twice per chart, as `xAxis` and `yAxis` on
54
+ * `PcacAxisChartConfig`, so the two never drift apart and a new per-axis option lands in one place.
55
+ *
56
+ * Every field is optional so an object-literal / JSON config can give just the ones it cares
57
+ * about (or omit the axis entirely); the initializers apply to anything built with `new`, and
58
+ * `resolveAxisConfig()` fills the rest in for builders.
59
+ */
60
+ class PcacAxisConfig {
61
+ /**
62
+ * Don't draw this axis at all (labels, tick marks, line), and give the margins it would have
63
+ * occupied back to the plot area. Hiding the y axis reclaims the left and top margins; hiding
64
+ * the x axis reclaims the bottom and right. The chart's total footprint doesn't change.
65
+ */
66
+ hide = false;
67
+ /**
68
+ * Draw grid lines from this axis's ticks (or, on a category axis, from each category) across
69
+ * the plot: horizontal lines for the y axis, vertical for the x. Left unset, each chart keeps
70
+ * the grid it has always drawn - the y axis's on vertical bar and line/area/plot charts, the
71
+ * x axis's on the horizontal bar chart - and no other; set it to turn either grid on or off
72
+ * explicitly. Deliberately has no initializer, since "unset" is what carries the per-chart
73
+ * default.
74
+ */
75
+ showGrid;
76
+ /**
77
+ * Requested number of ticks (D3's `ticks()` hint, so the actual count can differ slightly) -
78
+ * also the number of grid lines for this axis. Ignored by a category (band) axis, which has one
79
+ * tick per category. Default 5.
80
+ */
81
+ ticks = 5;
82
+ /**
83
+ * Length in pixels of the small tick marks. Tick marks are not drawn at all unless this is set
84
+ * (the theme hides them, as it always has), so setting a size is also what turns them on; D3's
85
+ * default 6px is a sensible first value. `0` keeps them off but still pulls the labels in to
86
+ * the axis. Labels follow the marks (D3 places them at tick length + 3px), and the chart's
87
+ * margins grow or shrink by the same amount so the plot area makes room for them - a longer
88
+ * tick means a slightly smaller plot, never labels pushed off the edge. Only the per-tick marks
89
+ * change; the axis line's two end-caps keep their default length. Color comes from the theme
90
+ * (`.pcac-axis-tick-marks .tick line`, `$gray-900`, the same as the axis line).
91
+ */
92
+ tickSize;
93
+ /**
94
+ * Draw a solid line along the axis itself (the full length of the axis, with D3's short
95
+ * end-caps). Off by default, as the theme has always hidden it. Color comes from the theme
96
+ * (`.pcac-axis-line .domain`, `$gray-900`).
97
+ */
98
+ showLine = false;
99
+ /**
100
+ * A title for the whole axis (e.g. "Revenue ($)"), drawn centered along it just inside the
101
+ * chart's edge: below the tick labels for the x axis, rotated to read bottom-to-top left of
102
+ * them for the y axis. The chart's margin grows by `PcacChart.AXIS_LABEL_SPACE` to make room,
103
+ * shrinking the plot area the same way a longer tick does. Not drawn on a hidden axis. Styled
104
+ * by the theme's `.pcac-axis-label` rule.
105
+ */
106
+ label;
107
+ /**
108
+ * Min / mid / max sub labels (e.g. "Low" / "Medium" / "High") in their own row just outside the
109
+ * tick labels, and inside the `label` if there is one: `min` left-aligned at the axis's start,
110
+ * `mid` centered, `max` right-aligned at its end, so they never spill past the axis. On the
111
+ * y axis they run bottom-to-top like the label, `min` at the bottom. The margin grows by
112
+ * `PcacChart.AXIS_SUB_LABEL_SPACE` when any is set, shrinking the plot area. Not drawn on a
113
+ * hidden axis. Styled by the theme's `.pcac-axis-sub-label` rule.
114
+ */
115
+ subLabels;
116
+ /**
117
+ * How the values along this axis are interpreted and shown - the tick labels, and the value
118
+ * (`yAxis`) or key (`xAxis`) in the default tooltip. On the line/area/plot charts the x axis's
119
+ * format is also what decides how a point's `key` maps to an x position (by index for the
120
+ * default, `Decimal` and `DateTime` by the key's own value on a linear/time scale - see
121
+ * `PcacLineAreaChartConfig`); a bar chart's category axis ignores it. Default `None`, which
122
+ * on those charts behaves as `DatasetLength`.
123
+ */
124
+ format = PcacFormatEnum.None;
125
+ /**
126
+ * The value axis's domain: it runs from `domainMin` to `domainMax`, ignoring the data. Defaults
127
+ * 0 and 100. Only the type of axis that has a domain to set reads them - a bar chart's category
128
+ * axis ignores both, its value axis reads `domainMax` only (bars always grow from 0), and a
129
+ * line/area/plot chart's x axis reads them only under the `Decimal` (numbers) and `DateTime`
130
+ * (anything `new Date()` accepts, and required) formats - the index-based formats size the
131
+ * axis from the data.
132
+ */
133
+ domainMin = 0;
134
+ domainMax = 100;
135
+ }
136
+ /** True if `axis` has at least one sub label to draw (an empty `subLabels` object counts as none). */
137
+ function hasAxisSubLabels(axis) {
138
+ const sub = axis?.subLabels;
139
+ return !!sub && (!!sub.min || !!sub.mid || !!sub.max);
140
+ }
141
+ /**
142
+ * Room reserved in a margin for an axis `label`: the theme's 12px label plus a gap from the tick
143
+ * labels. Reserved on the side the label sits on (`bottom` for x, `left` for y), and taken out
144
+ * of the plot area, the same way a longer tick is. Change together with `.pcac-axis-label`.
145
+ */
146
+ const PCAC_AXIS_LABEL_SPACE = 18;
147
+ /** Likewise for a row of `subLabels`: the theme's 11px text plus a gap (`.pcac-axis-sub-label`). */
148
+ const PCAC_AXIS_SUB_LABEL_SPACE = 16;
149
+ /**
150
+ * Total margin an axis's `label` and `subLabels` take on its side. `PcacChart` reserves it and
151
+ * the axis builder places the text by it, so the two stay in step.
152
+ */
153
+ function axisLabelSpace(axis) {
154
+ return (axis?.label ? PCAC_AXIS_LABEL_SPACE : 0) + (hasAxisSubLabels(axis) ? PCAC_AXIS_SUB_LABEL_SPACE : 0);
155
+ }
156
+ /**
157
+ * @param showGridDefault what `showGrid` resolves to when the consumer left it unset - each chart
158
+ * passes `true` for the axis whose grid it has always drawn and `false` for the other.
159
+ */
160
+ function resolveAxisConfig(axis, showGridDefault = false) {
161
+ const defaults = new PcacAxisConfig();
162
+ return {
163
+ hide: axis?.hide ?? defaults.hide,
164
+ showGrid: axis?.showGrid ?? showGridDefault,
165
+ ticks: axis?.ticks ?? defaults.ticks,
166
+ tickSize: axis?.tickSize,
167
+ showLine: axis?.showLine ?? defaults.showLine,
168
+ label: axis?.label,
169
+ subLabels: axis?.subLabels,
170
+ format: axis?.format ?? defaults.format,
171
+ domainMin: axis?.domainMin ?? defaults.domainMin,
172
+ domainMax: axis?.domainMax ?? defaults.domainMax,
173
+ };
174
+ }
175
+ /**
176
+ * Base configuration for every chart that has an x and y axis (bar, line, area, plot - not pie).
177
+ */
178
+ class PcacAxisChartConfig extends PcacChartConfig {
179
+ xAxis = new PcacAxisConfig();
180
+ yAxis = new PcacAxisConfig();
181
+ }
43
182
  class PcacData {
44
183
  key = null;
45
184
  value = null;
@@ -78,60 +217,118 @@ class PcacAxisBuilder {
78
217
  this.drawXAxis(config);
79
218
  this.drawYAxis(config);
80
219
  }
220
+ /**
221
+ * Moves both axis groups to the end of the chart's `<g>`, so they paint over everything drawn
222
+ * since `drawAxis()`. Axes are drawn first (so scales and margins are settled before content),
223
+ * which leaves anything sitting at x = 0 - a line chart's first segment and dot, an area's fill,
224
+ * a horizontal bar chart's bars - painting over the y axis line. Builders call this after the
225
+ * content that should sit *under* the axes, and draw anything that should stay on top (the
226
+ * line/area/plot charts' dots) afterwards. Safe to sit above the hover/zoom overlay rects too,
227
+ * because the groups are `pointer-events: none` (set where each group is appended).
228
+ */
229
+ raiseAxes(svg) {
230
+ svg.selectAll('.pcac-x-axis, .pcac-y-axis').raise();
231
+ }
81
232
  drawYAxis(config) {
82
- if (config.hideYAxis)
233
+ if (config.yAxis.hide)
83
234
  return;
84
- const yAxis = axisLeft(config.yScale).ticks(config.numberOfTicks);
85
- if (config.yFormat) {
86
- switch (config.yFormat) {
87
- case PcacFormatEnum.Percentage:
88
- yAxis.tickFormat(d => d + "%");
89
- break;
90
- case PcacFormatEnum.Minutes:
91
- yAxis.tickFormat((d) => d + 'm');
92
- break;
93
- case PcacFormatEnum.Fahrenheit:
94
- yAxis.tickFormat((d) => d + ' F');
95
- break;
96
- }
235
+ const yAxis = axisLeft(config.yScale).ticks(config.yAxis.ticks);
236
+ if (config.yAxis.tickSize !== undefined) {
237
+ yAxis.tickSizeInner(config.yAxis.tickSize);
97
238
  }
98
- config.svg.append('g')
239
+ switch (config.yAxis.format) {
240
+ case PcacFormatEnum.Percentage:
241
+ yAxis.tickFormat(d => d + "%");
242
+ break;
243
+ case PcacFormatEnum.Minutes:
244
+ yAxis.tickFormat((d) => d + 'm');
245
+ break;
246
+ case PcacFormatEnum.Fahrenheit:
247
+ yAxis.tickFormat((d) => d + ' F');
248
+ break;
249
+ }
250
+ const group = config.svg.append('g')
99
251
  .attr('class', 'pcac-y-axis')
252
+ // Axes are raised above the interactive overlays (see raiseAxes) and take no mouse events
253
+ .attr('pointer-events', 'none')
254
+ .classed('pcac-axis-tick-marks', config.yAxis.tickSize !== undefined)
255
+ .classed('pcac-axis-line', config.yAxis.showLine)
100
256
  .call(yAxis);
257
+ this.drawLabels(group, config.yAxis, { length: config.height, outer: config.margin.left, vertical: true });
101
258
  }
102
259
  drawXAxis(config) {
103
- if (config.hideXAxis)
260
+ if (config.xAxis.hide)
104
261
  return;
105
262
  config.svg.selectAll('.pcac-x-axis').remove();
106
- const xAxis = axisBottom(config.xScale).ticks(config.numberOfTicks);
107
- if (config.xFormat) {
108
- switch (config.xFormat) {
109
- case PcacFormatEnum.Percentage:
110
- xAxis.tickFormat(d => d + "%");
111
- break;
112
- case PcacFormatEnum.Minutes:
113
- xAxis.tickFormat((d) => d + 'm');
114
- break;
115
- case PcacFormatEnum.Fahrenheit:
116
- xAxis.tickFormat((d) => d + ' F');
117
- break;
118
- case PcacFormatEnum.OneDayHours:
119
- xAxis.tickFormat((d) => {
120
- const h = d;
121
- const hour = h % 12 === 0 ? 12 : h % 12;
122
- const period = h < 12 ? 'am' : 'pm';
123
- return `${hour}${period}`;
124
- });
125
- break;
126
- case PcacFormatEnum.Decimal:
127
- xAxis.tickFormat((d) => format(".2s")(d));
128
- break;
129
- }
263
+ const xAxis = axisBottom(config.xScale).ticks(config.xAxis.ticks);
264
+ if (config.xAxis.tickSize !== undefined) {
265
+ xAxis.tickSizeInner(config.xAxis.tickSize);
130
266
  }
131
- config.svg.append('g')
267
+ switch (config.xAxis.format) {
268
+ case PcacFormatEnum.Percentage:
269
+ xAxis.tickFormat(d => d + "%");
270
+ break;
271
+ case PcacFormatEnum.Minutes:
272
+ xAxis.tickFormat((d) => d + 'm');
273
+ break;
274
+ case PcacFormatEnum.Fahrenheit:
275
+ xAxis.tickFormat((d) => d + ' F');
276
+ break;
277
+ case PcacFormatEnum.OneDayHours:
278
+ xAxis.tickFormat((d) => {
279
+ const h = d;
280
+ const hour = h % 12 === 0 ? 12 : h % 12;
281
+ const period = h < 12 ? 'am' : 'pm';
282
+ return `${hour}${period}`;
283
+ });
284
+ break;
285
+ case PcacFormatEnum.Decimal:
286
+ xAxis.tickFormat((d) => format(".2s")(d));
287
+ break;
288
+ }
289
+ const group = config.svg.append('g')
132
290
  .attr('class', 'pcac-x-axis')
291
+ .attr('pointer-events', 'none')
292
+ .classed('pcac-axis-tick-marks', config.xAxis.tickSize !== undefined)
293
+ .classed('pcac-axis-line', config.xAxis.showLine)
133
294
  .attr('transform', 'translate(0,' + config.height + ')')
134
295
  .call(xAxis);
296
+ this.drawLabels(group, config.xAxis, { length: config.width, outer: config.margin.bottom, vertical: false });
297
+ }
298
+ /**
299
+ * The axis `label` and `subLabels`. Both are placed by two distances: along the axis (0 at its
300
+ * start, `length` at its end - for the y axis, 0 is the bottom) and out from it, toward the
301
+ * chart's edge. The label goes at the very edge (`outer`, i.e. the margin), the sub-label row
302
+ * `PCAC_AXIS_LABEL_SPACE` inside that when there's a label. For the y axis the text is rotated
303
+ * to read bottom-to-top, which swaps the two: after `rotate(-90)` x runs up the axis (so the
304
+ * bottom is x = -length) and y runs left. `fill` is set explicitly because d3-axis puts
305
+ * `fill="none"` on the group; its own tick text does the same.
306
+ */
307
+ drawLabels(group, axis, layout) {
308
+ const text = (cls, along, out, anchor, content) => {
309
+ group.append('text')
310
+ .attr('class', cls)
311
+ .attr('fill', 'currentColor')
312
+ .attr('text-anchor', anchor)
313
+ .attr('transform', layout.vertical ? 'rotate(-90)' : null)
314
+ .attr('x', layout.vertical ? along - layout.length : along)
315
+ .attr('y', layout.vertical ? -out : out)
316
+ .attr('dy', layout.vertical ? '1em' : '-0.35em')
317
+ .text(content);
318
+ };
319
+ if (axis.label) {
320
+ text('pcac-axis-label', layout.length / 2, layout.outer, 'middle', axis.label);
321
+ }
322
+ const sub = axis.subLabels;
323
+ if (sub) {
324
+ const edge = layout.outer - (axis.label ? PCAC_AXIS_LABEL_SPACE : 0);
325
+ if (sub.min)
326
+ text('pcac-axis-sub-label', 0, edge, 'start', sub.min);
327
+ if (sub.mid)
328
+ text('pcac-axis-sub-label', layout.length / 2, edge, 'middle', sub.mid);
329
+ if (sub.max)
330
+ text('pcac-axis-sub-label', layout.length, edge, 'end', sub.max);
331
+ }
135
332
  }
136
333
  static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.1.6", ngImport: i0, type: PcacAxisBuilder, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
137
334
  static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.1.6", ngImport: i0, type: PcacAxisBuilder, providedIn: 'root' });
@@ -143,32 +340,47 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.1.6", ngImpor
143
340
  }]
144
341
  }] });
145
342
 
343
+ /** Where along its axis each grid line sits, in pixels. */
344
+ function gridLines(scale, numberOfTicks) {
345
+ if (typeof scale.ticks === 'function') {
346
+ return scale.ticks(numberOfTicks).map(d => scale(d) ?? 0);
347
+ }
348
+ const band = scale;
349
+ const half = typeof band.bandwidth === 'function' ? band.bandwidth() / 2 : 0;
350
+ return scale.domain().map(d => (scale(d) ?? 0) + half);
351
+ }
146
352
  class PcacGridBuilder {
353
+ /**
354
+ * 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 find and redraw just this grid - the
356
+ * line/area/plot charts do on zoom, when the x ticks move.
357
+ */
147
358
  drawVerticalGrid(config) {
148
- const xScale = config.xScale;
149
359
  config.svg.append('g')
150
- .attr('class', 'pcac-grid')
360
+ .attr('class', 'pcac-grid pcac-grid-vertical')
151
361
  .selectAll('g.rule')
152
- .data(xScale.ticks(config.numberOfTicks))
362
+ .data(gridLines(config.xScale, config.numberOfTicks))
153
363
  .enter().append('svg:g')
154
364
  .attr('class', 'pcac-grid-rule')
155
- .attr('transform', (d) => (`translate(${xScale(d)}, 0)`))
365
+ .attr('transform', (x) => (`translate(${x}, 0)`))
156
366
  .append('svg:line')
157
367
  .attr('y1', 0)
158
368
  .attr('y2', config.height)
159
369
  .attr('class', (d, i) => (i === 0 ? 'pcac-grid-rule-last' : ''));
160
370
  }
371
+ /**
372
+ * Horizontal lines, one per y-axis tick (or category), running the plot's full width.
373
+ */
161
374
  drawHorizontalGrid(config) {
162
- const yScale = config.yScale;
163
375
  config.svg.append('g')
164
- .attr('class', 'pcac-grid')
376
+ .attr('class', 'pcac-grid pcac-grid-horizontal')
165
377
  .selectAll('g.pcac-grid-rule')
166
- .data(yScale.ticks(config.numberOfTicks))
378
+ .data(gridLines(config.yScale, config.numberOfTicks))
167
379
  .enter().append('svg:g')
168
380
  .attr('class', 'pcac-grid-rule')
169
381
  .append('svg:line')
170
- .attr('y1', (d) => yScale(d) ?? null)
171
- .attr('y2', (d) => yScale(d) ?? null)
382
+ .attr('y1', (y) => y)
383
+ .attr('y2', (y) => y)
172
384
  .attr('x1', 0)
173
385
  .attr('x2', config.width)
174
386
  .attr('class', (_, i) => (i === 0 ? 'pcac-grid-rule-last' : ''));
@@ -411,8 +623,20 @@ class PcacChart {
411
623
  transitionService = inject(PcacTransitionService);
412
624
  tooltipBuilder = inject(PcacTooltipBuilder);
413
625
  colorService = inject(PcacColorService);
626
+ /** D3's own `tickSizeInner` default, which the default `margin` below is sized around. */
627
+ static DEFAULT_TICK_SIZE = 6;
628
+ /** See `PCAC_AXIS_LABEL_SPACE` / `PCAC_AXIS_SUB_LABEL_SPACE` in chart.model.ts. */
629
+ static AXIS_LABEL_SPACE = PCAC_AXIS_LABEL_SPACE;
630
+ static AXIS_SUB_LABEL_SPACE = PCAC_AXIS_SUB_LABEL_SPACE;
414
631
  margin = { top: 8, right: 16, bottom: 20, left: 40 };
415
632
  defaultMargin = { ...this.margin };
633
+ /**
634
+ * The current build's per-axis settings with defaults applied, set by `initializeAxisState()`.
635
+ * Builders read these (not the raw `config.xAxis`) everywhere: drawing the axes, choosing
636
+ * whether to draw a grid, how many grid lines, and so on. Pie has no axes and never sets them.
637
+ */
638
+ xAxis = resolveAxisConfig();
639
+ yAxis = resolveAxisConfig();
416
640
  svg;
417
641
  width = 400;
418
642
  height = 400;
@@ -434,6 +658,14 @@ class PcacChart {
434
658
  */
435
659
  lastContainerHeight = null;
436
660
  lastHeightFull = false;
661
+ /**
662
+ * How much the vertical margins have grown this build beyond their defaults - for tick marks
663
+ * and axis labels (`initializeAxisState()`; negative when a short tick took some away) and for
664
+ * content that hangs past the plot's edge (`reserveEdgeSpace()`). `resolveHeight()` subtracts
665
+ * it from the plot height so the SVG's total height doesn't change with any of them. Reset
666
+ * alongside the margins.
667
+ */
668
+ reservedHeight = 0;
437
669
  /**
438
670
  * Resolves the consumer's projected `<ng-template pcacTooltip>`, if any. Each chart component
439
671
  * points this at its own `contentChild(PcacTooltipDirective)` query. It's a getter rather than
@@ -443,14 +675,153 @@ class PcacChart {
443
675
  */
444
676
  tooltipTemplate = () => undefined;
445
677
  /**
446
- * Puts `margin` back to its defaults. Builders must call this at the top of `buildChart()`,
447
- * before any per-build adjustment (`hideAxis` zeroing sides, label widths measured into
448
- * `margin.left`): `margin` lives on this per-chart instance and so persists between builds,
449
- * and without a reset an adjustment made for one config silently carries into the next -
450
- * e.g. axes drawn with no room after `hideAxis` flips back to false.
678
+ * Puts `margin` back to its defaults. `initializeAxisState()` calls this first thing, before any
679
+ * per-build adjustment (hidden axes giving sides back, tick sizes growing them, label widths
680
+ * measured into `margin.left`): `margin` lives on this per-chart instance and so persists
681
+ * between builds, and without a reset an adjustment made for one config silently carries into
682
+ * the next - e.g. axes drawn with no room after `hide` flips back to false.
451
683
  */
452
684
  resetMargin() {
453
685
  this.margin = { ...this.defaultMargin };
686
+ this.reservedHeight = 0;
687
+ }
688
+ /**
689
+ * Resolves the config's `xAxis`/`yAxis` onto this chart and sets the margins up for them. Axis
690
+ * charts call this at the top of `buildChart()`, on their own copy of the config (it rewrites
691
+ * `config.height` for hidden axes), before `initializeChartState()` measures the plot area.
692
+ * In order:
693
+ *
694
+ * 1. `resetMargin()`.
695
+ * 2. Tick marks: room for a non-default `tickSize` is reserved on each axis that will actually
696
+ * be drawn (`reserveTickSizeMargins`).
697
+ * 3. Labels: `AXIS_LABEL_SPACE` is added to `bottom` / `left` for an axis with a `label`, and
698
+ * `AXIS_SUB_LABEL_SPACE` for one with any `subLabels` (see `axisLabelSpace`), again only if
699
+ * the axis will be drawn.
700
+ * 4. Hidden axes: an axis with `hide` gives its margins back to the plot. The y axis owns
701
+ * `left` and `top` (the top margin is only there so the topmost y label isn't clipped), the
702
+ * x axis `bottom` and `right` (likewise for the rightmost x label). Each reclaimed vertical
703
+ * margin is added onto `config.height` so the SVG's total height stays what the consumer
704
+ * configured; horizontally `initializeChartState()` picks the change up on its own.
705
+ *
706
+ * @param defaultGrid the axis whose grid this chart draws when the consumer hasn't said
707
+ * (`PcacAxisConfig.showGrid`): `'y'` for horizontal lines, `'x'` for vertical.
708
+ * @param hiddenAxisMargin what a hidden axis's sides shrink to, rather than 0. The
709
+ * line/area/plot charts keep 8px so a dot on the edge of the plot isn't clipped by the SVG.
710
+ */
711
+ initializeAxisState(config, defaultGrid, hiddenAxisMargin = 0) {
712
+ this.xAxis = resolveAxisConfig(config.xAxis, defaultGrid === 'x');
713
+ this.yAxis = resolveAxisConfig(config.yAxis, defaultGrid === 'y');
714
+ this.resetMargin();
715
+ this.reserveTickSizeMargins(this.xAxis.hide ? undefined : this.xAxis.tickSize, this.yAxis.hide ? undefined : this.yAxis.tickSize);
716
+ if (!this.xAxis.hide) {
717
+ const space = axisLabelSpace(this.xAxis);
718
+ this.margin.bottom += space;
719
+ this.reservedHeight += space;
720
+ }
721
+ if (!this.yAxis.hide) {
722
+ this.margin.left += axisLabelSpace(this.yAxis);
723
+ }
724
+ if (this.yAxis.hide) {
725
+ config.height = config.height + this.margin.top - hiddenAxisMargin;
726
+ this.margin.top = hiddenAxisMargin;
727
+ this.margin.left = hiddenAxisMargin;
728
+ }
729
+ if (this.xAxis.hide) {
730
+ config.height = config.height + this.margin.bottom - hiddenAxisMargin;
731
+ this.margin.bottom = hiddenAxisMargin;
732
+ this.margin.right = hiddenAxisMargin;
733
+ }
734
+ }
735
+ /**
736
+ * The `PcacAxisBuilder` config for this chart's current state - everything but the scales is
737
+ * already on the instance. Builders pass the result to `drawAxis()` (or `drawXAxis()` with a
738
+ * rescaled x, on zoom).
739
+ */
740
+ axisBuilderConfig(xScale, yScale) {
741
+ return {
742
+ svg: this.svg,
743
+ width: this.width,
744
+ height: this.height,
745
+ margin: this.margin,
746
+ xScale,
747
+ yScale,
748
+ xAxis: this.xAxis,
749
+ yAxis: this.yAxis
750
+ };
751
+ }
752
+ /**
753
+ * Draws whichever grids the resolved axes ask for: vertical lines from the x axis's ticks,
754
+ * horizontal from the y's (`PcacAxisConfig.showGrid`). `which` restricts it to one axis - the
755
+ * line/area/plot charts redraw just the x grid against the rescaled x on zoom.
756
+ */
757
+ drawGrids(xScale, yScale, which = 'both') {
758
+ const base = { svg: this.svg, width: this.width, height: this.height, xScale, yScale };
759
+ if (which !== 'y' && this.xAxis.showGrid) {
760
+ this.gridBuilder.drawVerticalGrid({ ...base, numberOfTicks: this.xAxis.ticks });
761
+ }
762
+ if (which !== 'x' && this.yAxis.showGrid) {
763
+ this.gridBuilder.drawHorizontalGrid({ ...base, numberOfTicks: this.yAxis.ticks });
764
+ }
765
+ }
766
+ /**
767
+ * Makes room in the margins for tick marks longer (or shorter) than D3's default. The default
768
+ * margins are sized around that default (`DEFAULT_TICK_SIZE`), and D3 places each tick label at
769
+ * tick length + padding, so a longer tick pushes its labels outward by exactly the difference -
770
+ * into space the margin doesn't have unless it grows by the same amount. Growing the margin here,
771
+ * *before* `initializeChartState()` measures the plot area, is what makes the chart shrink to fit
772
+ * its labels rather than pushing them off the edge of the SVG. A shorter tick hands the
773
+ * difference back to the plot area the same way.
774
+ *
775
+ * Horizontally that happens on its own: `initializeChartState()` derives `width` from the
776
+ * container minus the margins. Vertically it doesn't - `config.height` is the plot area, and
777
+ * the SVG is that plus the margins - so a taller bottom margin would grow the SVG instead. The
778
+ * delta is therefore also remembered in `reservedHeight` and taken back out of the plot
779
+ * height by `resolveHeight()`, keeping the chart's total footprint where the consumer put it.
780
+ *
781
+ * Called by `initializeAxisState()` right after `resetMargin()`, for the axes that will be
782
+ * drawn. Only the axis a tick size is given for is affected; `undefined` leaves that margin
783
+ * alone.
784
+ * @param xTickSize `tickSizeInner` for the bottom (x) axis, which lives in `margin.bottom`
785
+ * @param yTickSize `tickSizeInner` for the left (y) axis, which lives in `margin.left`
786
+ */
787
+ reserveTickSizeMargins(xTickSize, yTickSize) {
788
+ if (xTickSize !== undefined) {
789
+ const bottom = Math.max(0, this.margin.bottom + xTickSize - PcacChart.DEFAULT_TICK_SIZE);
790
+ this.reservedHeight = bottom - this.margin.bottom;
791
+ this.margin.bottom = bottom;
792
+ }
793
+ if (yTickSize !== undefined) {
794
+ this.margin.left = Math.max(0, this.margin.left + yTickSize - PcacChart.DEFAULT_TICK_SIZE);
795
+ }
796
+ }
797
+ /**
798
+ * Makes sure each given margin is at least that big, for content drawn at the very edge of the
799
+ * plot area that reaches past it - a point image on the line/area/plot charts, say, whose box
800
+ * is centered on a point at the top of the y domain and so hangs half of itself above the plot.
801
+ * Those charts already widen their clip-path to let such a point through; this is what stops
802
+ * the `<svg>` itself from cutting it off, the SVG being only the plot area plus the margins.
803
+ *
804
+ * "At least", not "add": the default margins, and any tick / label room already reserved in
805
+ * them, may well cover it already (`margin.left` is 40 by default), and adding on top of that
806
+ * would give up plot area for nothing. Vertical growth is taken out of the plot height the same
807
+ * way `reserveTickSizeMargins()`'s is, so the SVG's total height stays what the consumer
808
+ * configured; horizontally `initializeChartState()` picks the change up on its own. A side
809
+ * left `undefined` is untouched.
810
+ *
811
+ * Call after `initializeAxisState()` (which resets the margins) and before
812
+ * `initializeChartState()` (which measures the plot area against them).
813
+ */
814
+ reserveEdgeSpace(space) {
815
+ for (const side of ['top', 'right', 'bottom', 'left']) {
816
+ const wanted = space[side];
817
+ if (wanted === undefined || wanted <= this.margin[side]) {
818
+ continue;
819
+ }
820
+ if (side === 'top' || side === 'bottom') {
821
+ this.reservedHeight += wanted - this.margin[side];
822
+ }
823
+ this.margin[side] = wanted;
824
+ }
454
825
  }
455
826
  /**
456
827
  * Shows the tooltip for a hovered datum, through the chart's `pcacTooltip` template when one is
@@ -512,10 +883,14 @@ class PcacChart {
512
883
  * @param config Chart specific configuration
513
884
  */
514
885
  resolveHeight(container, config) {
886
+ // See `reserveTickSizeMargins()` / `reserveEdgeSpace()`: room made in the vertical margins
887
+ // comes out of the plot area, not added on to the SVG. Applies to the floor in the `heightFull`
888
+ // case too, for the same reason.
889
+ const height = Math.max(0, config.height - this.reservedHeight);
515
890
  if (!config.heightFull) {
516
- return config.height;
891
+ return height;
517
892
  }
518
- return Math.max(config.height, container.clientHeight - this.margin.top - this.margin.bottom);
893
+ return Math.max(height, container.clientHeight - this.margin.top - this.margin.bottom);
519
894
  }
520
895
  /**
521
896
  * True if the container's current raw layout width differs from the one measured by the last
@@ -575,12 +950,19 @@ class PcacChart {
575
950
  * In some cases, that state needs to be calculated based on the content that resides in that margin.
576
951
  * For example, labels on a horizontal bar chart are dynamic and such the margin needs to be calculated ahead of
577
952
  * chart axis construction.
953
+ *
954
+ * Measures with the y axis's own `tickSize` (the measured box spans the tick line as well as
955
+ * the label, so measuring at a different length would put the labels off by the difference)
956
+ * and adds its `axisLabelSpace()` back on top, since the measurement replaces whatever
957
+ * `initializeAxisState()` reserved for the label / sub labels.
578
958
  * @param chartElm Reference to SVG on dom
579
- * @param data Generic multi-dimensional PcacData structure
580
959
  * @param yScale D3 scale transformation object (d3.ScaleBand)
581
960
  */
582
- setHorizontalMarginsBasedOnContent(chartElm, data, yScale) {
961
+ setHorizontalMarginsBasedOnContent(chartElm, yScale) {
583
962
  const axisY = axisLeft(yScale).ticks(5);
963
+ if (this.yAxis.tickSize !== undefined) {
964
+ axisY.tickSizeInner(this.yAxis.tickSize);
965
+ }
584
966
  let max = 0;
585
967
  select$1(chartElm.nativeElement).append('g')
586
968
  .call(axisY)
@@ -596,8 +978,9 @@ class PcacChart {
596
978
  // double-counted it, and since `margin` persists on the builder between builds, the amount
597
979
  // double-counted grew on the next rebuild - the plot area came out narrower than the
598
980
  // container allowed and then shrank further after the first resize.
599
- this.width = this.width + this.margin.left - max;
600
- this.margin.left = max;
981
+ const left = max + axisLabelSpace(this.yAxis);
982
+ this.width = this.width + this.margin.left - left;
983
+ this.margin.left = left;
601
984
  }
602
985
  }
603
986
 
@@ -639,10 +1022,7 @@ class BarHorizontalChartBuilder extends PcacChart {
639
1022
  return;
640
1023
  }
641
1024
  this.config = JSON.parse(JSON.stringify(config));
642
- this.resetMargin();
643
- if (this.config.hideAxis) {
644
- this.adjustForHiddenAxis();
645
- }
1025
+ this.initializeAxisState(this.config, 'x');
646
1026
  if (!this.initializeChartState(chartElm, this.config)) {
647
1027
  return;
648
1028
  }
@@ -654,19 +1034,13 @@ class BarHorizontalChartBuilder extends PcacChart {
654
1034
  this.buildScales(chartElm, this.config);
655
1035
  this.drawChart(chartElm, this.config);
656
1036
  }
657
- adjustForHiddenAxis() {
658
- this.config.height = this.config.height + this.margin.top + this.margin.bottom;
659
- this.margin.top = 0;
660
- this.margin.bottom = 0;
661
- this.margin.left = 0;
662
- this.margin.right = 0;
663
- }
664
1037
  buildScales(chartElm, config) {
665
1038
  config.data[0].data.map((d) => {
666
1039
  return d.value;
667
1040
  });
1041
+ // Bars grow from 0, so only `domainMax` is read; `xAxis.domainMin` is ignored.
668
1042
  this.xScale = scaleLinear()
669
- .domain([0, config.domainMax]);
1043
+ .domain([0, this.xAxis.domainMax]);
670
1044
  this.yScaleStacked = scaleBand()
671
1045
  .domain(config.data.map((d) => d.key))
672
1046
  .range([this.height, 0])
@@ -675,32 +1049,18 @@ class BarHorizontalChartBuilder extends PcacChart {
675
1049
  .padding(0.05)
676
1050
  .rangeRound([0, this.yScaleStacked.bandwidth()])
677
1051
  .domain(config.data[0].data.map((d) => d.key));
678
- this.setHorizontalMarginsBasedOnContent(chartElm, config.data, this.yScaleStacked);
1052
+ // The left margin is sized to the y axis's labels - unless there is no y axis to size it to
1053
+ if (!this.yAxis.hide) {
1054
+ this.setHorizontalMarginsBasedOnContent(chartElm, this.yScaleStacked);
1055
+ }
679
1056
  this.xScale.range([0, this.width]);
680
1057
  }
681
1058
  drawChart(chartElm, config) {
682
1059
  this.buildContainer(chartElm);
683
- this.axisBuilder.drawAxis({
684
- svg: this.svg,
685
- numberOfTicks: config.numberOfTicks || 5,
686
- height: this.height,
687
- xScale: this.xScale,
688
- yScale: this.yScaleStacked,
689
- xFormat: config.tickFormat || PcacFormatEnum.None,
690
- yFormat: PcacFormatEnum.None,
691
- hideXAxis: config.hideAxis
692
- });
693
- if (!config.hideGrid) {
694
- this.gridBuilder.drawVerticalGrid({
695
- svg: this.svg,
696
- numberOfTicks: config.numberOfTicks || 5,
697
- width: this.width,
698
- height: this.height,
699
- xScale: this.xScale,
700
- yScale: this.yScaleStacked
701
- });
702
- }
1060
+ this.axisBuilder.drawAxis(this.axisBuilderConfig(this.xScale, this.yScaleStacked));
1061
+ this.drawGrids(this.xScale, this.yScaleStacked);
703
1062
  this.addGroups(config);
1063
+ this.axisBuilder.raiseAxes(this.svg);
704
1064
  }
705
1065
  addGroups(config) {
706
1066
  const groupsContainer = this.svg.append('g')
@@ -781,7 +1141,7 @@ class BarHorizontalChartBuilder extends PcacChart {
781
1141
  .on('mousemove', function (event, d) {
782
1142
  const groupIndex = Number(this.parentNode.getAttribute('data-group-id'));
783
1143
  const index = Number(this.getAttribute('data-group-bar-id'));
784
- self.showTooltip(event, d, { index, parent: config.data[groupIndex], parentIndex: groupIndex, valueFormat: config.tickFormat || PcacFormatEnum.None });
1144
+ self.showTooltip(event, d, { index, parent: config.data[groupIndex], parentIndex: groupIndex, valueFormat: self.xAxis.format });
785
1145
  })
786
1146
  .on('mouseout', function () {
787
1147
  self.hideTooltip();
@@ -813,7 +1173,7 @@ class BarHorizontalChartBuilder extends PcacChart {
813
1173
  return i;
814
1174
  })
815
1175
  .on('mousemove', (event) => {
816
- this.showTooltip(event, config.thresholds[0], { index: 0, isThreshold: true, valueFormat: config.tickFormat || PcacFormatEnum.None });
1176
+ this.showTooltip(event, config.thresholds[0], { index: 0, isThreshold: true, valueFormat: this.xAxis.format });
817
1177
  })
818
1178
  .transition()
819
1179
  .duration(this.transitionService.getTransitionDuration())
@@ -833,7 +1193,7 @@ class BarHorizontalChartBuilder extends PcacChart {
833
1193
  const threshold = config.thresholds[index];
834
1194
  // A threshold's own PcacData is typically just a value, so its parent is the *data* group
835
1195
  // it's drawn against (which has the key) rather than the threshold entry it came from.
836
- self.showTooltip(event, config.isStacked ? threshold.data[0] : threshold, { index, parent: config.data[index], parentIndex: index, isThreshold: true, valueFormat: config.tickFormat || PcacFormatEnum.None });
1196
+ self.showTooltip(event, config.isStacked ? threshold.data[0] : threshold, { index, parent: config.data[index], parentIndex: index, isThreshold: true, valueFormat: self.xAxis.format });
837
1197
  })
838
1198
  .transition()
839
1199
  .duration(this.transitionService.getTransitionDuration())
@@ -853,7 +1213,7 @@ class BarHorizontalChartBuilder extends PcacChart {
853
1213
  const index = Number(target.getAttribute("data-group-threshold-id"));
854
1214
  const groupIndex = Number(this.parentElement.dataset['groupId']);
855
1215
  // Parent is the data group (see drawThresholdsPerGroup), not the threshold entry itself.
856
- self.showTooltip(event, config.thresholds[groupIndex].data[index], { index, parent: config.data[groupIndex], parentIndex: groupIndex, isThreshold: true, valueFormat: config.tickFormat || PcacFormatEnum.None });
1216
+ self.showTooltip(event, config.thresholds[groupIndex].data[index], { index, parent: config.data[groupIndex], parentIndex: groupIndex, isThreshold: true, valueFormat: self.xAxis.format });
857
1217
  })
858
1218
  .transition()
859
1219
  .duration(this.transitionService.getTransitionDuration())
@@ -1033,14 +1393,9 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.1.6", ngImpor
1033
1393
  args: [{ selector: 'pcac-bar-horizontal-chart', host: { '[class.pcac-height-full]': 'config().heightFull' }, encapsulation: ViewEncapsulation.None, providers: [BarHorizontalChartBuilder], template: "<section class=\"pcac-bar-horizontal-chart\">\n <svg #chart></svg>\n</section>\n", styles: [".pcac-bar-horizontal-chart{display:block}.pcac-bar-horizontal-chart svg{width:100%;display:block}pcac-bar-horizontal-chart.pcac-height-full{display:block;height:100%}pcac-bar-horizontal-chart.pcac-height-full .pcac-bar-horizontal-chart{height:100%}\n"] }]
1034
1394
  }], ctorParameters: () => [], propDecorators: { config: [{ type: i0.Input, args: [{ isSignal: true, alias: "config", required: true }] }], chartElm: [{ type: i0.ViewChild, args: ['chart', { isSignal: true }] }], barClicked: [{ type: i0.Output, args: ["barClicked"] }], tooltipTemplate: [{ type: i0.ContentChild, args: [i0.forwardRef(() => PcacTooltipDirective), { isSignal: true }] }] } });
1035
1395
 
1036
- class PcacBarHorizontalChartConfig extends PcacChartConfig {
1037
- domainMax = 100;
1038
- numberOfTicks = 5;
1396
+ class PcacBarHorizontalChartConfig extends PcacAxisChartConfig {
1039
1397
  isStacked = false;
1040
1398
  thresholds = [];
1041
- tickFormat = PcacFormatEnum.None;
1042
- hideGrid = false;
1043
- hideAxis = false;
1044
1399
  spreadColorsPerGroup = false;
1045
1400
  colorOverride = new PcacBarHorizontalChartColorOverrideConfig();
1046
1401
  }
@@ -1064,13 +1419,11 @@ class BarVerticalChartBuilder extends PcacChart {
1064
1419
  if (!config?.data?.length) {
1065
1420
  return;
1066
1421
  }
1067
- // Shallow copy: adjustForHiddenAxis() rewrites `height`, and that must not land on the
1068
- // consumer's own config object (it previously did, growing it by the margins on first build).
1422
+ // Shallow copy: initializeAxisState() rewrites `height` for hidden axes, and that must not
1423
+ // land on the consumer's own config object (it previously did, growing it by the margins on
1424
+ // first build).
1069
1425
  config = { ...config };
1070
- this.resetMargin();
1071
- if (config.hideAxis) {
1072
- this.adjustForHiddenAxis(config);
1073
- }
1426
+ this.initializeAxisState(config, 'y');
1074
1427
  if (!this.initializeChartState(chartElm, config)) {
1075
1428
  return;
1076
1429
  }
@@ -1082,32 +1435,13 @@ class BarVerticalChartBuilder extends PcacChart {
1082
1435
  this.buildScales(config);
1083
1436
  this.drawChart(chartElm, config);
1084
1437
  }
1085
- adjustForHiddenAxis(config) {
1086
- const hasGroupLabel = this.hasGroupLabel(config);
1087
- config.height = config.height + this.margin.top;
1088
- if (!hasGroupLabel) {
1089
- config.height = config.height + this.margin.bottom;
1090
- }
1091
- this.margin.top = 0;
1092
- this.margin.bottom = hasGroupLabel ? this.margin.bottom : 0;
1093
- this.margin.left = 0;
1094
- this.margin.right = 0;
1095
- }
1096
- hasGroupLabel(config) {
1097
- let hasGroupLabel = false;
1098
- config.data.forEach(node => {
1099
- if (node.key) {
1100
- hasGroupLabel = true;
1101
- }
1102
- });
1103
- return hasGroupLabel;
1104
- }
1105
1438
  buildScales(config) {
1106
1439
  // `this.height`, not `config.height`: with `heightFull` on they differ (see
1107
1440
  // PcacChart.resolveHeight), and the axis, grid and bar baselines all use the resolved one.
1441
+ // Bars grow from 0, so only `domainMax` is read; `yAxis.domainMin` is ignored.
1108
1442
  this.yScale = scaleLinear()
1109
1443
  .rangeRound([0, this.height])
1110
- .domain([config.domainMax, 0]);
1444
+ .domain([this.yAxis.domainMax, 0]);
1111
1445
  this.xScaleStacked = scaleBand()
1112
1446
  .domain(config.data.map((d) => d.key))
1113
1447
  .rangeRound([0, this.width])
@@ -1119,27 +1453,10 @@ class BarVerticalChartBuilder extends PcacChart {
1119
1453
  }
1120
1454
  drawChart(chartElm, config) {
1121
1455
  this.buildContainer(chartElm);
1122
- this.axisBuilder.drawAxis({
1123
- svg: this.svg,
1124
- numberOfTicks: config.numberOfTicks || 5,
1125
- height: this.height,
1126
- xScale: this.xScaleStacked,
1127
- yScale: this.yScale,
1128
- xFormat: PcacFormatEnum.None,
1129
- yFormat: config.tickFormat || PcacFormatEnum.None,
1130
- hideYAxis: config.hideAxis
1131
- });
1132
- if (!config.hideGrid) {
1133
- this.gridBuilder.drawHorizontalGrid({
1134
- svg: this.svg,
1135
- numberOfTicks: config.numberOfTicks || 5,
1136
- width: this.width,
1137
- height: this.height,
1138
- xScale: this.xScaleStacked,
1139
- yScale: this.yScale
1140
- });
1141
- }
1456
+ this.axisBuilder.drawAxis(this.axisBuilderConfig(this.xScaleStacked, this.yScale));
1457
+ this.drawGrids(this.xScaleStacked, this.yScale);
1142
1458
  this.addGroups(config);
1459
+ this.axisBuilder.raiseAxes(this.svg);
1143
1460
  }
1144
1461
  addGroups(config) {
1145
1462
  const groupsContainer = this.svg.append('g')
@@ -1223,7 +1540,7 @@ class BarVerticalChartBuilder extends PcacChart {
1223
1540
  .on('mousemove', function (event, d) {
1224
1541
  const groupIndex = Number(this.parentNode.getAttribute('data-group-id'));
1225
1542
  const index = Number(this.getAttribute('data-group-bar-id'));
1226
- self.showTooltip(event, d, { index, parent: config.data[groupIndex], parentIndex: groupIndex, valueFormat: config.tickFormat || PcacFormatEnum.None });
1543
+ self.showTooltip(event, d, { index, parent: config.data[groupIndex], parentIndex: groupIndex, valueFormat: self.yAxis.format });
1227
1544
  })
1228
1545
  .on('mouseout', function () {
1229
1546
  self.hideTooltip();
@@ -1262,7 +1579,7 @@ class BarVerticalChartBuilder extends PcacChart {
1262
1579
  return i;
1263
1580
  })
1264
1581
  .on('mousemove', (event) => {
1265
- this.showTooltip(event, config.thresholds[0], { index: 0, isThreshold: true, valueFormat: config.tickFormat || PcacFormatEnum.None });
1582
+ this.showTooltip(event, config.thresholds[0], { index: 0, isThreshold: true, valueFormat: this.yAxis.format });
1266
1583
  })
1267
1584
  .transition()
1268
1585
  .duration(this.transitionService.getTransitionDuration())
@@ -1282,7 +1599,7 @@ class BarVerticalChartBuilder extends PcacChart {
1282
1599
  const threshold = config.thresholds[index];
1283
1600
  // A threshold's own PcacData is typically just a value, so its parent is the *data* group
1284
1601
  // it's drawn against (which has the key) rather than the threshold entry it came from.
1285
- self.showTooltip(event, config.isStacked ? threshold.data[0] : threshold, { index, parent: config.data[index], parentIndex: index, isThreshold: true, valueFormat: config.tickFormat || PcacFormatEnum.None });
1602
+ self.showTooltip(event, config.isStacked ? threshold.data[0] : threshold, { index, parent: config.data[index], parentIndex: index, isThreshold: true, valueFormat: self.yAxis.format });
1286
1603
  })
1287
1604
  .transition()
1288
1605
  .duration(this.transitionService.getTransitionDuration())
@@ -1301,7 +1618,7 @@ class BarVerticalChartBuilder extends PcacChart {
1301
1618
  const index = Number(target.getAttribute("data-group-threshold-id"));
1302
1619
  const groupIndex = Number(this.parentElement.dataset['groupId']);
1303
1620
  // Parent is the data group (see drawThresholdsPerGroup), not the threshold entry itself.
1304
- self.showTooltip(event, config.thresholds[groupIndex].data[index], { index, parent: config.data[groupIndex], parentIndex: groupIndex, isThreshold: true, valueFormat: config.tickFormat || PcacFormatEnum.None });
1621
+ self.showTooltip(event, config.thresholds[groupIndex].data[index], { index, parent: config.data[groupIndex], parentIndex: groupIndex, isThreshold: true, valueFormat: self.yAxis.format });
1305
1622
  })
1306
1623
  .attr('width', this.xScaleGrouped.bandwidth())
1307
1624
  .transition()
@@ -1390,14 +1707,9 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.1.6", ngImpor
1390
1707
  args: [{ selector: 'pcac-bar-vertical-chart', host: { '[class.pcac-height-full]': 'config().heightFull' }, encapsulation: ViewEncapsulation.None, providers: [BarVerticalChartBuilder], template: "<section class=\"pcac-bar-vertical-chart\">\n <svg #chart></svg>\n</section>\n", styles: [".pcac-bar-vertical-chart svg{width:100%;display:block}pcac-bar-vertical-chart.pcac-height-full{display:block;height:100%}pcac-bar-vertical-chart.pcac-height-full .pcac-bar-vertical-chart{height:100%}\n"] }]
1391
1708
  }], ctorParameters: () => [], propDecorators: { config: [{ type: i0.Input, args: [{ isSignal: true, alias: "config", required: true }] }], chartElm: [{ type: i0.ViewChild, args: ['chart', { isSignal: true }] }], barClicked: [{ type: i0.Output, args: ["barClicked"] }], tooltipTemplate: [{ type: i0.ContentChild, args: [i0.forwardRef(() => PcacTooltipDirective), { isSignal: true }] }] } });
1392
1709
 
1393
- class PcacBarVerticalChartConfig extends PcacChartConfig {
1394
- domainMax = 100;
1395
- numberOfTicks = 5;
1710
+ class PcacBarVerticalChartConfig extends PcacAxisChartConfig {
1396
1711
  isStacked = false;
1397
1712
  thresholds = [];
1398
- tickFormat = PcacFormatEnum.None;
1399
- hideGrid = false;
1400
- hideAxis = false;
1401
1713
  spreadColorsPerGroup = false;
1402
1714
  colorOverride = new PcacBarVerticalChartColorOverrideConfig();
1403
1715
  }
@@ -1449,18 +1761,18 @@ class PcacPointImageConfig {
1449
1761
  maxWidth = 16;
1450
1762
  maxHeight = 16;
1451
1763
  }
1452
- class PcacLineAreaChartConfig extends PcacChartConfig {
1764
+ /**
1765
+ * The axes' `format` and `domainMin`/`domainMax` (`PcacAxisConfig`) do more here than on the bar
1766
+ * charts. `yAxis.domainMin`/`domainMax` are the y scale's domain (default 0..100). `xAxis.format`
1767
+ * decides how each point's `key` becomes an x position: `Decimal` reads it as a number on a
1768
+ * linear scale from `xAxis.domainMin` to `domainMax`, `DateTime` parses it with `new Date()` on
1769
+ * a time scale between the two (both required), and every other format - including the default -
1770
+ * places points by their index within their series, sizing the axis from the first series'
1771
+ * length and ignoring the domain fields.
1772
+ */
1773
+ class PcacLineAreaChartConfig extends PcacAxisChartConfig {
1453
1774
  enableEffects = true;
1454
1775
  enableZoom = true;
1455
- numberOfTicks = 5;
1456
- hideGrid = false;
1457
- hideAxis = false;
1458
- yFormat = PcacFormatEnum.DatasetLength;
1459
- xFormat = PcacFormatEnum.DatasetLength;
1460
- yDomainMax;
1461
- yDomainMin;
1462
- xDomainMin;
1463
- xDomainMax;
1464
1776
  /**
1465
1777
  * Hex color codes to override the default colors
1466
1778
  */
@@ -1621,42 +1933,38 @@ class PlaChartScales {
1621
1933
  x;
1622
1934
  y;
1623
1935
  }
1936
+ /**
1937
+ * Takes the chart's *resolved* axes (`PcacChart.xAxis`/`yAxis`), not the raw config, so the
1938
+ * `format`/`domainMin`/`domainMax` defaults are already applied - see `PcacLineAreaChartConfig`
1939
+ * for what each format does with them.
1940
+ */
1624
1941
  class PlaChartScalesBuilder {
1625
- build(config, chartWidth, chartHeight) {
1942
+ build(xAxis, yAxis, data, chartWidth, chartHeight) {
1626
1943
  const resp = new PlaChartScales();
1627
- resp.x = this.buildXScale(config, chartWidth);
1628
- resp.y = this.buildYScale(config, chartHeight);
1944
+ resp.x = this.buildXScale(xAxis, data, chartWidth);
1945
+ resp.y = this.buildYScale(yAxis, chartHeight);
1629
1946
  return resp;
1630
1947
  }
1631
- buildXScale(config, chartWidth) {
1632
- let resp;
1633
- switch (config.xFormat) {
1948
+ buildXScale(xAxis, data, chartWidth) {
1949
+ switch (xAxis.format) {
1634
1950
  case PcacFormatEnum.DateTime:
1635
- resp = scaleTime()
1636
- .domain([new Date(config.xDomainMin), new Date(config.xDomainMax)])
1951
+ return scaleTime()
1952
+ .domain([new Date(xAxis.domainMin), new Date(xAxis.domainMax)])
1637
1953
  .range([0, chartWidth]);
1638
- break;
1639
1954
  case PcacFormatEnum.Decimal:
1640
- resp = scaleLinear()
1641
- .domain([config.xDomainMin || 0, config.xDomainMax || 100])
1642
- .range([0, chartWidth]);
1643
- break;
1644
- case PcacFormatEnum.DatasetLength:
1645
- resp = scaleLinear()
1646
- .domain([0, config.data[0].data.length - 1])
1955
+ return scaleLinear()
1956
+ .domain([xAxis.domainMin, xAxis.domainMax])
1647
1957
  .range([0, chartWidth]);
1648
- break;
1649
1958
  default:
1650
- resp = scaleLinear()
1651
- .domain([0, config.data[0].data.length - 1])
1959
+ // DatasetLength and every other format position points by index
1960
+ return scaleLinear()
1961
+ .domain([0, data[0].data.length - 1])
1652
1962
  .range([0, chartWidth]);
1653
- break;
1654
1963
  }
1655
- return resp;
1656
1964
  }
1657
- buildYScale(config, chartHeight) {
1965
+ buildYScale(yAxis, chartHeight) {
1658
1966
  return scaleLinear()
1659
- .domain([config.yDomainMin || 0, config.yDomainMax || 100])
1967
+ .domain([yAxis.domainMin, yAxis.domainMax])
1660
1968
  .range([chartHeight, 0]);
1661
1969
  }
1662
1970
  }
@@ -1699,9 +2007,18 @@ function buildAreaGenerator(format, scales, chartHeight) {
1699
2007
  });
1700
2008
  }
1701
2009
 
2010
+ /**
2011
+ * `extent` (the viewport the gesture happens in) is set to the plot area explicitly rather than
2012
+ * left to d3's default, which reads the *owning `<svg>`'s* size - plot plus margins. With the
2013
+ * viewport wider than `translateExtent`, d3's constraint centers the plot within it instead of
2014
+ * pinning the two together, so the first gesture (even one that zooms straight back out to k = 1)
2015
+ * left the chart shifted right/down by half the margins: x domain min detached from the y axis,
2016
+ * domain max pushed off the right edge.
2017
+ */
1702
2018
  function buildZoomBehavior(width, height, zoomedFn) {
1703
2019
  return zoom()
1704
2020
  .scaleExtent([1, 10])
2021
+ .extent([[0, 0], [width, height]])
1705
2022
  .translateExtent([[0, 0], [width, height]])
1706
2023
  .on("zoom", zoomedFn);
1707
2024
  }
@@ -1725,7 +2042,9 @@ class PlaChartBuilder extends PcacChart {
1725
2042
  * How far the clip-path rect extends past the drawable [0, width] x [0, height] area on every
1726
2043
  * side, so a point sitting exactly on the domain's edge isn't cut in half. 10px comfortably
1727
2044
  * clears a dot (r = 4, or 6 on hover); a point image can be much bigger than that, so
1728
- * buildChart() widens this to half the largest image dimension whenever any point has one.
2045
+ * buildChart() widens this to half the largest image dimension whenever any point has one
2046
+ * (and grows the margins to match - see `reservePointImageSpace()` - since the clip-path only
2047
+ * matters up to the edge of the SVG).
1729
2048
  */
1730
2049
  clipBuffer = 10;
1731
2050
  pointImage;
@@ -1741,14 +2060,9 @@ class PlaChartBuilder extends PcacChart {
1741
2060
  hide: false,
1742
2061
  data: []
1743
2062
  }));
1744
- this.resetMargin();
1745
- if (this.config.hideAxis) {
1746
- this.config.height = this.config.height + 12;
1747
- this.margin.top = 8;
1748
- this.margin.bottom = 8;
1749
- this.margin.left = 8;
1750
- this.margin.right = 8;
1751
- }
2063
+ // A hidden axis keeps 8px rather than 0 so a dot on the edge of the plot isn't clipped
2064
+ this.initializeAxisState(this.config, 'y', 8);
2065
+ this.reservePointImageSpace();
1752
2066
  if (!this.initializeChartState(chartElm, this.config)) {
1753
2067
  return;
1754
2068
  }
@@ -1758,43 +2072,39 @@ class PlaChartBuilder extends PcacChart {
1758
2072
  if (this.config.colorOverride?.length) {
1759
2073
  this.colors = this.config.colorOverride;
1760
2074
  }
1761
- this.pointImage = { ...new PcacPointImageConfig(), ...this.config.pointImage };
1762
- this.clipBuffer = this.config.data.some((series) => series.data.some((point) => !!point.image))
1763
- ? Math.max(10, Math.ceil(this.pointImage.maxWidth / 2), Math.ceil(this.pointImage.maxHeight / 2))
1764
- : 10;
1765
- this.scales = new PlaChartScalesBuilder().build(this.config, this.width, this.height);
1766
- this.lineGenerator = buildLineGenerator(this.config.xFormat, this.scales);
1767
- this.areaGenerator = buildAreaGenerator(this.config.xFormat, this.scales, this.height);
2075
+ this.scales = new PlaChartScalesBuilder().build(this.xAxis, this.yAxis, this.config.data, this.width, this.height);
2076
+ this.lineGenerator = buildLineGenerator(this.xAxis.format, this.scales);
2077
+ this.areaGenerator = buildAreaGenerator(this.xAxis.format, this.scales, this.height);
1768
2078
  if (this.config.enableZoom) {
1769
2079
  this.zoomBehavior = buildZoomBehavior(this.width, this.height, (event) => {
1770
2080
  // Rescale x
1771
2081
  const newX = event.transform.rescaleX(this.scales.x);
1772
2082
  // Update axis
1773
- this.axisBuilder.drawXAxis({
1774
- svg: this.svg,
1775
- numberOfTicks: this.config.numberOfTicks || 5,
1776
- height: this.height,
1777
- xScale: newX,
1778
- yScale: this.scales.y,
1779
- yFormat: this.config.yFormat,
1780
- xFormat: this.config.xFormat
1781
- });
2083
+ this.axisBuilder.drawXAxis(this.axisBuilderConfig(newX, this.scales.y));
1782
2084
  // Update lines/areas. Fresh generators against the rescaled x, so they go through the
1783
2085
  // same getXFormat() as the dots below - positioning by bare index here (which this used
1784
2086
  // to do, and which also overwrote the original generators' x accessor for good) is only
1785
2087
  // right for the default DatasetLength format; a DateTime/Decimal chart's lines drifted
1786
2088
  // away from its dots as soon as it was zoomed.
1787
2089
  const zoomedScales = { x: newX, y: this.scales.y };
1788
- const zoomedLine = buildLineGenerator(this.config.xFormat, zoomedScales);
1789
- const zoomedArea = buildAreaGenerator(this.config.xFormat, zoomedScales, this.height);
2090
+ const zoomedLine = buildLineGenerator(this.xAxis.format, zoomedScales);
2091
+ const zoomedArea = buildAreaGenerator(this.xAxis.format, zoomedScales, this.height);
1790
2092
  this.svg.selectAll('.line').attr('d', (d) => zoomedLine(d));
1791
2093
  this.svg.selectAll('.area').attr('d', (d) => zoomedArea(d));
1792
2094
  // Update dots / point images. Nested selectAll (not a flat svg.selectAll('.point')) so
1793
- // that `i` is the point's index *within its own series* - the xFormat default
2095
+ // that `i` is the point's index *within its own series* - the x format default
1794
2096
  // (DatasetLength) positions by index, and a flat selection would number every series'
1795
2097
  // points consecutively, shoving the second series' points off to the right on zoom.
1796
2098
  this.svg.selectAll('.dots').selectAll('.point')
1797
2099
  .attr('transform', (d, i) => this.pointTransform(d, i, newX));
2100
+ // The vertical grid hangs off the x ticks too, so redraw it against the rescaled x and
2101
+ // drop it back underneath everything (append puts it on top).
2102
+ this.svg.selectAll('.pcac-grid-vertical').remove();
2103
+ this.drawGrids(newX, this.scales.y, 'x');
2104
+ this.svg.selectAll('.pcac-grid-vertical').lower();
2105
+ // drawXAxis re-appends the x axis at the end of the group, above the dots; put them back
2106
+ // on top so a point on the baseline isn't covered (see drawChart's draw order).
2107
+ this.svg.selectAll('.dots').raise();
1798
2108
  });
1799
2109
  }
1800
2110
  this.drawChart(chartElm, this.config, type);
@@ -1803,27 +2113,8 @@ class PlaChartBuilder extends PcacChart {
1803
2113
  this.buildContainer(chartElm);
1804
2114
  this.attachZoomBehavior();
1805
2115
  this.createReusableClipPath();
1806
- if (!config.hideAxis) {
1807
- this.axisBuilder.drawAxis({
1808
- svg: this.svg,
1809
- numberOfTicks: config.numberOfTicks || 5,
1810
- height: this.height,
1811
- xScale: this.scales.x,
1812
- yScale: this.scales.y,
1813
- yFormat: config.yFormat,
1814
- xFormat: config.xFormat
1815
- });
1816
- }
1817
- if (!config.hideGrid) {
1818
- this.gridBuilder.drawHorizontalGrid({
1819
- svg: this.svg,
1820
- numberOfTicks: config.numberOfTicks || 5,
1821
- width: this.width,
1822
- height: this.height,
1823
- xScale: this.scales.x,
1824
- yScale: this.scales.y
1825
- });
1826
- }
2116
+ this.axisBuilder.drawAxis(this.axisBuilderConfig(this.scales.x, this.scales.y));
2117
+ this.drawGrids(this.scales.x, this.scales.y);
1827
2118
  this.drawLineArea(config, type);
1828
2119
  if (config.enableEffects) {
1829
2120
  this.effectsBuilder.buildEffects({
@@ -1836,8 +2127,31 @@ class PlaChartBuilder extends PcacChart {
1836
2127
  y: this.scales.y
1837
2128
  });
1838
2129
  }
2130
+ // Axes above the lines/areas (so an axis line isn't covered by a series sitting at x = 0),
2131
+ // but the dots above the axes: a point on the baseline or the y axis stays whole.
2132
+ this.axisBuilder.raiseAxes(this.svg);
1839
2133
  this.drawDots(config);
1840
2134
  }
2135
+ /**
2136
+ * Resolves `pointImage` and, when any point actually has an `image`, makes room for one at the
2137
+ * edge of the domain: the clip-path buffer grows to half the box so the `.dots` group lets it
2138
+ * through, and the margins grow to at least that same half so the `<svg>` doesn't cut off what
2139
+ * the clip-path let through (an image box is centered on its point, so at the top of the y
2140
+ * domain half of it sits above the plot area - in `margin.top`, which is only 8px by default).
2141
+ * Must run between `initializeAxisState()` and `initializeChartState()`, see `reserveEdgeSpace`.
2142
+ */
2143
+ reservePointImageSpace() {
2144
+ this.pointImage = { ...new PcacPointImageConfig(), ...this.config.pointImage };
2145
+ const hasImages = this.config.data.some((series) => series.data.some((point) => !!point.image));
2146
+ if (!hasImages) {
2147
+ this.clipBuffer = 10;
2148
+ return;
2149
+ }
2150
+ const halfWidth = Math.ceil(this.pointImage.maxWidth / 2);
2151
+ const halfHeight = Math.ceil(this.pointImage.maxHeight / 2);
2152
+ this.clipBuffer = Math.max(10, halfWidth, halfHeight);
2153
+ this.reserveEdgeSpace({ top: halfHeight, bottom: halfHeight, left: halfWidth, right: halfWidth });
2154
+ }
1841
2155
  createReusableClipPath() {
1842
2156
  this.clipPathId = `pcac-clip-${Date.now()}-${Math.floor(Math.random() * 10000)}`;
1843
2157
  this.svg.selectAll(`defs #${this.clipPathId}`).remove();
@@ -1948,8 +2262,8 @@ class PlaChartBuilder extends PcacChart {
1948
2262
  index: series.data.indexOf(d),
1949
2263
  parent: series,
1950
2264
  parentIndex: index,
1951
- valueFormat: self.config.yFormat,
1952
- keyFormat: self.config.xFormat,
2265
+ valueFormat: self.yAxis.format,
2266
+ keyFormat: self.xAxis.format,
1953
2267
  });
1954
2268
  // No-op for an image point (no circle inside to grow).
1955
2269
  select$1(this).select('.dot')
@@ -1998,7 +2312,7 @@ class PlaChartBuilder extends PcacChart {
1998
2312
  }
1999
2313
  }
2000
2314
  pointTransform(d, i, xScale) {
2001
- return `translate(${getXFormat(this.config.xFormat, d, i, xScale)}, 0)`;
2315
+ return `translate(${getXFormat(this.xAxis.format, d, i, xScale)}, 0)`;
2002
2316
  }
2003
2317
  static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.1.6", ngImport: i0, type: PlaChartBuilder, deps: null, target: i0.ɵɵFactoryTarget.Injectable });
2004
2318
  static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.1.6", ngImport: i0, type: PlaChartBuilder });
@@ -2271,7 +2585,6 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.1.6", ngImpor
2271
2585
  }], ctorParameters: () => [], propDecorators: { config: [{ type: i0.Input, args: [{ isSignal: true, alias: "config", required: true }] }], chartElm: [{ type: i0.ViewChild, args: ['chart', { isSignal: true }] }], sliceClicked: [{ type: i0.Output, args: ["sliceClicked"] }], tooltipTemplate: [{ type: i0.ContentChild, args: [i0.forwardRef(() => PcacTooltipDirective), { isSignal: true }] }] } });
2272
2586
 
2273
2587
  class PcacPieChartConfig extends PcacChartConfig {
2274
- numberOfTicks = 5;
2275
2588
  }
2276
2589
 
2277
2590
  /*
@@ -2282,5 +2595,5 @@ class PcacPieChartConfig extends PcacChartConfig {
2282
2595
  * Generated bundle index. Do not edit.
2283
2596
  */
2284
2597
 
2285
- export { BarHorizontalChartBuilder, BarVerticalChartBuilder, PcacAreaChart, PcacAreaChartConfig, PcacAxisBuilder, PcacBarHorizontalChartColorOverrideConfig, PcacBarHorizontalChartComponent, PcacBarHorizontalChartConfig, PcacBarVerticalChartColorOverrideConfig, PcacBarVerticalChartComponent, PcacBarVerticalChartConfig, PcacChart, PcacChartConfig, PcacChartResizeService, PcacColorService, PcacData, PcacFormatEnum, PcacGridBuilder, PcacLegend, PcacLegendConfig, PcacLegendConfigItem, PcacLineChart, PcacLineChartConfig, PcacPieChartComponent, PcacPieChartConfig, PcacPlotChart, PcacPlotChartConfig, PcacPointImageConfig, PcacTooltipBuilder, PcacTooltipDirective, PcacTransitionService, PieChartBuilder };
2598
+ export { BarHorizontalChartBuilder, BarVerticalChartBuilder, PCAC_AXIS_LABEL_SPACE, PCAC_AXIS_SUB_LABEL_SPACE, PcacAreaChart, PcacAreaChartConfig, PcacAxisBuilder, PcacAxisChartConfig, PcacAxisConfig, PcacAxisSubLabels, PcacBarHorizontalChartColorOverrideConfig, PcacBarHorizontalChartComponent, PcacBarHorizontalChartConfig, PcacBarVerticalChartColorOverrideConfig, PcacBarVerticalChartComponent, PcacBarVerticalChartConfig, PcacChart, PcacChartConfig, PcacChartResizeService, PcacColorService, PcacData, PcacFormatEnum, PcacGridBuilder, PcacLegend, PcacLegendConfig, PcacLegendConfigItem, PcacLineChart, PcacLineChartConfig, PcacPieChartComponent, PcacPieChartConfig, PcacPlotChart, PcacPlotChartConfig, PcacPointImageConfig, PcacTooltipBuilder, PcacTooltipDirective, PcacTransitionService, PieChartBuilder, axisLabelSpace, hasAxisSubLabels, resolveAxisConfig };
2286
2599
  //# sourceMappingURL=pioneer-code-pioneer-charts.mjs.map