@swimlane/ngx-charts 24.0.0-alpha.1 → 24.0.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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  ## HEAD (unreleased)
4
4
 
5
+ ## 24.0.1
6
+
7
+ - Fix: Y-axis labels were cut off, misaligned when server-side rendered.
8
+
9
+ ## 24.0.0
10
+
11
+ - Fix: Area Charts were not server-side renderable
12
+
5
13
  ## 24.0.0-alpha.1
6
14
 
7
15
  - Fix: Area Charts were not server-side renderable
@@ -2066,13 +2066,19 @@ class YAxisTicksComponent {
2066
2066
  this.update();
2067
2067
  }
2068
2068
  ngAfterViewInit() {
2069
- setTimeout(() => this.updateDims());
2069
+ // Browser: measure after paint. SSR width is applied synchronously in update().
2070
+ if (isPlatformBrowser(this.platformId)) {
2071
+ setTimeout(() => this.updateDims());
2072
+ }
2070
2073
  }
2071
2074
  updateDims() {
2072
2075
  if (!isPlatformBrowser(this.platformId)) {
2073
- // for SSR, use approximate value instead of measured
2074
- this.width = this.getApproximateAxisWidth();
2075
- this.dimensionsChanged.emit({ width: this.width });
2076
+ // SSR: approximate — no DOM metrics available
2077
+ const width = this.getApproximateAxisWidth();
2078
+ if (width > 0 && width !== this.width) {
2079
+ this.width = width;
2080
+ this.dimensionsChanged.emit({ width: this.width });
2081
+ }
2076
2082
  return;
2077
2083
  }
2078
2084
  const width = parseInt(this.ticksElement.nativeElement.getBoundingClientRect().width, 10);
@@ -2161,7 +2167,13 @@ class YAxisTicksComponent {
2161
2167
  break;
2162
2168
  default:
2163
2169
  }
2164
- setTimeout(() => this.updateDims());
2170
+ // SSR must reserve axis width before HTML serialize (no deferred setTimeout).
2171
+ if (!isPlatformBrowser(this.platformId)) {
2172
+ this.updateDims();
2173
+ }
2174
+ else {
2175
+ setTimeout(() => this.updateDims());
2176
+ }
2165
2177
  }
