basalt-ui 2.27.2 → 2.29.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/CHANGELOG.md +466 -307
- package/MIGRATING.md +358 -12
- package/agent/rules/basalt-batteries.md +1 -1
- package/agent/rules/basalt-mantine.md +1 -1
- package/agent/rules/basalt-tokens.md +2 -2
- package/agent/skills/basalt-app/SKILL.md +3 -3
- package/agent/skills/basalt-charts/SKILL.md +1 -1
- package/agent/skills/basalt-design/SKILL.md +12 -6
- package/agent/skills/basalt-design/references/catalogue.md +42 -36
- package/agent/skills/basalt-design/references/recipes.md +47 -0
- package/agent/skills/basalt-design/references/states.md +1 -1
- package/configs/oxlint-basalt.json +2 -1
- package/configs/oxlint-plugin.js +112 -3
- package/configs/oxlint-plugin.test.ts +99 -4
- package/configs/oxlint.json +1 -0
- package/dist/agent/chat/thread-message.d.ts +1 -160
- package/dist/agent/chat/thread-message.d.ts.map +1 -1
- package/dist/agent/chat/thread-message.js +7 -162
- package/dist/agent/chat/virtualize.d.ts +5 -5
- package/dist/charts/ChartCard.d.ts +14 -7
- package/dist/charts/ChartCard.d.ts.map +1 -1
- package/dist/charts/ChartCard.js +112 -23
- package/dist/charts/Lines.d.ts.map +1 -1
- package/dist/charts/Lines.js +11 -4
- package/dist/charts/Sparkline.d.ts +14 -2
- package/dist/charts/Sparkline.d.ts.map +1 -1
- package/dist/charts/Sparkline.js +155 -44
- package/dist/charts/chart-box.module.css +59 -2
- package/dist/charts/chart-box.module.css.d.ts +5 -0
- package/dist/charts/chart-card.module.css +28 -17
- package/dist/charts/chart-card.module.css.d.ts +3 -0
- package/dist/charts/compile.d.ts +1 -1
- package/dist/charts/compile.d.ts.map +1 -1
- package/dist/charts/compile.js +38 -27
- package/dist/charts/labels.d.ts +13 -3
- package/dist/charts/labels.d.ts.map +1 -1
- package/dist/charts/labels.js +131 -20
- package/dist/charts/legend.d.ts.map +1 -1
- package/dist/charts/legend.js +1 -1
- package/dist/charts/ref-label.d.ts +37 -8
- package/dist/charts/ref-label.d.ts.map +1 -1
- package/dist/charts/ref-label.js +55 -10
- package/dist/charts/sparkline.module.css +50 -0
- package/dist/charts/sparkline.module.css.d.ts +3 -0
- package/dist/charts/state.d.ts +4 -4
- package/dist/charts/state.d.ts.map +1 -1
- package/dist/charts/state.js +23 -5
- package/dist/cli/doctor-upgrade.d.ts +11 -0
- package/dist/cli/doctor-upgrade.d.ts.map +1 -1
- package/dist/cli/doctor-upgrade.js +54 -0
- package/dist/cli/doctor.d.ts.map +1 -1
- package/dist/cli/doctor.js +32 -0
- package/dist/cli/help.d.ts.map +1 -1
- package/dist/cli/help.js +8 -3
- package/dist/cli/index.d.ts +13 -13
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +36 -2
- package/dist/cli/init.d.ts +6 -0
- package/dist/cli/init.d.ts.map +1 -1
- package/dist/cli/init.js +118 -27
- package/dist/cli/router-wiring.d.ts +3 -0
- package/dist/cli/router-wiring.d.ts.map +1 -1
- package/dist/cli/router-wiring.js +2 -0
- package/dist/cli/shot/capture.d.ts +34 -2
- package/dist/cli/shot/capture.d.ts.map +1 -1
- package/dist/cli/shot/capture.js +95 -12
- package/dist/cli/shot/detect.d.ts +4 -1
- package/dist/cli/shot/detect.d.ts.map +1 -1
- package/dist/cli/shot/detect.js +38 -16
- package/dist/cli/shot/index.d.ts.map +1 -1
- package/dist/cli/shot/index.js +9 -2
- package/dist/cli/shot/measure.d.ts.map +1 -1
- package/dist/cli/shot/measure.js +11 -2
- package/dist/cli/shot/types.d.ts +4 -1
- package/dist/cli/shot/types.d.ts.map +1 -1
- package/dist/cli/sync.d.ts.map +1 -1
- package/dist/cli/sync.js +10 -2
- package/dist/common/could-not-load.d.ts +13 -0
- package/dist/common/could-not-load.d.ts.map +1 -0
- package/dist/common/could-not-load.js +14 -0
- package/dist/common/empty-copy.d.ts +16 -0
- package/dist/common/empty-copy.d.ts.map +1 -0
- package/dist/common/empty-copy.js +9 -0
- package/dist/common/index.d.ts +1 -0
- package/dist/common/index.d.ts.map +1 -1
- package/dist/common/index.js +3 -0
- package/dist/common/props.d.ts +20 -0
- package/dist/common/props.d.ts.map +1 -1
- package/dist/common/props.js +13 -1
- package/dist/common/retry-button.module.css +7 -3
- package/dist/common/virtual-rows.d.ts +196 -0
- package/dist/common/virtual-rows.d.ts.map +1 -0
- package/dist/common/virtual-rows.js +290 -0
- package/dist/controls/controls.module.css +33 -1
- package/dist/controls/controls.module.css.d.ts +2 -0
- package/dist/controls/enum-filter.d.ts.map +1 -1
- package/dist/controls/enum-filter.js +8 -2
- package/dist/controls/filter-pill.d.ts +27 -3
- package/dist/controls/filter-pill.d.ts.map +1 -1
- package/dist/controls/filter-pill.js +15 -4
- package/dist/controls/form-layout.d.ts +9 -3
- package/dist/controls/form-layout.d.ts.map +1 -1
- package/dist/controls/form-layout.js +74 -12
- package/dist/controls/form-layout.module.css +112 -7
- package/dist/controls/form-layout.module.css.d.ts +1 -0
- package/dist/controls/multi-select-filter.d.ts +2 -2
- package/dist/controls/multi-select-filter.d.ts.map +1 -1
- package/dist/controls/multi-select-filter.js +12 -6
- package/dist/controls/range-filter.d.ts +1 -1
- package/dist/controls/range-filter.d.ts.map +1 -1
- package/dist/controls/range-filter.js +8 -2
- package/dist/dashboard/bar-list.d.ts +5 -5
- package/dist/dashboard/bar-list.d.ts.map +1 -1
- package/dist/dashboard/bar-list.js +15 -8
- package/dist/dashboard/bar-list.module.css +21 -5
- package/dist/dashboard/board-nav.d.ts +22 -0
- package/dist/dashboard/board-nav.d.ts.map +1 -0
- package/dist/dashboard/board-nav.js +49 -0
- package/dist/dashboard/board.d.ts +52 -0
- package/dist/dashboard/board.d.ts.map +1 -0
- package/dist/dashboard/board.js +247 -0
- package/dist/dashboard/board.module.css +179 -0
- package/dist/dashboard/board.module.css.d.ts +28 -0
- package/dist/dashboard/empty-state.d.ts +6 -6
- package/dist/dashboard/empty-state.d.ts.map +1 -1
- package/dist/dashboard/empty-state.js +17 -8
- package/dist/dashboard/event-list-model.d.ts +54 -0
- package/dist/dashboard/event-list-model.d.ts.map +1 -0
- package/dist/dashboard/event-list-model.js +76 -0
- package/dist/dashboard/event-list-rows.d.ts +23 -0
- package/dist/dashboard/event-list-rows.d.ts.map +1 -0
- package/dist/dashboard/event-list-rows.js +89 -0
- package/dist/dashboard/event-list-scroll.d.ts +55 -0
- package/dist/dashboard/event-list-scroll.d.ts.map +1 -0
- package/dist/dashboard/event-list-scroll.js +216 -0
- package/dist/dashboard/event-list-skeleton.d.ts +10 -0
- package/dist/dashboard/event-list-skeleton.d.ts.map +1 -0
- package/dist/dashboard/event-list-skeleton.js +34 -0
- package/dist/dashboard/event-list.d.ts +65 -0
- package/dist/dashboard/event-list.d.ts.map +1 -0
- package/dist/dashboard/event-list.js +208 -0
- package/dist/dashboard/event-list.module.css +336 -0
- package/dist/dashboard/event-list.module.css.d.ts +34 -0
- package/dist/dashboard/index.d.ts +2 -0
- package/dist/dashboard/index.d.ts.map +1 -1
- package/dist/dashboard/index.js +4 -0
- package/dist/dashboard/meter-model.d.ts +6 -3
- package/dist/dashboard/meter-model.d.ts.map +1 -1
- package/dist/dashboard/meter-model.js +9 -4
- package/dist/dashboard/meter-parts.d.ts +3 -0
- package/dist/dashboard/meter-parts.d.ts.map +1 -1
- package/dist/dashboard/meter-parts.js +36 -6
- package/dist/dashboard/meter.d.ts +3 -3
- package/dist/dashboard/meter.d.ts.map +1 -1
- package/dist/dashboard/meter.js +10 -2
- package/dist/dashboard/meter.module.css +20 -12
- package/dist/dashboard/meter.module.css.d.ts +0 -1
- package/dist/dashboard/page-summary.d.ts +3 -3
- package/dist/dashboard/page-summary.d.ts.map +1 -1
- package/dist/dashboard/page-summary.js +7 -2
- package/dist/dashboard/page-summary.module.css +5 -2
- package/dist/dashboard/query-state.d.ts +9 -8
- package/dist/dashboard/query-state.d.ts.map +1 -1
- package/dist/dashboard/query-state.js +29 -11
- package/dist/dashboard/section.d.ts +11 -3
- package/dist/dashboard/section.d.ts.map +1 -1
- package/dist/dashboard/section.js +34 -7
- package/dist/dashboard/settings-section.d.ts +8 -5
- package/dist/dashboard/settings-section.d.ts.map +1 -1
- package/dist/dashboard/settings-section.js +12 -8
- package/dist/dashboard/settings-section.module.css +25 -4
- package/dist/dashboard/settings-section.module.css.d.ts +1 -0
- package/dist/dashboard/stat-card-parts.d.ts +5 -1
- package/dist/dashboard/stat-card-parts.d.ts.map +1 -1
- package/dist/dashboard/stat-card-parts.js +8 -3
- package/dist/dashboard/stat-card.d.ts +3 -3
- package/dist/dashboard/stat-card.d.ts.map +1 -1
- package/dist/dashboard/stat-card.js +47 -12
- package/dist/dashboard/stat-card.module.css +42 -7
- package/dist/dashboard/stat-card.module.css.d.ts +3 -0
- package/dist/dashboard/stat-group.d.ts +2 -2
- package/dist/dashboard/stat-group.d.ts.map +1 -1
- package/dist/dashboard/stat-group.js +10 -3
- package/dist/dashboard/stat-strip.d.ts +5 -4
- package/dist/dashboard/stat-strip.d.ts.map +1 -1
- package/dist/dashboard/stat-strip.js +41 -31
- package/dist/dashboard/stat-strip.module.css +8 -6
- package/dist/dashboard/visual-slot.d.ts +4 -0
- package/dist/dashboard/visual-slot.d.ts.map +1 -1
- package/dist/dashboard/visual-slot.js +2 -0
- package/dist/dashboard/widget-header.d.ts +6 -6
- package/dist/dashboard/widget-header.d.ts.map +1 -1
- package/dist/dashboard/widget-header.js +4 -2
- package/dist/dashboard/widget-header.module.css +12 -15
- package/dist/format/shared.d.ts.map +1 -1
- package/dist/format/shared.js +24 -1
- package/dist/guard/index.d.ts +1 -1
- package/dist/guard/index.d.ts.map +1 -1
- package/dist/guard/index.js +4 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/shell/app-main.module.css +18 -0
- package/dist/styles.css +69 -0
- package/dist/surfaces.d.ts +2 -2
- package/dist/surfaces.d.ts.map +1 -1
- package/dist/surfaces.js +8 -2
- package/dist/table/cell-kinds.d.ts +47 -0
- package/dist/table/cell-kinds.d.ts.map +1 -0
- package/dist/table/cell-kinds.js +180 -0
- package/dist/table/cell-kinds.module.css +55 -0
- package/dist/table/cell-kinds.module.css.d.ts +15 -0
- package/dist/table/data-table-fold.d.ts +155 -0
- package/dist/table/data-table-fold.d.ts.map +1 -0
- package/dist/table/data-table-fold.js +320 -0
- package/dist/table/data-table-state.d.ts +10 -3
- package/dist/table/data-table-state.d.ts.map +1 -1
- package/dist/table/data-table-state.js +11 -6
- package/dist/table/data-table.d.ts +13 -28
- package/dist/table/data-table.d.ts.map +1 -1
- package/dist/table/data-table.js +362 -255
- package/dist/table/data-table.module.css +282 -3
- package/dist/table/data-table.module.css.d.ts +16 -0
- package/dist/table/table-features.d.ts +34 -4
- package/dist/table/table-features.d.ts.map +1 -1
- package/dist/table/table-features.js +10 -0
- package/dist/theme/card-inset.module.css +35 -0
- package/dist/theme/controls.module.css +55 -2
- package/dist/theme/ctl-theme.d.ts.map +1 -1
- package/dist/theme/ctl-theme.js +5 -0
- package/dist/theme/index.d.ts.map +1 -1
- package/dist/theme/index.js +14 -10
- package/dist/tokens/core.d.ts +1 -0
- package/dist/tokens/core.d.ts.map +1 -1
- package/dist/tokens/core.js +1 -0
- package/dist/tokens/derive.js +1 -1
- package/dist/tokens/palette.d.ts +2 -1
- package/dist/tokens/palette.d.ts.map +1 -1
- package/llms.txt +295 -5
- package/package.json +1 -1
- package/src/agent/chat/thread-message.tsx +10 -403
- package/src/agent/chat/virtualize.ts +5 -5
- package/src/charts/ChartCard.tsx +175 -31
- package/src/charts/Lines.tsx +13 -4
- package/src/charts/Sparkline.tsx +213 -48
- package/src/charts/chart-box.module.css +59 -2
- package/src/charts/chart-box.module.css.d.ts +5 -0
- package/src/charts/chart-card.module.css +28 -17
- package/src/charts/chart-card.module.css.d.ts +3 -0
- package/src/charts/compile.ts +59 -33
- package/src/charts/labels.ts +175 -24
- package/src/charts/legend.tsx +3 -2
- package/src/charts/ref-label.ts +114 -18
- package/src/charts/sparkline.module.css +50 -0
- package/src/charts/sparkline.module.css.d.ts +3 -0
- package/src/charts/state.tsx +40 -10
- package/src/cli/doctor-upgrade.ts +75 -0
- package/src/cli/doctor.ts +36 -0
- package/src/cli/help.ts +16 -6
- package/src/cli/index.ts +73 -67
- package/src/cli/init.ts +157 -36
- package/src/cli/router-wiring.ts +2 -2
- package/src/cli/shot/capture.ts +138 -16
- package/src/cli/shot/detect.ts +46 -22
- package/src/cli/shot/index.ts +8 -2
- package/src/cli/shot/measure.ts +11 -2
- package/src/cli/shot/types.ts +4 -1
- package/src/cli/sync.ts +12 -2
- package/src/common/could-not-load.ts +22 -0
- package/src/common/empty-copy.ts +17 -0
- package/src/common/index.ts +1 -0
- package/src/common/props.ts +30 -0
- package/src/common/retry-button.module.css +7 -3
- package/src/common/virtual-rows.tsx +682 -0
- package/src/controls/controls.module.css +33 -1
- package/src/controls/controls.module.css.d.ts +2 -0
- package/src/controls/enum-filter.tsx +8 -2
- package/src/controls/filter-pill.tsx +80 -27
- package/src/controls/form-layout.module.css +112 -7
- package/src/controls/form-layout.module.css.d.ts +1 -0
- package/src/controls/form-layout.tsx +115 -22
- package/src/controls/multi-select-filter.tsx +22 -11
- package/src/controls/range-filter.tsx +9 -3
- package/src/dashboard/bar-list.module.css +21 -5
- package/src/dashboard/bar-list.tsx +53 -45
- package/src/dashboard/board-nav.ts +67 -0
- package/src/dashboard/board.module.css +179 -0
- package/src/dashboard/board.module.css.d.ts +28 -0
- package/src/dashboard/board.tsx +415 -0
- package/src/dashboard/empty-state.tsx +21 -11
- package/src/dashboard/event-list-model.ts +125 -0
- package/src/dashboard/event-list-rows.tsx +117 -0
- package/src/dashboard/event-list-scroll.ts +291 -0
- package/src/dashboard/event-list-skeleton.tsx +62 -0
- package/src/dashboard/event-list.module.css +336 -0
- package/src/dashboard/event-list.module.css.d.ts +34 -0
- package/src/dashboard/event-list.tsx +336 -0
- package/src/dashboard/index.ts +2 -0
- package/src/dashboard/meter-model.ts +16 -7
- package/src/dashboard/meter-parts.tsx +53 -14
- package/src/dashboard/meter.module.css +20 -12
- package/src/dashboard/meter.module.css.d.ts +0 -1
- package/src/dashboard/meter.tsx +36 -18
- package/src/dashboard/page-summary.module.css +5 -2
- package/src/dashboard/page-summary.tsx +13 -6
- package/src/dashboard/query-state.tsx +55 -30
- package/src/dashboard/section.tsx +83 -41
- package/src/dashboard/settings-section.module.css +25 -4
- package/src/dashboard/settings-section.module.css.d.ts +1 -0
- package/src/dashboard/settings-section.tsx +18 -8
- package/src/dashboard/stat-card-parts.tsx +20 -3
- package/src/dashboard/stat-card.module.css +42 -7
- package/src/dashboard/stat-card.module.css.d.ts +3 -0
- package/src/dashboard/stat-card.tsx +55 -13
- package/src/dashboard/stat-group.tsx +22 -14
- package/src/dashboard/stat-strip.module.css +8 -6
- package/src/dashboard/stat-strip.tsx +29 -20
- package/src/dashboard/visual-slot.ts +5 -0
- package/src/dashboard/widget-header.module.css +12 -15
- package/src/dashboard/widget-header.tsx +8 -5
- package/src/format/shared.ts +30 -1
- package/src/guard/index.ts +7 -1
- package/src/index.ts +4 -0
- package/src/shell/app-main.module.css +18 -0
- package/src/styles.css +69 -0
- package/src/surfaces.ts +7 -1
- package/src/table/cell-kinds.module.css +55 -0
- package/src/table/cell-kinds.module.css.d.ts +15 -0
- package/src/table/cell-kinds.tsx +263 -0
- package/src/table/data-table-fold.tsx +567 -0
- package/src/table/data-table-state.ts +24 -13
- package/src/table/data-table.module.css +282 -3
- package/src/table/data-table.module.css.d.ts +16 -0
- package/src/table/data-table.tsx +430 -362
- package/src/table/table-features.ts +44 -4
- package/src/theme/__snapshots__/plain-theme.test.ts.snap +2 -66
- package/src/theme/card-inset.module.css +35 -0
- package/src/theme/controls.module.css +55 -2
- package/src/theme/ctl-theme.tsx +8 -0
- package/src/theme/index.ts +12 -8
- package/src/tokens/core.ts +1 -0
- package/src/tokens/derive.ts +1 -1
- package/src/tokens/palette.ts +4 -1
package/src/charts/compile.ts
CHANGED
|
@@ -38,8 +38,9 @@ import { cursorBinding, followerOptions, quietWhileFollowing } from './cursor'
|
|
|
38
38
|
import { cursorDots } from './marks'
|
|
39
39
|
import { bucketTicks, strideEvery } from './ticks'
|
|
40
40
|
import { MAIN_PANE, plotAccess, plotContext } from './plot-context'
|
|
41
|
-
import {
|
|
42
|
-
import
|
|
41
|
+
import { claimedLabelBoxes } from './labels'
|
|
42
|
+
import { refLabelDy, refLabelPosition } from './ref-label'
|
|
43
|
+
import type { LabelBox, RefLabelPoint } from './ref-label'
|
|
43
44
|
import type { PlotSizes } from './plot-context'
|
|
44
45
|
import { resolveContainerClass } from '../tokens/size-classes'
|
|
45
46
|
import { resolveYDomain, xScaleFor, xZoneFractions, yScaleFor } from './scales'
|
|
@@ -326,10 +327,15 @@ function wholeStepFormat<T>(
|
|
|
326
327
|
/** A fixed domain's end this near the last round tick (as a share of the tick step) stays unlabelled. */
|
|
327
328
|
const END_TICK_MIN_GAP = 0.4
|
|
328
329
|
|
|
330
|
+
/** A label without its digits (any script), separators and spaces: the unit or symbol text it carries. */
|
|
331
|
+
const labelShape = (label: string): string => label.replace(/[\p{Nd}.,\s]/gu, '')
|
|
332
|
+
|
|
329
333
|
/**
|
|
330
|
-
* The compact ladder's
|
|
331
|
-
*
|
|
332
|
-
*
|
|
334
|
+
* The compact ladder's few ticks can stop short of a fixed domain's end (`[0, 25]` ticks `0 10 20`).
|
|
335
|
+
* The end joins them only when it carries something the round ticks do not: a formatter that states
|
|
336
|
+
* the unit on the end alone (`25 days`) would lose it otherwise. A uniform formatter (`77 kg` beside
|
|
337
|
+
* `76 kg`) gains nothing from a tick off the step, so the end stays unlabelled and every label sits
|
|
338
|
+
* on an even step.
|
|
333
339
|
*/
|
|
334
340
|
function withEndTick<T>(
|
|
335
341
|
cfg: AxisConfig<T> | undefined,
|
|
@@ -339,11 +345,12 @@ function withEndTick<T>(
|
|
|
339
345
|
if (!Array.isArray(cfg?.domain) || cfg.scale === 'log' || cfg.nice === true) return null
|
|
340
346
|
const [lo, hi] = [Math.min(...axis.domain), Math.max(...axis.domain)]
|
|
341
347
|
const round = scaleLinear().domain([lo, hi]).ticks(count)
|
|
342
|
-
const last = round.at(-1)
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
348
|
+
const [first, next, last] = [round.at(0), round.at(1), round.at(-1)]
|
|
349
|
+
if (first === undefined || next === undefined || last === undefined || last >= hi) return null
|
|
350
|
+
if (hi - last < (next - first) * END_TICK_MIN_GAP) return null
|
|
351
|
+
return labelShape(axis.format(hi)) === labelShape(axis.format(last))
|
|
352
|
+
? null
|
|
353
|
+
: { values: [...round, hi] }
|
|
347
354
|
}
|
|
348
355
|
|
|
349
356
|
export function yGuide<T>(opts: {
|
|
@@ -787,8 +794,6 @@ function underlayMarks<T, X extends ChartX>(
|
|
|
787
794
|
/** A band's bar spans this share of its half step (`paddingInner` 0.2 leaves 0.8 of the step). */
|
|
788
795
|
const BAR_BODY = 0.8
|
|
789
796
|
const REF_LABEL_DX = 6
|
|
790
|
-
const REF_LABEL_DY_ABOVE = -5
|
|
791
|
-
const REF_LABEL_DY_BELOW = 11
|
|
792
797
|
|
|
793
798
|
/** What a reference line's caption needs to choose its spot: the plot in px and the data on its axis. */
|
|
794
799
|
type RefLabelContext<T, X extends ChartX> = {
|
|
@@ -796,6 +801,8 @@ type RefLabelContext<T, X extends ChartX> = {
|
|
|
796
801
|
plotHeight: number
|
|
797
802
|
/** One polyline per visible series on the line's axis. */
|
|
798
803
|
lines: (side: 'left' | 'right' | undefined) => readonly (readonly RefLabelPoint[])[]
|
|
804
|
+
/** The end labels placed on the left axis: a caption there keeps clear of them. */
|
|
805
|
+
claimed: readonly LabelBox[]
|
|
799
806
|
axes: Axes<T>
|
|
800
807
|
model: XScaleModel<X>
|
|
801
808
|
}
|
|
@@ -818,6 +825,7 @@ const refLabelMark = <T, X extends ChartX>(
|
|
|
818
825
|
label: r.label ?? '',
|
|
819
826
|
fontSize: VX.text.micro,
|
|
820
827
|
series: ctx.lines(r.axis),
|
|
828
|
+
claimed: startFirst ? [] : ctx.claimed,
|
|
821
829
|
})
|
|
822
830
|
const atStart = position.startsWith('start')
|
|
823
831
|
return decorative(
|
|
@@ -832,7 +840,7 @@ const refLabelMark = <T, X extends ChartX>(
|
|
|
832
840
|
fontSize: VX.text.micro,
|
|
833
841
|
anchor: atStart ? 'start' : 'end',
|
|
834
842
|
dx: atStart ? REF_LABEL_DX : -REF_LABEL_DX,
|
|
835
|
-
dy: position.
|
|
843
|
+
dy: refLabelDy(position, VX.text.micro),
|
|
836
844
|
}),
|
|
837
845
|
)
|
|
838
846
|
}
|
|
@@ -1027,6 +1035,8 @@ const stripAnchor = (
|
|
|
1027
1035
|
}
|
|
1028
1036
|
const STRIP_TOOLTIP_PLACEMENT = ['bottom', 'top'] as const
|
|
1029
1037
|
const STRIP_TOOLTIP_OFFSET = 8
|
|
1038
|
+
/** Air between a strip tooltip opened below and the scene's bottom edge (under the tick labels), px. */
|
|
1039
|
+
const STRIP_TICK_AIR = 2
|
|
1030
1040
|
/**
|
|
1031
1041
|
* A slotted `Bars` (`tooltipBelow` and `bare`): a micro-visual whose tooltip is the one-line chip
|
|
1032
1042
|
* (`chart-box.module.css`, `data-basalt-slot`) in the line the cell reserves above the plot. It
|
|
@@ -1041,7 +1051,7 @@ const slotAnchor = (
|
|
|
1041
1051
|
return { x: (Math.min(...xs) + Math.max(...xs)) / 2, y: plot.y }
|
|
1042
1052
|
}
|
|
1043
1053
|
const SLOT_TOOLTIP_PLACEMENT = ['top'] as const
|
|
1044
|
-
/** The plot's focus ring (`svg.ts-chart:focus-visible`, `chart-box.module.css`) is a 2px outline at a 0.125rem offset, and the chip keeps 1px of air above it. Mirrors that CSS ring at `--mantine-scale` 1; the chip's `block-size` in the same file is the reserve less this. */
|
|
1054
|
+
/** The plot's focus ring (`svg.ts-chart:focus-visible`, `chart-box.module.css`) is a 2px outline at a 0.125rem offset, and the chip keeps 1px of air above it. Mirrors that CSS ring at `--mantine-scale` 1; the chip's `block-size` in the same file is the reserve less this. A chart's ring is inset (W49) but a Sparkline's stays outer, and the chip is sized to the Sparkline's reserve. */
|
|
1045
1055
|
const SLOT_RING_CLEARANCE = 2 + 2 + 1
|
|
1046
1056
|
|
|
1047
1057
|
/** A tooltip's chrome (title row, padding) and each series row, px: the guess the bar anchor reserves. */
|
|
@@ -1081,6 +1091,8 @@ const barAnchor =
|
|
|
1081
1091
|
const TIP_CARD = '[data-basalt-chart-card], [data-strip-cell], .mantine-Card-root'
|
|
1082
1092
|
/** Air between a card-clamped tooltip and the card's edge, px. */
|
|
1083
1093
|
const TIP_CARD_AIR = 4
|
|
1094
|
+
/** Air between the plot's left edge and a tooltip, so the first point's cursor dot stays visible, px. */
|
|
1095
|
+
const TIP_DOT_AIR = 10
|
|
1084
1096
|
/** A card narrower than this keeps the engine's width and horizontal placement (a ~120px strip
|
|
1085
1097
|
* cell would squeeze a tooltip to its 6rem label floor); the vertical rule applies to every card. */
|
|
1086
1098
|
const TIP_CARD_ROOMY = 192
|
|
@@ -1092,7 +1104,7 @@ const TIP_CARD_ROOMY = 192
|
|
|
1092
1104
|
* `top`, so each write is followed by a settle inside the card (`settleInCard`).
|
|
1093
1105
|
*/
|
|
1094
1106
|
/** What a card clamp does: `card` a chart tooltip (both axes, the card's width), `slot` a slotted
|
|
1095
|
-
* `Bars` chip (flush above its plot), `strip` a `BandStrip`'s (sideways
|
|
1107
|
+
* `Bars` chip (flush above its plot), `strip` a `BandStrip`'s (sideways, and below the x tick labels when it opens under the strip). */
|
|
1096
1108
|
type TipMode = 'card' | 'slot' | 'strip'
|
|
1097
1109
|
|
|
1098
1110
|
const cardPortal = (mode: TipMode): ChartTooltipPortalExtension => ({
|
|
@@ -1150,10 +1162,13 @@ export function settleInCard(
|
|
|
1150
1162
|
y: surface.top + (p.y / sceneH) * surface.height,
|
|
1151
1163
|
})
|
|
1152
1164
|
if (mode === 'strip') {
|
|
1153
|
-
// A `BandStrip`: sideways
|
|
1165
|
+
// A `BandStrip`: sideways, and clear of the tick labels below; the engine's width stands.
|
|
1154
1166
|
if (w0 + 2 * TIP_CARD_AIR <= card.width)
|
|
1155
1167
|
left = Math.min(Math.max(left, card.left + TIP_CARD_AIR), card.right - w0 - TIP_CARD_AIR)
|
|
1156
1168
|
if (Math.abs(left - Number.parseFloat(tip.style.left)) > 0.5) tip.style.left = `${left}px`
|
|
1169
|
+
// Opening below the strip, it starts under the scene (the x tick labels sit in its bottom margin).
|
|
1170
|
+
const clear = surface.bottom + STRIP_TICK_AIR
|
|
1171
|
+
if (tip.dataset['placement'] === 'bottom' && top < clear) tip.style.top = `${clear}px`
|
|
1157
1172
|
return
|
|
1158
1173
|
}
|
|
1159
1174
|
const roomy = card.width >= TIP_CARD_ROOMY
|
|
@@ -1177,6 +1192,14 @@ export function settleInCard(
|
|
|
1177
1192
|
const [w, h] = [tip.offsetWidth, tip.offsetHeight]
|
|
1178
1193
|
const anchor = toClient(ctx.anchor)
|
|
1179
1194
|
const gap = Math.max(0, Number.isFinite(ctx.offset) ? (ctx.offset ?? 10) : 10)
|
|
1195
|
+
// The plot's rect: the axis tick labels sit in the margins around it, so a card tooltip stays inside
|
|
1196
|
+
// it when it fits (a first-point tooltip would otherwise cover the y ticks and an x tick).
|
|
1197
|
+
const margin = ctx.scene.margin ?? { top: 0, right: 0, bottom: 0, left: 0 }
|
|
1198
|
+
const plotTL = toClient({ x: margin.left, y: margin.top })
|
|
1199
|
+
const plotBR = toClient({ x: sceneW - margin.right, y: sceneH - margin.bottom })
|
|
1200
|
+
// Per axis: a tooltip taller than a short plot still keeps its left edge clear of the y tick labels.
|
|
1201
|
+
const fenceX = !slot && plotBR.x - plotTL.x >= w + TIP_DOT_AIR
|
|
1202
|
+
const fenceY = !slot && plotBR.y - plotTL.y >= h
|
|
1180
1203
|
const maxTop = card.bottom - TIP_CARD_AIR - h
|
|
1181
1204
|
// The card's header (title and facts) is off-limits while the plot area below it can hold the tooltip.
|
|
1182
1205
|
const headerBottom = slot
|
|
@@ -1203,6 +1226,9 @@ export function settleInCard(
|
|
|
1203
1226
|
if (roomy) left = anchor.x - w / 2
|
|
1204
1227
|
}
|
|
1205
1228
|
if (!slot) top = maxTop < minTop ? minTop : Math.min(Math.max(top, minTop), maxTop)
|
|
1229
|
+
if (fenceY) top = Math.min(Math.max(top, plotTL.y), plotBR.y - h)
|
|
1230
|
+
// The left bound also clears the focused point's dot, drawn on the plot's edge at the first point.
|
|
1231
|
+
if (fenceX) left = Math.min(Math.max(left, plotTL.x + TIP_DOT_AIR), plotBR.x - w)
|
|
1206
1232
|
if (grown || w + 2 * TIP_CARD_AIR <= card.width)
|
|
1207
1233
|
left = Math.min(
|
|
1208
1234
|
Math.max(left, card.left + TIP_CARD_AIR),
|
|
@@ -1337,24 +1363,23 @@ export function compileCartesian<T, X extends ChartX>(
|
|
|
1337
1363
|
const cls = resolveContainerClass(width)
|
|
1338
1364
|
const axes = axesAt(cls)
|
|
1339
1365
|
const access = plotAccess(definition, input.plot, MAIN_PANE)
|
|
1340
|
-
const
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
access,
|
|
1356
|
-
),
|
|
1366
|
+
const plot = plotContext(
|
|
1367
|
+
{
|
|
1368
|
+
rows,
|
|
1369
|
+
visible,
|
|
1370
|
+
highlighted: input.highlighted ?? null,
|
|
1371
|
+
containerClass: cls,
|
|
1372
|
+
width,
|
|
1373
|
+
height,
|
|
1374
|
+
data: input.data,
|
|
1375
|
+
xFraction: model.fraction,
|
|
1376
|
+
y2: axes.right ? 'y2' : undefined,
|
|
1377
|
+
yDomain: axes.left.domain,
|
|
1378
|
+
y2Domain: axes.right?.domain,
|
|
1379
|
+
},
|
|
1380
|
+
access,
|
|
1357
1381
|
)
|
|
1382
|
+
const kindMarks = input.marks(plot)
|
|
1358
1383
|
const under = underlayMarks(input, model, axes)
|
|
1359
1384
|
const over = refLineMarks(input, axes, () => {
|
|
1360
1385
|
// Reading the plot size recompiles once on the measured area; the frame stands in before it.
|
|
@@ -1364,6 +1389,7 @@ export function compileCartesian<T, X extends ChartX>(
|
|
|
1364
1389
|
plotWidth: size?.width ?? width,
|
|
1365
1390
|
plotHeight: size?.height ?? height,
|
|
1366
1391
|
lines: linesOn,
|
|
1392
|
+
claimed: claimedLabelBoxes(plot),
|
|
1367
1393
|
axes,
|
|
1368
1394
|
model,
|
|
1369
1395
|
}
|
package/src/charts/labels.ts
CHANGED
|
@@ -5,12 +5,14 @@
|
|
|
5
5
|
import { decorative } from '@tanstack/charts/mark/decorative'
|
|
6
6
|
import { text } from '@tanstack/charts/text'
|
|
7
7
|
import { VX } from '../tokens'
|
|
8
|
+
import { yRangeWithin } from './ref-label'
|
|
9
|
+
import type { LabelBox, RefLabelPoint } from './ref-label'
|
|
8
10
|
import { rowX } from './types'
|
|
9
11
|
import type { ChartMarks, ChartSeries, ChartX, PlotContext, PlotRow } from './types'
|
|
10
12
|
|
|
11
13
|
const MAX_END_LABELS = 3
|
|
12
|
-
/** Two end labels closer than this (px, vertically) are pushed apart to it. */
|
|
13
|
-
const END_LABEL_MIN_GAP =
|
|
14
|
+
/** Two end labels closer than this (px, vertically) are pushed apart to it: one label line (11px at 1.35) plus two pixels of air. */
|
|
15
|
+
const END_LABEL_MIN_GAP = 17
|
|
14
16
|
const END_LABEL_DX = 6
|
|
15
17
|
/**
|
|
16
18
|
* A glyph's advance as a share of the font size, for the room check. 0.85 covers proportional
|
|
@@ -24,6 +26,16 @@ const END_LABEL_FLIP_DY = -9
|
|
|
24
26
|
const END_LABEL_FLIP_DY_BELOW = 13
|
|
25
27
|
/** The room a label's centre keeps from the plot's top and bottom edge, px: half its line. */
|
|
26
28
|
const END_LABEL_EDGE = 7
|
|
29
|
+
/** A glyph's advance as a share of the font size, for a label's drawn box (the room check's {@link END_LABEL_GLYPH_EM} errs wide). */
|
|
30
|
+
const BOX_GLYPH_EM = 0.68
|
|
31
|
+
/** A label's ink is this share of its font size tall, centred on its text's y. */
|
|
32
|
+
const BOX_INK = 0.75
|
|
33
|
+
/** Air a label keeps from any line, px, and the margin a claimed box carries for a caption. */
|
|
34
|
+
const LINE_AIR = 4.5
|
|
35
|
+
/** What a label across a reference rule from its own line costs in {@link place}'s air score, px. */
|
|
36
|
+
const CROSSED_PENALTY = 100
|
|
37
|
+
/** The most flipped labels whose two sides are all tried (2 ** n assignments). */
|
|
38
|
+
const MAX_SIDE_SEARCH = 5
|
|
27
39
|
|
|
28
40
|
export type EndLabelAnchor = 'start' | 'end'
|
|
29
41
|
/** Which side of its line a flipped label sits: away from the line's last segment. */
|
|
@@ -56,8 +68,20 @@ export type EndLabel<T, X extends ChartX> = {
|
|
|
56
68
|
anchor: EndLabelAnchor
|
|
57
69
|
/** Only a flipped label (left of its point) can meet its own line; it takes the clear side. */
|
|
58
70
|
side: EndLabelSide
|
|
59
|
-
/** A vertical shift in px (negative is up) that keeps this label clear of its neighbours. */
|
|
71
|
+
/** A vertical shift in px (negative is up) that keeps this label clear of its neighbours and the lines. */
|
|
60
72
|
nudge: number
|
|
73
|
+
/** The room the drawn label takes, for a reference caption to keep clear of. */
|
|
74
|
+
box: LabelBox
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const claimedBoxes = new WeakMap<object, readonly LabelBox[]>()
|
|
78
|
+
/** Records the boxes the end labels of `plot` take (once per plot, by `Lines`). */
|
|
79
|
+
export function claimLabelBoxes(plot: object, boxes: readonly LabelBox[]): void {
|
|
80
|
+
claimedBoxes.set(plot, boxes)
|
|
81
|
+
}
|
|
82
|
+
/** The boxes {@link claimLabelBoxes} recorded for `plot`: what a reference caption avoids. */
|
|
83
|
+
export function claimedLabelBoxes(plot: object): readonly LabelBox[] {
|
|
84
|
+
return claimedBoxes.get(plot) ?? []
|
|
61
85
|
}
|
|
62
86
|
|
|
63
87
|
type LastPoint<T, X extends ChartX> = {
|
|
@@ -72,16 +96,21 @@ type LastPoint<T, X extends ChartX> = {
|
|
|
72
96
|
/**
|
|
73
97
|
* Pushes labels apart: `ys` are the labels' heights in px from the plot's bottom, returned as the
|
|
74
98
|
* shift each needs (positive is up) so no two sit closer than {@link END_LABEL_MIN_GAP} and the
|
|
75
|
-
* lowest and the topmost stay {@link END_LABEL_EDGE} inside the plot
|
|
99
|
+
* lowest and the topmost stay {@link END_LABEL_EDGE} inside the plot; a stack too tall for the plot
|
|
100
|
+
* shrinks its gap to fit. Order is kept.
|
|
76
101
|
*/
|
|
77
102
|
export function spreadLabels(ys: readonly number[], plotHeight: number): number[] {
|
|
78
103
|
const order = ys.map((y, i) => i).toSorted((a, b) => (ys[a] as number) - (ys[b] as number))
|
|
79
104
|
const placed = new Map<number, number>()
|
|
105
|
+
// A stack taller than the plot closes its gaps evenly rather than leave the plot.
|
|
106
|
+
const room = Math.max(plotHeight - 2 * END_LABEL_EDGE, 0)
|
|
107
|
+
const gap =
|
|
108
|
+
ys.length > 1 ? Math.min(END_LABEL_MIN_GAP, room / (ys.length - 1)) : END_LABEL_MIN_GAP
|
|
80
109
|
let floor = END_LABEL_EDGE
|
|
81
110
|
for (const i of order) {
|
|
82
111
|
const at = Math.max(ys[i] as number, floor)
|
|
83
112
|
placed.set(i, at)
|
|
84
|
-
floor = at +
|
|
113
|
+
floor = at + gap
|
|
85
114
|
}
|
|
86
115
|
// Pushed past the top: slide the whole stack back down, then re-sweep from the top.
|
|
87
116
|
const top = plotHeight - END_LABEL_EDGE
|
|
@@ -89,7 +118,7 @@ export function spreadLabels(ys: readonly number[], plotHeight: number): number[
|
|
|
89
118
|
for (const i of order.toReversed()) {
|
|
90
119
|
const at = Math.min(placed.get(i) as number, ceiling, top)
|
|
91
120
|
placed.set(i, at)
|
|
92
|
-
ceiling = at -
|
|
121
|
+
ceiling = at - gap
|
|
93
122
|
}
|
|
94
123
|
return ys.map((y, i) => (placed.get(i) as number) - y)
|
|
95
124
|
}
|
|
@@ -105,6 +134,8 @@ export function endLabelPlan<T, X extends ChartX>(
|
|
|
105
134
|
plot: PlotContext<T, X>,
|
|
106
135
|
lines: readonly ChartSeries<T>[],
|
|
107
136
|
naming: LinesNaming = 'auto',
|
|
137
|
+
/** The values of the reference lines on the left axis: rules a label must not sit on. */
|
|
138
|
+
rules: readonly number[] = [],
|
|
108
139
|
): EndLabel<T, X>[] {
|
|
109
140
|
if (naming === 'legend' || plot.y2 !== undefined || lines.length === 0) return []
|
|
110
141
|
if (naming === 'auto' && (plot.containerClass !== 'wide' || lines.length > MAX_END_LABELS))
|
|
@@ -119,30 +150,150 @@ export function endLabelPlan<T, X extends ChartX>(
|
|
|
119
150
|
})
|
|
120
151
|
// No `y2` here (returned above), so every label reads the left domain.
|
|
121
152
|
const [lo, hi] = plot.yDomain
|
|
122
|
-
const
|
|
123
|
-
|
|
153
|
+
const anchors = last.map(({ series, fraction }) =>
|
|
154
|
+
endLabelAnchor({
|
|
124
155
|
fraction,
|
|
125
156
|
plotWidth: plot.plotWidth,
|
|
126
157
|
label: series.label,
|
|
127
158
|
fontSize: VX.text.micro,
|
|
128
|
-
})
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
159
|
+
}),
|
|
160
|
+
)
|
|
161
|
+
// Flipped labels (left of their point) sit over their own line. A line falling into its end
|
|
162
|
+
// comes from above, so its label drops below; a rising line's rises above. Several flipped lines
|
|
163
|
+
// that move the same way would all take the same side and stack in the gap between them, so they
|
|
164
|
+
// wrap the lines instead: falling, the lowest below and the rest above; rising, the mirror.
|
|
165
|
+
const falls = last.map(({ y, prev }) => prev !== null && prev > y)
|
|
166
|
+
const flipped = last.flatMap((_, i) => (anchors[i] === 'end' ? [i] : []))
|
|
167
|
+
const lineY = (i: number): number => (last[i] as (typeof last)[number]).y
|
|
168
|
+
const sameWay =
|
|
169
|
+
flipped.length > 1 && flipped.every((i) => falls[i] === falls[flipped[0] as number])
|
|
170
|
+
const wrapped = sameWay ? flipped.toSorted((a, b) => lineY(a) - lineY(b)) : []
|
|
171
|
+
// The one that keeps the far side: the lowest when falling, the highest when rising.
|
|
172
|
+
const outlier = falls[flipped[0] as number] ? wrapped[0] : wrapped.at(-1)
|
|
173
|
+
const preferred = last.map((_, i): EndLabelSide => {
|
|
174
|
+
const fall = falls[i] as boolean
|
|
175
|
+
return sameWay && anchors[i] === 'end'
|
|
176
|
+
? (i === outlier) === fall
|
|
177
|
+
? 'below'
|
|
178
|
+
: 'above'
|
|
179
|
+
: fall
|
|
180
|
+
? 'below'
|
|
181
|
+
: 'above'
|
|
182
|
+
})
|
|
183
|
+
const toPx = (y: number): number => ((y - lo) / (hi - lo || 1)) * plot.plotHeight
|
|
184
|
+
const nominal = (i: number, side: EndLabelSide): number =>
|
|
185
|
+
toPx(lineY(i)) - endLabelOffset(anchors[i] as EndLabelAnchor, side).dy
|
|
186
|
+
const half = (VX.text.micro * BOX_INK) / 2
|
|
187
|
+
const plotW = Math.max(plot.plotWidth, 1)
|
|
188
|
+
const spans = last.map(({ series, fraction }, i) => {
|
|
189
|
+
const room = (END_LABEL_DX + series.label.length * VX.text.micro * BOX_GLYPH_EM) / plotW
|
|
190
|
+
const edge = END_LABEL_DX / plotW
|
|
191
|
+
return anchors[i] === 'end'
|
|
192
|
+
? ([fraction - room, fraction - edge] as const)
|
|
193
|
+
: ([fraction + edge, fraction + room] as const)
|
|
134
194
|
})
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
195
|
+
// Every line a label could sit on, as polylines in px from the plot's bottom: each visible
|
|
196
|
+
// series and each reference rule. Only the stretch under the labels matters.
|
|
197
|
+
const from = Math.min(...spans.map(([x0]) => x0))
|
|
198
|
+
const obstacles: RefLabelPoint[][] = [
|
|
199
|
+
...plot.visible.map((s) => {
|
|
200
|
+
const pts: RefLabelPoint[] = []
|
|
201
|
+
let before: RefLabelPoint | undefined
|
|
202
|
+
for (const r of plot.rows(s.key)) {
|
|
203
|
+
const fx = r.y === null ? null : plot.xFraction(r.x)
|
|
204
|
+
if (fx === null || r.y === null) continue
|
|
205
|
+
const pt = { fx, y: toPx(r.y) }
|
|
206
|
+
if (fx < from) before = pt
|
|
207
|
+
else {
|
|
208
|
+
if (before !== undefined) pts.push(before)
|
|
209
|
+
before = undefined
|
|
210
|
+
pts.push(pt)
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (before !== undefined) pts.push(before)
|
|
214
|
+
return pts
|
|
215
|
+
}),
|
|
216
|
+
...rules.map((v) => [
|
|
217
|
+
{ fx: 0, y: toPx(v) },
|
|
218
|
+
{ fx: 1, y: toPx(v) },
|
|
219
|
+
]),
|
|
220
|
+
]
|
|
221
|
+
/** Per label, the line ranges (px) its x-window runs into: fixed, so computed once. */
|
|
222
|
+
const windowRanges = spans.map(([x0, x1]) =>
|
|
223
|
+
obstacles.flatMap((pts) => {
|
|
224
|
+
const range = yRangeWithin(pts, x0, x1)
|
|
225
|
+
return range === null ? [] : [range]
|
|
226
|
+
}),
|
|
138
227
|
)
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
228
|
+
const ranges = (i: number): readonly [number, number][] => windowRanges[i] as [number, number][]
|
|
229
|
+
/** Where the centre goes to clear the lines on `side` of `start`, stepping past each one it meets. */
|
|
230
|
+
const cleared = (i: number, side: EndLabelSide, start: number): number => {
|
|
231
|
+
const blockers = ranges(i)
|
|
232
|
+
let c = start
|
|
233
|
+
for (let n = 0; n <= blockers.length; n++) {
|
|
234
|
+
const hit = blockers.find(([a, b]) => b + LINE_AIR > c - half && a - LINE_AIR < c + half)
|
|
235
|
+
if (hit === undefined) break
|
|
236
|
+
c = side === 'above' ? hit[1] + LINE_AIR + half : hit[0] - LINE_AIR - half
|
|
237
|
+
}
|
|
238
|
+
return c
|
|
239
|
+
}
|
|
240
|
+
const airOf = (i: number, c: number): number =>
|
|
241
|
+
Math.min(
|
|
242
|
+
Number.POSITIVE_INFINITY,
|
|
243
|
+
...ranges(i).map(([a, b]) => Math.max(a - (c + half), c - half - b)),
|
|
244
|
+
)
|
|
245
|
+
const place = (sides: readonly EndLabelSide[]) => {
|
|
246
|
+
const start = last.map((_, i) =>
|
|
247
|
+
cleared(i, sides[i] as EndLabelSide, nominal(i, sides[i] as EndLabelSide)),
|
|
248
|
+
)
|
|
249
|
+
const shifts = spreadLabels(start, plot.plotHeight)
|
|
250
|
+
const centres = start.map((c, i) => c + (shifts[i] as number))
|
|
251
|
+
// A label across a reference rule from its own line's end reads as the rule's label: worse
|
|
252
|
+
// than any amount of lost air.
|
|
253
|
+
const crossed = centres.filter((c, i) => {
|
|
254
|
+
const end = toPx(lineY(i))
|
|
255
|
+
return rules.some(
|
|
256
|
+
(v) => (toPx(v) - c) * (toPx(v) - end) < 0 && (c - end) * (toPx(v) - end) > 0,
|
|
257
|
+
)
|
|
258
|
+
}).length
|
|
259
|
+
const score =
|
|
260
|
+
centres.reduce((sum, c, i) => sum + Math.min(airOf(i, c), LINE_AIR), 0) -
|
|
261
|
+
CROSSED_PENALTY * crossed
|
|
262
|
+
return { sides, centres, score }
|
|
263
|
+
}
|
|
264
|
+
// Each flipped label may take either side: the assignment that keeps most air from the lines
|
|
265
|
+
// wins, the preferred one on a tie. Level labels (right of
|
|
266
|
+
// their point) keep theirs.
|
|
267
|
+
const options = flipped.length <= MAX_SIDE_SEARCH ? flipped.length : 0
|
|
268
|
+
let best = place(preferred)
|
|
269
|
+
for (let mask = 1; mask < 1 << options; mask++) {
|
|
270
|
+
const sides = preferred.map((side, i): EndLabelSide => {
|
|
271
|
+
const at = flipped.indexOf(i)
|
|
272
|
+
return at >= 0 && (mask >> at) & 1 ? (side === 'above' ? 'below' : 'above') : side
|
|
273
|
+
})
|
|
274
|
+
const trial = place(sides)
|
|
275
|
+
if (trial.score > best.score + 0.01) best = trial
|
|
276
|
+
}
|
|
277
|
+
return last.map(({ series, row }, i) => {
|
|
278
|
+
const side = best.sides[i] as EndLabelSide
|
|
279
|
+
const centre = best.centres[i] as number
|
|
280
|
+
const [x0, x1] = spans[i] as readonly [number, number]
|
|
281
|
+
const perPx = (hi - lo) / (plot.plotHeight || 1)
|
|
282
|
+
// Two pixels of margin each way: the caption keeps its own gap from the claimed box.
|
|
283
|
+
return {
|
|
284
|
+
series,
|
|
285
|
+
row,
|
|
286
|
+
anchor: anchors[i] as EndLabelAnchor,
|
|
287
|
+
side,
|
|
288
|
+
nudge: nominal(i, side) - centre,
|
|
289
|
+
box: {
|
|
290
|
+
fx0: x0 - 2 / plotW,
|
|
291
|
+
fx1: x1 + 2 / plotW,
|
|
292
|
+
y0: lo + (centre - half - 2) * perPx,
|
|
293
|
+
y1: lo + (centre + half + 2) * perPx,
|
|
294
|
+
},
|
|
295
|
+
}
|
|
296
|
+
})
|
|
146
297
|
}
|
|
147
298
|
|
|
148
299
|
/** The offset of a label from its point: right of it, level; or left of it, above or below. */
|
package/src/charts/legend.tsx
CHANGED
|
@@ -115,7 +115,7 @@ export type SignedSeries<T> = ChartSeries<T> & { colorNegative?: string | undefi
|
|
|
115
115
|
|
|
116
116
|
type SwatchStyle = Pick<
|
|
117
117
|
SignedSeries<never>,
|
|
118
|
-
'color' | 'colorNegative' | 'mark' | 'dash' | 'strokeOpacity' | 'fillOpacity'
|
|
118
|
+
'color' | 'colorNegative' | 'mark' | 'dash' | 'strokeOpacity' | 'fillOpacity' | 'role'
|
|
119
119
|
>
|
|
120
120
|
|
|
121
121
|
/** A swatch fill: the series colour, or a hard-stop split for a sign-coloured bar. */
|
|
@@ -149,7 +149,8 @@ function LineSwatch({ s, compact }: { s: SwatchStyle; compact: boolean }): React
|
|
|
149
149
|
y1={4}
|
|
150
150
|
y2={4}
|
|
151
151
|
stroke={s.color}
|
|
152
|
-
|
|
152
|
+
// A reference line is drawn as a 1px rule (`refLineMarks`), a series from 2px: the key keeps that difference.
|
|
153
|
+
strokeWidth={s.role === 'reference' ? 1 : 2}
|
|
153
154
|
strokeLinecap={s.dash === 'dashed' ? 'butt' : 'round'}
|
|
154
155
|
strokeOpacity={s.strokeOpacity ?? 1}
|
|
155
156
|
{...(s.dash === 'dashed' && { strokeDasharray: dash })}
|