@swimlane/ngx-charts 25.0.2 → 25.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  ## HEAD (unreleased)
4
4
 
5
+ ## 25.0.3
6
+
7
+ - Fix: X-axis tick labels were clipped at the bottom of the chart during SSR, notably rotated labels in PDF exports
8
+ - Fix: Area chart Y-axis tick labels were clipped on the left during SSR when values were long
9
+
5
10
  ## 25.0.2
6
11
 
7
12
  - Fix: SSR issues stemming from Angular update
@@ -1562,6 +1562,7 @@ class XAxisTicksComponent {
1562
1562
  this.maxAllowedLength = 16;
1563
1563
  this.height = 0;
1564
1564
  this.approxHeight = 10;
1565
+ this.ssrHeight = 10;
1565
1566
  this.maxPossibleLengthForTickIfWrapped = 16;
1566
1567
  this.referenceLineLength = 0;
1567
1568
  }
@@ -1574,7 +1575,7 @@ class XAxisTicksComponent {
1574
1575
  updateDims() {
1575
1576
  if (!isPlatformBrowser(this.platformId)) {
1576
1577
  // for SSR, use approximate value instead of measured
1577
- this.dimensionsChanged.emit({ height: this.approxHeight });
1578
+ this.dimensionsChanged.emit({ height: this.ssrHeight });
1578
1579
  return;
1579
1580
  }
1580
1581
  const height = parseInt(this.ticksElement.nativeElement.getBoundingClientRect().height, 10);
@@ -1584,7 +1585,25 @@ class XAxisTicksComponent {
1584
1585
  setTimeout(() => this.updateDims());
1585
1586
  }
1586
1587
  }
1588
+ /**
1589
+ * Tick label height without DOM measurement. SSR serializes before the deferred `dimensionsChanged`
1590
+ * emission is applied, so charts seed their x-axis height from this during `update()`.
1591
+ */
1592
+ static approximateTicksHeight(options) {
1593
+ const ticks = new XAxisTicksComponent('server');
1594
+ for (const [key, value] of Object.entries(options)) {
1595
+ if (value !== undefined && value !== null) {
1596
+ ticks[key] = value;
1597
+ }
1598
+ }
1599
+ ticks.layout();
1600
+ return ticks.ssrHeight;
1601
+ }
1587
1602
  update() {
1603
+ this.layout();
1604
+ setTimeout(() => this.updateDims());
1605
+ }
1606
+ layout() {
1588
1607
  const scale = this.scale;
1589
1608
  this.adjustedScale = this.scale.bandwidth
1590
1609
  ? function (d) {
@@ -1658,7 +1677,26 @@ class XAxisTicksComponent {
1658
1677
  else {
1659
1678
  this.textAnchor = TextAnchor.Middle;
1660
1679
  }
1661
- setTimeout(() => this.updateDims());
1680
+ this.ssrHeight = this.getSsrHeight(angle);
1681
+ }
1682
+ /**
1683
+ * Estimated rendered height of the tick labels, matching what getBoundingClientRect reports in the
1684
+ * browser: the rotated text run plus the projected glyph height, or the wrapped line stack.
1685
+ */
1686
+ getSsrHeight(angle) {
1687
+ const lineHeight = 14;
1688
+ const charWidth = 7;
1689
+ let height = lineHeight;
1690
+ if (angle) {
1691
+ const radians = (Math.abs(angle) * Math.PI) / 180;
1692
+ const textWidth = this.maxTicksLength * charWidth;
1693
+ height = Math.ceil(Math.sin(radians) * textWidth + Math.cos(radians) * lineHeight);
1694
+ }
1695
+ else if (this.isWrapTicksSupported && this.ticks?.length) {
1696
+ const longestTick = this.ticks.reduce((earlier, current) => (current.length > earlier.length ? current : earlier), '');
1697
+ height = lineHeight * (this.tickChunks(longestTick).length || 1);
1698
+ }
1699
+ return Math.min(Math.max(height, this.approxHeight), 200);
1662
1700
  }
1663
1701
  setReferencelines() {
1664
1702
  this.refMin = this.adjustedScale(Math.min.apply(null, this.referenceLines.map(item => item.value)));
@@ -2464,6 +2502,23 @@ class YAxisTicksComponent {
2464
2502
  const labelLength = String(label).trim().length;
2465
2503
  return labelLength > limit ? Math.max(0, limit - 3) : limit;
2466
2504
  }
2505
+ /**
2506
+ * Tick label width without DOM measurement. SSR serializes before the tick component's
2507
+ * `dimensionsChanged` emission is applied, so charts seed their y-axis width from this during `update()`.
2508
+ */
2509
+ static approximateTicksWidth(options) {
2510
+ const ticks = new YAxisTicksComponent('server');
2511
+ ticks.orient = Orientation.Left;
2512
+ for (const [key, value] of Object.entries(options)) {
2513
+ if (value !== undefined && value !== null) {
2514
+ ticks[key] = value;
2515
+ }
2516
+ }
2517
+ ticks.ticks = ticks.getTicks();
2518
+ ticks.tickFormat = ticks.tickFormatting ?? ticks.scale.tickFormat(...ticks.tickArguments);
2519
+ ticks.resolveLayoutAxisWidth();
2520
+ return ticks.layoutAxisWidth;
2521
+ }
2467
2522
  static approximateTickLabelsWidth(labels, trimTicks = true, maxTickLength = 16, wrapTicks = true) {
2468
2523
  return YAxisTicksComponent.resolveAxisLayout(labels, trimTicks, maxTickLength, wrapTicks).yAxisWidth;
2469
2524
  }
@@ -5698,7 +5753,9 @@ class AreaChartComponent extends BaseChartComponent {
5698
5753
  }
5699
5754
  this.yDomain = this.getYDomain();
5700
5755
  this.seriesDomain = this.getSeriesDomain();
5756
+ this.seedYAxisWidthForSsr();
5701
5757
  this.xScale = this.getXScale(this.xDomain, this.dims.width);
5758
+ this.seedXAxisHeightForSsr();
5702
5759
  this.yScale = this.getYScale(this.yDomain, this.dims.height);
5703
5760
  this.updateTimeline();
5704
5761
  this.setColors();
@@ -5867,6 +5924,43 @@ class AreaChartComponent extends BaseChartComponent {
5867
5924
  this.yAxisWidth = width;
5868
5925
  this.update();
5869
5926
  }
5927
+ /** SSR serializes before the y-axis tick width emission lands, so reserve its width up front. */
5928
+ seedYAxisWidthForSsr() {
5929
+ if (!this.yAxis || !isPlatformServer(this.platformId)) {
5930
+ return;
5931
+ }
5932
+ const width = YAxisTicksComponent.approximateTicksWidth({
5933
+ scale: this.getYScale(this.yDomain, this.dims.height),
5934
+ height: this.dims.height,
5935
+ tickValues: this.yAxisTicks,
5936
+ tickFormatting: this.yAxisTickFormatting,
5937
+ trimTicks: this.trimYAxisTicks,
5938
+ maxTickLength: this.maxYAxisTickLength,
5939
+ wrapTicks: this.wrapTicks
5940
+ });
5941
+ const delta = width - this.yAxisWidth;
5942
+ this.dims.width = Math.max(0, this.dims.width - delta);
5943
+ this.dims.xOffset += delta;
5944
+ this.yAxisWidth = width;
5945
+ }
5946
+ /** SSR serializes before the deferred x-axis tick measurement lands, so reserve its height up front. */
5947
+ seedXAxisHeightForSsr() {
5948
+ if (!this.xAxis || !isPlatformServer(this.platformId)) {
5949
+ return;
5950
+ }
5951
+ const height = XAxisTicksComponent.approximateTicksHeight({
5952
+ scale: this.xScale,
5953
+ width: this.dims.width,
5954
+ tickValues: this.xAxisTicks,
5955
+ tickFormatting: this.xAxisTickFormatting,
5956
+ trimTicks: this.trimXAxisTicks,
5957
+ rotateTicks: this.rotateXAxisTicks,
5958
+ maxTickLength: this.maxXAxisTickLength,
5959
+ wrapTicks: this.wrapTicks
5960
+ });
5961
+ this.dims.height = Math.max(0, this.dims.height - (height - this.xAxisHeight));
5962
+ this.xAxisHeight = height;
5963
+ }
5870
5964
  updateXAxisHeight({ height }) {
5871
5965
  this.xAxisHeight = height;
5872
5966
  this.update();
@@ -6315,7 +6409,9 @@ class AreaChartNormalizedComponent extends BaseChartComponent {
6315
6409
  this.xDomain = this.filteredDomain;
6316
6410
  }
6317
6411
  this.seriesDomain = this.getSeriesDomain();
6412
+ this.seedYAxisWidthForSsr();
6318
6413
  this.xScale = this.getXScale(this.xDomain, this.dims.width);
6414
+ this.seedXAxisHeightForSsr();
6319
6415
  this.yScale = this.getYScale(this.yDomain, this.dims.height);
6320
6416
  for (let i = 0; i < this.xSet.length; i++) {
6321
6417
  const val = this.xSet[i];
@@ -6490,6 +6586,43 @@ class AreaChartNormalizedComponent extends BaseChartComponent {
6490
6586
  this.yAxisWidth = width;
6491
6587
  this.update();
6492
6588
  }
6589
+ /** SSR serializes before the y-axis tick width emission lands, so reserve its width up front. */
6590
+ seedYAxisWidthForSsr() {
6591
+ if (!this.yAxis || !isPlatformServer(this.platformId)) {
6592
+ return;
6593
+ }
6594
+ const width = YAxisTicksComponent.approximateTicksWidth({
6595
+ scale: this.getYScale(this.yDomain, this.dims.height),
6596
+ height: this.dims.height,
6597
+ tickValues: this.yAxisTicks,
6598
+ tickFormatting: this.yAxisTickFormatting,
6599
+ trimTicks: this.trimYAxisTicks,
6600
+ maxTickLength: this.maxYAxisTickLength,
6601
+ wrapTicks: this.wrapTicks
6602
+ });
6603
+ const delta = width - this.yAxisWidth;
6604
+ this.dims.width = Math.max(0, this.dims.width - delta);
6605
+ this.dims.xOffset += delta;
6606
+ this.yAxisWidth = width;
6607
+ }
6608
+ /** SSR serializes before the deferred x-axis tick measurement lands, so reserve its height up front. */
6609
+ seedXAxisHeightForSsr() {
6610
+ if (!this.xAxis || !isPlatformServer(this.platformId)) {
6611
+ return;
6612
+ }
6613
+ const height = XAxisTicksComponent.approximateTicksHeight({
6614
+ scale: this.xScale,
6615
+ width: this.dims.width,
6616
+ tickValues: this.xAxisTicks,
6617
+ tickFormatting: this.xAxisTickFormatting,
6618
+ trimTicks: this.trimXAxisTicks,
6619
+ rotateTicks: this.rotateXAxisTicks,
6620
+ maxTickLength: this.maxXAxisTickLength,
6621
+ wrapTicks: this.wrapTicks
6622
+ });
6623
+ this.dims.height = Math.max(0, this.dims.height - (height - this.xAxisHeight));
6624
+ this.xAxisHeight = height;
6625
+ }
6493
6626
  updateXAxisHeight({ height }) {
6494
6627
  this.xAxisHeight = height;
6495
6628
  this.update();
@@ -6916,7 +7049,9 @@ class AreaChartStackedComponent extends BaseChartComponent {
6916
7049
  }
6917
7050
  this.yDomain = this.getYDomain();
6918
7051
  this.seriesDomain = this.getSeriesDomain();
7052
+ this.seedYAxisWidthForSsr();
6919
7053
  this.xScale = this.getXScale(this.xDomain, this.dims.width);
7054
+ this.seedXAxisHeightForSsr();
6920
7055
  this.yScale = this.getYScale(this.yDomain, this.dims.height);
6921
7056
  for (let i = 0; i < this.xSet.length; i++) {
6922
7057
  const val = this.xSet[i];
@@ -7100,6 +7235,43 @@ class AreaChartStackedComponent extends BaseChartComponent {
7100
7235
  this.yAxisWidth = width;
7101
7236
  this.update();
7102
7237
  }
7238
+ /** SSR serializes before the y-axis tick width emission lands, so reserve its width up front. */
7239
+ seedYAxisWidthForSsr() {
7240
+ if (!this.yAxis || !isPlatformServer(this.platformId)) {
7241
+ return;
7242
+ }
7243
+ const width = YAxisTicksComponent.approximateTicksWidth({
7244
+ scale: this.getYScale(this.yDomain, this.dims.height),
7245
+ height: this.dims.height,
7246
+ tickValues: this.yAxisTicks,
7247
+ tickFormatting: this.yAxisTickFormatting,
7248
+ trimTicks: this.trimYAxisTicks,
7249
+ maxTickLength: this.maxYAxisTickLength,
7250
+ wrapTicks: this.wrapTicks
7251
+ });
7252
+ const delta = width - this.yAxisWidth;
7253
+ this.dims.width = Math.max(0, this.dims.width - delta);
7254
+ this.dims.xOffset += delta;
7255
+ this.yAxisWidth = width;
7256
+ }
7257
+ /** SSR serializes before the deferred x-axis tick measurement lands, so reserve its height up front. */
7258
+ seedXAxisHeightForSsr() {
7259
+ if (!this.xAxis || !isPlatformServer(this.platformId)) {
7260
+ return;
7261
+ }
7262
+ const height = XAxisTicksComponent.approximateTicksHeight({
7263
+ scale: this.xScale,
7264
+ width: this.dims.width,
7265
+ tickValues: this.xAxisTicks,
7266
+ tickFormatting: this.xAxisTickFormatting,
7267
+ trimTicks: this.trimXAxisTicks,
7268
+ rotateTicks: this.rotateXAxisTicks,
7269
+ maxTickLength: this.maxXAxisTickLength,
7270
+ wrapTicks: this.wrapTicks
7271
+ });
7272
+ this.dims.height = Math.max(0, this.dims.height - (height - this.xAxisHeight));
7273
+ this.xAxisHeight = height;
7274
+ }
7103
7275
  updateXAxisHeight({ height }) {
7104
7276
  this.xAxisHeight = height;
7105
7277
  this.update();