@opendata-ai/openchart-vanilla 7.9.3 → 7.11.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.js CHANGED
@@ -3841,7 +3841,7 @@ function escapeHtml(str) {
3841
3841
 
3842
3842
  // src/mount.ts
3843
3843
  import { isGraphSpec, isLayerSpec } from "@opendata-ai/openchart-core";
3844
- import { compileChart, compileLayer } from "@opendata-ai/openchart-engine";
3844
+ import { compileChart, compileLayer, legendGap } from "@opendata-ai/openchart-engine";
3845
3845
 
3846
3846
  // src/interactions/chart-events.ts
3847
3847
  function buildMarkDataMap(layout) {
@@ -6856,6 +6856,7 @@ function buildSingleAxisTweens(svg, prevAxis, nextAxis, orientation, layout, twe
6856
6856
  }
6857
6857
 
6858
6858
  // src/mount.ts
6859
+ var FALLBACK_HEIGHT = 400;
6859
6860
  function resolveDarkMode3(mode) {
6860
6861
  if (mode === "force") return true;
6861
6862
  if (mode === "off" || mode === void 0) return false;
@@ -6903,6 +6904,9 @@ function createChart(container, spec, options) {
6903
6904
  let isFirstRender = true;
6904
6905
  let cleanupAnimations = null;
6905
6906
  let pendingResize = false;
6907
+ let isAutoHeight = null;
6908
+ let lastRenderedWidth = null;
6909
+ let lastRenderedSvgHeight = null;
6906
6910
  let transitionHandle = null;
6907
6911
  let transitionSnapshot = null;
6908
6912
  let fontsReloadPending = false;
@@ -6919,8 +6923,7 @@ function createChart(container, spec, options) {
6919
6923
  let fontFamily = resolveEffectiveFont();
6920
6924
  let measureText = createMeasureText(fontFamily);
6921
6925
  let renderGen = 0;
6922
- function compile() {
6923
- const { width, height } = getContainerDimensions();
6926
+ function compileAt(width, height) {
6924
6927
  const darkMode = resolveDarkMode3(options?.darkMode);
6925
6928
  const compileOpts = {
6926
6929
  width,
@@ -6935,6 +6938,27 @@ function createChart(container, spec, options) {
6935
6938
  }
6936
6939
  return compileChart(withRuntimeHidden(currentSpec), compileOpts);
6937
6940
  }
6941
+ function verticalOverheads(layout, width) {
6942
+ const topLegendBlock = layout.legend.position === "top" && "entries" in layout.legend && layout.legend.entries.length > 0 ? layout.legend.bounds.height + legendGap(width) : 0;
6943
+ return layout.chrome.topHeight + layout.chrome.bottomHeight + topLegendBlock + (layout.metrics?.height ?? 0);
6944
+ }
6945
+ function compile() {
6946
+ const { width, height } = getContainerDimensions();
6947
+ const growsWithChrome = isAutoHeight === true && !isLayerSpec(currentSpec) && !isGraphSpec(currentSpec) && currentSpec.display !== "sparkline";
6948
+ if (!growsWithChrome) {
6949
+ return compileAt(width, height);
6950
+ }
6951
+ let layout = compileAt(width, height);
6952
+ const overheads = verticalOverheads(layout, width);
6953
+ if (overheads > 0) {
6954
+ layout = compileAt(width, height + overheads);
6955
+ const overheads2 = verticalOverheads(layout, width);
6956
+ if (overheads2 !== overheads) {
6957
+ layout = compileAt(width, height + overheads2);
6958
+ }
6959
+ }
6960
+ return layout;
6961
+ }
6938
6962
  function withRuntimeHidden(spec2) {
6939
6963
  if (runtimeHiddenSeries.size === 0 && runtimeShownSeries.size === 0) return spec2;
6940
6964
  const userHidden = spec2.hiddenSeries ?? [];
@@ -7027,9 +7051,15 @@ function createChart(container, spec, options) {
7027
7051
  height: Math.max(height || 40, 20)
7028
7052
  };
7029
7053
  }
7054
+ if (isAutoHeight === null && (rect.width > 0 || rect.height > 0)) {
7055
+ isAutoHeight = rect.height === 0 && rect.width > 0;
7056
+ }
7030
7057
  return {
7031
7058
  width: Math.max(rect.width || 600, 100),
7032
- height: Math.max(rect.height || 400, 100)
7059
+ // Latched-auto mounts always compile against the fixed budget: after
7060
+ // the first render the container's measured height is just our own
7061
+ // SVG, so reading it back would re-pin whatever we rendered last.
7062
+ height: isAutoHeight === true ? FALLBACK_HEIGHT : Math.max(rect.height || FALLBACK_HEIGHT, 100)
7033
7063
  };
7034
7064
  }
7035
7065
  function getSpecAnnotations() {
@@ -7273,6 +7303,8 @@ function createChart(container, spec, options) {
7273
7303
  srTable = null;
7274
7304
  }
7275
7305
  currentLayout = compile();
7306
+ lastRenderedWidth = currentLayout.dimensions.width;
7307
+ lastRenderedSvgHeight = currentLayout.dimensions.height;
7276
7308
  const shouldAnimate = isFirstRender && !!currentLayout.animation?.enter;
7277
7309
  const crosshair = !!currentLayout.crosshair;
7278
7310
  svgElement = renderChartSVG(currentLayout, container, {
@@ -7544,7 +7576,17 @@ function createChart(container, spec, options) {
7544
7576
  );
7545
7577
  container.dataset.ocFontsState = pending ? "pending" : "ready";
7546
7578
  if (options?.responsive !== false) {
7547
- disconnectResize = observeResize(container, () => {
7579
+ disconnectResize = observeResize(container, (width, height) => {
7580
+ if (isAutoHeight === true) {
7581
+ const widthChanged = width !== lastRenderedWidth;
7582
+ const selfInducedHeight = height === lastRenderedSvgHeight;
7583
+ if (!widthChanged && selfInducedHeight) return;
7584
+ if (!selfInducedHeight) {
7585
+ isAutoHeight = null;
7586
+ }
7587
+ resize();
7588
+ return;
7589
+ }
7548
7590
  resize();
7549
7591
  });
7550
7592
  }