2166
2178
  setReferencelines() {
2167
2179
  this.refMin = this.adjustedScale(Math.min.apply(null, this.referenceLines.map(item => item.value)));
@@ -2202,10 +2214,21 @@ class YAxisTicksComponent {
2202
2214
  tickTrim(label) {
2203
2215
  return this.trimTicks ? trimLabel(label, this.maxTickLength) : label;
2204
2216
  }
2217
+ /** Approx px width for SSR / pre-measure seeding. */
2218
+ static { this.APPROX_CHAR_WIDTH = 7; }
2219
+ static approximateTickLabelsWidth(labels, trimTicks = true, maxTickLength = 16) {
2220
+ if (!labels?.length) {
2221
+ return 0;
2222
+ }
2223
+ const maxChars = Math.max(0, ...labels.map(label => (trimTicks ? trimLabel(label, maxTickLength) : String(label)).length));
2224
+ return maxChars * YAxisTicksComponent.APPROX_CHAR_WIDTH;
2225
+ }
2205
2226
  getApproximateAxisWidth() {
2206
- const maxChars = Math.max(...this.ticks.map(t => this.tickTrim(this.tickFormat(t)).length));
2207
- const charWidth = 7;
2208
- return maxChars * charWidth;
2227
+ if (!this.ticks?.length || !this.tickFormat) {
2228
+ return 0;
2229
+ }
2230
+ const labels = this.ticks.map(t => this.tickTrim(this.tickFormat(t)));
2231
+ return YAxisTicksComponent.approximateTickLabelsWidth(labels, false, this.maxTickLength);
2209
2232
  }
2210
2233
  tickChunks(label) {
2211
2234
  if (label.toString().length > this.maxTickLength && this.scale.bandwidth) {
@@ -2430,6 +2453,8 @@ class YAxisComponent {
2430
2453
  this.tickStroke = '#CCC';
2431
2454
  this.strokeWidth = 1;
2432
2455
  this.padding = 5;
2456
+ /** Last tick-label width emitted to the parent chart (not conflated with labelOffset). */
2457
+ this.emittedTickWidth = null;
2433
2458
  }
2434
2459
  ngOnChanges(changes) {
2435
2460
  this.update();
@@ -2448,18 +2473,18 @@ class YAxisComponent {
2448
2473
  }
2449
2474
  }
2450
2475
  emitTicksWidth({ width }) {
2451
- if (width !== this.labelOffset && this.yOrient === Orientation.Right) {
2452
- this.labelOffset = width + this.labelOffset;
2453
- setTimeout(() => {
2454
- this.dimensionsChanged.emit({ width });
2455
- }, 0);
2476
+ if (width == null || width === this.emittedTickWidth) {
2477
+ return;
2456
2478
  }
2457
- else if (width !== this.labelOffset) {
2479
+ this.emittedTickWidth = width;
2480
+ if (this.yOrient === Orientation.Right) {
2481
+ this.labelOffset = width + 65;
2482
+ }
2483
+ else {
2458
2484
  this.labelOffset = width;
2459
- setTimeout(() => {
2460
- this.dimensionsChanged.emit({ width });
2461
- }, 0);
2462
2485
  }
2486
+ // Sync emit so SSR HTML serializes with reserved y-axis width (no setTimeout).
2487
+ this.dimensionsChanged.emit({ width });
2463
2488
  }
2464
2489
  static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.12", ngImport: i0, type: YAxisComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
2465
2490
  static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "21.2.12", type: YAxisComponent, isStandalone: false, selector: "g[ngx-charts-y-axis]", inputs: { yScale: "yScale", dims: "dims", trimTicks: "trimTicks", maxTickLength: "maxTickLength", tickFormatting: "tickFormatting", ticks: "ticks", showGridLines: "showGridLines", showLabel: "showLabel", labelText: "labelText", yAxisTickCount: "yAxisTickCount", yOrient: "yOrient", referenceLines: "referenceLines", showRefLines: "showRefLines", showRefLabels: "showRefLabels", yAxisOffset: "yAxisOffset", wrapTicks: "wrapTicks" }, outputs: { dimensionsChanged: "dimensionsChanged" }, viewQueries: [{ propertyName: "ticksComponent", first: true, predicate: YAxisTicksComponent, descendants: true }], usesOnChanges: true, ngImport: i0, template: `
@@ -7866,6 +7891,8 @@ class BarHorizontalComponent extends BaseChartComponent {
7866
7891
  this.dataLabelMaxWidth = { negative: 0, positive: 0 };
7867
7892
  }
7868
7893
  this.margin = [10, 20 + this.dataLabelMaxWidth.positive, 10, 20 + this.dataLabelMaxWidth.negative];
7894
+ // Seed axis width before first measure so SSR/print HTML reserves label space.
7895
+ this.seedYAxisWidthFromResults();
7869
7896
  this.dims = calculateViewDimensions({
7870
7897
  width: this.width,
7871
7898
  height: this.height,
@@ -7892,6 +7919,14 @@ class BarHorizontalComponent extends BaseChartComponent {
7892
7919
  refLines.forEach(line => parent.appendChild(line));
7893
7920
  }
7894
7921
  }
7922
+ /** Approx Y-axis width from category labels when not yet measured (SSR-safe). */
7923
+ seedYAxisWidthFromResults() {
7924
+ if (!this.yAxis || this.yAxisWidth > 0 || !this.results?.length) {
7925
+ return;
7926
+ }
7927
+ const labels = this.results.map(d => formatLabel(d.name ?? d.label));
7928
+ this.yAxisWidth = YAxisTicksComponent.approximateTickLabelsWidth(labels, this.trimYAxisTicks !== false, this.maxYAxisTickLength ?? 16);
7929
+ }
7895
7930
  getXScale() {
7896
7931
  this.xDomain = this.getXDomain();
7897
7932
  const scale = scaleLinear().range([0, this.dims.width]).domain(this.xDomain);
@@ -7944,10 +7979,16 @@ class BarHorizontalComponent extends BaseChartComponent {
7944
7979
  return opts;
7945
7980
  }
7946
7981
  updateYAxisWidth({ width }) {
7982
+ if (width === this.yAxisWidth) {
7983
+ return;
7984
+ }
7947
7985
  this.yAxisWidth = width;
7948
7986
  this.update();
7949
7987
  }
7950
7988
  updateXAxisHeight({ height }) {
7989
+ if (height === this.xAxisHeight) {
7990
+ return;
7991
+ }
7951
7992
  this.xAxisHeight = height;
7952
7993
  this.update();
7953
7994
  }