@opendata-ai/openchart-engine 9.2.2 → 9.3.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.
package/dist/index.d.ts CHANGED
@@ -290,7 +290,7 @@ interface NormalizedChartSpec {
290
290
  autoThin: boolean;
291
291
  compactAnnotationLimit?: number | null;
292
292
  theme: ThemeConfig;
293
- /** Whether the OpenData watermark is enabled. */
293
+ /** Whether the OpenChart watermark is enabled. */
294
294
  watermark: boolean;
295
295
  /** Series names to hide from rendering. */
296
296
  hiddenSeries: string[];
package/dist/index.js CHANGED
@@ -6781,12 +6781,17 @@ function interpolateInDomain(numValue, domain, positionOf) {
6781
6781
  const t = (numValue - sorted[lower].n) / (sorted[upper].n - sorted[lower].n);
6782
6782
  return lowerPos + t * (upperPos - lowerPos);
6783
6783
  }
6784
+ var MIN_EPOCH_MS = 1e4;
6785
+ function parseTemporalAnchor(value) {
6786
+ if (typeof value === "number" && Math.abs(value) >= MIN_EPOCH_MS) return new Date(value);
6787
+ return new Date(String(value));
6788
+ }
6784
6789
  function resolvePosition(value, scale) {
6785
6790
  if (!scale) return null;
6786
6791
  const s = scale.scale;
6787
6792
  const type = scale.type;
6788
6793
  if (type === "time" || type === "utc") {
6789
- const date = new Date(String(value));
6794
+ const date = parseTemporalAnchor(value);
6790
6795
  if (Number.isNaN(date.getTime())) return null;
6791
6796
  return s(date);
6792
6797
  }
@@ -15216,40 +15221,25 @@ function applyTemporalBarInterval(scale, spec, data) {
15216
15221
  new Date(+end + (+interval.offset(end) - +end) / 2)
15217
15222
  ]);
15218
15223
  }
