@pioneer-code/pioneer-charts 22.1.5 → 22.2.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.
@@ -40,6 +40,123 @@ 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
+ /** True if `axis` has at least one sub label to draw (an empty `subLabels` object counts as none). */
118
+ function hasAxisSubLabels(axis) {
119
+ const sub = axis?.subLabels;
120
+ return !!sub && (!!sub.min || !!sub.mid || !!sub.max);
121
+ }
122
+ /**
123
+ * Room reserved in a margin for an axis `label`: the theme's 12px label plus a gap from the tick
124
+ * labels. Reserved on the side the label sits on (`bottom` for x, `left` for y), and taken out
125
+ * of the plot area, the same way a longer tick is. Change together with `.pcac-axis-label`.
126
+ */
127
+ const PCAC_AXIS_LABEL_SPACE = 18;
128
+ /** Likewise for a row of `subLabels`: the theme's 11px text plus a gap (`.pcac-axis-sub-label`). */
129
+ const PCAC_AXIS_SUB_LABEL_SPACE = 16;
130
+ /**
131
+ * Total margin an axis's `label` and `subLabels` take on its side. `PcacChart` reserves it and
132
+ * the axis builder places the text by it, so the two stay in step.
133
+ */
134
+ function axisLabelSpace(axis) {
135
+ return (axis?.label ? PCAC_AXIS_LABEL_SPACE : 0) + (hasAxisSubLabels(axis) ? PCAC_AXIS_SUB_LABEL_SPACE : 0);
136
+ }
137
+ /**
138
+ * @param showGridDefault what `showGrid` resolves to when the consumer left it unset - each chart
139
+ * passes `true` for the axis whose grid it has always drawn and `false` for the other.
140
+ */
141
+ function resolveAxisConfig(axis, showGridDefault = false) {
142
+ const defaults = new PcacAxisConfig();
143
+ return {
144
+ hide: axis?.hide ?? defaults.hide,
145
+ showGrid: axis?.showGrid ?? showGridDefault,
146
+ ticks: axis?.ticks ?? defaults.ticks,
147
+ tickSize: axis?.tickSize,
148
+ showLine: axis?.showLine ?? defaults.showLine,
149
+ label: axis?.label,
150
+ subLabels: axis?.subLabels,
151
+ };
152
+ }
153
+ /**
154
+ * Base configuration for every chart that has an x and y axis (bar, line, area, plot - not pie).
155
+ */
156
+ class PcacAxisChartConfig extends PcacChartConfig {
157
+ xAxis = new PcacAxisConfig();
158
+ yAxis = new PcacAxisConfig();
159
+ }
43
160
  class PcacData {
44
161
  key = null;
45
162
  value = null;
@@ -78,10 +195,25 @@ class PcacAxisBuilder {
78
195
  this.drawXAxis(config);
79
196
  this.drawYAxis(config);
80
197
  }
198
+ /**
199
+ * Moves both axis groups to the end of the chart's `<g>`, so they paint over everything drawn
200
+ * since `drawAxis()`. Axes are drawn first (so scales and margins are settled before content),
201
+ * which leaves anything sitting at x = 0 - a line chart's first segment and dot, an area's fill,
202
+ * a horizontal bar chart's bars - painting over the y axis line. Builders call this after the
203
+ * content that should sit *under* the axes, and draw anything that should stay on top (the
204
+ * line/area/plot charts' dots) afterwards. Safe to sit above the hover/zoom overlay rects too,
205
+ * because the groups are `pointer-events: none` (set where each group is appended).
206
+ */
207
+ raiseAxes(svg) {
208
+ svg.selectAll('.pcac-x-axis, .pcac-y-axis').raise();
209
+ }
81
210
  drawYAxis(config) {
82
- if (config.hideYAxis)
211
+ if (config.yAxis.hide)
83
212
  return;
84
- const yAxis = axisLeft(config.yScale).ticks(config.numberOfTicks);
213
+ const yAxis = axisLeft(config.yScale).ticks(config.yAxis.ticks);
214
+ if (config.yAxis.tickSize !== undefined) {
215
+ yAxis.tickSizeInner(config.yAxis.tickSize);
216
+ }
85
217
  if (config.yFormat) {
86
218
  switch (config.yFormat) {
87
219
  case PcacFormatEnum.Percentage:
@@ -95,15 +227,23 @@ class PcacAxisBuilder {
95
227
  break;
96
228
  }
97
229
  }
98
- config.svg.append('g')
230
+ const group = config.svg.append('g')
99
231
  .attr('class', 'pcac-y-axis')
232
+ // Axes are raised above the interactive overlays (see raiseAxes) and take no mouse events
233
+ .attr('pointer-events', 'none')
234
+ .classed('pcac-axis-tick-marks', config.yAxis.tickSize !== undefined)
235
+ .classed('pcac-axis-line', config.yAxis.showLine)
100
236
  .call(yAxis);
237
+ this.drawLabels(group, config.yAxis, { length: config.height, outer: config.margin.left, vertical: true });
101
238
  }
102
239
  drawXAxis(config) {
103
- if (config.hideXAxis)
240
+ if (config.xAxis.hide)
104
241
  return;
105
242
  config.svg.selectAll('.pcac-x-axis').remove();
106
- const xAxis = axisBottom(config.xScale).ticks(config.numberOfTicks);
243
+ const xAxis = axisBottom(config.xScale).ticks(config.xAxis.ticks);
244
+ if (config.xAxis.tickSize !== undefined) {
245
+ xAxis.tickSizeInner(config.xAxis.tickSize);
246
+ }
107
247
  if (config.xFormat) {
108
248
  switch (config.xFormat) {
109
249
  case PcacFormatEnum.Percentage:
@@ -128,10 +268,49 @@ class PcacAxisBuilder {
128
268
  break;
129
269
  }
130
270
  }
131
- config.svg.append('g')
271
+ const group = config.svg.append('g')
132
272
  .attr('class', 'pcac-x-axis')
273
+ .attr('pointer-events', 'none')
274
+ .classed('pcac-axis-tick-marks', config.xAxis.tickSize !== undefined)
275
+ .classed('pcac-axis-line', config.xAxis.showLine)
133
276
  .attr('transform', 'translate(0,' + config.height + ')')
134
277
  .call(xAxis);
278
+ this.drawLabels(group, config.xAxis, { length: config.width, outer: config.margin.bottom, vertical: false });
279
+ }
280
+ /**
281
+ * The axis `label` and `subLabels`. Both are placed by two distances: along the axis (0 at its
282
+ * start, `length` at its end - for the y axis, 0 is the bottom) and out from it, toward the
283
+ * chart's edge. The label goes at the very edge (`outer`, i.e. the margin), the sub-label row
284
+ * `PCAC_AXIS_LABEL_SPACE` inside that when there's a label. For the y axis the text is rotated
285
+ * to read bottom-to-top, which swaps the two: after `rotate(-90)` x runs up the axis (so the
286
+ * bottom is x = -length) and y runs left. `fill` is set explicitly because d3-axis puts
287
+ * `fill="none"` on the group; its own tick text does the same.
288
+ */
289
+ drawLabels(group, axis, layout) {
290
+ const text = (cls, along, out, anchor, content) => {
291
+ group.append('text')
292
+ .attr('class', cls)
293
+ .attr('fill', 'currentColor')
294
+ .attr('text-anchor', anchor)
295
+ .attr('transform', layout.vertical ? 'rotate(-90)' : null)
296
+ .attr('x', layout.vertical ? along - layout.length : along)
297
+ .attr('y', layout.vertical ? -out : out)
298
+ .attr('dy', layout.vertical ? '1em' : '-0.35em')
299
+ .text(content);
300
+ };
301
+ if (axis.label) {
302
+ text('pcac-axis-label', layout.length / 2, layout.outer, 'middle', axis.label);
303
+ }
304
+ const sub = axis.subLabels;
305
+ if (sub) {
306
+ const edge = layout.outer - (axis.label ? PCAC_AXIS_LABEL_SPACE : 0);
307
+ if (sub.min)
308
+ text('pcac-axis-sub-label', 0, edge, 'start', sub.min);
309
+ if (sub.mid)
310
+ text('pcac-axis-sub-label', layout.length / 2, edge, 'middle', sub.mid);
311
+ if (sub.max)
312
+ text('pcac-axis-sub-label', layout.length, edge, 'end', sub.max);
313
+ }
135
314
  }
136
315
  static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.1.6", ngImport: i0, type: PcacAxisBuilder, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
137
316
  static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.1.6", ngImport: i0, type: PcacAxisBuilder, providedIn: 'root' });
@@ -143,32 +322,47 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.1.6", ngImpor
143
322
  }]
144
323
  }] });
