@opendata-ai/openchart-core 6.12.0 → 6.15.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
@@ -287,7 +287,7 @@ interface MarkDef {
287
287
  /** Data field type, following Vega-Lite conventions. */
288
288
  type FieldType = 'quantitative' | 'temporal' | 'nominal' | 'ordinal';
289
289
  /** Aggregate function applied to a field before encoding. */
290
- type AggregateOp = 'count' | 'sum' | 'mean' | 'median' | 'min' | 'max';
290
+ type AggregateOp = 'count' | 'sum' | 'mean' | 'median' | 'min' | 'max' | 'variance' | 'stdev' | 'distinct' | 'q1' | 'q3';
291
291
  /** Axis configuration for an encoding channel. */
292
292
  interface AxisConfig {
293
293
  /** Axis title text. If omitted, the field name is used. */
@@ -318,10 +318,6 @@ interface AxisConfig {
318
318
  titlePadding?: number;
319
319
  /** Padding between tick labels and axis. */
320
320
  labelPadding?: number;
321
- /** @deprecated Use `title` instead. */
322
- label?: string;
323
- /** @deprecated Use `labelAngle` instead. */
324
- tickAngle?: number;
325
321
  }
326
322
  /** Scale configuration for an encoding channel. */
327
323
  interface ScaleConfig {
@@ -382,10 +378,47 @@ interface EncodingChannel {
382
378
  scale?: ScaleConfig;
383
379
  /**
384
380
  * Stacking behavior for quantitative channels (Vega-Lite aligned).
385
- * - undefined | true | 'zero': stack (default, cumulative segments)
381
+ * - undefined | true | 'zero': stack from zero baseline (default)
382
+ * - 'normalize': stack and normalize to fraction of total (0-1 per category)
383
+ * - 'center': center stacks around zero (streamgraph style)
386
384
  * - null | false: no stacking (grouped/dodged side-by-side)
387
385
  */
388
- stack?: boolean | 'zero' | null;
386
+ stack?: boolean | 'zero' | 'normalize' | 'center' | null;
387
+ /**
388
+ * Encoding-level bin shorthand (Vega-Lite aligned).
389
+ * When set, auto-generates a BinTransform during normalization and updates
390
+ * the field reference to the binned output (convention: `bin_<fieldName>`).
391
+ * - true: bin with default params
392
+ * - BinParams: bin with custom params (maxbins, step, etc.)
393
+ */
394
+ bin?: boolean | BinParams;
395
+ /**
396
+ * Encoding-level timeUnit shorthand (Vega-Lite aligned).
397
+ * When set, auto-generates a TimeUnitTransform during normalization and
398
+ * updates the field reference to the output (convention: `<timeUnit>_<fieldName>`).
399
+ */
400
+ timeUnit?: TimeUnit;
401
+ /**
402
+ * Sort order for categorical (nominal/ordinal) scale domains (Vega-Lite aligned).
403
+ * - 'ascending': sort domain values ascending (default VL behavior)
404
+ * - 'descending': sort domain values descending
405
+ * - null: use data order (no sorting)
406
+ * - undefined: ascending (VL default)
407
+ */
408
+ sort?: 'ascending' | 'descending' | null;
409
+ /**
410
+ * Display title override (Vega-Lite aligned).
411
+ * Used as the label in tooltips instead of the raw field name.
412
+ * Also usable for axis titles, but `axis.title` takes precedence there.
413
+ */
414
+ title?: string;
415
+ /**
416
+ * Format string for values (Vega-Lite aligned).
417
+ * For quantitative fields: d3-format string (e.g. ",.0f", "$,.2f").
418
+ * For temporal fields: d3-time-format string (e.g. "%Y", "%b %d").
419
+ * Used in tooltips; `axis.format` takes precedence for axis tick labels.
420
+ */
421
+ format?: string;
389
422
  }
390
423
  /**
391
424
  * Encoding object mapping visual channels to data fields.
@@ -1149,8 +1182,30 @@ interface TimeUnitTransform {
1149
1182
  field: string;
1150
1183
  as: string;
1151
1184
  }
1185
+ /**
1186
+ * Aggregate transform: group rows and compute summary statistics (VL aligned).
1187
+ * Produces one row per group with the groupby fields and computed aggregates.
1188
+ */
1189
+ interface AggregateTransform {
1190
+ aggregate: Array<{
1191
+ op: AggregateOp;
1192
+ field: string;
1193
+ as: string;
1194
+ }>;
1195
+ groupby: string[];
1196
+ }
1197
+ /**
1198
+ * Fold transform: unpivot wide-format columns into key/value rows (VL aligned).
1199
+ * For each input row, produces N output rows (one per fold field) with all
1200
+ * non-fold fields copied plus a key column (field name) and value column (field value).
1201
+ */
1202
+ interface FoldTransform {
1203
+ fold: string[];
1204
+ /** Output field names for [key, value]. Defaults to ['key', 'value']. */
1205
+ as?: [string, string];
1206
+ }
1152
1207
  /** Discriminated union of all transform types. */
1153
- type Transform = FilterTransform | BinTransform | CalculateTransform | TimeUnitTransform;
1208
+ type Transform = FilterTransform | BinTransform | CalculateTransform | TimeUnitTransform | AggregateTransform | FoldTransform;
1154
1209
  /**
1155
1210
  * A single condition with a test predicate and resulting value/field.
1156
1211
  * When the test passes for a datum, the condition's value/field is used.
@@ -2136,6 +2191,8 @@ interface ChartLayout {
2136
2191
  animation?: ResolvedAnimation;
2137
2192
  /** Whether the tryOpenData.ai watermark is enabled. */
2138
2193
  watermark: boolean;
2194
+ /** Real text measurement function from the adapter (for accurate SVG text wrapping). */
2195
+ measureText?: MeasureTextFn;
2139
2196
  }
2140
2197
  /** A resolved column definition with computed properties. */
2141
2198
  interface ResolvedColumn {
@@ -3040,4 +3097,4 @@ declare function scatterChart(data: DataRow[], x: FieldRef, y: FieldRef, options
3040
3097
  */
3041
3098
  declare function dataTable(data: DataRow[], options?: TableBuilderOptions): TableSpec;
3042
3099
 
3043
- export { type A11yMetadata, type AggregateOp, type AnimationConfig, type AnimationEase, type AnimationPhaseConfig, type AnimationSpec, type AnimationStagger, type Annotation, type AnnotationAnchor, type AnnotationOffset, type AnnotationPosition, type ArcMark, type AreaMark, type AxisConfig, type AxisLabelDensity, type AxisLayout, type AxisTick, BRAND_FONT_SIZE, BRAND_MIN_WIDTH, BRAND_RESERVE_WIDTH, type BarColumnConfig, type BarTableCell, type BinParams, type BinTransform, type Breakpoint, CATEGORICAL_PALETTE, CHART_ENCODING_RULES, CHART_TYPES, type CalculateExpression, type CalculateTransform, 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 ColorBlindnessType, type ColumnConfig, type CompileOptions, type CompileTableOptions, type Condition, type ConditionalValueDef, DEFAULT_THEME, DIVERGING_PALETTES, type DarkMode, type DataRow, type DateGranularity, type DivergingPalette, EXTENDED_OFFSET_STRATEGIES, type ElementEdit, type ElementRef, type Encoding, type EncodingChannel, type EncodingRule, type FieldPredicate, type FieldRef, type FieldType, type FilterPredicate, type FilterTransform, type FlagTableCell, GRAPH_ENCODING_RULES, type GradientDef, 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, type HeatmapColumnConfig, type HeatmapTableCell, type HeightClass, type ImageColumnConfig, type ImageTableCell, type LabelCandidate, type LabelConfig, type LabelDensity, type LabelMode, type LabelPriority, type LayerSpec, type LayoutStrategy, type LegendConfig, type LegendEntry, type LegendLayout, type LegendPosition, type LineMark, type LinearGradient, type LogicalAnd, type LogicalNot, type LogicalOr, MARK_DISPLAY_NAMES, MARK_ENCODING_RULES, MARK_TYPES, type Margins, type Mark, type MarkAria, type MarkDef, type MarkEvent, type MarkType, type MeasureTextFn, type NodeOverride, OFFSET_STRATEGIES, type OffsetStrategy, type PaginationState, type Point, type PointMark, type RadialGradient, type RangeAnnotation, type Rect, type RectMark, type RefLineAnnotation, type ResolveConfig, type ResolveMode, type ResolvedAnimation, type ResolvedAnnotation, type ResolvedChrome, type ResolvedChromeElement, type ResolvedColumn, type ResolvedLabel, 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, type TableBuilderOptions, type TableCell, type TableCellBase, type TableLayout, type TableRow, type TableSpec, type TableSpecWithoutData, type TextAnnotation, type TextMarkLayout, type TextStyle, type TextTableCell, type Theme, type ThemeChromeDefaults, type ThemeColors, type ThemeConfig, type ThemeFontSizes, type ThemeFontWeights, type ThemeFonts, type ThemeSpacing, type TickMarkLayout, type TimeUnit, type TimeUnitTransform, type TooltipContent, type TooltipField, type Transform, type VizSpec, abbreviateNumber, adaptColorForDarkMode, adaptTheme, areaChart, barChart, buildD3Formatter, buildTemporalFormatter, checkPaletteDistinguishability, columnChart, computeChrome, computeLabelBounds, contrastRatio, dataTable, detectCollision, donutChart, dotChart, elementRef, estimateTextWidth, findAccessibleColor, formatDate, formatNumber, generateAltText, generateAriaLabels, generateDataTable, getBreakpoint, getHeightClass, getLayoutStrategy, getRepresentativeColor, inferFieldType, isChartSpec, isConditionalDef, isEncodingChannel, isGradientDef, isGraphSpec, isLayerSpec, isRangeAnnotation, isRefLineAnnotation, isSankeySpec, isTableSpec, isTextAnnotation, lineChart, meetsAA, pieChart, resolveCollisions, resolveMarkDef, resolveMarkType, resolveTheme, scatterChart, simulateColorBlindness };
3100
+ export { type A11yMetadata, type AggregateOp, type AggregateTransform, type AnimationConfig, type AnimationEase, type AnimationPhaseConfig, type AnimationSpec, type AnimationStagger, type Annotation, type AnnotationAnchor, type AnnotationOffset, type AnnotationPosition, type ArcMark, type AreaMark, type AxisConfig, type AxisLabelDensity, type AxisLayout, type AxisTick, BRAND_FONT_SIZE, BRAND_MIN_WIDTH, BRAND_RESERVE_WIDTH, type BarColumnConfig, type BarTableCell, type BinParams, type BinTransform, type Breakpoint, CATEGORICAL_PALETTE, CHART_ENCODING_RULES, CHART_TYPES, type CalculateExpression, type CalculateTransform, 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 ColorBlindnessType, type ColumnConfig, type CompileOptions, type CompileTableOptions, type Condition, type ConditionalValueDef, DEFAULT_THEME, DIVERGING_PALETTES, type DarkMode, type DataRow, type DateGranularity, type DivergingPalette, EXTENDED_OFFSET_STRATEGIES, type ElementEdit, type ElementRef, type Encoding, type EncodingChannel, type EncodingRule, type FieldPredicate, type FieldRef, type FieldType, type FilterPredicate, type FilterTransform, type FlagTableCell, type FoldTransform, GRAPH_ENCODING_RULES, type GradientDef, 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, type HeatmapColumnConfig, type HeatmapTableCell, type HeightClass, type ImageColumnConfig, type ImageTableCell, type LabelCandidate, type LabelConfig, type LabelDensity, type LabelMode, type LabelPriority, type LayerSpec, type LayoutStrategy, type LegendConfig, type LegendEntry, type LegendLayout, type LegendPosition, type LineMark, type LinearGradient, type LogicalAnd, type LogicalNot, type LogicalOr, MARK_DISPLAY_NAMES, MARK_ENCODING_RULES, MARK_TYPES, type Margins, type Mark, type MarkAria, type MarkDef, type MarkEvent, type MarkType, type MeasureTextFn, type NodeOverride, OFFSET_STRATEGIES, type OffsetStrategy, type PaginationState, type Point, type PointMark, type RadialGradient, type RangeAnnotation, type Rect, type RectMark, type RefLineAnnotation, type ResolveConfig, type ResolveMode, type ResolvedAnimation, type ResolvedAnnotation, type ResolvedChrome, type ResolvedChromeElement, type ResolvedColumn, type ResolvedLabel, 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, type TableBuilderOptions, type TableCell, type TableCellBase, type TableLayout, type TableRow, type TableSpec, type TableSpecWithoutData, type TextAnnotation, type TextMarkLayout, type TextStyle, type TextTableCell, type Theme, type ThemeChromeDefaults, type ThemeColors, type ThemeConfig, type ThemeFontSizes, type ThemeFontWeights, type ThemeFonts, type ThemeSpacing, type TickMarkLayout, type TimeUnit, type TimeUnitTransform, type TooltipContent, type TooltipField, type Transform, type VizSpec, abbreviateNumber, adaptColorForDarkMode, adaptTheme, areaChart, barChart, buildD3Formatter, buildTemporalFormatter, checkPaletteDistinguishability, columnChart, computeChrome, computeLabelBounds, contrastRatio, dataTable, detectCollision, donutChart, dotChart, elementRef, estimateTextWidth, findAccessibleColor, formatDate, formatNumber, generateAltText, generateAriaLabels, generateDataTable, getBreakpoint, getHeightClass, getLayoutStrategy, getRepresentativeColor, inferFieldType, isChartSpec, isConditionalDef, isEncodingChannel, isGradientDef, isGraphSpec, isLayerSpec, isRangeAnnotation, isRefLineAnnotation, isSankeySpec, isTableSpec, isTextAnnotation, lineChart, meetsAA, pieChart, resolveCollisions, resolveMarkDef, resolveMarkType, resolveTheme, scatterChart, simulateColorBlindness };
package/dist/index.js CHANGED
@@ -1048,7 +1048,15 @@ function estimateCharWidth(fontSize, fontWeight = 400) {
1048
1048
  return fontSize * AVG_CHAR_WIDTH_RATIO * weightFactor;
1049
1049
  }
1050
1050
  function estimateTextWidth(text, fontSize, fontWeight = 400) {
1051
- return text.length * estimateCharWidth(fontSize, fontWeight);
1051
+ const charWidth = estimateCharWidth(fontSize, fontWeight);
1052
+ if (text.includes("\n")) {
1053
+ let maxLen = 0;
1054
+ for (const line of text.split("\n")) {
1055
+ if (line.length > maxLen) maxLen = line.length;
1056
+ }
1057
+ return maxLen * charWidth;
1058
+ }
1059
+ return text.length * charWidth;
1052
1060
  }
1053
1061
  var BRAND_RESERVE_WIDTH = 110;
1054
1062
  var BRAND_FONT_SIZE = 12;
@@ -1083,14 +1091,32 @@ function buildTextStyle(defaults, fontFamily, textColor, width, overrides) {
1083
1091
  dominantBaseline: "hanging"
1084
1092
  };
1085
1093
  }
1086
- function estimateLineCount(text, style, maxWidth, _measureText) {
1094
+ function estimateLineCount(text, style, maxWidth, measureText) {
1087
1095
  if (maxWidth <= 0) return 1;
1088
1096
  const segments = text.split("\n");
1089
1097
  if (segments.length > 1) {
1090
1098
  return segments.reduce((total, segment) => {
1091
- return total + (segment.length === 0 ? 1 : estimateLineCount(segment, style, maxWidth, _measureText));
1099
+ return total + (segment.length === 0 ? 1 : estimateLineCount(segment, style, maxWidth, measureText));
1092
1100
  }, 0);
1093
1101
  }
1102
+ if (measureText) {
1103
+ const textWidth = measureText(text, style.fontSize, style.fontWeight).width;
1104
+ if (textWidth <= maxWidth) return 1;
1105
+ const words2 = text.split(" ");
1106
+ let lines2 = 1;
1107
+ let current2 = "";
1108
+ for (const word of words2) {
1109
+ const candidate = current2 ? `${current2} ${word}` : word;
1110
+ const candidateWidth = measureText(candidate, style.fontSize, style.fontWeight).width;
1111
+ if (candidateWidth > maxWidth && current2) {
1112
+ lines2++;
1113
+ current2 = word;
1114
+ } else {
1115
+ current2 = candidate;
1116
+ }
1117
+ }
1118
+ return lines2;
1119
+ }
1094
1120
  const charWidth = estimateCharWidth(style.fontSize, style.fontWeight);
1095
1121
  const maxChars = Math.floor(maxWidth / charWidth);
1096
1122
  if (text.length <= maxChars) return 1;