@zombie-mermaid/ascii-renderer 2.2.1
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/LICENSE +22 -0
- package/dist/index.cjs +8 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +146 -0
- package/dist/index.d.ts +146 -0
- package/dist/index.js +5392 -0
- package/dist/index.js.map +1 -0
- package/package.json +36 -0
- package/src/__tests__/ascii-arrowhead-direction-1083.test.ts +51 -0
- package/src/__tests__/ascii-canvas-first-claim-wins-1093.test.ts +89 -0
- package/src/__tests__/ascii-canvas-size-offset-1093.test.ts +95 -0
- package/src/__tests__/ascii-canvas-write.test.ts +132 -0
- package/src/__tests__/ascii-chain-edge-overlap-1067.test.ts +100 -0
- package/src/__tests__/ascii-charset-border-junctions.test.ts +63 -0
- package/src/__tests__/ascii-cjk-width.test.ts +150 -0
- package/src/__tests__/ascii-class-box-occupancy.test.ts +462 -0
- package/src/__tests__/ascii-class-column-width-488-489.test.ts +639 -0
- package/src/__tests__/ascii-class-cross-level-jog-corruption.test.ts +187 -0
- package/src/__tests__/ascii-class-detour-label-routing-487.test.ts +114 -0
- package/src/__tests__/ascii-class-diagram-compartments.test.ts +70 -0
- package/src/__tests__/ascii-class-label-box-collision.test.ts +60 -0
- package/src/__tests__/ascii-class-label-row-collision-531.test.ts +135 -0
- package/src/__tests__/ascii-class-label-territory-row-awareness.test.ts +57 -0
- package/src/__tests__/ascii-class-padding.test.ts +104 -0
- package/src/__tests__/ascii-class-parent-alignment-971.test.ts +139 -0
- package/src/__tests__/ascii-class-parent-alignment-972.test.ts +208 -0
- package/src/__tests__/ascii-class-reciprocal-relationships-448.test.ts +169 -0
- package/src/__tests__/ascii-combining-mark-width.test.ts +70 -0
- package/src/__tests__/ascii-coords-overlay.test.ts +64 -0
- package/src/__tests__/ascii-decision-lr-box-start.test.ts +106 -0
- package/src/__tests__/ascii-display-width-unit.test.ts +154 -0
- package/src/__tests__/ascii-draw-arrows-coverage.test.ts +224 -0
- package/src/__tests__/ascii-draw-arrows-single-point-path.test.ts +105 -0
- package/src/__tests__/ascii-edge-bundling-rank-violation-454.test.ts +72 -0
- package/src/__tests__/ascii-edge-ending-glyphs.test.ts +206 -0
- package/src/__tests__/ascii-edge-label-diagonal-fallback-418.test.ts +95 -0
- package/src/__tests__/ascii-edge-routing-fixes.test.ts +178 -0
- package/src/__tests__/ascii-edge-routing-single-point-path.test.ts +103 -0
- package/src/__tests__/ascii-edge-style-consistency-1067.test.ts +89 -0
- package/src/__tests__/ascii-edge-styles.test.ts +149 -0
- package/src/__tests__/ascii-emoji-cluster-width.test.ts +101 -0
- package/src/__tests__/ascii-er-box-occupancy.test.ts +251 -0
- package/src/__tests__/ascii-er-cardinality.test.ts +40 -0
- package/src/__tests__/ascii-er-corner-glyphs.test.ts +249 -0
- package/src/__tests__/ascii-er-jog-stray-line.test.ts +111 -0
- package/src/__tests__/ascii-er-label-padding.test.ts +118 -0
- package/src/__tests__/ascii-er-padding.test.ts +114 -0
- package/src/__tests__/ascii-er-relationship-label-corruption-350.test.ts +458 -0
- package/src/__tests__/ascii-er-relationship-overwrite.test.ts +325 -0
- package/src/__tests__/ascii-er-stray-connectors.test.ts +344 -0
- package/src/__tests__/ascii-er-unrelated-stem-separation-411.test.ts +98 -0
- package/src/__tests__/ascii-er-vertical-one-marker.test.ts +110 -0
- package/src/__tests__/ascii-label-line-terminal-fallback.test.ts +168 -0
- package/src/__tests__/ascii-lane-search.test.ts +207 -0
- package/src/__tests__/ascii-multibox-cjk-width.test.ts +184 -0
- package/src/__tests__/ascii-multiline.test.ts +288 -0
- package/src/__tests__/ascii-padding-edge-cases.test.ts +154 -0
- package/src/__tests__/ascii-pathfinder-route-edge.test.ts +184 -0
- package/src/__tests__/ascii-sequence-alt-else-label.test.ts +236 -0
- package/src/__tests__/ascii-sequence-block-wall-clearance.test.ts +217 -0
- package/src/__tests__/ascii-sequence-box-group.test.ts +159 -0
- package/src/__tests__/ascii-sequence-cjk-width.test.ts +235 -0
- package/src/__tests__/ascii-sequence-create-destroy.test.ts +114 -0
- package/src/__tests__/ascii-sequence-form-invariants.test.ts +432 -0
- package/src/__tests__/ascii-sequence-mermaid-parity.test.ts +219 -0
- package/src/__tests__/ascii-sequence-notes.test.ts +61 -0
- package/src/__tests__/ascii-sequence-padding.test.ts +119 -0
- package/src/__tests__/ascii-sequence-self-arrow.test.ts +206 -0
- package/src/__tests__/ascii-shape-diamond.test.ts +38 -0
- package/src/__tests__/ascii-shape-rectangle.test.ts +258 -0
- package/src/__tests__/ascii-shape-rounded.test.ts +36 -0
- package/src/__tests__/ascii-shapes-circle.test.ts +41 -0
- package/src/__tests__/ascii-shapes-hexagon.test.ts +42 -0
- package/src/__tests__/ascii-shapes-special.test.ts +344 -0
- package/src/__tests__/ascii-shapes-stadium.test.ts +217 -0
- package/src/__tests__/ascii-shapes-state.test.ts +224 -0
- package/src/__tests__/ascii-state-bidirectional-label-swap-530.test.ts +130 -0
- package/src/__tests__/ascii-subgraph-direction-honored-445.test.ts +90 -0
- package/src/__tests__/ascii-subgraph-label-border-clip.test.ts +152 -0
- package/src/__tests__/ascii-subgraph-title-padding.test.ts +77 -0
- package/src/__tests__/ascii-territory-unit.test.ts +219 -0
- package/src/__tests__/ascii-validate.test.ts +220 -0
- package/src/__tests__/ascii.test.ts +325 -0
- package/src/__tests__/class-arrow-directions.test.ts +505 -0
- package/src/__tests__/draw-lines.test.ts +93 -0
- package/src/__tests__/edge-cell-styles.test.ts +278 -0
- package/src/__tests__/grid-occupancy.test.ts +240 -0
- package/src/__tests__/helpers/ascii-form.ts +142 -0
- package/src/__tests__/helpers/terminal-display-width.ts +74 -0
- package/src/__tests__/pathfinder.test.ts +239 -0
- package/src/__tests__/testdata/ascii/ampersand_lhs.txt +18 -0
- package/src/__tests__/testdata/ascii/ampersand_lhs_and_rhs.txt +18 -0
- package/src/__tests__/testdata/ascii/ampersand_rhs.txt +18 -0
- package/src/__tests__/testdata/ascii/ampersand_td_fanin.txt +18 -0
- package/src/__tests__/testdata/ascii/ampersand_td_fanout.txt +18 -0
- package/src/__tests__/testdata/ascii/ampersand_without_edge.txt +18 -0
- package/src/__tests__/testdata/ascii/back_reference_from_child.txt +10 -0
- package/src/__tests__/testdata/ascii/backlink_from_bottom.txt +22 -0
- package/src/__tests__/testdata/ascii/backlink_from_top.txt +22 -0
- package/src/__tests__/testdata/ascii/backlink_with_short_y_padding.txt +20 -0
- package/src/__tests__/testdata/ascii/cls_all_relationships.txt +19 -0
- package/src/__tests__/testdata/ascii/cls_annotation.txt +29 -0
- package/src/__tests__/testdata/ascii/cls_association.txt +14 -0
- package/src/__tests__/testdata/ascii/cls_basic.txt +15 -0
- package/src/__tests__/testdata/ascii/cls_dependency.txt +14 -0
- package/src/__tests__/testdata/ascii/cls_inheritance.txt +20 -0
- package/src/__tests__/testdata/ascii/cls_methods.txt +21 -0
- package/src/__tests__/testdata/ascii/comments.txt +23 -0
- package/src/__tests__/testdata/ascii/custom_padding.txt +10 -0
- package/src/__tests__/testdata/ascii/duplicate_labels.txt +19 -0
- package/src/__tests__/testdata/ascii/er_attributes.txt +21 -0
- package/src/__tests__/testdata/ascii/er_basic.txt +8 -0
- package/src/__tests__/testdata/ascii/er_identifying.txt +18 -0
- package/src/__tests__/testdata/ascii/flowchart_tb_simple.txt +29 -0
- package/src/__tests__/testdata/ascii/graph_bt_direction.txt +28 -0
- package/src/__tests__/testdata/ascii/graph_tb_direction.txt +26 -0
- package/src/__tests__/testdata/ascii/nested_subgraphs_with_labels.txt +36 -0
- package/src/__tests__/testdata/ascii/preserve_order_of_definition.txt +23 -0
- package/src/__tests__/testdata/ascii/self_reference.txt +10 -0
- package/src/__tests__/testdata/ascii/self_reference_with_edge.txt +10 -0
- package/src/__tests__/testdata/ascii/seq_basic.txt +17 -0
- package/src/__tests__/testdata/ascii/seq_multiple_messages.txt +25 -0
- package/src/__tests__/testdata/ascii/seq_self_message.txt +18 -0
- package/src/__tests__/testdata/ascii/single_node.txt +8 -0
- package/src/__tests__/testdata/ascii/single_node_longer_name.txt +8 -0
- package/src/__tests__/testdata/ascii/subgraph_complex_mixed.txt +38 -0
- package/src/__tests__/testdata/ascii/subgraph_complex_nested.txt +49 -0
- package/src/__tests__/testdata/ascii/subgraph_direction_override.txt +47 -0
- package/src/__tests__/testdata/ascii/subgraph_empty.txt +10 -0
- package/src/__tests__/testdata/ascii/subgraph_mixed_nodes.txt +20 -0
- package/src/__tests__/testdata/ascii/subgraph_mixed_nodes_td.txt +48 -0
- package/src/__tests__/testdata/ascii/subgraph_multiple_edges.txt +32 -0
- package/src/__tests__/testdata/ascii/subgraph_multiple_nodes.txt +16 -0
- package/src/__tests__/testdata/ascii/subgraph_nested.txt +24 -0
- package/src/__tests__/testdata/ascii/subgraph_nested_with_external.txt +30 -0
- package/src/__tests__/testdata/ascii/subgraph_node_outside_lr.txt +17 -0
- package/src/__tests__/testdata/ascii/subgraph_single_node.txt +16 -0
- package/src/__tests__/testdata/ascii/subgraph_td_direction.txt +26 -0
- package/src/__tests__/testdata/ascii/subgraph_td_multiple.txt +44 -0
- package/src/__tests__/testdata/ascii/subgraph_td_multiple_paddingy.txt +42 -0
- package/src/__tests__/testdata/ascii/subgraph_three_levels_nested.txt +32 -0
- package/src/__tests__/testdata/ascii/subgraph_three_separate.txt +24 -0
- package/src/__tests__/testdata/ascii/subgraph_two_separate.txt +20 -0
- package/src/__tests__/testdata/ascii/subgraph_with_labels.txt +20 -0
- package/src/__tests__/testdata/ascii/three_nodes.txt +9 -0
- package/src/__tests__/testdata/ascii/three_nodes_single_line.txt +8 -0
- package/src/__tests__/testdata/ascii/two_layer_single_graph.txt +19 -0
- package/src/__tests__/testdata/ascii/two_layer_single_graph_longer_names.txt +19 -0
- package/src/__tests__/testdata/ascii/two_nodes_linked.txt +8 -0
- package/src/__tests__/testdata/ascii/two_nodes_longer_names.txt +8 -0
- package/src/__tests__/testdata/ascii/two_root_nodes.txt +19 -0
- package/src/__tests__/testdata/ascii/two_root_nodes_longer_names.txt +19 -0
- package/src/__tests__/testdata/ascii/two_single_root_nodes.txt +19 -0
- package/src/__tests__/testdata/unicode/ampersand_lhs.txt +18 -0
- package/src/__tests__/testdata/unicode/ampersand_lhs_and_rhs.txt +18 -0
- package/src/__tests__/testdata/unicode/ampersand_rhs.txt +18 -0
- package/src/__tests__/testdata/unicode/ampersand_without_edge.txt +18 -0
- package/src/__tests__/testdata/unicode/back_reference_from_child.txt +10 -0
- package/src/__tests__/testdata/unicode/backlink_from_bottom.txt +22 -0
- package/src/__tests__/testdata/unicode/backlink_from_top.txt +22 -0
- package/src/__tests__/testdata/unicode/cls_all_relationships.txt +19 -0
- package/src/__tests__/testdata/unicode/cls_annotation.txt +29 -0
- package/src/__tests__/testdata/unicode/cls_association.txt +14 -0
- package/src/__tests__/testdata/unicode/cls_basic.txt +15 -0
- package/src/__tests__/testdata/unicode/cls_dependency.txt +14 -0
- package/src/__tests__/testdata/unicode/cls_inheritance.txt +20 -0
- package/src/__tests__/testdata/unicode/cls_methods.txt +21 -0
- package/src/__tests__/testdata/unicode/comments.txt +23 -0
- package/src/__tests__/testdata/unicode/duplicate_labels.txt +19 -0
- package/src/__tests__/testdata/unicode/er_attributes.txt +21 -0
- package/src/__tests__/testdata/unicode/er_basic.txt +8 -0
- package/src/__tests__/testdata/unicode/er_identifying.txt +18 -0
- package/src/__tests__/testdata/unicode/graph_bt_direction.txt +28 -0
- package/src/__tests__/testdata/unicode/preserve_order_of_definition.txt +23 -0
- package/src/__tests__/testdata/unicode/self_reference.txt +10 -0
- package/src/__tests__/testdata/unicode/self_reference_with_edge.txt +10 -0
- package/src/__tests__/testdata/unicode/seq_basic.txt +17 -0
- package/src/__tests__/testdata/unicode/seq_multiple_messages.txt +25 -0
- package/src/__tests__/testdata/unicode/seq_self_message.txt +18 -0
- package/src/__tests__/testdata/unicode/single_node.txt +8 -0
- package/src/__tests__/testdata/unicode/single_node_longer_name.txt +8 -0
- package/src/__tests__/testdata/unicode/three_nodes.txt +9 -0
- package/src/__tests__/testdata/unicode/three_nodes_single_line.txt +8 -0
- package/src/__tests__/testdata/unicode/two_layer_single_graph.txt +19 -0
- package/src/__tests__/testdata/unicode/two_layer_single_graph_longer_names.txt +19 -0
- package/src/__tests__/testdata/unicode/two_nodes_linked.txt +8 -0
- package/src/__tests__/testdata/unicode/two_nodes_longer_names.txt +8 -0
- package/src/__tests__/testdata/unicode/two_root_nodes.txt +19 -0
- package/src/__tests__/testdata/unicode/two_root_nodes_longer_names.txt +19 -0
- package/src/__tests__/testdata/unicode/two_single_root_nodes.txt +19 -0
- package/src/__tests__/xychart-ascii.test.ts +376 -0
- package/src/ansi.ts +490 -0
- package/src/canvas.ts +757 -0
- package/src/class-diagram.ts +2001 -0
- package/src/converter.ts +446 -0
- package/src/coords.ts +58 -0
- package/src/display-width.ts +151 -0
- package/src/draw-arrows.ts +593 -0
- package/src/draw-boxes.ts +267 -0
- package/src/draw-bundles.ts +611 -0
- package/src/draw-lines.ts +174 -0
- package/src/draw-subgraphs.ts +108 -0
- package/src/draw.ts +350 -0
- package/src/edge-bundling.ts +435 -0
- package/src/edge-cell-styles.ts +209 -0
- package/src/edge-routing.ts +1070 -0
- package/src/er-diagram.ts +1488 -0
- package/src/flowchart.ts +94 -0
- package/src/grid-occupancy.ts +234 -0
- package/src/grid.ts +1309 -0
- package/src/hyperlinks.ts +248 -0
- package/src/index.ts +163 -0
- package/src/lane-search.ts +68 -0
- package/src/multiline-utils.ts +82 -0
- package/src/pathfinder.ts +448 -0
- package/src/registry.ts +88 -0
- package/src/sequence.ts +1318 -0
- package/src/shapes/circle.ts +31 -0
- package/src/shapes/corners.ts +273 -0
- package/src/shapes/diamond.ts +31 -0
- package/src/shapes/hexagon.ts +35 -0
- package/src/shapes/index.ts +123 -0
- package/src/shapes/rectangle.ts +199 -0
- package/src/shapes/rounded.ts +31 -0
- package/src/shapes/special.ts +360 -0
- package/src/shapes/stadium.ts +122 -0
- package/src/shapes/state.ts +204 -0
- package/src/shapes/types.ts +78 -0
- package/src/territory.ts +136 -0
- package/src/types.ts +454 -0
- package/src/validate.ts +189 -0
- package/src/xychart.ts +1085 -0
package/src/xychart.ts
ADDED
|
@@ -0,0 +1,1085 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// ASCII renderer — XY Chart
|
|
3
|
+
//
|
|
4
|
+
// Renders xychart-beta diagrams to ASCII/Unicode text art.
|
|
5
|
+
// Uses the parsed XYChart type directly (not PositionedXYChart) since
|
|
6
|
+
// pixel coordinates don't map to character grids.
|
|
7
|
+
//
|
|
8
|
+
// Bar charts: █ (Unicode) or # (ASCII) block characters.
|
|
9
|
+
// Line charts: continuous staircase routing with rounded corners (╭╮╰╯│─).
|
|
10
|
+
//
|
|
11
|
+
// Multi-series support: each series gets a distinct color from a palette.
|
|
12
|
+
// ============================================================================
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
parseXYChart,
|
|
16
|
+
getSeriesColor,
|
|
17
|
+
CHART_ACCENT_FALLBACK,
|
|
18
|
+
} from '@zombie-mermaid/mermaid-parser'
|
|
19
|
+
import type { XYChart } from '@zombie-mermaid/mermaid-parser'
|
|
20
|
+
import type {
|
|
21
|
+
AsciiConfig,
|
|
22
|
+
AsciiTheme,
|
|
23
|
+
ColorMode,
|
|
24
|
+
CharRole,
|
|
25
|
+
Canvas,
|
|
26
|
+
RoleCanvas,
|
|
27
|
+
} from './types.ts'
|
|
28
|
+
import { colorizeText } from './ansi.ts'
|
|
29
|
+
import { mkCanvas, mkRoleCanvas, write } from './canvas.ts'
|
|
30
|
+
import { splitStatements } from '@zombie-mermaid/core'
|
|
31
|
+
|
|
32
|
+
// ============================================================================
|
|
33
|
+
// Constants
|
|
34
|
+
// ============================================================================
|
|
35
|
+
|
|
36
|
+
const PLOT_WIDTH = 60
|
|
37
|
+
const PLOT_HEIGHT = 20
|
|
38
|
+
|
|
39
|
+
// Unicode box-drawing characters
|
|
40
|
+
const UNI = {
|
|
41
|
+
hLine: '─',
|
|
42
|
+
vLine: '│',
|
|
43
|
+
origin: '┼',
|
|
44
|
+
yTick: '┤',
|
|
45
|
+
xTick: '┬',
|
|
46
|
+
bar: '█',
|
|
47
|
+
grid: '·',
|
|
48
|
+
cornerTL: '╭', // top-left: down+right
|
|
49
|
+
cornerTR: '╮', // top-right: down+left
|
|
50
|
+
cornerBL: '╰', // bottom-left: up+right
|
|
51
|
+
cornerBR: '╯', // bottom-right: up+left
|
|
52
|
+
} as const
|
|
53
|
+
|
|
54
|
+
// ASCII fallback characters
|
|
55
|
+
const ASC = {
|
|
56
|
+
hLine: '-',
|
|
57
|
+
vLine: '|',
|
|
58
|
+
origin: '+',
|
|
59
|
+
yTick: '+',
|
|
60
|
+
xTick: '+',
|
|
61
|
+
bar: '#',
|
|
62
|
+
grid: '.',
|
|
63
|
+
cornerTL: '+',
|
|
64
|
+
cornerTR: '+',
|
|
65
|
+
cornerBL: '+',
|
|
66
|
+
cornerBR: '+',
|
|
67
|
+
} as const
|
|
68
|
+
|
|
69
|
+
// ============================================================================
|
|
70
|
+
// Multi-series color support
|
|
71
|
+
// ============================================================================
|
|
72
|
+
|
|
73
|
+
/** Per-cell hex color override canvas. Parallel to RoleCanvas. */
|
|
74
|
+
type HexCanvas = (string | null)[][]
|
|
75
|
+
|
|
76
|
+
/** Generate an array of hex colors, one per series. */
|
|
77
|
+
function getSeriesColors(total: number, theme: AsciiTheme): string[] {
|
|
78
|
+
const accent = theme.accent ?? CHART_ACCENT_FALLBACK
|
|
79
|
+
if (total <= 1) return [accent]
|
|
80
|
+
return Array.from({ length: total }, (_, i) =>
|
|
81
|
+
getSeriesColor(i, accent, theme.bg),
|
|
82
|
+
)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Map a CharRole to its hex color from the theme (for canvasToString fallback). */
|
|
86
|
+
function roleToHex(role: CharRole, theme: AsciiTheme): string {
|
|
87
|
+
switch (role) {
|
|
88
|
+
case 'text':
|
|
89
|
+
return theme.fg
|
|
90
|
+
case 'border':
|
|
91
|
+
return theme.border
|
|
92
|
+
case 'line':
|
|
93
|
+
return theme.line
|
|
94
|
+
case 'arrow':
|
|
95
|
+
return theme.arrow
|
|
96
|
+
case 'corner':
|
|
97
|
+
return theme.corner ?? theme.line
|
|
98
|
+
case 'junction':
|
|
99
|
+
return theme.junction ?? theme.border
|
|
100
|
+
default:
|
|
101
|
+
return theme.fg
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ============================================================================
|
|
106
|
+
// Public API
|
|
107
|
+
// ============================================================================
|
|
108
|
+
|
|
109
|
+
export function renderXYChartAscii(
|
|
110
|
+
text: string,
|
|
111
|
+
config: AsciiConfig,
|
|
112
|
+
colorMode: ColorMode,
|
|
113
|
+
theme: AsciiTheme,
|
|
114
|
+
): string {
|
|
115
|
+
const lines = splitStatements(text)
|
|
116
|
+
const chart = parseXYChart(lines)
|
|
117
|
+
const ch = config.useAscii ? ASC : UNI
|
|
118
|
+
|
|
119
|
+
if (chart.horizontal) {
|
|
120
|
+
return renderHorizontal(chart, ch, colorMode, theme)
|
|
121
|
+
}
|
|
122
|
+
return renderVertical(chart, ch, colorMode, theme)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ============================================================================
|
|
126
|
+
// Vertical chart layout + rendering
|
|
127
|
+
// ============================================================================
|
|
128
|
+
|
|
129
|
+
function renderVertical(
|
|
130
|
+
chart: XYChart,
|
|
131
|
+
ch: typeof UNI | typeof ASC,
|
|
132
|
+
colorMode: ColorMode,
|
|
133
|
+
theme: AsciiTheme,
|
|
134
|
+
): string {
|
|
135
|
+
const dataCount = getDataCount(chart)
|
|
136
|
+
if (dataCount === 0) return ''
|
|
137
|
+
|
|
138
|
+
// parseXYChart always derives a y-axis range (from data or a 0-100
|
|
139
|
+
// fallback — see xychart/parser.ts) before returning, so this is never
|
|
140
|
+
// actually undefined for parsed input. That guarantee lives in a
|
|
141
|
+
// different module, so narrow it here explicitly instead of trusting it
|
|
142
|
+
// silently across the boundary.
|
|
143
|
+
const yRange = chart.yAxis.range
|
|
144
|
+
if (!yRange) {
|
|
145
|
+
/* v8 ignore next */
|
|
146
|
+
throw new Error('XY chart: y-axis range was not set by the parser')
|
|
147
|
+
}
|
|
148
|
+
const yTicks = niceTickValues(yRange.min, yRange.max)
|
|
149
|
+
const yLabels = yTicks.map((v) => formatTickValue(v))
|
|
150
|
+
const yGutter = Math.max(...yLabels.map((l) => l.length)) + 1
|
|
151
|
+
|
|
152
|
+
const plotW = Math.max(PLOT_WIDTH, dataCount * 6)
|
|
153
|
+
const plotH = PLOT_HEIGHT
|
|
154
|
+
const bandW = Math.floor(plotW / dataCount)
|
|
155
|
+
const catLabels = getCategoryLabels(chart, dataCount)
|
|
156
|
+
|
|
157
|
+
// Canvas dimensions
|
|
158
|
+
const hasTitle = !!chart.title
|
|
159
|
+
const hasXTitle = !!chart.xAxis.title
|
|
160
|
+
const hasYTitle = !!chart.yAxis.title
|
|
161
|
+
const hasLegend = chart.series.length > 1
|
|
162
|
+
const titleRow = hasTitle ? 0 : -1
|
|
163
|
+
const plotTop = (hasTitle ? 2 : 0) + (hasLegend ? 1 : 0)
|
|
164
|
+
// Reserve a column for the rotated y-axis title (rendered top-to-bottom,
|
|
165
|
+
// one character per row, mirroring how mermaid.js rotates it 90deg along
|
|
166
|
+
// the left edge) plus a 1-column gap before the tick-value gutter.
|
|
167
|
+
const yTitleWidth = hasYTitle ? 2 : 0
|
|
168
|
+
const plotLeft = yTitleWidth + yGutter + 1 // +1 for axis character
|
|
169
|
+
const totalW = plotLeft + bandW * dataCount + 2
|
|
170
|
+
const xAxisRow = plotTop + plotH
|
|
171
|
+
const xLabelRow = xAxisRow + 1
|
|
172
|
+
const xTitleRow = hasXTitle ? xLabelRow + 1 : -1
|
|
173
|
+
const totalH =
|
|
174
|
+
xLabelRow + 1 + (hasXTitle ? 1 : 0) + (hasLegend && !hasTitle ? 0 : 0)
|
|
175
|
+
|
|
176
|
+
// Create canvas
|
|
177
|
+
const canvas = createCanvas(totalW, totalH)
|
|
178
|
+
const roles = createRoleCanvas(totalW, totalH)
|
|
179
|
+
const hexColors = createHexCanvas(totalW, totalH)
|
|
180
|
+
|
|
181
|
+
// Series colors
|
|
182
|
+
const seriesColors = getSeriesColors(chart.series.length, theme)
|
|
183
|
+
|
|
184
|
+
// Scales
|
|
185
|
+
const valueToRow = (v: number): number => {
|
|
186
|
+
const t = (v - yRange.min) / (yRange.max - yRange.min || 1)
|
|
187
|
+
return Math.round(t * (plotH - 1))
|
|
188
|
+
}
|
|
189
|
+
const bandCenter = (i: number): number =>
|
|
190
|
+
plotLeft + Math.floor(bandW * (i + 0.5))
|
|
191
|
+
|
|
192
|
+
// 1. Title
|
|
193
|
+
if (chart.title && titleRow >= 0) {
|
|
194
|
+
writeText(
|
|
195
|
+
canvas,
|
|
196
|
+
roles,
|
|
197
|
+
titleRow,
|
|
198
|
+
Math.floor(totalW / 2 - chart.title.length / 2),
|
|
199
|
+
chart.title,
|
|
200
|
+
'text',
|
|
201
|
+
)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// 2. Legend
|
|
205
|
+
if (hasLegend) {
|
|
206
|
+
const legendRow = hasTitle ? 1 : 0
|
|
207
|
+
drawLegend(
|
|
208
|
+
canvas,
|
|
209
|
+
roles,
|
|
210
|
+
hexColors,
|
|
211
|
+
chart,
|
|
212
|
+
legendRow,
|
|
213
|
+
totalW,
|
|
214
|
+
ch,
|
|
215
|
+
seriesColors,
|
|
216
|
+
)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// 3. Y-axis line + ticks + labels
|
|
220
|
+
for (let row = 0; row < plotH; row++) {
|
|
221
|
+
const displayRow = plotTop + (plotH - 1 - row)
|
|
222
|
+
set(canvas, roles, displayRow, plotLeft - 1, ch.vLine, 'border')
|
|
223
|
+
}
|
|
224
|
+
// Origin
|
|
225
|
+
set(canvas, roles, xAxisRow, plotLeft - 1, ch.origin, 'border')
|
|
226
|
+
|
|
227
|
+
for (const tick of yTicks) {
|
|
228
|
+
const row = valueToRow(tick)
|
|
229
|
+
if (row < 0 || row >= plotH) continue
|
|
230
|
+
const displayRow = plotTop + (plotH - 1 - row)
|
|
231
|
+
const label = formatTickValue(tick)
|
|
232
|
+
// Tick mark on axis
|
|
233
|
+
set(
|
|
234
|
+
canvas,
|
|
235
|
+
roles,
|
|
236
|
+
displayRow,
|
|
237
|
+
plotLeft - 1,
|
|
238
|
+
row === 0 ? ch.origin : ch.yTick,
|
|
239
|
+
'border',
|
|
240
|
+
)
|
|
241
|
+
// Label
|
|
242
|
+
const labelStart = yTitleWidth + yGutter - label.length
|
|
243
|
+
writeText(canvas, roles, displayRow, Math.max(0, labelStart), label, 'text')
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// 4. X-axis line + ticks + labels
|
|
247
|
+
for (let c = plotLeft; c < plotLeft + bandW * dataCount; c++) {
|
|
248
|
+
set(canvas, roles, xAxisRow, c, ch.hLine, 'border')
|
|
249
|
+
}
|
|
250
|
+
for (let i = 0; i < dataCount; i++) {
|
|
251
|
+
const cx = bandCenter(i)
|
|
252
|
+
set(canvas, roles, xAxisRow, cx, ch.xTick, 'border')
|
|
253
|
+
// Label below
|
|
254
|
+
const label = catLabels[i]!
|
|
255
|
+
const labelStart = cx - Math.floor(label.length / 2)
|
|
256
|
+
writeText(canvas, roles, xLabelRow, Math.max(0, labelStart), label, 'text')
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// 5. X-axis title
|
|
260
|
+
if (chart.xAxis.title && xTitleRow >= 0) {
|
|
261
|
+
const title = chart.xAxis.title
|
|
262
|
+
writeText(
|
|
263
|
+
canvas,
|
|
264
|
+
roles,
|
|
265
|
+
xTitleRow,
|
|
266
|
+
Math.floor(totalW / 2 - title.length / 2),
|
|
267
|
+
title,
|
|
268
|
+
'text',
|
|
269
|
+
)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// 5b. Y-axis title — rendered rotated: one character per row, top-to-
|
|
273
|
+
// bottom, in the leftmost column, mirroring how mermaid.js's SVG rotates
|
|
274
|
+
// this label 90deg along the left edge of the chart. Vertically centered
|
|
275
|
+
// within the plot's row range.
|
|
276
|
+
if (chart.yAxis.title) {
|
|
277
|
+
const title = chart.yAxis.title
|
|
278
|
+
const startRow =
|
|
279
|
+
plotTop + Math.max(0, Math.floor((plotH - title.length) / 2))
|
|
280
|
+
for (let i = 0; i < title.length; i++) {
|
|
281
|
+
const row = startRow + i
|
|
282
|
+
if (row >= plotTop + plotH) break
|
|
283
|
+
set(canvas, roles, row, 0, title[i]!, 'text')
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// 6. Grid lines (subtle horizontal dots at y-tick positions)
|
|
288
|
+
for (const tick of yTicks) {
|
|
289
|
+
const row = valueToRow(tick)
|
|
290
|
+
if (row < 0 || row >= plotH) continue
|
|
291
|
+
const displayRow = plotTop + (plotH - 1 - row)
|
|
292
|
+
for (let c = plotLeft; c < plotLeft + bandW * dataCount; c++) {
|
|
293
|
+
if (get(canvas, displayRow, c) === ' ') {
|
|
294
|
+
set(canvas, roles, displayRow, c, ch.grid, 'line')
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// 7. Bars — track global series index for per-series colors
|
|
300
|
+
const barEntries: { data: number[]; globalIdx: number }[] = []
|
|
301
|
+
for (let si = 0; si < chart.series.length; si++) {
|
|
302
|
+
if (chart.series[si]!.type === 'bar')
|
|
303
|
+
barEntries.push({ data: chart.series[si]!.data, globalIdx: si })
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (barEntries.length > 0) {
|
|
307
|
+
// Multiple bar series in the same xychart-beta share one x-position and
|
|
308
|
+
// width per category — mermaid.js overlays/occludes them rather than
|
|
309
|
+
// laying them out as side-by-side grouped bars (confirmed against real
|
|
310
|
+
// mermaid.js SVG output: bar-plot-N rects for every series in a category
|
|
311
|
+
// carry identical x/width, differing only in y/height — see issue #452).
|
|
312
|
+
// Draw order (series declaration order) is what produces the occlusion:
|
|
313
|
+
// a later series drawn on top of an earlier, taller-or-shorter one.
|
|
314
|
+
const usable = Math.max(1, bandW - 2)
|
|
315
|
+
const singleBarW = Math.max(1, Math.min(usable, 8))
|
|
316
|
+
const baseRow = valueToRow(Math.max(0, yRange.min))
|
|
317
|
+
|
|
318
|
+
for (let bIdx = 0; bIdx < barEntries.length; bIdx++) {
|
|
319
|
+
const entry = barEntries[bIdx]!
|
|
320
|
+
const hexColor = seriesColors[entry.globalIdx]!
|
|
321
|
+
for (let i = 0; i < entry.data.length; i++) {
|
|
322
|
+
const cx = bandCenter(i)
|
|
323
|
+
const bx = cx - Math.floor(singleBarW / 2)
|
|
324
|
+
const valRow = valueToRow(entry.data[i]!)
|
|
325
|
+
const fromRow = Math.min(baseRow, valRow)
|
|
326
|
+
const toRow = Math.max(baseRow, valRow)
|
|
327
|
+
|
|
328
|
+
for (let row = fromRow; row <= toRow; row++) {
|
|
329
|
+
const displayRow = plotTop + (plotH - 1 - row)
|
|
330
|
+
for (let c = bx; c < bx + singleBarW; c++) {
|
|
331
|
+
set(
|
|
332
|
+
canvas,
|
|
333
|
+
roles,
|
|
334
|
+
displayRow,
|
|
335
|
+
c,
|
|
336
|
+
ch.bar,
|
|
337
|
+
'arrow',
|
|
338
|
+
hexColors,
|
|
339
|
+
hexColor,
|
|
340
|
+
)
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// 8. Lines (staircase routing with rounded corners)
|
|
348
|
+
const lineEntries: { data: number[]; globalIdx: number }[] = []
|
|
349
|
+
for (let si = 0; si < chart.series.length; si++) {
|
|
350
|
+
if (chart.series[si]!.type === 'line')
|
|
351
|
+
lineEntries.push({ data: chart.series[si]!.data, globalIdx: si })
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
for (const entry of lineEntries) {
|
|
355
|
+
if (entry.data.length === 0) continue
|
|
356
|
+
const hexColor = seriesColors[entry.globalIdx]!
|
|
357
|
+
drawStaircaseLine(
|
|
358
|
+
canvas,
|
|
359
|
+
roles,
|
|
360
|
+
entry.data,
|
|
361
|
+
bandCenter,
|
|
362
|
+
valueToRow,
|
|
363
|
+
plotTop,
|
|
364
|
+
plotH,
|
|
365
|
+
plotLeft,
|
|
366
|
+
bandW * dataCount,
|
|
367
|
+
ch,
|
|
368
|
+
hexColors,
|
|
369
|
+
hexColor,
|
|
370
|
+
)
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
return canvasToString(canvas, roles, hexColors, colorMode, theme)
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// ============================================================================
|
|
377
|
+
// Horizontal chart layout + rendering
|
|
378
|
+
// ============================================================================
|
|
379
|
+
|
|
380
|
+
function renderHorizontal(
|
|
381
|
+
chart: XYChart,
|
|
382
|
+
ch: typeof UNI | typeof ASC,
|
|
383
|
+
colorMode: ColorMode,
|
|
384
|
+
theme: AsciiTheme,
|
|
385
|
+
): string {
|
|
386
|
+
const dataCount = getDataCount(chart)
|
|
387
|
+
if (dataCount === 0) return ''
|
|
388
|
+
|
|
389
|
+
// See the matching comment in renderVertical: the parser always derives a
|
|
390
|
+
// y-axis range before returning, but that guarantee lives in a different
|
|
391
|
+
// module, so it's narrowed here explicitly rather than trusted silently.
|
|
392
|
+
const yRange = chart.yAxis.range
|
|
393
|
+
if (!yRange) {
|
|
394
|
+
/* v8 ignore next */
|
|
395
|
+
throw new Error('XY chart: y-axis range was not set by the parser')
|
|
396
|
+
}
|
|
397
|
+
const valueTicks = niceTickValues(yRange.min, yRange.max)
|
|
398
|
+
const catLabels = getCategoryLabels(chart, dataCount)
|
|
399
|
+
const catGutter = Math.max(...catLabels.map((l) => l.length)) + 1
|
|
400
|
+
|
|
401
|
+
const plotW = Math.max(PLOT_WIDTH, 40)
|
|
402
|
+
const bandH = Math.max(2, Math.floor(PLOT_HEIGHT / dataCount))
|
|
403
|
+
const plotH = bandH * dataCount
|
|
404
|
+
|
|
405
|
+
const hasTitle = !!chart.title
|
|
406
|
+
const hasYTitle = !!chart.yAxis.title
|
|
407
|
+
const hasLegend = chart.series.length > 1
|
|
408
|
+
const plotTop = (hasTitle ? 2 : 0) + (hasLegend ? 1 : 0)
|
|
409
|
+
const plotLeft = catGutter + 1
|
|
410
|
+
const totalW = plotLeft + plotW + 2
|
|
411
|
+
const totalH = plotTop + plotH + 2 + (hasYTitle ? 1 : 0)
|
|
412
|
+
const xAxisRow = plotTop + plotH
|
|
413
|
+
|
|
414
|
+
const canvas = createCanvas(totalW, totalH)
|
|
415
|
+
const roles = createRoleCanvas(totalW, totalH)
|
|
416
|
+
const hexColors = createHexCanvas(totalW, totalH)
|
|
417
|
+
|
|
418
|
+
// Series colors
|
|
419
|
+
const seriesColors = getSeriesColors(chart.series.length, theme)
|
|
420
|
+
|
|
421
|
+
// Value scale (horizontal)
|
|
422
|
+
const valueToCol = (v: number): number => {
|
|
423
|
+
const t = (v - yRange.min) / (yRange.max - yRange.min || 1)
|
|
424
|
+
return plotLeft + Math.round(t * (plotW - 1))
|
|
425
|
+
}
|
|
426
|
+
const bandMid = (i: number): number => plotTop + Math.floor(bandH * (i + 0.5))
|
|
427
|
+
|
|
428
|
+
// Title
|
|
429
|
+
if (chart.title) {
|
|
430
|
+
writeText(
|
|
431
|
+
canvas,
|
|
432
|
+
roles,
|
|
433
|
+
0,
|
|
434
|
+
Math.floor(totalW / 2 - chart.title.length / 2),
|
|
435
|
+
chart.title,
|
|
436
|
+
'text',
|
|
437
|
+
)
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// Legend
|
|
441
|
+
if (hasLegend) {
|
|
442
|
+
const legendRow = hasTitle ? 1 : 0
|
|
443
|
+
drawLegend(
|
|
444
|
+
canvas,
|
|
445
|
+
roles,
|
|
446
|
+
hexColors,
|
|
447
|
+
chart,
|
|
448
|
+
legendRow,
|
|
449
|
+
totalW,
|
|
450
|
+
ch,
|
|
451
|
+
seriesColors,
|
|
452
|
+
)
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// Y-axis (category axis on left)
|
|
456
|
+
for (let r = plotTop; r < plotTop + plotH; r++) {
|
|
457
|
+
set(canvas, roles, r, plotLeft - 1, ch.vLine, 'border')
|
|
458
|
+
}
|
|
459
|
+
set(canvas, roles, xAxisRow, plotLeft - 1, ch.origin, 'border')
|
|
460
|
+
|
|
461
|
+
for (let i = 0; i < dataCount; i++) {
|
|
462
|
+
const my = bandMid(i)
|
|
463
|
+
const label = catLabels[i]!
|
|
464
|
+
const labelStart = catGutter - label.length
|
|
465
|
+
writeText(canvas, roles, my, Math.max(0, labelStart), label, 'text')
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// X-axis (value axis on bottom)
|
|
469
|
+
for (let c = plotLeft; c < plotLeft + plotW; c++) {
|
|
470
|
+
set(canvas, roles, xAxisRow, c, ch.hLine, 'border')
|
|
471
|
+
}
|
|
472
|
+
for (const tick of valueTicks) {
|
|
473
|
+
const cx = valueToCol(tick)
|
|
474
|
+
if (cx < plotLeft || cx >= plotLeft + plotW) continue
|
|
475
|
+
set(canvas, roles, xAxisRow, cx, ch.xTick, 'border')
|
|
476
|
+
const label = formatTickValue(tick)
|
|
477
|
+
writeText(
|
|
478
|
+
canvas,
|
|
479
|
+
roles,
|
|
480
|
+
xAxisRow + 1,
|
|
481
|
+
cx - Math.floor(label.length / 2),
|
|
482
|
+
label,
|
|
483
|
+
'text',
|
|
484
|
+
)
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// Y-axis title
|
|
488
|
+
if (chart.yAxis.title) {
|
|
489
|
+
const title = chart.yAxis.title
|
|
490
|
+
writeText(
|
|
491
|
+
canvas,
|
|
492
|
+
roles,
|
|
493
|
+
totalH - 1,
|
|
494
|
+
Math.floor(totalW / 2 - title.length / 2),
|
|
495
|
+
title,
|
|
496
|
+
'text',
|
|
497
|
+
)
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// Grid lines (vertical at value tick positions)
|
|
501
|
+
for (const tick of valueTicks) {
|
|
502
|
+
const cx = valueToCol(tick)
|
|
503
|
+
if (cx < plotLeft || cx >= plotLeft + plotW) continue
|
|
504
|
+
for (let r = plotTop; r < plotTop + plotH; r++) {
|
|
505
|
+
if (get(canvas, r, cx) === ' ') {
|
|
506
|
+
set(canvas, roles, r, cx, ch.grid, 'line')
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
// Bars (horizontal) — with per-series colors
|
|
512
|
+
const barEntries: { data: number[]; globalIdx: number }[] = []
|
|
513
|
+
for (let si = 0; si < chart.series.length; si++) {
|
|
514
|
+
if (chart.series[si]!.type === 'bar')
|
|
515
|
+
barEntries.push({ data: chart.series[si]!.data, globalIdx: si })
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
if (barEntries.length > 0) {
|
|
519
|
+
// Same overlay/occlusion behavior as the vertical layout above (issue
|
|
520
|
+
// #452): every bar series in a category shares one row rather than
|
|
521
|
+
// being split into grouped sub-rows, so a later series' bar paints over
|
|
522
|
+
// an earlier one.
|
|
523
|
+
const singleBarH = 1
|
|
524
|
+
const baseCol = valueToCol(Math.max(0, yRange.min))
|
|
525
|
+
|
|
526
|
+
for (let bIdx = 0; bIdx < barEntries.length; bIdx++) {
|
|
527
|
+
const entry = barEntries[bIdx]!
|
|
528
|
+
const hexColor = seriesColors[entry.globalIdx]!
|
|
529
|
+
for (let i = 0; i < entry.data.length; i++) {
|
|
530
|
+
const by = bandMid(i)
|
|
531
|
+
const valCol = valueToCol(entry.data[i]!)
|
|
532
|
+
const fromCol = Math.min(baseCol, valCol)
|
|
533
|
+
const toCol = Math.max(baseCol, valCol)
|
|
534
|
+
|
|
535
|
+
for (let r = by; r < by + singleBarH; r++) {
|
|
536
|
+
for (let c = fromCol; c <= toCol; c++) {
|
|
537
|
+
set(canvas, roles, r, c, ch.bar, 'arrow', hexColors, hexColor)
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
// Lines (horizontal staircase: value on x, category on y) — with per-series colors
|
|
545
|
+
const lineEntries: { data: number[]; globalIdx: number }[] = []
|
|
546
|
+
for (let si = 0; si < chart.series.length; si++) {
|
|
547
|
+
if (chart.series[si]!.type === 'line')
|
|
548
|
+
lineEntries.push({ data: chart.series[si]!.data, globalIdx: si })
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
for (const entry of lineEntries) {
|
|
552
|
+
if (entry.data.length === 0) continue
|
|
553
|
+
const hexColor = seriesColors[entry.globalIdx]!
|
|
554
|
+
drawHorizontalStaircaseLine(
|
|
555
|
+
canvas,
|
|
556
|
+
roles,
|
|
557
|
+
entry.data,
|
|
558
|
+
bandMid,
|
|
559
|
+
valueToCol,
|
|
560
|
+
plotTop,
|
|
561
|
+
plotH,
|
|
562
|
+
plotLeft,
|
|
563
|
+
plotW,
|
|
564
|
+
ch,
|
|
565
|
+
hexColors,
|
|
566
|
+
hexColor,
|
|
567
|
+
)
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
return canvasToString(canvas, roles, hexColors, colorMode, theme)
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
// ============================================================================
|
|
574
|
+
// Staircase line drawing — vertical charts
|
|
575
|
+
//
|
|
576
|
+
// Connects data points with flat segments (─) at each value's row,
|
|
577
|
+
// vertical segments (│) between rows, and rounded corners (╭╮╰╯)
|
|
578
|
+
// at transitions. The vertical step happens at the midpoint column
|
|
579
|
+
// between adjacent data points.
|
|
580
|
+
// ============================================================================
|
|
581
|
+
|
|
582
|
+
function drawStaircaseLine(
|
|
583
|
+
canvas: Canvas,
|
|
584
|
+
roles: RoleCanvas,
|
|
585
|
+
data: number[],
|
|
586
|
+
bandCenter: (i: number) => number,
|
|
587
|
+
valueToRow: (v: number) => number,
|
|
588
|
+
plotTop: number,
|
|
589
|
+
plotH: number,
|
|
590
|
+
plotLeft: number,
|
|
591
|
+
plotTotalW: number,
|
|
592
|
+
ch: typeof UNI | typeof ASC,
|
|
593
|
+
hexCanvas?: HexCanvas,
|
|
594
|
+
hexColor?: string | null,
|
|
595
|
+
): void {
|
|
596
|
+
if (data.length === 0) return
|
|
597
|
+
|
|
598
|
+
const points = data.map((v, i) => ({
|
|
599
|
+
col: bandCenter(i),
|
|
600
|
+
row: valueToRow(v),
|
|
601
|
+
}))
|
|
602
|
+
|
|
603
|
+
// Helper to draw on the canvas (row 0 = bottom, displayed inverted)
|
|
604
|
+
const drawAt = (col: number, row: number, char: string) => {
|
|
605
|
+
const displayRow = plotTop + (plotH - 1 - row)
|
|
606
|
+
if (displayRow >= 0 && col >= plotLeft && col < plotLeft + plotTotalW) {
|
|
607
|
+
set(canvas, roles, displayRow, col, char, 'arrow', hexCanvas, hexColor)
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
// Single point: just draw a flat segment
|
|
612
|
+
if (points.length === 1) {
|
|
613
|
+
drawAt(points[0]!.col, points[0]!.row, ch.hLine)
|
|
614
|
+
return
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
for (let i = 0; i < points.length - 1; i++) {
|
|
618
|
+
const p1 = points[i]!
|
|
619
|
+
const p2 = points[i + 1]!
|
|
620
|
+
|
|
621
|
+
if (p1.row === p2.row) {
|
|
622
|
+
// Flat: draw ─ across
|
|
623
|
+
for (let c = p1.col; c <= p2.col; c++) {
|
|
624
|
+
drawAt(c, p1.row, ch.hLine)
|
|
625
|
+
}
|
|
626
|
+
continue
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
const midCol = Math.round((p1.col + p2.col) / 2)
|
|
630
|
+
const goingUp = p2.row > p1.row
|
|
631
|
+
|
|
632
|
+
// 1. Flat at p1's row from p1.col to midCol-1
|
|
633
|
+
for (let c = p1.col; c < midCol; c++) {
|
|
634
|
+
drawAt(c, p1.row, ch.hLine)
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
// 2. Corner at (midCol, p1.row)
|
|
638
|
+
// goingUp: ─ from LEFT, │ going UP → LEFT+TOP = ╯ (cornerBR)
|
|
639
|
+
// goingDown: ─ from LEFT, │ going DOWN → LEFT+BOT = ╮ (cornerTR)
|
|
640
|
+
if (goingUp) {
|
|
641
|
+
drawAt(midCol, p1.row, ch.cornerBR) // ╯
|
|
642
|
+
} else {
|
|
643
|
+
drawAt(midCol, p1.row, ch.cornerTR) // ╮
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
// 3. Vertical from p1.row to p2.row (exclusive of endpoints)
|
|
647
|
+
const minRow = Math.min(p1.row, p2.row)
|
|
648
|
+
const maxRow = Math.max(p1.row, p2.row)
|
|
649
|
+
for (let row = minRow + 1; row < maxRow; row++) {
|
|
650
|
+
drawAt(midCol, row, ch.vLine)
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
// 4. Corner at (midCol, p2.row)
|
|
654
|
+
// goingUp: │ from BOTTOM, ─ going RIGHT → BOT+RIGHT = ╭ (cornerTL)
|
|
655
|
+
// goingDown: │ from TOP, ─ going RIGHT → TOP+RIGHT = ╰ (cornerBL)
|
|
656
|
+
if (goingUp) {
|
|
657
|
+
drawAt(midCol, p2.row, ch.cornerTL) // ╭
|
|
658
|
+
} else {
|
|
659
|
+
drawAt(midCol, p2.row, ch.cornerBL) // ╰
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
// 5. Flat at p2's row from midCol+1 to p2.col
|
|
663
|
+
for (let c = midCol + 1; c <= p2.col; c++) {
|
|
664
|
+
drawAt(c, p2.row, ch.hLine)
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
// Leading flat for first segment (before p1.col)
|
|
668
|
+
if (i === 0) {
|
|
669
|
+
const leadStart = Math.max(
|
|
670
|
+
plotLeft,
|
|
671
|
+
p1.col - Math.floor((p2.col - p1.col) / 4),
|
|
672
|
+
)
|
|
673
|
+
for (let c = leadStart; c < p1.col; c++) {
|
|
674
|
+
drawAt(c, p1.row, ch.hLine)
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
// Trailing flat for last segment (after p2.col)
|
|
679
|
+
if (i === points.length - 2) {
|
|
680
|
+
const trailEnd = Math.min(
|
|
681
|
+
plotLeft + plotTotalW - 1,
|
|
682
|
+
p2.col + Math.floor((p2.col - p1.col) / 4),
|
|
683
|
+
)
|
|
684
|
+
for (let c = p2.col + 1; c <= trailEnd; c++) {
|
|
685
|
+
drawAt(c, p2.row, ch.hLine)
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
// ============================================================================
|
|
692
|
+
// Staircase line drawing — horizontal charts
|
|
693
|
+
//
|
|
694
|
+
// Same staircase approach but with axes swapped:
|
|
695
|
+
// data values map to columns (horizontal position) and categories map to
|
|
696
|
+
// rows (vertical position). Flat segments are vertical (│), transitions
|
|
697
|
+
// are horizontal (─), with the same rounded corners.
|
|
698
|
+
// ============================================================================
|
|
699
|
+
|
|
700
|
+
function drawHorizontalStaircaseLine(
|
|
701
|
+
canvas: Canvas,
|
|
702
|
+
roles: RoleCanvas,
|
|
703
|
+
data: number[],
|
|
704
|
+
bandMid: (i: number) => number,
|
|
705
|
+
valueToCol: (v: number) => number,
|
|
706
|
+
plotTop: number,
|
|
707
|
+
plotH: number,
|
|
708
|
+
plotLeft: number,
|
|
709
|
+
plotW: number,
|
|
710
|
+
ch: typeof UNI | typeof ASC,
|
|
711
|
+
hexCanvas?: HexCanvas,
|
|
712
|
+
hexColor?: string | null,
|
|
713
|
+
): void {
|
|
714
|
+
if (data.length === 0) return
|
|
715
|
+
|
|
716
|
+
const points = data.map((v, i) => ({
|
|
717
|
+
row: bandMid(i),
|
|
718
|
+
col: valueToCol(v),
|
|
719
|
+
}))
|
|
720
|
+
|
|
721
|
+
const drawAt = (row: number, col: number, char: string) => {
|
|
722
|
+
if (
|
|
723
|
+
row >= plotTop &&
|
|
724
|
+
row < plotTop + plotH &&
|
|
725
|
+
col >= plotLeft &&
|
|
726
|
+
col < plotLeft + plotW
|
|
727
|
+
) {
|
|
728
|
+
set(canvas, roles, row, col, char, 'arrow', hexCanvas, hexColor)
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
if (points.length === 1) {
|
|
733
|
+
drawAt(points[0]!.row, points[0]!.col, ch.vLine)
|
|
734
|
+
return
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
for (let i = 0; i < points.length - 1; i++) {
|
|
738
|
+
const p1 = points[i]!
|
|
739
|
+
const p2 = points[i + 1]!
|
|
740
|
+
|
|
741
|
+
if (p1.col === p2.col) {
|
|
742
|
+
// Same value: draw │ down
|
|
743
|
+
for (let r = p1.row; r <= p2.row; r++) {
|
|
744
|
+
drawAt(r, p1.col, ch.vLine)
|
|
745
|
+
}
|
|
746
|
+
continue
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
const midRow = Math.round((p1.row + p2.row) / 2)
|
|
750
|
+
const goingRight = p2.col > p1.col
|
|
751
|
+
|
|
752
|
+
// 1. Vertical at p1's col from p1.row to midRow-1
|
|
753
|
+
for (let r = p1.row; r < midRow; r++) {
|
|
754
|
+
drawAt(r, p1.col, ch.vLine)
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
// 2. Corner at (midRow, p1.col)
|
|
758
|
+
// goingRight: │ from TOP, ─ going RIGHT → TOP+RIGHT = ╰ (cornerBL)
|
|
759
|
+
// goingLeft: │ from TOP, ─ going LEFT → TOP+LEFT = ╯ (cornerBR)
|
|
760
|
+
if (goingRight) {
|
|
761
|
+
drawAt(midRow, p1.col, ch.cornerBL) // ╰
|
|
762
|
+
} else {
|
|
763
|
+
drawAt(midRow, p1.col, ch.cornerBR) // ╯
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
// 3. Horizontal from p1.col to p2.col (exclusive)
|
|
767
|
+
const minCol = Math.min(p1.col, p2.col)
|
|
768
|
+
const maxCol = Math.max(p1.col, p2.col)
|
|
769
|
+
for (let c = minCol + 1; c < maxCol; c++) {
|
|
770
|
+
drawAt(midRow, c, ch.hLine)
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
// 4. Corner at (midRow, p2.col)
|
|
774
|
+
// goingRight: ─ from LEFT, │ going DOWN → LEFT+BOT = ╮ (cornerTR)
|
|
775
|
+
// goingLeft: ─ from RIGHT, │ going DOWN → RIGHT+BOT = ╭ (cornerTL)
|
|
776
|
+
if (goingRight) {
|
|
777
|
+
drawAt(midRow, p2.col, ch.cornerTR) // ╮
|
|
778
|
+
} else {
|
|
779
|
+
drawAt(midRow, p2.col, ch.cornerTL) // ╭
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
// 5. Vertical at p2's col from midRow+1 to p2.row
|
|
783
|
+
for (let r = midRow + 1; r <= p2.row; r++) {
|
|
784
|
+
drawAt(r, p2.col, ch.vLine)
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
// ============================================================================
|
|
790
|
+
// Legend — shows series symbols with per-series colors
|
|
791
|
+
// ============================================================================
|
|
792
|
+
|
|
793
|
+
function drawLegend(
|
|
794
|
+
canvas: Canvas,
|
|
795
|
+
roles: RoleCanvas,
|
|
796
|
+
hexCanvas: HexCanvas,
|
|
797
|
+
chart: XYChart,
|
|
798
|
+
row: number,
|
|
799
|
+
totalW: number,
|
|
800
|
+
ch: typeof UNI | typeof ASC,
|
|
801
|
+
seriesColors: string[],
|
|
802
|
+
): void {
|
|
803
|
+
// Build legend items with global series indices
|
|
804
|
+
type LegendItem = { symbol: string; label: string; globalIdx: number }
|
|
805
|
+
const items: LegendItem[] = []
|
|
806
|
+
let barIdx = 0,
|
|
807
|
+
lineIdx = 0
|
|
808
|
+
for (let si = 0; si < chart.series.length; si++) {
|
|
809
|
+
const s = chart.series[si]!
|
|
810
|
+
if (s.type === 'bar') {
|
|
811
|
+
items.push({ symbol: ch.bar, label: `Bar ${barIdx + 1}`, globalIdx: si })
|
|
812
|
+
barIdx++
|
|
813
|
+
} else {
|
|
814
|
+
items.push({
|
|
815
|
+
symbol: ch.hLine,
|
|
816
|
+
label: `Line ${lineIdx + 1}`,
|
|
817
|
+
globalIdx: si,
|
|
818
|
+
})
|
|
819
|
+
lineIdx++
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
// Calculate total legend width: "symbol space label symbol space label ..."
|
|
824
|
+
let totalLen = 0
|
|
825
|
+
for (let i = 0; i < items.length; i++) {
|
|
826
|
+
if (i > 0) totalLen += 2 // gap between items
|
|
827
|
+
totalLen += 1 + 1 + items[i]!.label.length // symbol + space + label
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
const startCol = Math.max(0, Math.floor(totalW / 2 - totalLen / 2))
|
|
831
|
+
let col = startCol
|
|
832
|
+
|
|
833
|
+
for (let i = 0; i < items.length; i++) {
|
|
834
|
+
if (i > 0) col += 2 // gap
|
|
835
|
+
const item = items[i]!
|
|
836
|
+
// Symbol with series-specific color
|
|
837
|
+
set(
|
|
838
|
+
canvas,
|
|
839
|
+
roles,
|
|
840
|
+
row,
|
|
841
|
+
col,
|
|
842
|
+
item.symbol,
|
|
843
|
+
'arrow',
|
|
844
|
+
hexCanvas,
|
|
845
|
+
seriesColors[item.globalIdx],
|
|
846
|
+
)
|
|
847
|
+
col += 1
|
|
848
|
+
// Space (already ' ' from canvas init)
|
|
849
|
+
col += 1
|
|
850
|
+
// Label text
|
|
851
|
+
writeText(canvas, roles, row, col, item.label, 'text')
|
|
852
|
+
col += item.label.length
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
// ============================================================================
|
|
857
|
+
// Canvas utilities
|
|
858
|
+
// ============================================================================
|
|
859
|
+
|
|
860
|
+
// createCanvas/createRoleCanvas delegate to the shared canvas.ts primitives
|
|
861
|
+
// (mkCanvas/mkRoleCanvas), which take inclusive max-index arguments rather
|
|
862
|
+
// than width/height — hence the `- 1`. createHexCanvas has no shared
|
|
863
|
+
// equivalent: the per-cell hex color overlay is specific to xychart's
|
|
864
|
+
// multi-series coloring and isn't part of the general Canvas/RoleCanvas
|
|
865
|
+
// model in canvas.ts, so it keeps its own small implementation.
|
|
866
|
+
function createCanvas(width: number, height: number): Canvas {
|
|
867
|
+
return mkCanvas(width - 1, height - 1)
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
function createRoleCanvas(width: number, height: number): RoleCanvas {
|
|
871
|
+
return mkRoleCanvas(width - 1, height - 1)
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
function createHexCanvas(width: number, height: number): HexCanvas {
|
|
875
|
+
return Array.from({ length: width }, () =>
|
|
876
|
+
Array.from<string | null>({ length: height }).fill(null),
|
|
877
|
+
)
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
function set(
|
|
881
|
+
canvas: Canvas,
|
|
882
|
+
roles: RoleCanvas,
|
|
883
|
+
row: number,
|
|
884
|
+
col: number,
|
|
885
|
+
char: string,
|
|
886
|
+
role: CharRole,
|
|
887
|
+
hexCanvas?: HexCanvas,
|
|
888
|
+
hex?: string | null,
|
|
889
|
+
): void {
|
|
890
|
+
// Delegate the character+role write to the shared bounds-checked
|
|
891
|
+
// primitive (same clipping semantics as before: out-of-range coordinates
|
|
892
|
+
// are silently ignored). The hex-color overlay isn't known to canvas.ts,
|
|
893
|
+
// so it's still set here directly, guarded by the same bounds check.
|
|
894
|
+
const inBounds =
|
|
895
|
+
col >= 0 &&
|
|
896
|
+
col < canvas.length &&
|
|
897
|
+
row >= 0 &&
|
|
898
|
+
row < (canvas[0]?.length ?? 0)
|
|
899
|
+
if (!inBounds) return
|
|
900
|
+
write(canvas, col, row, char, { role, roleCanvas: roles })
|
|
901
|
+
if (hexCanvas && hex) hexCanvas[col]![row] = hex
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
function get(canvas: Canvas, row: number, col: number): string {
|
|
905
|
+
if (col >= 0 && col < canvas.length && row >= 0 && row < canvas[0]!.length) {
|
|
906
|
+
return canvas[col]![row]!
|
|
907
|
+
}
|
|
908
|
+
return ' '
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
function writeText(
|
|
912
|
+
canvas: Canvas,
|
|
913
|
+
roles: RoleCanvas,
|
|
914
|
+
row: number,
|
|
915
|
+
startCol: number,
|
|
916
|
+
text: string,
|
|
917
|
+
role: CharRole,
|
|
918
|
+
): void {
|
|
919
|
+
for (let i = 0; i < text.length; i++) {
|
|
920
|
+
set(canvas, roles, row, startCol + i, text[i]!, role)
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
// ============================================================================
|
|
925
|
+
// Canvas → string (with per-cell hex color support)
|
|
926
|
+
// ============================================================================
|
|
927
|
+
|
|
928
|
+
function canvasToString(
|
|
929
|
+
canvas: Canvas,
|
|
930
|
+
roles: RoleCanvas,
|
|
931
|
+
hexCanvas: HexCanvas,
|
|
932
|
+
colorMode: ColorMode,
|
|
933
|
+
theme: AsciiTheme,
|
|
934
|
+
): string {
|
|
935
|
+
if (canvas.length === 0) return ''
|
|
936
|
+
const height = canvas[0]!.length
|
|
937
|
+
const width = canvas.length
|
|
938
|
+
const lines: string[] = []
|
|
939
|
+
|
|
940
|
+
for (let row = 0; row < height; row++) {
|
|
941
|
+
const chars: string[] = []
|
|
942
|
+
const rowRoles: (CharRole | null)[] = []
|
|
943
|
+
const rowHex: (string | null)[] = []
|
|
944
|
+
for (let col = 0; col < width; col++) {
|
|
945
|
+
chars.push(canvas[col]![row]!)
|
|
946
|
+
rowRoles.push(roles[col]![row]!)
|
|
947
|
+
rowHex.push(hexCanvas[col]![row]!)
|
|
948
|
+
}
|
|
949
|
+
// Trim trailing spaces
|
|
950
|
+
let end = chars.length - 1
|
|
951
|
+
while (end >= 0 && chars[end] === ' ') end--
|
|
952
|
+
if (end < 0) {
|
|
953
|
+
lines.push('')
|
|
954
|
+
} else {
|
|
955
|
+
lines.push(
|
|
956
|
+
colorizeRow(
|
|
957
|
+
chars.slice(0, end + 1),
|
|
958
|
+
rowRoles.slice(0, end + 1),
|
|
959
|
+
rowHex.slice(0, end + 1),
|
|
960
|
+
theme,
|
|
961
|
+
colorMode,
|
|
962
|
+
),
|
|
963
|
+
)
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
// Trim trailing empty lines
|
|
968
|
+
while (lines.length > 0 && lines[lines.length - 1] === '') {
|
|
969
|
+
lines.pop()
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
return lines.join('\n')
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
/**
|
|
976
|
+
* Colorize a row of characters, using hex color overrides where available
|
|
977
|
+
* and falling back to role-based theme colors otherwise.
|
|
978
|
+
* Groups consecutive same-color characters for efficient escape sequences.
|
|
979
|
+
*/
|
|
980
|
+
function colorizeRow(
|
|
981
|
+
chars: string[],
|
|
982
|
+
roles: (CharRole | null)[],
|
|
983
|
+
hexOverrides: (string | null)[],
|
|
984
|
+
theme: AsciiTheme,
|
|
985
|
+
mode: ColorMode,
|
|
986
|
+
): string {
|
|
987
|
+
if (mode === 'none') return chars.join('')
|
|
988
|
+
|
|
989
|
+
let result = ''
|
|
990
|
+
let currentColor: string | null = null
|
|
991
|
+
let buffer = ''
|
|
992
|
+
|
|
993
|
+
for (let i = 0; i < chars.length; i++) {
|
|
994
|
+
const char = chars[i]!
|
|
995
|
+
|
|
996
|
+
if (char === ' ') {
|
|
997
|
+
// Flush buffer before whitespace
|
|
998
|
+
if (buffer.length > 0) {
|
|
999
|
+
result += currentColor
|
|
1000
|
+
? colorizeText(buffer, currentColor, mode)
|
|
1001
|
+
: buffer
|
|
1002
|
+
buffer = ''
|
|
1003
|
+
currentColor = null
|
|
1004
|
+
}
|
|
1005
|
+
result += ' '
|
|
1006
|
+
continue
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
// Effective color: hex override > role-based > null
|
|
1010
|
+
const hexOvr = hexOverrides[i] ?? null
|
|
1011
|
+
const roleVal = roles[i] ?? null
|
|
1012
|
+
const color = hexOvr ?? (roleVal ? roleToHex(roleVal, theme) : null)
|
|
1013
|
+
|
|
1014
|
+
if (color === currentColor) {
|
|
1015
|
+
buffer += char
|
|
1016
|
+
} else {
|
|
1017
|
+
// Flush previous group
|
|
1018
|
+
if (buffer.length > 0) {
|
|
1019
|
+
result += currentColor
|
|
1020
|
+
? colorizeText(buffer, currentColor, mode)
|
|
1021
|
+
: buffer
|
|
1022
|
+
}
|
|
1023
|
+
buffer = char
|
|
1024
|
+
currentColor = color
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
// Flush remaining
|
|
1029
|
+
if (buffer.length > 0) {
|
|
1030
|
+
result += currentColor ? colorizeText(buffer, currentColor, mode) : buffer
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
return result
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
// ============================================================================
|
|
1037
|
+
// Helpers (chart-level)
|
|
1038
|
+
// ============================================================================
|
|
1039
|
+
|
|
1040
|
+
function getDataCount(chart: XYChart): number {
|
|
1041
|
+
if (chart.xAxis.categories) return chart.xAxis.categories.length
|
|
1042
|
+
for (const s of chart.series) {
|
|
1043
|
+
if (s.data.length > 0) return s.data.length
|
|
1044
|
+
}
|
|
1045
|
+
return 0
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
function getCategoryLabels(chart: XYChart, count: number): string[] {
|
|
1049
|
+
if (chart.xAxis.categories) return chart.xAxis.categories
|
|
1050
|
+
if (chart.xAxis.range) {
|
|
1051
|
+
const { min, max } = chart.xAxis.range
|
|
1052
|
+
const step = count > 1 ? (max - min) / (count - 1) : 0
|
|
1053
|
+
return Array.from({ length: count }, (_, i) =>
|
|
1054
|
+
formatTickValue(min + step * i),
|
|
1055
|
+
)
|
|
1056
|
+
}
|
|
1057
|
+
return Array.from({ length: count }, (_, i) => String(i + 1))
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
/** Generate nice tick values for a numeric range. */
|
|
1061
|
+
function niceTickValues(min: number, max: number): number[] {
|
|
1062
|
+
const range = max - min
|
|
1063
|
+
if (range <= 0) return [min]
|
|
1064
|
+
|
|
1065
|
+
const rawInterval = range / 6
|
|
1066
|
+
const magnitude = Math.pow(10, Math.floor(Math.log10(rawInterval)))
|
|
1067
|
+
const residual = rawInterval / magnitude
|
|
1068
|
+
let niceInterval: number
|
|
1069
|
+
if (residual <= 1.5) niceInterval = magnitude
|
|
1070
|
+
else if (residual <= 3) niceInterval = 2 * magnitude
|
|
1071
|
+
else if (residual <= 7) niceInterval = 5 * magnitude
|
|
1072
|
+
else niceInterval = 10 * magnitude
|
|
1073
|
+
|
|
1074
|
+
const start = Math.ceil(min / niceInterval) * niceInterval
|
|
1075
|
+
const ticks: number[] = []
|
|
1076
|
+
for (let v = start; v <= max + niceInterval * 0.001; v += niceInterval) {
|
|
1077
|
+
ticks.push(Math.round(v * 1e10) / 1e10)
|
|
1078
|
+
}
|
|
1079
|
+
return ticks
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
function formatTickValue(v: number): string {
|
|
1083
|
+
if (Number.isInteger(v)) return String(v)
|
|
1084
|
+
return v.toFixed(Math.abs(v) < 10 ? 1 : 0)
|
|
1085
|
+
}
|