@opendata-ai/openchart-core 6.19.3 → 6.20.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 +28 -1
- package/dist/index.js +49 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/layout/__tests__/text-measure.test.ts +9 -0
- package/src/layout/index.ts +1 -0
- package/src/layout/text-wrap.ts +93 -0
package/dist/index.d.ts
CHANGED
|
@@ -2785,6 +2785,33 @@ declare const BRAND_FONT_SIZE = 12;
|
|
|
2785
2785
|
/** Minimum chart width to render the brand watermark (px). */
|
|
2786
2786
|
declare const BRAND_MIN_WIDTH = 120;
|
|
2787
2787
|
|
|
2788
|
+
/**
|
|
2789
|
+
* Word-wrap a text string into lines that fit within a max width.
|
|
2790
|
+
*
|
|
2791
|
+
* Shared by the SVG chart renderer and the sankey renderer. When a
|
|
2792
|
+
* `measureText` callback is provided, uses real DOM measurement for
|
|
2793
|
+
* accurate wrapping. Otherwise falls back to a character-width
|
|
2794
|
+
* heuristic driven by `estimateCharWidth` from text-measure.ts.
|
|
2795
|
+
*
|
|
2796
|
+
* Callers that want heuristic-only behavior (e.g. sankey) should omit
|
|
2797
|
+
* the measureText argument. Do not change the signature without
|
|
2798
|
+
* re-verifying visual baselines for every caller.
|
|
2799
|
+
*/
|
|
2800
|
+
|
|
2801
|
+
/**
|
|
2802
|
+
* Break text into lines that fit within maxWidth using word wrapping.
|
|
2803
|
+
*
|
|
2804
|
+
* Splits on explicit newlines first, then word-wraps each segment.
|
|
2805
|
+
* Preserves empty segments so consecutive newlines produce blank lines.
|
|
2806
|
+
*
|
|
2807
|
+
* @param text - The text to wrap. May contain `\n` for forced breaks.
|
|
2808
|
+
* @param fontSize - Font size in pixels.
|
|
2809
|
+
* @param fontWeight - Font weight (100-900).
|
|
2810
|
+
* @param maxWidth - Maximum line width in pixels. Non-positive values return `[text]` unchanged.
|
|
2811
|
+
* @param measureText - Optional real text measurer. When omitted, uses a character-width heuristic.
|
|
2812
|
+
*/
|
|
2813
|
+
declare function wrapText(text: string, fontSize: number, fontWeight: number, maxWidth: number, measureText?: MeasureTextFn): string[];
|
|
2814
|
+
|
|
2788
2815
|
/**
|
|
2789
2816
|
* Label collision detection and resolution.
|
|
2790
2817
|
*
|
|
@@ -3107,4 +3134,4 @@ declare function scatterChart(data: DataRow[], x: FieldRef, y: FieldRef, options
|
|
|
3107
3134
|
*/
|
|
3108
3135
|
declare function dataTable(data: DataRow[], options?: TableBuilderOptions): TableSpec;
|
|
3109
3136
|
|
|
3110
|
-
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 };
|
|
3137
|
+
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, wrapText };
|
package/dist/index.js
CHANGED
|
@@ -1271,6 +1271,53 @@ function computeChrome(chrome, theme, width, measureText, chromeMode = "full", p
|
|
|
1271
1271
|
};
|
|
1272
1272
|
}
|
|
1273
1273
|
|
|
1274
|
+
// src/layout/text-wrap.ts
|
|
1275
|
+
function wrapText(text, fontSize, fontWeight, maxWidth, measureText) {
|
|
1276
|
+
if (maxWidth <= 0) return [text];
|
|
1277
|
+
const segments = text.split("\n");
|
|
1278
|
+
if (segments.length > 1) {
|
|
1279
|
+
return segments.flatMap(
|
|
1280
|
+
(segment) => segment.length === 0 ? [""] : wrapText(segment, fontSize, fontWeight, maxWidth, measureText)
|
|
1281
|
+
);
|
|
1282
|
+
}
|
|
1283
|
+
if (measureText) {
|
|
1284
|
+
const textWidth = measureText(text, fontSize, fontWeight).width;
|
|
1285
|
+
if (textWidth <= maxWidth) return [text];
|
|
1286
|
+
const words2 = text.split(" ");
|
|
1287
|
+
const lines2 = [];
|
|
1288
|
+
let current2 = "";
|
|
1289
|
+
for (const word of words2) {
|
|
1290
|
+
const candidate = current2 ? `${current2} ${word}` : word;
|
|
1291
|
+
const candidateWidth = measureText(candidate, fontSize, fontWeight).width;
|
|
1292
|
+
if (candidateWidth > maxWidth && current2) {
|
|
1293
|
+
lines2.push(current2);
|
|
1294
|
+
current2 = word;
|
|
1295
|
+
} else {
|
|
1296
|
+
current2 = candidate;
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
if (current2) lines2.push(current2);
|
|
1300
|
+
return lines2;
|
|
1301
|
+
}
|
|
1302
|
+
const charWidth = estimateCharWidth(fontSize, fontWeight);
|
|
1303
|
+
const maxChars = Math.floor(maxWidth / charWidth);
|
|
1304
|
+
if (text.length <= maxChars) return [text];
|
|
1305
|
+
const words = text.split(" ");
|
|
1306
|
+
const lines = [];
|
|
1307
|
+
let current = "";
|
|
1308
|
+
for (const word of words) {
|
|
1309
|
+
const candidate = current ? `${current} ${word}` : word;
|
|
1310
|
+
if (candidate.length > maxChars && current) {
|
|
1311
|
+
lines.push(current);
|
|
1312
|
+
current = word;
|
|
1313
|
+
} else {
|
|
1314
|
+
current = candidate;
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
if (current) lines.push(current);
|
|
1318
|
+
return lines;
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1274
1321
|
// src/responsive/breakpoints.ts
|
|
1275
1322
|
var BREAKPOINT_COMPACT_MAX = 400;
|
|
1276
1323
|
var BREAKPOINT_MEDIUM_MAX = 700;
|
|
@@ -2843,6 +2890,7 @@ export {
|
|
|
2843
2890
|
resolveMarkType,
|
|
2844
2891
|
resolveTheme,
|
|
2845
2892
|
scatterChart,
|
|
2846
|
-
simulateColorBlindness
|
|
2893
|
+
simulateColorBlindness,
|
|
2894
|
+
wrapText
|
|
2847
2895
|
};
|
|
2848
2896
|
//# sourceMappingURL=index.js.map
|