15224
+ var POSITION_MARKS = /* @__PURE__ */ new Set(["point", "line", "beeswarm", "range", "text"]);
15225
+ function unanchoredChannels(markType, encoding) {
15226
+ if (POSITION_MARKS.has(markType)) return ["x", "y"];
15227
+ if (markType === "area") return ["x"];
15228
+ if (isBinnedBarEncoding(markType, encoding)) return ["x"];
15229
+ return [];
15230
+ }
15231
+ function withPositionZeroDefaults(markType, encoding) {
15232
+ let result = encoding;
15233
+ for (const axis of unanchoredChannels(markType, encoding)) {
15234
+ const channel = encoding[axis];
15235
+ if (channel?.type !== "quantitative" || channel.scale?.zero !== void 0) continue;
15236
+ result = { ...result, [axis]: { ...channel, scale: { ...channel.scale, zero: false } } };
15237
+ }
15238
+ return result;
15239
+ }
15219
15240
  function computeScales(spec, chartArea, data) {
15220
15241
  const result = {};
15221
- const encoding = spec.encoding;
15222
- if (spec.markType === "point" || spec.markType === "line" || spec.markType === "beeswarm" || spec.markType === "range" || spec.markType === "text") {
15223
- if (encoding.x?.type === "quantitative" && encoding.x.scale?.zero === void 0) {
15224
- if (!encoding.x.scale) {
15225
- encoding.x.scale = { zero: false };
15226
- } else {
15227
- encoding.x.scale.zero = false;
15228
- }
15229
- }
15230
- if (encoding.y?.type === "quantitative" && encoding.y.scale?.zero === void 0) {
15231
- if (!encoding.y.scale) {
15232
- encoding.y.scale = { zero: false };
15233
- } else {
15234
- encoding.y.scale.zero = false;
15235
- }
15236
- }
15237
- }
15238
- if (spec.markType === "area" && encoding.x?.type === "quantitative" && encoding.x.scale?.zero === void 0) {
15239
- if (!encoding.x.scale) {
15240
- encoding.x.scale = { zero: false };
15241
- } else {
15242
- encoding.x.scale.zero = false;
15243
- }
15244
- }
15245
- const binnedX = isBinnedBarEncoding(spec.markType, encoding) ? encoding.x : void 0;
15246
- if (binnedX && binnedX.scale?.zero === void 0) {
15247
- if (!binnedX.scale) {
15248
- binnedX.scale = { zero: false };
15249
- } else {
15250
- binnedX.scale.zero = false;
15251
- }
15252
- }
15242
+ const encoding = withPositionZeroDefaults(spec.markType, spec.encoding);
15253
15243
  if (encoding.x) {
15254
15244
  let xData = data;
15255
15245
  let xChannel = encoding.x;
@@ -22784,15 +22774,9 @@ function computeSharedDomains(leaves, options) {
22784
22774
  (leaf) => buildRenderSpec(prepareChartSpec(leaf, { ...options, onWarn: () => {
22785
22775
  } }).chartSpec)
22786
22776
  );
22787
- const domains = prepared.map((spec) => {
22788
- const encoding = { ...spec.encoding };
22789
- for (const axis of ["x", "y"]) {
22790
- const channel = encoding[axis];
22791
- if (channel)
22792
- encoding[axis] = { ...channel, ...channel.scale ? { scale: { ...channel.scale } } : {} };
22793
- }
22794
- return computeScales({ ...spec, encoding }, { x: 0, y: 0, width: 1, height: 1 }, spec.data);
22795
- });
22777
+ const domains = prepared.map(
22778
+ (spec) => computeScales(spec, { x: 0, y: 0, width: 1, height: 1 }, spec.data)
22779
+ );
22796
22780
  for (const channel of ["x", "y"]) {
22797
22781
  const explicitIndex = leaves.findIndex((leaf) => leaf.encoding?.[channel]?.scale?.domain);
22798
22782
  const explicit = leaves[explicitIndex]?.encoding?.[channel];
@@ -24286,6 +24270,7 @@ import {
24286
24270
  estimateTextWidth as estimateTextWidth30,
24287
24271
  formatNumber as formatNumber5,
24288
24272
  getBreakpoint as getBreakpoint4,
24273
+ getHeightClass as getHeightClass5,
24289
24274
  HPAD_COMPACT_FRACTION as HPAD_COMPACT_FRACTION4,
24290
24275
  HPAD_COMPACT_MIN as HPAD_COMPACT_MIN4,
24291
24276
  mergeThemeConfig as mergeThemeConfig5,
@@ -26524,6 +26509,7 @@ function captureGeoLabelGeometries(requests, projection, limits = {}) {
26524
26509
  }
26525
26510
 
26526
26511
  // src/geo/layout.ts
26512
+ var MIN_GEO_MAP_HEIGHT = 40;
26527
26513
  function geoMapFullArea(width, height, padding, topHeight, bottomHeight, chromeLayout) {
26528
26514
  return {
26529
26515
  x: padding,
@@ -26532,6 +26518,23 @@ function geoMapFullArea(width, height, padding, topHeight, bottomHeight, chromeL
26532
26518
  height: chromeLayout === "grow" ? height - padding * 2 : height - topHeight - bottomHeight - padding * 2
26533
26519
  };
26534
26520
  }
26521
+ function fitGeoMapChrome(chromeAt, areaFor, canReduce, onReduced) {
26522
+ let chrome = chromeAt("full");
26523
+ let area3 = areaFor(chrome);
26524
+ if (!canReduce) return { chrome, area: area3 };
26525
+ let reducedTo;
26526
+ for (const mode of ["compact", "hidden"]) {
26527
+ if (area3.height >= MIN_GEO_MAP_HEIGHT) break;
26528
+ const next = chromeAt(mode);
26529
+ const nextArea = areaFor(next);
26530
+ if (nextArea.height <= area3.height) continue;
26531
+ chrome = next;
26532
+ area3 = nextArea;
26533
+ reducedTo = mode;
26534
+ }
26535
+ if (reducedTo) onReduced(reducedTo);
26536
+ return { chrome, area: area3 };
26537
+ }
26535
26538
  function reserveGeoMapArea(fullArea, legendBlockHeight, position) {
26536
26539
  const legendReserveGap = legendBlockHeight > 0 ? 8 : 0;
26537
26540
  return {
@@ -26642,7 +26645,20 @@ function planGeoMapFrame(fullArea, legendBlockHeight, bandPosition, authoredPosi
26642
26645
  message: "Inline legends do not apply to maps; using the top band."
26643
26646
  });
26644
26647
  }
26645
- return { ...reserveGeoMapArea(fullArea, legendBlockHeight, bandPosition), diagnostics };
26648
+ const band = reserveGeoMapArea(fullArea, legendBlockHeight, bandPosition);
26649
+ if (legendBlockHeight > 0 && band.area.height < MIN_GEO_MAP_HEIGHT) {
26650
+ diagnostics.push({
26651
+ code: "MAP_LEGEND_DROPPED",
26652
+ severity: "info",
26653
+ status: "failed",
26654
+ path: "/legend",
26655
+ viewport,
26656
+ origin: "default",
26657
+ message: `The legend would leave the map under ${MIN_GEO_MAP_HEIGHT}px of height; drawing the map without it.`
26658
+ });
26659
+ return { ...reserveGeoMapArea(fullArea, 0, bandPosition), legendDropped: true, diagnostics };
26660
+ }
26661
+ return { ...band, diagnostics };
26646
26662
  }
26647
26663
  function measureGeoContinuousKey(options) {
26648
26664
  const { content, labelStyle, measureText, title, noData } = options;
@@ -27641,7 +27657,9 @@ function compileGeoMapImpl(spec, options, scope) {
27641
27657
  emitSpecWarnings([...sugarWarnings, ...warnings], options.onWarn);
27642
27658
  const topology = validateGeoFeatures(mapSpec.geo);
27643
27659
  const rawWatermark = spec.watermark;
27644
- const watermark = rawWatermark !== void 0 ? mapSpec.watermark : options.watermark ?? true;
27660
+ const heightClass = getHeightClass5(options.height);
27661
+ const defaultWatermark = heightClass !== "minimal" && heightClass !== "cramped";
27662
+ const watermark = rawWatermark !== void 0 ? mapSpec.watermark : options.watermark ?? defaultWatermark;
27645
27663
  const mergedThemeConfig = mergeThemeConfig5(mapSpec.theme, options.theme);
27646
27664
  const lightTheme = resolveTheme5(mergedThemeConfig);
27647
27665
  let theme = lightTheme;
@@ -27650,41 +27668,63 @@ function compileGeoMapImpl(spec, options, scope) {
27650
27668
  }
27651
27669
  const isDarkMode = options.darkMode;
27652
27670
  const padding = getBreakpoint4(options.width) === "compact" ? Math.max(HPAD_COMPACT_MIN4, Math.round(theme.spacing.padding * HPAD_COMPACT_FRACTION4)) : theme.spacing.padding;
27653
- const chrome = computeChrome5(
27654
- mapSpec.chrome,
27655
- theme,
27656
- options.width,
27657
- options.measureText,
27658
- "full",
27659
- padding,
27660
- watermark
27661
- );
27662
27671
  const chromeLayout = resolveChromeLayout(
27663
27672
  spec,
27664
27673
  options
27665
27674
  );
27666
- let fullArea = geoMapFullArea(
27667
- options.width,
27668
- options.height,
27669
- padding,
27670
- chrome.topHeight,
27671
- chrome.bottomHeight,
27672
- chromeLayout
27675
+ const { chrome, area: chromeArea } = fitGeoMapChrome(
27676
+ (mode) => computeChrome5(
27677
+ mapSpec.chrome,
27678
+ theme,
27679
+ options.width,
27680
+ options.measureText,
27681
+ mode,
27682
+ padding,
27683
+ watermark,
27684
+ 0,
27685
+ // The cramped 100-199px range shrinks the default title, as on charts
27686
+ // (layout/plan.ts), so a tile keeps its label instead of losing the map.
27687
+ heightClass === "cramped"
27688
+ ),
27689
+ (resolved) => geoMapFullArea(
27690
+ options.width,
27691
+ options.height,
27692
+ padding,
27693
+ resolved.topHeight,
27694
+ resolved.bottomHeight,
27695
+ chromeLayout
27696
+ ),
27697
+ chromeLayout !== "grow" && !!mapSpec.chrome,
27698
+ (mode) => options.onDiagnostic?.({
27699
+ code: "MAP_CHROME_REDUCED",
27700
+ severity: "info",
27701
+ status: "failed",
27702
+ path: "/chrome",
27703
+ viewport: { width: options.width, height: options.height },
27704
+ origin: "author",
27705
+ message: `Full chrome would leave the map under ${MIN_GEO_MAP_HEIGHT}px of height; using ${mode} chrome.`
27706
+ })
27673
27707
  );
27708
+ let fullArea = chromeArea;
27674
27709
  if ((fullArea.width <= 0 || fullArea.height <= 0) && mapSpec.geo.insets?.length)
27675
27710
  throw new GeoViewGeometryError(
27676
27711
  "GEO_INSET_SPACE",
27677
27712
  "No positive geographic frame is available for the authored insets."
27678
27713
  );
27679
- if ((fullArea.width <= 0 || fullArea.height <= 0) && !hasGeoLabels(mapSpec)) {
27680
- return emptyLayout4(chrome, theme, options, watermark);
27681
- }
27682
27714
  const viewsEnabled = mapSpec.geo.objectKey !== void 0 || mapSpec.geo.fitObjectKey !== void 0 || mapSpec.geo.sourcePlane !== void 0 || !!mapSpec.geo.detailLayers?.length || !!mapSpec.geo.insets?.length;
27683
27715
  const viewSources = viewsEnabled ? resolveGeoViewSources(mapSpec.geo) : void 0;
27684
27716
  const objectKey = viewSources?.primary.objectKey ?? Object.keys(topology.objects)[0];
27685
27717
  const geoCollection = viewSources?.primary.collection ?? topoFeature2(topology, topology.objects[objectKey]);
27686
27718
  const insetSelection = viewSources ? selectGeoInsetFeatures(geoCollection.features, mapSpec.geo.insets ?? []) : void 0;
27687
27719
  const geoFeatures = geoCollection.features;
27720
+ const emptyAltText = describeGeoMap(
27721
+ geoFeatures.length,
27722
+ mapSpec.encoding.color,
27723
+ mapSpec.points?.data.length ?? 0
27724
+ );
27725
+ if ((fullArea.width <= 0 || fullArea.height <= 0) && !hasGeoLabels(mapSpec)) {
27726
+ return emptyLayout4(chrome, theme, options, watermark, emptyAltText);
27727
+ }
27688
27728
  const compileWarnings = [];
27689
27729
  let joinWarnings = [];
27690
27730
  let joined = /* @__PURE__ */ new Map();
@@ -27728,7 +27768,7 @@ function compileGeoMapImpl(spec, options, scope) {
27728
27768
  viewport: { width: options.width, height: options.height }
27729
27769
  });
27730
27770
  const emptyWithLabels = () => {
27731
- const empty = emptyLayout4(chrome, theme, options, watermark);
27771
+ const empty = emptyLayout4(chrome, theme, options, watermark, emptyAltText);
27732
27772
  if (preparedLabels) {
27733
27773
  labelDiagnostic(
27734
27774
  "MAP_LABEL_FRAME_UNFIT",
@@ -28095,6 +28135,10 @@ function compileGeoMapImpl(spec, options, scope) {
28095
28135
  );
28096
28136
  for (const diagnostic of frame.diagnostics) options.onDiagnostic?.(diagnostic);
28097
28137
  const sideFrame = frame.sideLegend ? { area: frame.area, legend: frame.sideLegend } : null;
28138
+ const bandLegendDropped = frame.legendDropped === true;
28139
+ if (bandLegendDropped) legendBlockHeight = 0;
28140
+ const drawChoroplethLegend = showChoroplethLegend && !bandLegendDropped;
28141
+ const drawPointLegend = showLegend && (pointLegendOverlay || !bandLegendDropped);
28098
28142
  const legendReserveGap = frame.legendReserveGap;
28099
28143
  const mapAreaHeight = frame.area.height;
28100
28144
  const zoom = mapSpec.geo.zoom;
@@ -28236,7 +28280,7 @@ function compileGeoMapImpl(spec, options, scope) {
28236
28280
  droppedFeatures
28237
28281
  });
28238
28282
  featureMarks = result.marks;
28239
- if (showChoroplethLegend && continuousContent) {
28283
+ if (drawChoroplethLegend && continuousContent) {
28240
28284
  const labelStyle = {
28241
28285
  fontFamily: theme.fonts.family,
28242
28286
  fontSize: theme.fonts.sizes.small,
@@ -28357,7 +28401,7 @@ function compileGeoMapImpl(spec, options, scope) {
28357
28401
  mapAreaHeight,
28358
28402
  legendReserveGap,
28359
28403
  legendPosition,
28360
- showLegend: showChoroplethLegend,
28404
+ showLegend: drawChoroplethLegend,
28361
28405
  droppedFeatures
28362
28406
  });
28363
28407
  featureMarks = result.marks;
@@ -28529,7 +28573,7 @@ function compileGeoMapImpl(spec, options, scope) {
28529
28573
  context: { count: droppedPoints.length, samples: droppedPoints.slice(0, 5) }
28530
28574
  });
28531
28575
  }
28532
- if (pts.color && pts.color.type !== "quantitative" && pointCategories.length > 0 && showLegend) {
28576
+ if (pts.color && pts.color.type !== "quantitative" && pointCategories.length > 0 && drawPointLegend) {
28533
28577
  const labelStyle = {
28534
28578
  fontFamily: theme.fonts.family,
28535
28579
  fontSize: theme.fonts.sizes.small,
@@ -28579,7 +28623,7 @@ function compileGeoMapImpl(spec, options, scope) {
28579
28623
  swatchChipFill: isDarkMode ? "rgba(255,255,255,0.06)" : "rgba(0,0,0,0.04)"
28580
28624
  };
28581
28625
  }
28582
- if (pts.color && pts.color.type === "quantitative" && showLegend) {
28626
+ if (pts.color && pts.color.type === "quantitative" && drawPointLegend) {
28583
28627
  const ptContinuousContent = pointContinuousContent;
28584
28628
  if (ptContinuousContent) {
28585
28629
  const labelStyle = {
@@ -28811,7 +28855,7 @@ function compileGeoMapImpl(spec, options, scope) {
28811
28855
  }
28812
28856
  const a11yDataField = colorEncoding?.field;
28813
28857
  const a11y = {
28814
- altText: pointMarks.length > 0 ? colorEncoding ? `Map showing ${geoFeatures.length} regions with ${isQuantitative ? "quantitative" : "categorical"} data and ${pointMarks.length} point markers` : `Map showing ${geoFeatures.length} regions with ${pointMarks.length} point markers` : colorEncoding ? `Map showing ${geoFeatures.length} regions with ${isQuantitative ? "quantitative" : "categorical"} data` : `Map showing ${geoFeatures.length} regions`,
28858
+ altText: describeGeoMap(geoFeatures.length, colorEncoding, pointMarks.length),
28815
28859
  dataTableFallback: [
28816
28860
  ...featureMarks.filter((m) => m.data).map((m) => [
28817
28861
  m.name ?? String(m.id),
@@ -29072,7 +29116,7 @@ function compileGeoMapImpl(spec, options, scope) {
29072
29116
  }
29073
29117
  }
29074
29118
  let colorKeys;
29075
- if (featureKeyExtra || pointKeyExtra) {
29119
+ if ((featureKeyExtra || pointKeyExtra) && !bandLegendDropped) {
29076
29120
  const featureGroups = /* @__PURE__ */ new Map();
29077
29121
  for (const feature of geoFeatures) {
29078
29122
  const id = String(feature.id);
@@ -29588,7 +29632,12 @@ function resolveFocus(focus, features, pointMarks, warnings) {
29588
29632
  ids: matchedIds
29589
29633
  };
29590
29634
  }
29591
- function emptyLayout4(chrome, theme, options, watermark) {
29635
+ function describeGeoMap(regions, color, points) {
29636
+ const colorData = color ? ` with ${color.type === "quantitative" ? "quantitative" : "categorical"} data` : "";
29637
+ if (points === 0) return `Map showing ${regions} regions${colorData}`;
29638
+ return `Map showing ${regions} regions${colorData}${color ? " and" : " with"} ${points} point markers`;
29639
+ }
29640
+ function emptyLayout4(chrome, theme, options, watermark, altText) {
29592
29641
  return {
29593
29642
  area: { x: 0, y: 0, width: 0, height: 0 },
29594
29643
  chrome,
@@ -29609,7 +29658,7 @@ function emptyLayout4(chrome, theme, options, watermark) {
29609
29658
  pointSizeLegend: null,
29610
29659
  tooltipDescriptors: /* @__PURE__ */ new Map(),
29611
29660
  a11y: {
29612
- altText: "Empty map",
29661
+ altText,
29613
29662
  dataTableFallback: [],
29614
29663
  role: "img",
29615
29664
  keyboardNavigable: false
@@ -31423,6 +31472,9 @@ import {
31423
31472
  formatDate as formatDate8,
31424
31473
  resolveNumberFormatter as resolveNumberFormatter6
31425
31474
  } from "@opendata-ai/openchart-core";
31475
+ import { utcFormat as utcFormat3 } from "d3-time-format";
31476
+ var TIME_DIRECTIVE = /%/;
31477
+ var ISO_8601 = /^(\d{4})-(\d{2})-(\d{2})(T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?)?(Z|[+-]\d{2}:?\d{2})?$/;
31426
31478
  function isNumericValue(value) {
31427
31479
  if (typeof value === "number") return Number.isFinite(value);
31428
31480
  return false;
@@ -31431,6 +31483,28 @@ function isDateValue(value) {
31431
31483
  if (value instanceof Date) return !Number.isNaN(value.getTime());
31432
31484
  return false;
31433
31485
  }
31486
+ function isRealCalendarDay(year, month, day) {
31487
+ const date = new Date(Date.UTC(year, month - 1, day));
31488
+ return date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day;
31489
+ }
31490
+ function toDate2(value) {
31491
+ if (value instanceof Date) return Number.isNaN(value.getTime()) ? void 0 : value;
31492
+ if (typeof value !== "string") return void 0;
31493
+ const parts = ISO_8601.exec(value);
31494
+ if (!parts) return void 0;
31495
+ const [, year, month, day, time, offset] = parts;
31496
+ if (!isRealCalendarDay(Number(year), Number(month), Number(day))) return void 0;
31497
+ const parsed = new Date(time && !offset ? `${value}Z` : value);
31498
+ return Number.isNaN(parsed.getTime()) ? void 0 : parsed;
31499
+ }
31500
+ function formatTemporalValue(value, column) {
31501
+ if (column.type !== "temporal" || !column.format || !TIME_DIRECTIVE.test(column.format)) {
31502
+ return void 0;
31503
+ }
31504
+ const date = toDate2(value);
31505
+ if (!date) return void 0;
31506
+ return utcFormat3(column.format)(date);
31507
+ }
31434
31508
  function formatCell(value, column) {
31435
31509
  const style = {};
31436
31510
  if (value == null) {
@@ -31450,6 +31524,10 @@ function formatCell(value, column) {
31450
31524
  };
31451
31525
  }
31452
31526
  }
31527
+ const temporal = formatTemporalValue(value, column);
31528
+ if (temporal !== void 0) {
31529
+ return { value, formattedValue: temporal, style };
31530
+ }
31453
31531
  if (isNumericValue(value)) {
31454
31532
  return {
31455
31533
  value,
@@ -31481,7 +31559,7 @@ function formatValueForSearch(value, column) {
31481
31559
  if (isNumericValue(value)) {
31482
31560
  return defaultNumberFormatter11({ surface: "table" })(value);
31483
31561
  }
31484
- return String(value);
31562
+ return formatTemporalValue(value, column) ?? String(value);
31485
31563
  }
31486
31564
 
31487
31565
  // src/tables/heatmap.ts