@opendata-ai/openchart-core 7.3.0 → 7.4.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
@@ -2831,6 +2831,8 @@ interface ResolvedLabel {
2831
2831
  halo?: boolean;
2832
2832
  /** Engine-measured text-block bounds. Renderers should use this for background rects instead of re-estimating. */
2833
2833
  bounds?: Rect;
2834
+ /** Source index carried through from the LabelCandidate, if the caller set one. */
2835
+ index?: number;
2834
2836
  }
2835
2837
  /** A resolved annotation with computed pixel positions. */
2836
2838
  interface ResolvedAnnotation {
@@ -3888,6 +3890,19 @@ declare const BRAND_MIN_WIDTH = 120;
3888
3890
  * padding, label gaps) to maximize data area on small screens.
3889
3891
  */
3890
3892
  declare const COMPACT_WIDTH = 420;
3893
+ /**
3894
+ * Distance from the top of a text box to its alphabetic baseline.
3895
+ *
3896
+ * Layout stores text positions as top-edge coordinates; renderers convert to
3897
+ * baseline coordinates with this ratio instead of `dominant-baseline: hanging`.
3898
+ * WebKit computes the hanging baseline from different font metrics than Blink
3899
+ * and never inherits dominant-baseline into tspans (WebKit bugs 139258 /
3900
+ * 297455), which shifted whole titles above the container on iOS Safari.
3901
+ * The 0.8 ratio matches Blink's rendered hanging position for typical UI
3902
+ * fonts (hanging sits at ~0.8 x ascent, Inter ascent ~= 0.97em) and the
3903
+ * Highcharts fontMetrics convention.
3904
+ */
3905
+ declare function textAscent(fontSize: number): number;
3891
3906
  declare const heuristicMeasure: MeasureTextFn;
3892
3907
  declare function resolveMeasurer(measureText?: MeasureTextFn): MeasureTextFn;
3893
3908
 
@@ -4048,6 +4063,13 @@ interface LabelCandidate {
4048
4063
  priority: LabelPriority;
4049
4064
  /** Text style to apply. */
4050
4065
  style: TextStyle;
4066
+ /**
4067
+ * Optional caller-assigned source index. resolveCollisions may reorder its
4068
+ * output (it sorts by priority), so callers that zip the result back against
4069
+ * a parallel array should carry an index here and read it off ResolvedLabel
4070
+ * rather than relying on positional order.
4071
+ */
4072
+ index?: number;
4051
4073
  }
4052
4074
  /**
4053
4075
  * Detect AABB (axis-aligned bounding box) overlap between two rectangles.
@@ -4083,6 +4105,15 @@ declare function resolveCollisions(labels: LabelCandidate[], strategies?: readon
4083
4105
  /**
4084
4106
  * Compute the bounding rect of a resolved label from its position and text.
4085
4107
  * Uses heuristic text measurement so it works without DOM access.
4108
+ *
4109
+ * label.y is the SVG text anchor point, whose meaning depends on the label's
4110
+ * dominant-baseline. The rect's top edge is derived accordingly so the box
4111
+ * covers the actual glyphs (obstacles were sitting ~0.8*fontSize below the
4112
+ * text for alphabetic-baseline labels, e.g. column value labels):
4113
+ * - 'central': y is the vertical center, so top = y - height/2.
4114
+ * - 'hanging'/'text-after-edge': y is the top edge already.
4115
+ * - default (alphabetic): y is the baseline, so top = y - fontSize*0.8
4116
+ * (matches textAscent, the top-to-baseline distance renderers apply).
4086
4117
  */
4087
4118
  declare function computeLabelBounds(label: ResolvedLabel): Rect;
4088
4119
 