145
324
 
325
+ /** Where along its axis each grid line sits, in pixels. */
326
+ function gridLines(scale, numberOfTicks) {
327
+ if (typeof scale.ticks === 'function') {
328
+ return scale.ticks(numberOfTicks).map(d => scale(d) ?? 0);
329
+ }
330
+ const band = scale;
331
+ const half = typeof band.bandwidth === 'function' ? band.bandwidth() / 2 : 0;
332
+ return scale.domain().map(d => (scale(d) ?? 0) + half);
333
+ }
146
334
  class PcacGridBuilder {
335
+ /**
336
+ * Vertical lines, one per x-axis tick (or category), running the plot's full height. The group
337
+ * is classed `pcac-grid-vertical` so a chart can find and redraw just this grid - the
338
+ * line/area/plot charts do on zoom, when the x ticks move.
339
+ */
147
340
  drawVerticalGrid(config) {
148
- const xScale = config.xScale;
149
341
  config.svg.append('g')
150
- .attr('class', 'pcac-grid')
342
+ .attr('class', 'pcac-grid pcac-grid-vertical')
151
343
  .selectAll('g.rule')
152
- .data(xScale.ticks(config.numberOfTicks))
344
+ .data(gridLines(config.xScale, config.numberOfTicks))
153
345
  .enter().append('svg:g')
154
346
  .attr('class', 'pcac-grid-rule')
155
- .attr('transform', (d) => (`translate(${xScale(d)}, 0)`))
347
+ .attr('transform', (x) => (`translate(${x}, 0)`))
156
348
  .append('svg:line')
157
349
  .attr('y1', 0)
158
350
  .attr('y2', config.height)
159
351
  .attr('class', (d, i) => (i === 0 ? 'pcac-grid-rule-last' : ''));
160
352
  }
353
+ /**
354
+ * Horizontal lines, one per y-axis tick (or category), running the plot's full width.
355
+ */
161
356
  drawHorizontalGrid(config) {
162
- const yScale = config.yScale;
163
357
  config.svg.append('g')
164
- .attr('class', 'pcac-grid')
358
+ .attr('class', 'pcac-grid pcac-grid-horizontal')
165
359
  .selectAll('g.pcac-grid-rule')
166
- .data(yScale.ticks(config.numberOfTicks))
360
+ .data(gridLines(config.yScale, config.numberOfTicks))
167
361
  .enter().append('svg:g')
168
362
  .attr('class', 'pcac-grid-rule')
169
363
  .append('svg:line')
170
- .attr('y1', (d) => yScale(d) ?? null)
171
- .attr('y2', (d) => yScale(d) ?? null)
364
+ .attr('y1', (y) => y)
365
+ .attr('y2', (y) => y)
172
366
  .attr('x1', 0)
173
367
  .attr('x2', config.width)
174
368
  .attr('class', (_, i) => (i === 0 ? 'pcac-grid-rule-last' : ''));
@@ -411,8 +605,20 @@ class PcacChart {
411
605
  transitionService = inject(PcacTransitionService);
412
606
  tooltipBuilder = inject(PcacTooltipBuilder);
413
607
  colorService = inject(PcacColorService);
608
+ /** D3's own `tickSizeInner` default, which the default `margin` below is sized around. */
609
+ static DEFAULT_TICK_SIZE = 6;
610
+ /** See `PCAC_AXIS_LABEL_SPACE` / `PCAC_AXIS_SUB_LABEL_SPACE` in chart.model.ts. */
611
+ static AXIS_LABEL_SPACE = PCAC_AXIS_LABEL_SPACE;
612
+ static AXIS_SUB_LABEL_SPACE = PCAC_AXIS_SUB_LABEL_SPACE;
414
613
  margin = { top: 8, right: 16, bottom: 20, left: 40 };
415
614
  defaultMargin = { ...this.margin };
615
+ /**
616
+ * The current build's per-axis settings with defaults applied, set by `initializeAxisState()`.
617
+ * Builders read these (not the raw `config.xAxis`) everywhere: drawing the axes, choosing
618
+ * whether to draw a grid, how many grid lines, and so on. Pie has no axes and never sets them.
619
+ */
620
+ xAxis = resolveAxisConfig();
621
+ yAxis = resolveAxisConfig();
416
622
  svg;
417
623
  width = 400;
418
624
  height = 400;
@@ -434,6 +640,12 @@ class PcacChart {
434
640
  */
435
641
  lastContainerHeight = null;
436
642
  lastHeightFull = false;
643
+ /**
644
+ * How much `initializeAxisState()` last added to `margin.bottom` for tick marks and axis
645
+ * labels (negative when a short tick took some away). `resolveHeight()` subtracts it from the
646
+ * plot height so the SVG's total height doesn't change with either. Reset alongside the margins.
647
+ */
648
+ reservedTickHeight = 0;
437
649
  /**
438
650
  * Resolves the consumer's projected `<ng-template pcacTooltip>`, if any. Each chart component
439
651
  * points this at its own `contentChild(PcacTooltipDirective)` query. It's a getter rather than
@@ -443,14 +655,126 @@ class PcacChart {
443
655
  */
444
656
  tooltipTemplate = () => undefined;
445
657
  /**
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.
658
+ * Puts `margin` back to its defaults. `initializeAxisState()` calls this first thing, before any
659
+ * per-build adjustment (hidden axes giving sides back, tick sizes growing them, label widths
660
+ * measured into `margin.left`): `margin` lives on this per-chart instance and so persists
661
+ * between builds, and without a reset an adjustment made for one config silently carries into
662
+ * the next - e.g. axes drawn with no room after `hide` flips back to false.
451
663
  */
452
664
  resetMargin() {
453
665
  this.margin = { ...this.defaultMargin };
666
+ this.reservedTickHeight = 0;
667
+ }
668
+ /**
669
+ * Resolves the config's `xAxis`/`yAxis` onto this chart and sets the margins up for them. Axis
670
+ * charts call this at the top of `buildChart()`, on their own copy of the config (it rewrites
671
+ * `config.height` for hidden axes), before `initializeChartState()` measures the plot area.
672
+ * In order:
673
+ *
674
+ * 1. `resetMargin()`.
675
+ * 2. Tick marks: room for a non-default `tickSize` is reserved on each axis that will actually
676
+ * be drawn (`reserveTickSizeMargins`).
677
+ * 3. Labels: `AXIS_LABEL_SPACE` is added to `bottom` / `left` for an axis with a `label`, and
678
+ * `AXIS_SUB_LABEL_SPACE` for one with any `subLabels` (see `axisLabelSpace`), again only if
679
+ * the axis will be drawn.
680
+ * 4. Hidden axes: an axis with `hide` gives its margins back to the plot. The y axis owns
681
+ * `left` and `top` (the top margin is only there so the topmost y label isn't clipped), the
682
+ * x axis `bottom` and `right` (likewise for the rightmost x label). Each reclaimed vertical
683
+ * margin is added onto `config.height` so the SVG's total height stays what the consumer
684
+ * configured; horizontally `initializeChartState()` picks the change up on its own.
685
+ *
686
+ * @param defaultGrid the axis whose grid this chart draws when the consumer hasn't said
687
+ * (`PcacAxisConfig.showGrid`): `'y'` for horizontal lines, `'x'` for vertical.
688
+ * @param hiddenAxisMargin what a hidden axis's sides shrink to, rather than 0. The
689
+ * line/area/plot charts keep 8px so a dot on the edge of the plot isn't clipped by the SVG.
690
+ */
691
+ initializeAxisState(config, defaultGrid, hiddenAxisMargin = 0) {
692
+ this.xAxis = resolveAxisConfig(config.xAxis, defaultGrid === 'x');
693
+ this.yAxis = resolveAxisConfig(config.yAxis, defaultGrid === 'y');
694
+ this.resetMargin();
695
+ this.reserveTickSizeMargins(this.xAxis.hide ? undefined : this.xAxis.tickSize, this.yAxis.hide ? undefined : this.yAxis.tickSize);
696
+ if (!this.xAxis.hide) {
697
+ const space = axisLabelSpace(this.xAxis);
698
+ this.margin.bottom += space;
699
+ this.reservedTickHeight += space;
700
+ }
701
+ if (!this.yAxis.hide) {
702
+ this.margin.left += axisLabelSpace(this.yAxis);
703
+ }
704
+ if (this.yAxis.hide) {
705
+ config.height = config.height + this.margin.top - hiddenAxisMargin;
706
+ this.margin.top = hiddenAxisMargin;
707
+ this.margin.left = hiddenAxisMargin;
708
+ }
709
+ if (this.xAxis.hide) {
710
+ config.height = config.height + this.margin.bottom - hiddenAxisMargin;
711
+ this.margin.bottom = hiddenAxisMargin;
712
+ this.margin.right = hiddenAxisMargin;
713
+ }
714
+ }
715
+ /**
716
+ * The `PcacAxisBuilder` config for this chart's current state - everything but the scales and
717
+ * formats is already on the instance. Builders pass the result to `drawAxis()` (or `drawXAxis()`
718
+ * with a rescaled x, on zoom).
719
+ */
720
+ axisBuilderConfig(xScale, yScale, xFormat, yFormat) {
721
+ return {
722
+ svg: this.svg,
723
+ width: this.width,
724
+ height: this.height,
725
+ margin: this.margin,
726
+ xScale,
727
+ yScale,
728
+ xAxis: this.xAxis,
729
+ yAxis: this.yAxis,
730
+ xFormat,
731
+ yFormat
732
+ };
733
+ }
734
+ /**
735
+ * Draws whichever grids the resolved axes ask for: vertical lines from the x axis's ticks,
736
+ * horizontal from the y's (`PcacAxisConfig.showGrid`). `which` restricts it to one axis - the
737
+ * line/area/plot charts redraw just the x grid against the rescaled x on zoom.
738
+ */
739
+ drawGrids(xScale, yScale, which = 'both') {
740
+ const base = { svg: this.svg, width: this.width, height: this.height, xScale, yScale };
741
+ if (which !== 'y' && this.xAxis.showGrid) {
742
+ this.gridBuilder.drawVerticalGrid({ ...base, numberOfTicks: this.xAxis.ticks });
743
+ }
744
+ if (which !== 'x' && this.yAxis.showGrid) {
745
+ this.gridBuilder.drawHorizontalGrid({ ...base, numberOfTicks: this.yAxis.ticks });
746
+ }
747
+ }
748
+ /**
749
+ * Makes room in the margins for tick marks longer (or shorter) than D3's default. The default
750
+ * margins are sized around that default (`DEFAULT_TICK_SIZE`), and D3 places each tick label at
751
+ * tick length + padding, so a longer tick pushes its labels outward by exactly the difference -
752
+ * into space the margin doesn't have unless it grows by the same amount. Growing the margin here,
753
+ * *before* `initializeChartState()` measures the plot area, is what makes the chart shrink to fit
754
+ * its labels rather than pushing them off the edge of the SVG. A shorter tick hands the
755
+ * difference back to the plot area the same way.
756
+ *
757
+ * Horizontally that happens on its own: `initializeChartState()` derives `width` from the
758
+ * container minus the margins. Vertically it doesn't - `config.height` is the plot area, and
759
+ * the SVG is that plus the margins - so a taller bottom margin would grow the SVG instead. The
760
+ * delta is therefore also remembered in `reservedTickHeight` and taken back out of the plot
761
+ * height by `resolveHeight()`, keeping the chart's total footprint where the consumer put it.
762
+ *
763
+ * Called by `initializeAxisState()` right after `resetMargin()`, for the axes that will be
764
+ * drawn. Only the axis a tick size is given for is affected; `undefined` leaves that margin
765
+ * alone.
766
+ * @param xTickSize `tickSizeInner` for the bottom (x) axis, which lives in `margin.bottom`
767
+ * @param yTickSize `tickSizeInner` for the left (y) axis, which lives in `margin.left`
768
+ */
769
+ reserveTickSizeMargins(xTickSize, yTickSize) {
770
+ if (xTickSize !== undefined) {
771
+ const bottom = Math.max(0, this.margin.bottom + xTickSize - PcacChart.DEFAULT_TICK_SIZE);
772
+ this.reservedTickHeight = bottom - this.margin.bottom;
773
+ this.margin.bottom = bottom;
774
+ }
775
+ if (yTickSize !== undefined) {
776
+ this.margin.left = Math.max(0, this.margin.left + yTickSize - PcacChart.DEFAULT_TICK_SIZE);
777
+ }
454
778
  }
455
779
  /**
456
780
  * Shows the tooltip for a hovered datum, through the chart's `pcacTooltip` template when one is
@@ -512,10 +836,13 @@ class PcacChart {
512
836
  * @param config Chart specific configuration
513
837
  */
514
838
  resolveHeight(container, config) {
839
+ // See `reserveTickSizeMargins()`: room made for longer ticks comes out of the plot area, not
840
+ // added on to the SVG. Applies to the floor in the `heightFull` case too, for the same reason.
841
+ const height = Math.max(0, config.height - this.reservedTickHeight);
515
842
  if (!config.heightFull) {
516
- return config.height;
843
+ return height;
517
844
  }
518
- return Math.max(config.height, container.clientHeight - this.margin.top - this.margin.bottom);
845
+ return Math.max(height, container.clientHeight - this.margin.top - this.margin.bottom);
519
846
  }
520
847
  /**
521
848
  * True if the container's current raw layout width differs from the one measured by the last
@@ -575,12 +902,19 @@ class PcacChart {
575
902
  * In some cases, that state needs to be calculated based on the content that resides in that margin.
576
903
  * For example, labels on a horizontal bar chart are dynamic and such the margin needs to be calculated ahead of
577
904
  * chart axis construction.
905
+ *
906
+ * Measures with the y axis's own `tickSize` (the measured box spans the tick line as well as
907
+ * the label, so measuring at a different length would put the labels off by the difference)
908
+ * and adds its `axisLabelSpace()` back on top, since the measurement replaces whatever
909
+ * `initializeAxisState()` reserved for the label / sub labels.
578
910
  * @param chartElm Reference to SVG on dom
579
- * @param data Generic multi-dimensional PcacData structure
580
911
  * @param yScale D3 scale transformation object (d3.ScaleBand)
581
912
  */
582
- setHorizontalMarginsBasedOnContent(chartElm, data, yScale) {
913
+ setHorizontalMarginsBasedOnContent(chartElm, yScale) {
583
914
  const axisY = axisLeft(yScale).ticks(5);
915
+ if (this.yAxis.tickSize !== undefined) {
916
+ axisY.tickSizeInner(this.yAxis.tickSize);
917
+ }
584
918
  let max = 0;
585
919
  select$1(chartElm.nativeElement).append('g')
586
920
  .call(axisY)
@@ -596,8 +930,9 @@ class PcacChart {
596
930
  // double-counted it, and since `margin` persists on the builder between builds, the amount
597
931
  // double-counted grew on the next rebuild - the plot area came out narrower than the
598
932
  // container allowed and then shrank further after the first resize.
599
- this.width = this.width + this.margin.left - max;
600
- this.margin.left = max;
933
+ const left = max + axisLabelSpace(this.yAxis);
934
+ this.width = this.width + this.margin.left - left;
935
+ this.margin.left = left;
601
936
  }
602
937
  }
603
938
 
@@ -639,10 +974,7 @@ class BarHorizontalChartBuilder extends PcacChart {
639
974
  return;
640
975
  }
641
976
  this.config = JSON.parse(JSON.stringify(config));
642
- this.resetMargin();
643
- if (this.config.hideAxis) {
644
- this.adjustForHiddenAxis();
645
- }
977
+ this.initializeAxisState(this.config, 'x');
646
978
  if (!this.initializeChartState(chartElm, this.config)) {
647
979
  return;
648
980
  }
@@ -654,13 +986,6 @@ class BarHorizontalChartBuilder extends PcacChart {
654
986
  this.buildScales(chartElm, this.config);
655
987
  this.drawChart(chartElm, this.config);
656
988
  }
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
989
  buildScales(chartElm, config) {
665
990
  config.data[0].data.map((d) => {
666
991
  return d.value;
@@ -675,32 +1000,18 @@ class BarHorizontalChartBuilder extends PcacChart {
675
1000
  .padding(0.05)
676
1001
  .rangeRound([0, this.yScaleStacked.bandwidth()])
677
1002
  .domain(config.data[0].data.map((d) => d.key));
678
- this.setHorizontalMarginsBasedOnContent(chartElm, config.data, this.yScaleStacked);
1003
+ // The left margin is sized to the y axis's labels - unless there is no y axis to size it to
1004
+ if (!this.yAxis.hide) {
1005
+ this.setHorizontalMarginsBasedOnContent(chartElm, this.yScaleStacked);
1006
+ }
679
1007
  this.xScale.range([0, this.width]);
680
1008
  }
681
1009
  drawChart(chartElm, config) {
682
1010
  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
- }
1011
+ this.axisBuilder.drawAxis(this.axisBuilderConfig(this.xScale, this.yScaleStacked, config.tickFormat || PcacFormatEnum.None, PcacFormatEnum.None));
1012
+ this.drawGrids(this.xScale, this.yScaleStacked);
703
1013
  this.addGroups(config);
1014
+ this.axisBuilder.raiseAxes(this.svg);
704
1015
  }
705
1016
  addGroups(config) {
706
1017
  const groupsContainer = this.svg.append('g')
@@ -1033,14 +1344,11 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.1.6", ngImpor
1033
1344
  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
1345
  }], 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
1346
 
1036
- class PcacBarHorizontalChartConfig extends PcacChartConfig {
1347
+ class PcacBarHorizontalChartConfig extends PcacAxisChartConfig {
1037
1348
  domainMax = 100;
1038
- numberOfTicks = 5;
1039
1349
  isStacked = false;
1040
1350
  thresholds = [];
1041
1351
  tickFormat = PcacFormatEnum.None;
1042
- hideGrid = false;
1043
- hideAxis = false;
1044
1352
  spreadColorsPerGroup = false;
1045
1353
  colorOverride = new PcacBarHorizontalChartColorOverrideConfig();
1046
1354
  }
@@ -1064,13 +1372,11 @@ class BarVerticalChartBuilder extends PcacChart {
1064
1372
  if (!config?.data?.length) {
1065
1373
  return;
1066
1374
  }
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).
1375
+ // Shallow copy: initializeAxisState() rewrites `height` for hidden axes, and that must not
1376
+ // land on the consumer's own config object (it previously did, growing it by the margins on
1377
+ // first build).
1069
1378
  config = { ...config };
1070
- this.resetMargin();
1071
- if (config.hideAxis) {
1072
- this.adjustForHiddenAxis(config);
1073
- }
1379
+ this.initializeAxisState(config, 'y');
1074
1380
  if (!this.initializeChartState(chartElm, config)) {
1075
1381
  return;
1076
1382
  }
@@ -1082,26 +1388,6 @@ class BarVerticalChartBuilder extends PcacChart {
1082
1388
  this.buildScales(config);
1083
1389
  this.drawChart(chartElm, config);
1084
1390
  }
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
1391
  buildScales(config) {
1106
1392
  // `this.height`, not `config.height`: with `heightFull` on they differ (see
1107
1393
  // PcacChart.resolveHeight), and the axis, grid and bar baselines all use the resolved one.
@@ -1119,27 +1405,10 @@ class BarVerticalChartBuilder extends PcacChart {
1119
1405
  }
1120
1406
  drawChart(chartElm, config) {
1121
1407
  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
- }
1408
+ this.axisBuilder.drawAxis(this.axisBuilderConfig(this.xScaleStacked, this.yScale, PcacFormatEnum.None, config.tickFormat || PcacFormatEnum.None));
1409
+ this.drawGrids(this.xScaleStacked, this.yScale);
1142
1410
  this.addGroups(config);
1411
+ this.axisBuilder.raiseAxes(this.svg);
1143
1412
  }
1144
1413
  addGroups(config) {
1145
1414
  const groupsContainer = this.svg.append('g')
@@ -1390,14 +1659,11 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.1.6", ngImpor
1390
1659
  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
1660
  }], 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
1661
 
1393
- class PcacBarVerticalChartConfig extends PcacChartConfig {
1662
+ class PcacBarVerticalChartConfig extends PcacAxisChartConfig {
1394
1663
  domainMax = 100;
1395
- numberOfTicks = 5;
1396
1664
  isStacked = false;
1397
1665
  thresholds = [];
1398
1666
  tickFormat = PcacFormatEnum.None;
1399
- hideGrid = false;
1400
- hideAxis = false;
1401
1667
  spreadColorsPerGroup = false;
1402
1668
  colorOverride = new PcacBarVerticalChartColorOverrideConfig();
1403
1669
  }
@@ -1449,12 +1715,9 @@ class PcacPointImageConfig {
1449
1715
  maxWidth = 16;
1450
1716
  maxHeight = 16;
1451
1717
  }
1452
- class PcacLineAreaChartConfig extends PcacChartConfig {
1718
+ class PcacLineAreaChartConfig extends PcacAxisChartConfig {
1453
1719
  enableEffects = true;
1454
1720
  enableZoom = true;
1455
- numberOfTicks = 5;
1456
- hideGrid = false;
1457
- hideAxis = false;
1458
1721
  yFormat = PcacFormatEnum.DatasetLength;
1459
1722
  xFormat = PcacFormatEnum.DatasetLength;
1460
1723
  yDomainMax;
@@ -1741,14 +2004,8 @@ class PlaChartBuilder extends PcacChart {
1741
2004
  hide: false,
1742
2005
  data: []
1743
2006
  }));
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
- }
2007
+ // A hidden axis keeps 8px rather than 0 so a dot on the edge of the plot isn't clipped
2008
+ this.initializeAxisState(this.config, 'y', 8);
1752
2009
  if (!this.initializeChartState(chartElm, this.config)) {
1753
2010
  return;
1754
2011
  }
@@ -1770,15 +2027,7 @@ class PlaChartBuilder extends PcacChart {
1770
2027
  // Rescale x
1771
2028
  const newX = event.transform.rescaleX(this.scales.x);
1772
2029
  // 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
- });
2030
+ this.axisBuilder.drawXAxis(this.axisBuilderConfig(newX, this.scales.y, this.config.xFormat, this.config.yFormat));
1782
2031
  // Update lines/areas. Fresh generators against the rescaled x, so they go through the
1783
2032
  // same getXFormat() as the dots below - positioning by bare index here (which this used
1784
2033
  // to do, and which also overwrote the original generators' x accessor for good) is only
@@ -1795,6 +2044,14 @@ class PlaChartBuilder extends PcacChart {
1795
2044
  // points consecutively, shoving the second series' points off to the right on zoom.
1796
2045
  this.svg.selectAll('.dots').selectAll('.point')
1797
2046
  .attr('transform', (d, i) => this.pointTransform(d, i, newX));
2047
+ // The vertical grid hangs off the x ticks too, so redraw it against the rescaled x and
2048
+ // drop it back underneath everything (append puts it on top).
2049
+ this.svg.selectAll('.pcac-grid-vertical').remove();
2050
+ this.drawGrids(newX, this.scales.y, 'x');
2051
+ this.svg.selectAll('.pcac-grid-vertical').lower();
2052
+ // drawXAxis re-appends the x axis at the end of the group, above the dots; put them back
2053
+ // on top so a point on the baseline isn't covered (see drawChart's draw order).
2054
+ this.svg.selectAll('.dots').raise();
1798
2055
  });
1799
2056
  }
1800
2057
  this.drawChart(chartElm, this.config, type);
@@ -1803,27 +2060,8 @@ class PlaChartBuilder extends PcacChart {
1803
2060
  this.buildContainer(chartElm);
1804
2061
  this.attachZoomBehavior();
1805
2062
  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
- }
2063
+ this.axisBuilder.drawAxis(this.axisBuilderConfig(this.scales.x, this.scales.y, config.xFormat, config.yFormat));
2064
+ this.drawGrids(this.scales.x, this.scales.y);
1827
2065
  this.drawLineArea(config, type);
1828
2066
  if (config.enableEffects) {
1829
2067
  this.effectsBuilder.buildEffects({
@@ -1836,6 +2074,9 @@ class PlaChartBuilder extends PcacChart {
1836
2074
  y: this.scales.y
1837
2075
  });
1838
2076
  }
2077
+ // Axes above the lines/areas (so an axis line isn't covered by a series sitting at x = 0),
2078
+ // but the dots above the axes: a point on the baseline or the y axis stays whole.
2079
+ this.axisBuilder.raiseAxes(this.svg);
1839
2080
  this.drawDots(config);
1840
2081
  }
1841
2082
  createReusableClipPath() {
@@ -2271,7 +2512,6 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.1.6", ngImpor
2271
2512
  }], 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
2513
 
2273
2514
  class PcacPieChartConfig extends PcacChartConfig {
2274
- numberOfTicks = 5;
2275
2515
  }
2276
2516
 
2277
2517
  /*
@@ -2282,5 +2522,5 @@ class PcacPieChartConfig extends PcacChartConfig {
2282
2522
  * Generated bundle index. Do not edit.
2283
2523
  */
2284
2524
 
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 };
2525
+ 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
2526
  //# sourceMappingURL=pioneer-code-pioneer-charts.mjs.map