@opendata-ai/openchart-engine 6.1.0 → 6.1.2

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/dist/index.js CHANGED
@@ -7226,6 +7226,23 @@ function computeDimensions(spec, options, legendLayout, theme, strategy) {
7226
7226
  }
7227
7227
  }
7228
7228
  }
7229
+ if (spec.annotations.length > 0 && encoding.x) {
7230
+ const xField = encoding.x.field;
7231
+ let maxX;
7232
+ for (const row of spec.data) {
7233
+ const v = row[xField];
7234
+ if (v != null && (maxX == null || String(v) >= String(maxX))) maxX = v;
7235
+ }
7236
+ if (maxX != null) {
7237
+ const maxXStr = String(maxX);
7238
+ for (const ann of spec.annotations) {
7239
+ if (ann.type === "text" && String(ann.x) === maxXStr) {
7240
+ const textWidth = estimateTextWidth8(ann.text, ann.fontSize ?? 11, ann.fontWeight ?? 600);
7241
+ margins.right = Math.max(margins.right, padding + textWidth + 12);
7242
+ }
7243
+ }
7244
+ }
7245
+ }
7229
7246
  if (encoding.y && !isRadial) {
7230
7247
  if (spec.markType === "bar" || spec.markType === "circle" || encoding.y.type === "nominal" || encoding.y.type === "ordinal") {
7231
7248
  const yField = encoding.y.field;
@@ -7240,18 +7257,28 @@ function computeDimensions(spec, options, legendLayout, theme, strategy) {
7240
7257
  }
7241
7258
  } else if (encoding.y.type === "quantitative" || encoding.y.type === "temporal") {
7242
7259
  const yField = encoding.y.field;
7260
+ const yAxisFormat = encoding.y.axis?.format;
7243
7261
  let maxAbsVal = 0;
7244
7262
  for (const row of spec.data) {
7245
7263
  const v = Number(row[yField]);
7246
7264
  if (Number.isFinite(v) && Math.abs(v) > maxAbsVal) maxAbsVal = Math.abs(v);
7247
7265
  }
7248
7266
  let sampleLabel;
7249
- if (maxAbsVal >= 1e9) sampleLabel = "1.5B";
7250
- else if (maxAbsVal >= 1e6) sampleLabel = "1.5M";
7251
- else if (maxAbsVal >= 1e3) sampleLabel = "1.5K";
7252
- else if (maxAbsVal >= 100) sampleLabel = "100";
7253
- else if (maxAbsVal >= 10) sampleLabel = "10";
7254
- else sampleLabel = "0.0";
7267
+ if (yAxisFormat) {
7268
+ try {
7269
+ const fmt = format(yAxisFormat);
7270
+ sampleLabel = fmt(maxAbsVal);
7271
+ } catch {
7272
+ sampleLabel = String(maxAbsVal);
7273
+ }
7274
+ } else {
7275
+ if (maxAbsVal >= 1e9) sampleLabel = "1.5B";
7276
+ else if (maxAbsVal >= 1e6) sampleLabel = "1.5M";
7277
+ else if (maxAbsVal >= 1e3) sampleLabel = "1.5K";
7278
+ else if (maxAbsVal >= 100) sampleLabel = "100";
7279
+ else if (maxAbsVal >= 10) sampleLabel = "10";
7280
+ else sampleLabel = "0.0";
7281
+ }
7255
7282
  const negPrefix = spec.data.some((r) => Number(r[yField]) < 0) ? "-" : "";
7256
7283
  const labelEst = negPrefix + sampleLabel;
7257
7284
  const labelWidth = estimateTextWidth8(
@@ -7262,7 +7289,8 @@ function computeDimensions(spec, options, legendLayout, theme, strategy) {
7262
7289
  margins.left = Math.max(margins.left, padding + labelWidth + 10);
7263
7290
  }
7264
7291
  }
7265
- if (encoding.y?.axis && encoding.y.axis.label && !isRadial) {
7292
+ const yAxis = encoding.y?.axis;
7293
+ if (yAxis && (yAxis.title || yAxis.label) && !isRadial) {
7266
7294
  const rotatedLabelMargin = 45 + Math.ceil(theme.fonts.sizes.body / 2) + 4;
7267
7295
  margins.left = Math.max(margins.left, padding + rotatedLabelMargin);
7268
7296
  }