@@ -4378,4 +4409,4 @@ interface TileMapBuilderOptions {
4378
4409
  */
4379
4410
  declare function tileMap(data: Record<string, number | null> | DataRow[], options?: TileMapBuilderOptions): TileMapSpec;
4380
4411
 
4381
- export { type A11yMetadata, AXIS_TITLE_GAP, AXIS_TITLE_OFFSET_COMPACT, AXIS_TITLE_OFFSET_DEFAULT, AXIS_TITLE_TRAILING_PAD, type AggregateOp, type AggregateTransform, type AnimationConfig, type AnimationEase, type AnimationPhaseConfig, type AnimationSpec, type AnimationStagger, type Annotation, type AnnotationAnchor, type AnnotationOffset, type AnnotationPosition, type ArcEncoding, type ArcMark, type AreaEncoding, type AreaMark, type AxisConfig, type AxisLabelDensity, type AxisLayout, type AxisTick, BRAND_FONT_SIZE, BRAND_MIN_WIDTH, BRAND_RESERVE_WIDTH, BREAKPOINT_COMPACT_MAX, BREAKPOINT_MEDIUM_MAX, type BarColumnConfig, type BarEncoding, type BarListEncoding, type BarListLayout, type BarListRowMark, type BarListSpec, type BarListSpecWithoutData, type BarTableCell, type BaseLegendLayout, type BinParams, type BinTransform, type Breakpoint, CATEGORICAL_PALETTE, CHART_ENCODING_RULES, CHART_TYPES, COMPACT_WIDTH, type CalculateExpression, type CalculateTransform, type CategoricalLegendLayout, type CategoricalPalette, type CategoryColorsConfig, type CategoryTableCell, type CellStyle, type ChannelRule, type ChartBuilderOptions, type ChartEventHandlers, type ChartLayout, type ChartSpec, type ChartSpecOverride, type ChartSpecWithoutData, type ChartType, type Chrome, type ChromeDefaults, type ChromeKey, type ChromeMode, type ChromeText, type ChromeTextStyle, type CircleEncoding, type ColorBlindnessType, type ColumnConfig, type CompileOptions, type CompileTableOptions, type Condition, type ConditionalValueDef, DEFAULT_THEME, DIVERGING_PALETTES, type DarkMode, type DataRow, type DateGranularity, type Display, type DivergingPalette, EXTENDED_OFFSET_STRATEGIES, type ElementEdit, type ElementRef, type Encoding, type EncodingChannel, type EncodingRule, type EndpointLabelEntry, type EndpointLabelsConfig, type EndpointLabelsLayout, type FieldPredicate, type FieldRef, type FieldType, type FilterPredicate, type FilterTransform, type FlagTableCell, type FoldTransform, GRAPH_ENCODING_RULES, type GradientColorStop, type GradientDef, type GradientLegendLayout, type GradientStop, type GraphChannelRule, type GraphEdge, type GraphEdgeLayout, type GraphEncoding, type GraphEncodingChannel, type GraphLayout, type GraphLayoutConfig, type GraphNode, type GraphNodeLayout, type GraphSpec, type GraphSpecWithoutData, type Gridline, HEIGHT_CRAMPED_MAX, HEIGHT_SHORT_MAX, HPAD_COMPACT_FRACTION, HPAD_COMPACT_MIN, type HeatmapColumnConfig, type HeatmapTableCell, type HeightClass, type ImageColumnConfig, type ImageTableCell, LABEL_GAP_COMPACT, LABEL_GAP_DEFAULT, LABEL_GAP_NARROW_MAX, type LabelCandidate, type LabelConfig, type LabelDensity, type LabelMode, type LabelPriority, type LabelSpec, type LayerSpec, type LayoutStrategy, type LegendConfig, type LegendEntry, type LegendEntryPosition, type LegendLayout, type LegendPosition, type LineEncoding, type LineMark, type LinearGradient, type LogicalAnd, type LogicalNot, type LogicalOr, type LollipopEncoding, MARK_DISPLAY_NAMES, MARK_ENCODING_RULES, MARK_TYPES, MAX_LEFT_LABEL_FRACTION_COMPACT, MAX_LEFT_LABEL_FRACTION_DEFAULT, MAX_LEFT_LABEL_FRACTION_MEDIUM, MAX_LEFT_LABEL_FRACTION_MEDIUM_MAX, type Margins, type Mark, type MarkAria, type MarkDef, type MarkEvent, type MarkType, type MeasureTextFn, type Metric, NARROW_VIEWPORT_MAX, type NodeOverride, OFFSET_STRATEGIES, type OffsetStrategy, type PaginationState, type Point, type PointEncoding, type PointMark, type RadialGradient, type RangeAnnotation, type Rect, type RectEncoding, type RectMark, type RefLineAnnotation, type RelativeTimeRef, type ResolveConfig, type ResolveMode, type ResolvedAnimation, type ResolvedAnnotation, type ResolvedChrome, type ResolvedChromeElement, type ResolvedColumn, type ResolvedLabel, type ResolvedMetricBar, type ResolvedMetricCell, type ResolvedTheme, type RuleMarkLayout, SEQUENTIAL_PALETTES, type SankeyEncoding, type SankeyLayout, type SankeyLinkColor, type SankeyLinkMark, type SankeyNodeAlign, type SankeyNodeMark, type SankeySpec, type SankeySpecWithoutData, type ScaleConfig, type ScaleType, type SequentialPalette, type SeriesStyle, type SortState, type SparklineColumnConfig, type SparklineData, type SparklineTableCell, type StoredVizSpec, TICK_LABEL_OFFSET, TOP_PAD_EXTRA_NARROW, TOP_PAD_NARROW_MAX, type TableBuilderOptions, type TableCell, type TableCellBase, type TableLayout, type TableRow, type TableSpec, type TableSpecWithoutData, type TextAnnotation, type TextEncoding, type TextMarkLayout, type TextStyle, type TextTableCell, type Theme, type ThemeChromeDefaults, type ThemeColors, type ThemeConfig, type ThemeFontSizes, type ThemeFontWeights, type ThemeFonts, type ThemeSpacing, type TickEncoding, type TickMarkLayout, type TileMapBuilderOptions, type TileMapEncoding, type TileMapLayout, type TileMapPalette, type TileMapSpec, type TileMapSpecWithoutData, type TileMapTileMark, type TimeUnit, type TimeUnitTransform, type TooltipContent, type TooltipField, type Transform, type VizSpec, type WindowFieldDef, type WindowOp, type WindowSortField, type WindowTransform, type XAxisExtentInput, X_AXIS_BAND_HEIGHT, X_AXIS_TITLE_BAND, X_AXIS_TITLE_BAND_ROTATED, abbreviateNumber, adaptColorForDarkMode, adaptTheme, areaChart, axisTitleOffset, barChart, buildD3Formatter, buildTemporalFormatter, checkPaletteDistinguishability, columnChart, computeChrome, computeLabelBounds, computeXAxisExtentFromLabels, contrastRatio, dataTable, detectCollision, donutChart, dotChart, elementRef, estimateTextWidth, findAccessibleColor, formatDate, formatNumber, generateAltText, generateAriaLabels, generateDataTable, getAxisTitleOffset, getBreakpoint, getHeightClass, getLayoutStrategy, getRepresentativeColor, heuristicMeasure, inferFieldType, isBarListSpec, isChartSpec, isConditionalDef, isEncodingChannel, isGradientDef, isGraphSpec, isLayerSpec, isRangeAnnotation, isRefLineAnnotation, isSankeySpec, isTableSpec, isTextAnnotation, isTileMapSpec, lineChart, meetsAA, overlapArea, pickLabelColor, pieChart, resolveCollisions, resolveMarkDef, resolveMarkType, resolveMeasurer, resolveTheme, scatterChart, simulateColorBlindness, tileMap, wrapText };
4412
+ export { type A11yMetadata, AXIS_TITLE_GAP, AXIS_TITLE_OFFSET_COMPACT, AXIS_TITLE_OFFSET_DEFAULT, AXIS_TITLE_TRAILING_PAD, type AggregateOp, type AggregateTransform, type AnimationConfig, type AnimationEase, type AnimationPhaseConfig, type AnimationSpec, type AnimationStagger, type Annotation, type AnnotationAnchor, type AnnotationOffset, type AnnotationPosition, type ArcEncoding, type ArcMark, type AreaEncoding, type AreaMark, type AxisConfig, type AxisLabelDensity, type AxisLayout, type AxisTick, BRAND_FONT_SIZE, BRAND_MIN_WIDTH, BRAND_RESERVE_WIDTH, BREAKPOINT_COMPACT_MAX, BREAKPOINT_MEDIUM_MAX, type BarColumnConfig, type BarEncoding, type BarListEncoding, type BarListLayout, type BarListRowMark, type BarListSpec, type BarListSpecWithoutData, type BarTableCell, type BaseLegendLayout, type BinParams, type BinTransform, type Breakpoint, CATEGORICAL_PALETTE, CHART_ENCODING_RULES, CHART_TYPES, COMPACT_WIDTH, type CalculateExpression, type CalculateTransform, type CategoricalLegendLayout, type CategoricalPalette, type CategoryColorsConfig, type CategoryTableCell, type CellStyle, type ChannelRule, type ChartBuilderOptions, type ChartEventHandlers, type ChartLayout, type ChartSpec, type ChartSpecOverride, type ChartSpecWithoutData, type ChartType, type Chrome, type ChromeDefaults, type ChromeKey, type ChromeMode, type ChromeText, type ChromeTextStyle, type CircleEncoding, type ColorBlindnessType, type ColumnConfig, type CompileOptions, type CompileTableOptions, type Condition, type ConditionalValueDef, DEFAULT_THEME, DIVERGING_PALETTES, type DarkMode, type DataRow, type DateGranularity, type Display, type DivergingPalette, EXTENDED_OFFSET_STRATEGIES, type ElementEdit, type ElementRef, type Encoding, type EncodingChannel, type EncodingRule, type EndpointLabelEntry, type EndpointLabelsConfig, type EndpointLabelsLayout, type FieldPredicate, type FieldRef, type FieldType, type FilterPredicate, type FilterTransform, type FlagTableCell, type FoldTransform, GRAPH_ENCODING_RULES, type GradientColorStop, type GradientDef, type GradientLegendLayout, type GradientStop, type GraphChannelRule, type GraphEdge, type GraphEdgeLayout, type GraphEncoding, type GraphEncodingChannel, type GraphLayout, type GraphLayoutConfig, type GraphNode, type GraphNodeLayout, type GraphSpec, type GraphSpecWithoutData, type Gridline, HEIGHT_CRAMPED_MAX, HEIGHT_SHORT_MAX, HPAD_COMPACT_FRACTION, HPAD_COMPACT_MIN, type HeatmapColumnConfig, type HeatmapTableCell, type HeightClass, type ImageColumnConfig, type ImageTableCell, LABEL_GAP_COMPACT, LABEL_GAP_DEFAULT, LABEL_GAP_NARROW_MAX, type LabelCandidate, type LabelConfig, type LabelDensity, type LabelMode, type LabelPriority, type LabelSpec, type LayerSpec, type LayoutStrategy, type LegendConfig, type LegendEntry, type LegendEntryPosition, type LegendLayout, type LegendPosition, type LineEncoding, type LineMark, type LinearGradient, type LogicalAnd, type LogicalNot, type LogicalOr, type LollipopEncoding, MARK_DISPLAY_NAMES, MARK_ENCODING_RULES, MARK_TYPES, MAX_LEFT_LABEL_FRACTION_COMPACT, MAX_LEFT_LABEL_FRACTION_DEFAULT, MAX_LEFT_LABEL_FRACTION_MEDIUM, MAX_LEFT_LABEL_FRACTION_MEDIUM_MAX, type Margins, type Mark, type MarkAria, type MarkDef, type MarkEvent, type MarkType, type MeasureTextFn, type Metric, NARROW_VIEWPORT_MAX, type NodeOverride, OFFSET_STRATEGIES, type OffsetStrategy, type PaginationState, type Point, type PointEncoding, type PointMark, type RadialGradient, type RangeAnnotation, type Rect, type RectEncoding, type RectMark, type RefLineAnnotation, type RelativeTimeRef, type ResolveConfig, type ResolveMode, type ResolvedAnimation, type ResolvedAnnotation, type ResolvedChrome, type ResolvedChromeElement, type ResolvedColumn, type ResolvedLabel, type ResolvedMetricBar, type ResolvedMetricCell, type ResolvedTheme, type RuleMarkLayout, SEQUENTIAL_PALETTES, type SankeyEncoding, type SankeyLayout, type SankeyLinkColor, type SankeyLinkMark, type SankeyNodeAlign, type SankeyNodeMark, type SankeySpec, type SankeySpecWithoutData, type ScaleConfig, type ScaleType, type SequentialPalette, type SeriesStyle, type SortState, type SparklineColumnConfig, type SparklineData, type SparklineTableCell, type StoredVizSpec, TICK_LABEL_OFFSET, TOP_PAD_EXTRA_NARROW, TOP_PAD_NARROW_MAX, type TableBuilderOptions, type TableCell, type TableCellBase, type TableLayout, type TableRow, type TableSpec, type TableSpecWithoutData, type TextAnnotation, type TextEncoding, type TextMarkLayout, type TextStyle, type TextTableCell, type Theme, type ThemeChromeDefaults, type ThemeColors, type ThemeConfig, type ThemeFontSizes, type ThemeFontWeights, type ThemeFonts, type ThemeSpacing, type TickEncoding, type TickMarkLayout, type TileMapBuilderOptions, type TileMapEncoding, type TileMapLayout, type TileMapPalette, type TileMapSpec, type TileMapSpecWithoutData, type TileMapTileMark, type TimeUnit, type TimeUnitTransform, type TooltipContent, type TooltipField, type Transform, type VizSpec, type WindowFieldDef, type WindowOp, type WindowSortField, type WindowTransform, type XAxisExtentInput, X_AXIS_BAND_HEIGHT, X_AXIS_TITLE_BAND, X_AXIS_TITLE_BAND_ROTATED, abbreviateNumber, adaptColorForDarkMode, adaptTheme, areaChart, axisTitleOffset, barChart, buildD3Formatter, buildTemporalFormatter, checkPaletteDistinguishability, columnChart, computeChrome, computeLabelBounds, computeXAxisExtentFromLabels, contrastRatio, dataTable, detectCollision, donutChart, dotChart, elementRef, estimateTextWidth, findAccessibleColor, formatDate, formatNumber, generateAltText, generateAriaLabels, generateDataTable, getAxisTitleOffset, getBreakpoint, getHeightClass, getLayoutStrategy, getRepresentativeColor, heuristicMeasure, inferFieldType, isBarListSpec, isChartSpec, isConditionalDef, isEncodingChannel, isGradientDef, isGraphSpec, isLayerSpec, isRangeAnnotation, isRefLineAnnotation, isSankeySpec, isTableSpec, isTextAnnotation, isTileMapSpec, lineChart, meetsAA, overlapArea, pickLabelColor, pieChart, resolveCollisions, resolveMarkDef, resolveMarkType, resolveMeasurer, resolveTheme, scatterChart, simulateColorBlindness, textAscent, tileMap, wrapText };
package/dist/index.js CHANGED
@@ -1119,6 +1119,9 @@ var COMPACT_WIDTH = 420;
1119
1119
  function estimateTextHeight(fontSize, lineCount = 1, lineHeight = 1.3) {
1120
1120
  return fontSize * lineHeight * lineCount;
1121
1121
  }
1122
+ function textAscent(fontSize) {
1123
+ return fontSize * 0.8;
1124
+ }
1122
1125
  var heuristicMeasure = (text, fontSize, fontWeight) => ({
1123
1126
  width: estimateTextWidth(text, fontSize, fontWeight),
1124
1127
  height: estimateTextHeight(fontSize)
@@ -1234,7 +1237,12 @@ function computeXAxisExtentFromLabels(input) {
1234
1237
  const w = measure(label, input.tickFontSize, input.tickFontWeight);
1235
1238
  if (w > maxLabelWidth) maxLabelWidth = w;
1236
1239
  }
1237
- const rotatedHeight = Math.min(maxLabelWidth * Math.sin(angleRad) + 6, 120);
1240
+ const lineHeight = input.tickFontSize * 1.2;
1241
+ const AXIS_TO_LABEL_GAP = 3;
1242
+ const rotatedHeight = Math.min(
1243
+ maxLabelWidth * Math.sin(angleRad) + lineHeight * Math.cos(angleRad) + AXIS_TO_LABEL_GAP,
1244
+ 120
1245
+ );
1238
1246
  return input.hasTitle ? rotatedHeight + X_AXIS_TITLE_BAND_ROTATED : rotatedHeight;
1239
1247
  }
1240
1248
  return input.hasTitle ? baseHeight + X_AXIS_TITLE_BAND : baseHeight;
@@ -1285,8 +1293,10 @@ var DEFAULT_THEME = {
1285
1293
  axisMargin: 6,
1286
1294
  xAxisHeight: X_AXIS_BAND_HEIGHT,
1287
1295
  // Gap (px) between the x-axis line and the TOP of the tick labels. The
1288
- // renderer anchors x-tick labels at their top edge (hanging baseline), so
1289
- // this is a literal top gap that holds regardless of font size.
1296
+ // renderer treats this as a literal top gap and then shifts the label down
1297
+ // by textAscent() to land it on the alphabetic baseline (rather than
1298
+ // dominant-baseline:hanging, which WebKit positions from different metrics),
1299
+ // so the gap holds regardless of font size.
1290
1300
  xAxisLabelPadding: 4
1291
1301
  },
1292
1302
  borderRadius: 2,
@@ -1613,8 +1623,7 @@ function buildTextStyle(defaults, fontFamily, textColor, width, overrides) {
1613
1623
  fontWeight: overrides?.fontWeight ?? defaults.fontWeight,
1614
1624
  fill: overrides?.color ?? textColor ?? defaults.color,
1615
1625
  lineHeight: defaults.lineHeight,
1616
- textAnchor: "start",
1617
- dominantBaseline: "hanging"
1626
+ textAnchor: "start"
1618
1627
  };
1619
1628
  }
1620
1629
  function estimateLineCount(text, style, maxWidth, measureText) {
@@ -1981,6 +1990,9 @@ function resolveCollisions(labels, strategies = OFFSET_STRATEGIES) {
1981
1990
  y: bestY,
1982
1991
  style: label.style,
1983
1992
  visible: true,
1993
+ // Only carry the index through when the caller set one, so labels that
1994
+ // don't need it keep an unchanged output shape.
1995
+ ...label.index !== void 0 ? { index: label.index } : {},
1984
1996
  connector: needsConnector ? {
1985
1997
  from: { x: bestX, y: bestY },
1986
1998
  to: { x: label.anchorX, y: label.anchorY },
@@ -1994,7 +2006,8 @@ function resolveCollisions(labels, strategies = OFFSET_STRATEGIES) {
1994
2006
  x: label.anchorX,
1995
2007
  y: label.anchorY,
1996
2008
  style: label.style,
1997
- visible: false
2009
+ visible: false,
2010
+ ...label.index !== void 0 ? { index: label.index } : {}
1998
2011
  });
1999
2012
  }
2000
2013
  }
@@ -2011,7 +2024,16 @@ function computeLabelBounds(label) {
2011
2024
  } else if (label.style.textAnchor === "end") {
2012
2025
  x = label.x - width;
2013
2026
  }
2014
- return { x, y: label.y, width, height };
2027
+ const baseline = label.style.dominantBaseline;
2028
+ let y;
2029
+ if (baseline === "central") {
2030
+ y = label.y - height / 2;
2031
+ } else if (baseline === "hanging" || baseline === "text-after-edge") {
2032
+ y = label.y;
2033
+ } else {
2034
+ y = label.y - fontSize * 0.8;
2035
+ }
2036
+ return { x, y, width, height };
2015
2037
  }
2016
2038
 
2017
2039
  // ../../node_modules/.bun/d3-format@3.1.2/node_modules/d3-format/src/formatDecimal.js
@@ -3455,6 +3477,7 @@ export {
3455
3477
  resolveTheme,
3456
3478
  scatterChart,
3457
3479
  simulateColorBlindness,
3480
+ textAscent,
3458
3481
  tileMap,
3459
3482
  wrapText
3460
3483
  };