derive-dashboards 0.5.1 → 0.7.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/README.md +4 -2
- package/dist/body.mjs +2 -2
- package/dist/charts/data/buckets.d.mts +11 -0
- package/dist/charts/data/buckets.mjs +66 -0
- package/dist/charts/data/clean.mjs +55 -0
- package/dist/charts/data/format.d.mts +19 -0
- package/dist/charts/data/format.mjs +174 -0
- package/dist/charts/data/input-error.d.mts +5 -0
- package/dist/charts/data/input-error.mjs +11 -0
- package/dist/charts/data/range.mjs +14 -0
- package/dist/charts/data/rows.d.mts +13 -0
- package/dist/charts/data/rows.mjs +31 -0
- package/dist/charts/data/shares.mjs +55 -0
- package/dist/charts/data/types.d.mts +106 -0
- package/dist/charts/data/words.d.mts +44 -0
- package/dist/charts/data/words.mjs +31 -0
- package/dist/charts/describe.mjs +110 -0
- package/dist/charts/index.d.mts +34 -0
- package/dist/charts/index.mjs +24 -0
- package/dist/charts/kinds/area-marks.mjs +120 -0
- package/dist/charts/kinds/area.d.mts +23 -0
- package/dist/charts/kinds/area.mjs +71 -0
- package/dist/charts/kinds/bars-data.d.mts +11 -0
- package/dist/charts/kinds/bars-data.mjs +30 -0
- package/dist/charts/kinds/bars-layout.mjs +30 -0
- package/dist/charts/kinds/bars-marks.mjs +93 -0
- package/dist/charts/kinds/bars-words.mjs +56 -0
- package/dist/charts/kinds/bars.d.mts +17 -0
- package/dist/charts/kinds/bars.mjs +68 -0
- package/dist/charts/kinds/columns-data.mjs +116 -0
- package/dist/charts/kinds/columns-layout.mjs +29 -0
- package/dist/charts/kinds/columns-marks.mjs +200 -0
- package/dist/charts/kinds/columns-words.mjs +26 -0
- package/dist/charts/kinds/columns.d.mts +13 -0
- package/dist/charts/kinds/columns.mjs +80 -0
- package/dist/charts/kinds/dumbbell-data.d.mts +22 -0
- package/dist/charts/kinds/dumbbell-data.mjs +59 -0
- package/dist/charts/kinds/dumbbell-layout.mjs +113 -0
- package/dist/charts/kinds/dumbbell-marks.mjs +119 -0
- package/dist/charts/kinds/dumbbell-words.d.mts +7 -0
- package/dist/charts/kinds/dumbbell-words.mjs +94 -0
- package/dist/charts/kinds/dumbbell.d.mts +14 -0
- package/dist/charts/kinds/dumbbell.mjs +67 -0
- package/dist/charts/kinds/funnel-data.d.mts +11 -0
- package/dist/charts/kinds/funnel-data.mjs +70 -0
- package/dist/charts/kinds/funnel-layout.mjs +32 -0
- package/dist/charts/kinds/funnel-marks.mjs +204 -0
- package/dist/charts/kinds/funnel-words.mjs +77 -0
- package/dist/charts/kinds/funnel.d.mts +11 -0
- package/dist/charts/kinds/funnel.mjs +75 -0
- package/dist/charts/kinds/grid-data.d.mts +18 -0
- package/dist/charts/kinds/grid-data.mjs +35 -0
- package/dist/charts/kinds/grid-layout.mjs +77 -0
- package/dist/charts/kinds/grid-marks.mjs +140 -0
- package/dist/charts/kinds/grid-time.mjs +41 -0
- package/dist/charts/kinds/grid-words.mjs +86 -0
- package/dist/charts/kinds/grid.d.mts +22 -0
- package/dist/charts/kinds/grid.mjs +55 -0
- package/dist/charts/kinds/histogram-data.mjs +142 -0
- package/dist/charts/kinds/histogram-layout.mjs +144 -0
- package/dist/charts/kinds/histogram-marks.mjs +110 -0
- package/dist/charts/kinds/histogram-words.mjs +104 -0
- package/dist/charts/kinds/histogram.d.mts +15 -0
- package/dist/charts/kinds/histogram.mjs +47 -0
- package/dist/charts/kinds/line-marks.mjs +36 -0
- package/dist/charts/kinds/line.d.mts +17 -0
- package/dist/charts/kinds/line.mjs +52 -0
- package/dist/charts/kinds/multiples-grid.mjs +107 -0
- package/dist/charts/kinds/multiples-head.mjs +44 -0
- package/dist/charts/kinds/multiples-layout.mjs +159 -0
- package/dist/charts/kinds/multiples-marks.mjs +95 -0
- package/dist/charts/kinds/multiples-words.mjs +110 -0
- package/dist/charts/kinds/multiples.d.mts +26 -0
- package/dist/charts/kinds/multiples.mjs +42 -0
- package/dist/charts/kinds/row-chart.mjs +112 -0
- package/dist/charts/kinds/row-marks.mjs +121 -0
- package/dist/charts/kinds/scatter-data.d.mts +18 -0
- package/dist/charts/kinds/scatter-data.mjs +123 -0
- package/dist/charts/kinds/scatter-layout.mjs +189 -0
- package/dist/charts/kinds/scatter-marks.mjs +149 -0
- package/dist/charts/kinds/scatter-words.mjs +80 -0
- package/dist/charts/kinds/scatter.d.mts +21 -0
- package/dist/charts/kinds/scatter.mjs +72 -0
- package/dist/charts/kinds/stat-data.mjs +91 -0
- package/dist/charts/kinds/stat-layout.mjs +108 -0
- package/dist/charts/kinds/stat-marks.mjs +85 -0
- package/dist/charts/kinds/stat-words.mjs +57 -0
- package/dist/charts/kinds/stat.d.mts +28 -0
- package/dist/charts/kinds/stat.mjs +35 -0
- package/dist/charts/kinds/time-chart.mjs +74 -0
- package/dist/charts/kinds/time-frame.mjs +113 -0
- package/dist/charts/kinds/time-notes.mjs +36 -0
- package/dist/charts/kinds/time-plan.d.mts +8 -0
- package/dist/charts/kinds/time-plan.mjs +57 -0
- package/dist/charts/kinds/time-plot.mjs +56 -0
- package/dist/charts/kinds/time-stops.mjs +56 -0
- package/dist/charts/kinds/value-plot.mjs +33 -0
- package/dist/charts/kinds/waterfall-data.d.mts +7 -0
- package/dist/charts/kinds/waterfall-data.mjs +71 -0
- package/dist/charts/kinds/waterfall-layout.mjs +38 -0
- package/dist/charts/kinds/waterfall-marks.mjs +82 -0
- package/dist/charts/kinds/waterfall-words.mjs +118 -0
- package/dist/charts/kinds/waterfall.d.mts +17 -0
- package/dist/charts/kinds/waterfall.mjs +66 -0
- package/dist/charts/layout/boxes.mjs +28 -0
- package/dist/charts/layout/dodge.mjs +75 -0
- package/dist/charts/layout/extent.mjs +25 -0
- package/dist/charts/layout/frame.mjs +97 -0
- package/dist/charts/layout/place-labels.mjs +101 -0
- package/dist/charts/layout/stack.mjs +31 -0
- package/dist/charts/marks/axes.mjs +137 -0
- package/dist/charts/marks/dot.mjs +31 -0
- package/dist/charts/marks/end-labels.mjs +124 -0
- package/dist/charts/marks/legend.mjs +141 -0
- package/dist/charts/marks/lines.d.mts +2 -0
- package/dist/charts/marks/lines.mjs +38 -0
- package/dist/charts/marks/lit.d.mts +3 -0
- package/dist/charts/marks/lit.mjs +63 -0
- package/dist/charts/marks/more.mjs +18 -0
- package/dist/charts/marks/notes.mjs +253 -0
- package/dist/charts/marks/series-line.mjs +110 -0
- package/dist/charts/marks/thin.mjs +138 -0
- package/dist/charts/marks/x-axis.mjs +32 -0
- package/dist/charts/scale/calendar.mjs +174 -0
- package/dist/charts/scale/time-ticks.mjs +154 -0
- package/dist/charts/scale/time.mjs +33 -0
- package/dist/charts/scale/value.mjs +161 -0
- package/dist/charts/scene/defs.mjs +33 -0
- package/dist/charts/scene/markup.mjs +35 -0
- package/dist/charts/scene/nearest.d.mts +11 -0
- package/dist/charts/scene/nearest.mjs +49 -0
- package/dist/charts/scene/paint.d.mts +12 -0
- package/dist/charts/scene/paint.mjs +13 -0
- package/dist/charts/scene/svg.d.mts +18 -0
- package/dist/charts/scene/svg.mjs +185 -0
- package/dist/charts/scene/types.d.mts +177 -0
- package/dist/charts/text/measure.d.mts +9 -0
- package/dist/charts/text/measure.mjs +123 -0
- package/dist/charts/theme.d.mts +47 -0
- package/dist/charts/theme.mjs +239 -0
- package/dist/charts-react/Chart.d.mts +91 -0
- package/dist/charts-react/Chart.mjs +166 -0
- package/dist/charts-react/Overlay.mjs +171 -0
- package/dist/charts-react/Picture.mjs +22 -0
- package/dist/charts-react/Reader.mjs +147 -0
- package/dist/charts-react/bucket-names.mjs +29 -0
- package/dist/charts-react/chart-unlayered.css +1 -0
- package/dist/charts-react/chart-unlayered.css.d.ts +1 -0
- package/dist/charts-react/chart.css +1 -0
- package/dist/charts-react/chart.css.d.ts +1 -0
- package/dist/charts-react/events.mjs +72 -0
- package/dist/charts-react/exact-sum.mjs +24 -0
- package/dist/charts-react/from-answer.mjs +185 -0
- package/dist/charts-react/index.d.mts +3 -0
- package/dist/charts-react/index.mjs +4 -0
- package/dist/charts-react/keys.mjs +117 -0
- package/dist/charts-react/opens.mjs +28 -0
- package/dist/charts-react/plan-bars.mjs +95 -0
- package/dist/charts-react/plan-funnel.mjs +120 -0
- package/dist/charts-react/plan-grid.mjs +121 -0
- package/dist/charts-react/plan-parts.d.mts +2 -0
- package/dist/charts-react/plan-parts.mjs +30 -0
- package/dist/charts-react/plan-time.mjs +84 -0
- package/dist/charts-react/plan.d.mts +3 -0
- package/dist/charts-react/plan.mjs +37 -0
- package/dist/charts-react/point-keys.mjs +22 -0
- package/dist/charts-react/pointer.mjs +99 -0
- package/dist/charts-react/reader-page.d.mts +8 -0
- package/dist/charts-react/reader-page.mjs +116 -0
- package/dist/charts-react/tiles.d.mts +24 -0
- package/dist/charts-react/tiles.mjs +142 -0
- package/dist/charts-react/tip-place.mjs +107 -0
- package/dist/charts-react/tip-room.mjs +31 -0
- package/dist/charts-react/useClientLayoutEffect.mjs +8 -0
- package/dist/charts-react/useScene.mjs +76 -0
- package/dist/charts-react/useSize.mjs +44 -0
- package/dist/charts-react/useStop.mjs +68 -0
- package/dist/core/conformance/memory/aggregate.mjs +2 -1
- package/dist/core/conformance/memory/rows.mjs +1 -1
- package/dist/core/conformance/memory/source.mjs +3 -2
- package/dist/core/document/display-comparisons.mjs +49 -0
- package/dist/core/document/display-entries.mjs +2 -1
- package/dist/core/document/display-validation.d.mts +12 -0
- package/dist/core/document/display-validation.mjs +56 -0
- package/dist/core/document/display.d.mts +27 -12
- package/dist/core/document/display.mjs +40 -49
- package/dist/core/document/view-layout.mjs +16 -0
- package/dist/core/engine/access.mjs +4 -2
- package/dist/core/engine/context.mjs +5 -7
- package/dist/core/engine/custom/cells.mjs +1 -1
- package/dist/core/engine/custom/records.mjs +2 -1
- package/dist/core/engine/dialect.d.mts +2 -0
- package/dist/core/engine/index.mjs +1 -1
- package/dist/core/engine/levels.mjs +33 -1
- package/dist/core/engine/plan.d.mts +2 -0
- package/dist/core/engine/postgres.mjs +1 -0
- package/dist/core/engine/rank.mjs +1 -1
- package/dist/core/engine/reader.d.mts +4 -0
- package/dist/core/engine/sql-partition.mjs +45 -0
- package/dist/core/engine/sql-reads.mjs +4 -1
- package/dist/core/engine/statement.mjs +4 -4
- package/dist/core/engine/version.d.mts +1 -1
- package/dist/core/engine/version.mjs +1 -1
- package/dist/core/error/codes.d.mts +1 -1
- package/dist/core/error/codes.mjs +1 -1
- package/dist/core/exact/decimal.d.mts +1 -4
- package/dist/core/exact/decimal.mjs +1 -14
- package/dist/core/exact/text.d.mts +5 -0
- package/dist/core/exact/text.mjs +19 -0
- package/dist/core/index.d.mts +9 -6
- package/dist/core/index.mjs +11 -8
- package/dist/core/model/types.d.mts +2 -0
- package/dist/core/model/validate/fields.mjs +1 -1
- package/dist/core/model/validate/known-members.mjs +2 -0
- package/dist/core/model/validate/shape.mjs +5 -1
- package/dist/core/oracle/visibility.mjs +2 -2
- package/dist/core/policy/bind.mjs +29 -1
- package/dist/core/query/drill/compose.d.mts +2 -0
- package/dist/core/query/drill/compose.mjs +1 -1
- package/dist/core/query/drill/synthetic.mjs +2 -1
- package/dist/core/query/drill/types.d.mts +7 -0
- package/dist/core/query/drill/types.mjs +3 -1
- package/dist/core/result/number.mjs +2 -1
- package/dist/core/result/time-axis.mjs +2 -1
- package/dist/core/schema/answers.d.mts +1176 -362
- package/dist/core/schema/commands.mjs +2 -1
- package/dist/core/schema/document.mjs +27 -2
- package/dist/core/schema/drill-answers.mjs +19 -1
- package/dist/core/schema/drill.d.mts +21 -6
- package/dist/core/schema/drill.mjs +7 -1
- package/dist/core/schema/index.d.mts +2 -1
- package/dist/core/schema/index.mjs +2 -1
- package/dist/core/schema/null-ops.d.mts +6 -0
- package/dist/core/schema/null-ops.mjs +18 -0
- package/dist/core/schema/query.mjs +30 -4
- package/dist/core/schema/tools.mjs +13 -0
- package/dist/core/schema/union-error.mjs +38 -0
- package/dist/core/time/bucket.mjs +2 -1
- package/dist/core/time/index.d.mts +3 -2
- package/dist/core/time/instant.d.mts +1 -2
- package/dist/core/time/instant.mjs +2 -30
- package/dist/core/time/range.mjs +2 -1
- package/dist/core/time/stored.mjs +2 -1
- package/dist/core/time/text-time.d.mts +10 -0
- package/dist/core/time/text-time.mjs +80 -0
- package/dist/core/types/api.d.mts +1 -1
- package/dist/core/types/api.mjs +1 -1
- package/dist/core/types/drill-api.d.mts +22 -2
- package/dist/dashboard-documents.d.mts +8 -0
- package/dist/dashboard-documents.mjs +52 -0
- package/dist/drill-access.mjs +40 -3
- package/dist/drill-comparison.mjs +71 -0
- package/dist/drill-crumbs.mjs +21 -2
- package/dist/drill-group-seal.mjs +13 -0
- package/dist/drill-open.mjs +7 -1
- package/dist/drill-seal.mjs +4 -6
- package/dist/drill-views.mjs +5 -13
- package/dist/drill.mjs +28 -58
- package/dist/editing.mjs +1 -1
- package/dist/endpoints/drill.mjs +2 -3
- package/dist/endpoints/index.d.mts +20 -0
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +6 -27
- package/dist/llms.txt +1 -1
- package/dist/react/Cell.mjs +2 -2
- package/dist/react/DashboardList.mjs +3 -3
- package/dist/react/Editing.mjs +3 -3
- package/dist/react/Header.mjs +2 -2
- package/dist/react/History.mjs +3 -3
- package/dist/react/Parts.mjs +2 -2
- package/dist/react/RowBody.d.mts +2 -4
- package/dist/react/RowBody.mjs +14 -39
- package/dist/react/Tile.mjs +4 -5
- package/dist/react/builder/FilterEditor.mjs +2 -2
- package/dist/react/builder/Picker.mjs +3 -3
- package/dist/react/builder/RangeEditor.mjs +3 -3
- package/dist/react/builder/Time.mjs +3 -3
- package/dist/react/builder/Values.mjs +2 -2
- package/dist/react/builder/WidgetBuilder.mjs +2 -2
- package/dist/react/digits.mjs +1 -1
- package/dist/react/drill/Bars.mjs +34 -38
- package/dist/react/drill/Comparison.mjs +204 -0
- package/dist/react/drill/Controls.mjs +8 -12
- package/dist/react/drill/DrillDrawer.mjs +42 -0
- package/dist/react/drill/DrillPanel.mjs +61 -41
- package/dist/react/drill/Header.mjs +65 -35
- package/dist/react/drill/ItemLink.mjs +40 -0
- package/dist/react/drill/Items.d.mts +2 -1
- package/dist/react/drill/Items.mjs +126 -99
- package/dist/react/drill/ItemsBody.mjs +38 -0
- package/dist/react/drill/ItemsFoot.mjs +24 -19
- package/dist/react/drill/Notes.mjs +23 -13
- package/dist/react/drill/PanelBody.mjs +7 -25
- package/dist/react/drill/RecordRow.mjs +47 -0
- package/dist/react/drill/Trend.mjs +186 -0
- package/dist/react/drill/board.mjs +21 -11
- package/dist/react/drill/drawer-context.mjs +5 -0
- package/dist/react/drill/item-parts.mjs +53 -35
- package/dist/react/drill/item-type.d.mts +2 -2
- package/dist/react/drill/lazy.d.mts +1 -4
- package/dist/react/drill/lazy.mjs +29 -11
- package/dist/react/drill/panel-focus.mjs +18 -7
- package/dist/react/drill/props.d.mts +7 -1
- package/dist/react/drill/props.mjs +39 -8
- package/dist/react/drill/record-page.mjs +61 -0
- package/dist/react/drill/state.mjs +17 -2
- package/dist/react/drill/trend-touch.mjs +33 -0
- package/dist/react/drill/useDrill.d.mts +3 -3
- package/dist/react/drill/useDrill.mjs +35 -15
- package/dist/react/drill/words.d.mts +14 -0
- package/dist/react/drill/words.mjs +20 -8
- package/dist/react/english.d.mts +4 -1
- package/dist/react/english.mjs +17 -2
- package/dist/react/filters/chip.mjs +2 -2
- package/dist/react/filters/menu-search.mjs +5 -5
- package/dist/react/filters/menu.mjs +2 -2
- package/dist/react/filters/number.mjs +2 -2
- package/dist/react/filters/operator.mjs +2 -2
- package/dist/react/filters/phone.mjs +2 -2
- package/dist/react/filters/values.mjs +4 -4
- package/dist/react/format-time.d.mts +2 -1
- package/dist/react/format-time.mjs +42 -34
- package/dist/react/format.mjs +15 -3
- package/dist/react/index.d.mts +4 -3
- package/dist/react/index.mjs +4 -3
- package/dist/react/messages.d.mts +23 -2
- package/dist/react/renderers/entries.d.mts +11 -0
- package/dist/react/renderers/list.mjs +8 -10
- package/dist/react/renderers/table-cells.mjs +38 -9
- package/dist/react/renderers/table-measure.mjs +2 -0
- package/dist/react/renderers/table-row.mjs +64 -7
- package/dist/react/renderers/table-sort.mjs +2 -8
- package/dist/react/renderers/table.mjs +16 -15
- package/dist/react/series.mjs +8 -3
- package/dist/react/theme-unlayered.css +1 -3144
- package/dist/react/theme.css +1 -3146
- package/dist/recharts/parts.mjs +1 -1
- package/dist/skills/derive-dashboards/SKILL.md +10 -5
- package/dist/store/apply.mjs +2 -2
- package/dist/store/editor.mjs +2 -2
- package/dist/store/writer.mjs +2 -2
- package/package.json +27 -2
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { orderAsWritten, topAsWritten } from "../data/format.mjs";
|
|
2
|
+
import { keyOf } from "../data/rows.mjs";
|
|
3
|
+
import { wordsOf } from "../data/words.mjs";
|
|
4
|
+
import { noValue, shownOf, summary, worthSaid } from "../describe.mjs";
|
|
5
|
+
import { rowStop } from "./row-marks.mjs";
|
|
6
|
+
import { labelOf, signedOrEnds } from "./waterfall-data.mjs";
|
|
7
|
+
import { colorOf } from "./waterfall-marks.mjs";
|
|
8
|
+
// src/kinds/waterfall-words.ts
|
|
9
|
+
/** What a pointer finds on a row: the step's value, and the running total after it. */
|
|
10
|
+
function stopOf(row, slot, at) {
|
|
11
|
+
const { frame, measure } = at;
|
|
12
|
+
const { leg } = row;
|
|
13
|
+
const items = leg.known ? [{
|
|
14
|
+
key: "value",
|
|
15
|
+
label: measure.label,
|
|
16
|
+
value: labelOf(leg, at),
|
|
17
|
+
color: colorOf(row, at),
|
|
18
|
+
x: at.x(leg.to),
|
|
19
|
+
y: frame.middle(slot),
|
|
20
|
+
aside: wordsOf(at.env).waterfall.runningTotal(measure.text(leg.to))
|
|
21
|
+
}] : [];
|
|
22
|
+
return rowStop(row.label, {
|
|
23
|
+
frame,
|
|
24
|
+
slot,
|
|
25
|
+
key: keyOf(row),
|
|
26
|
+
items,
|
|
27
|
+
nodes: at.nodes,
|
|
28
|
+
env: at.env
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
/** How the summary writes a change: "+$3,100", or a share's "40% to 46%" when not in points. */
|
|
32
|
+
const inWords = (leg, wording) => signedOrEnds(leg, wording, " to ");
|
|
33
|
+
/**
|
|
34
|
+
* The largest change one way: of the rises, or of the falls, as written, so a change written as
|
|
35
|
+
* zero is neither; none when the largest two of that way read alike.
|
|
36
|
+
*/
|
|
37
|
+
const largest = (changes, way, wording) => topAsWritten(changes, (c) => c.change * way, (v) => wording.measure.text(v * way), (c) => inWords(c, wording).sign === way);
|
|
38
|
+
/** Where the net change runs from and to, and how each end is said; nothing with one step. */
|
|
39
|
+
function endsOf(rows, measure) {
|
|
40
|
+
const first = rows[0];
|
|
41
|
+
const last = rows.at(-1);
|
|
42
|
+
if (first === void 0 || last === void 0 || rows.length < 2) return null;
|
|
43
|
+
const start = first.leg.total ? first.leg.to : 0;
|
|
44
|
+
const end = last.leg.to;
|
|
45
|
+
return {
|
|
46
|
+
start,
|
|
47
|
+
end,
|
|
48
|
+
from: first.leg.total ? `${first.label} ${measure.text(start)}` : "zero",
|
|
49
|
+
to: `${last.leg.total && last.leg.known ? `${last.label} ` : ""}${measure.text(end)}`
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* "Net change: Start $100,000 to End $115,000, +$15,000, which is better": the two ends, the
|
|
54
|
+
* change between them, and whether it went the better way when the measure has one. Two ends
|
|
55
|
+
* that read alike are said to be no change. Two that read apart with a change between them that
|
|
56
|
+
* reads as none ($0 to $0.01, a fifth of a cent apart) are said by the ends alone, so the ends
|
|
57
|
+
* and the amount never disagree. A share's net change is in points when the host says how, else
|
|
58
|
+
* its two ends say it alone.
|
|
59
|
+
*/
|
|
60
|
+
function netSaid(ends, wording, amounts) {
|
|
61
|
+
const { start, end } = ends;
|
|
62
|
+
const head = `Net change: ${ends.from} to ${ends.to}`;
|
|
63
|
+
if (orderAsWritten(end, start, wording.measure.text) === 0) return `${head}, no change`;
|
|
64
|
+
const written = inWords({
|
|
65
|
+
from: start,
|
|
66
|
+
to: end,
|
|
67
|
+
change: end - start
|
|
68
|
+
}, wording);
|
|
69
|
+
return `${head}${amounts && written.sign !== 0 ? `, ${written.text}` : ""}${worthSaid(written.sign, wording.measure)}`;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The net change, and what the changes add up to when that differs. It is said between two
|
|
73
|
+
* steps or more, whenever some step has a value: a waterfall that ends where it began says so,
|
|
74
|
+
* and one whose opening total has no value says its net change is not known (its start is not
|
|
75
|
+
* zero). Nothing when no step has a value: the summary says they had none.
|
|
76
|
+
*/
|
|
77
|
+
function netOf(rows, changes, wording) {
|
|
78
|
+
const { measure, points } = wording;
|
|
79
|
+
const ends = endsOf(rows, measure);
|
|
80
|
+
const opening = rows[0];
|
|
81
|
+
if (ends === null || opening === void 0 || !rows.some((row) => row.leg.known)) return [];
|
|
82
|
+
if (opening.leg.total && !opening.leg.known) return [`Net change: not known, as ${opening.label} has no value`];
|
|
83
|
+
const { start, end } = ends;
|
|
84
|
+
const sum = changes.reduce((all, c) => all + c.change, 0);
|
|
85
|
+
const differs = changes.length > 0 && measure.text(start + sum) !== measure.text(end);
|
|
86
|
+
const amounts = measure.share !== true || points !== void 0;
|
|
87
|
+
const summed = inWords({
|
|
88
|
+
from: start,
|
|
89
|
+
to: start + sum,
|
|
90
|
+
change: sum
|
|
91
|
+
}, wording).text;
|
|
92
|
+
return [netSaid(ends, wording, amounts), ...differs && amounts ? [`Sum of the changes: ${summed}`] : []];
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The waterfall in words: where it starts and ends, the net change, the largest each way (each
|
|
96
|
+
* change with whether it went the better way, as its colour says), and how many steps had no
|
|
97
|
+
* value.
|
|
98
|
+
*/
|
|
99
|
+
function describeWaterfall(rows, at) {
|
|
100
|
+
const changes = rows.flatMap(({ label, leg }) => leg.total || !leg.known ? [] : [{
|
|
101
|
+
label,
|
|
102
|
+
from: leg.from,
|
|
103
|
+
to: leg.to,
|
|
104
|
+
change: leg.change
|
|
105
|
+
}]);
|
|
106
|
+
const told = (word, change) => {
|
|
107
|
+
if (change === void 0) return [];
|
|
108
|
+
const { text, sign } = inWords(change, at);
|
|
109
|
+
return [`${word}: ${change.label}, ${text}${worthSaid(sign, at.measure)}`];
|
|
110
|
+
};
|
|
111
|
+
return summary([`Waterfall of ${at.measure.label}`, shownOf(rows.length, at.shown, "step")], [
|
|
112
|
+
...netOf(rows, changes, at),
|
|
113
|
+
...told("Largest rise", largest(changes, 1, at)),
|
|
114
|
+
...told("Largest fall", largest(changes, -1, at)),
|
|
115
|
+
noValue(rows.filter((row) => !row.leg.known).length, "step")
|
|
116
|
+
]);
|
|
117
|
+
}
|
|
118
|
+
export { describeWaterfall, stopOf };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Points } from "../data/format.mjs";
|
|
2
|
+
import { Env, Header, Measure } from "../data/types.mjs";
|
|
3
|
+
import { Scene } from "../scene/types.mjs";
|
|
4
|
+
import { WaterfallStep } from "./waterfall-data.mjs";
|
|
5
|
+
export type WaterfallOptions = Header & {
|
|
6
|
+
measure: Measure;
|
|
7
|
+
/** The palette place of the changes when the measure says no way is better. */
|
|
8
|
+
slot?: number;
|
|
9
|
+
/**
|
|
10
|
+
* How the host writes a change in a share, in points: "4 pts" for a size of 4, with no sign;
|
|
11
|
+
* the chart adds the sign.
|
|
12
|
+
*/
|
|
13
|
+
points?: Points;
|
|
14
|
+
};
|
|
15
|
+
/** A waterfall of totals and the changes between them. */
|
|
16
|
+
export declare function waterfallChart(given: readonly WaterfallStep[], options: WaterfallOptions, env: Env): Scene;
|
|
17
|
+
export type { WaterfallStep };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { bare, headerNodes } from "../layout/frame.mjs";
|
|
2
|
+
import { seriesPaint } from "../theme.mjs";
|
|
3
|
+
import { rowsMore, rowsZero } from "./row-marks.mjs";
|
|
4
|
+
import { hasRoom } from "./row-chart.mjs";
|
|
5
|
+
import { cleaned, walked } from "./waterfall-data.mjs";
|
|
6
|
+
import { layoutOf } from "./waterfall-layout.mjs";
|
|
7
|
+
import { joinNodes, rowNodes } from "./waterfall-marks.mjs";
|
|
8
|
+
import { describeWaterfall, stopOf } from "./waterfall-words.mjs";
|
|
9
|
+
// src/kinds/waterfall.ts
|
|
10
|
+
/** A waterfall of totals and the changes between them. */
|
|
11
|
+
function waterfallChart(given, options, env) {
|
|
12
|
+
const { measure } = options;
|
|
13
|
+
const rows = walked(cleaned(given));
|
|
14
|
+
const { frame, reach, x, kept, moreSlot, drawn, numbers, plot } = layoutOf(rows, options, env);
|
|
15
|
+
const { points } = options;
|
|
16
|
+
const roomy = hasRoom(frame, reach, drawn);
|
|
17
|
+
const summary = describeWaterfall(rows, {
|
|
18
|
+
measure,
|
|
19
|
+
points,
|
|
20
|
+
shown: roomy ? kept.length : 0
|
|
21
|
+
});
|
|
22
|
+
if (!roomy) return bare(env, options, summary);
|
|
23
|
+
const at = {
|
|
24
|
+
frame,
|
|
25
|
+
x,
|
|
26
|
+
measure,
|
|
27
|
+
points,
|
|
28
|
+
own: seriesPaint(options.slot ?? 0),
|
|
29
|
+
numbers,
|
|
30
|
+
env
|
|
31
|
+
};
|
|
32
|
+
const placed = kept.map(({ row, slot }) => ({
|
|
33
|
+
row,
|
|
34
|
+
slot,
|
|
35
|
+
nodes: rowNodes(row, slot, at)
|
|
36
|
+
}));
|
|
37
|
+
return {
|
|
38
|
+
width: env.width,
|
|
39
|
+
height: env.height,
|
|
40
|
+
plot,
|
|
41
|
+
summary,
|
|
42
|
+
nodes: [
|
|
43
|
+
...headerNodes(env, options),
|
|
44
|
+
...joinNodes(kept, at),
|
|
45
|
+
...placed.flatMap(({ nodes }) => nodes),
|
|
46
|
+
...rowsZero(kept.map(({ slot }) => slot), {
|
|
47
|
+
frame,
|
|
48
|
+
x,
|
|
49
|
+
plot
|
|
50
|
+
}),
|
|
51
|
+
...rowsMore(rows.length - kept.length, {
|
|
52
|
+
frame,
|
|
53
|
+
env,
|
|
54
|
+
slot: moreSlot
|
|
55
|
+
})
|
|
56
|
+
],
|
|
57
|
+
hover: {
|
|
58
|
+
snap: "y",
|
|
59
|
+
stops: placed.map(({ row, slot, nodes }) => stopOf(row, slot, {
|
|
60
|
+
...at,
|
|
61
|
+
nodes
|
|
62
|
+
}))
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export { waterfallChart };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// src/layout/boxes.ts
|
|
2
|
+
/** Whether a box is wholly inside another. */
|
|
3
|
+
const inside = (box, bounds) => box.x >= bounds.x && box.y >= bounds.y && box.x + box.width <= bounds.x + bounds.width && box.y + box.height <= bounds.y + bounds.height;
|
|
4
|
+
/** Whether two boxes share any area, with `pad` of room wanted between them. */
|
|
5
|
+
const overlaps = (a, b, pad = 0) => a.x < b.x + b.width + pad && b.x < a.x + a.width + pad && a.y < b.y + b.height + pad && b.y < a.y + a.height + pad;
|
|
6
|
+
/** Whether a box covers any part of a circle: its nearest point is inside the circle. */
|
|
7
|
+
function covers(box, circle) {
|
|
8
|
+
const dx = circle.x - Math.max(box.x, Math.min(circle.x, box.x + box.width));
|
|
9
|
+
const dy = circle.y - Math.max(box.y, Math.min(circle.y, box.y + box.height));
|
|
10
|
+
return dx * dx + dy * dy < circle.r * circle.r;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Room kept between two texts placed near each other: two pixels, so their letters never touch
|
|
14
|
+
* and a name still sits by its own mark.
|
|
15
|
+
*/
|
|
16
|
+
const APART = 2;
|
|
17
|
+
/**
|
|
18
|
+
* The first of some places a text may take that is inside the bounds, clear of every box
|
|
19
|
+
* already taken and covering no dot; nothing when none is. Every text that looks for a place
|
|
20
|
+
* (a name beside a dot, a name on a reference line) asks this, so "free" means one thing.
|
|
21
|
+
*/
|
|
22
|
+
function firstFree(spots, boxOf, clear) {
|
|
23
|
+
return spots.find((spot) => {
|
|
24
|
+
const box = boxOf(spot);
|
|
25
|
+
return inside(box, clear.bounds) && !clear.taken.some((other) => overlaps(box, other, APART)) && !clear.dots.some((dot) => covers(box, dot));
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
export { firstFree };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// src/layout/dodge.ts
|
|
2
|
+
/**
|
|
3
|
+
* Centres for the labels, in their own order: no two closer than their sizes and `gap` allow,
|
|
4
|
+
* all inside `min`..`max` when they fit there, and each as near where it wants to be as that
|
|
5
|
+
* leaves room for. Labels that want the same place keep the order they were given in.
|
|
6
|
+
*/
|
|
7
|
+
function dodge(labels, bounds) {
|
|
8
|
+
const order = labels.map((_l, i) => i).toSorted((a, b) => at(labels, a) - at(labels, b) || a - b);
|
|
9
|
+
const sorted = order.map((i) => labels[i] ?? {
|
|
10
|
+
at: 0,
|
|
11
|
+
size: 0
|
|
12
|
+
});
|
|
13
|
+
const offsets = offsetsOf(sorted, bounds.gap);
|
|
14
|
+
const inside = clamped(pooled(sorted.map((l, i) => l.at - (offsets[i] ?? 0))).map((y, i) => y + (offsets[i] ?? 0)), sorted, bounds);
|
|
15
|
+
const result = Array.from({ length: labels.length }, () => 0);
|
|
16
|
+
for (const [rank, index] of order.entries()) result[index] = inside[rank] ?? 0;
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
const at = (labels, i) => labels[i]?.at ?? 0;
|
|
20
|
+
/** Each label's least distance from the first one's centre, with every label touching the last. */
|
|
21
|
+
function offsetsOf(sorted, gap) {
|
|
22
|
+
const offsets = [];
|
|
23
|
+
let total = 0;
|
|
24
|
+
for (const [i, label] of sorted.entries()) {
|
|
25
|
+
const before = sorted[i - 1];
|
|
26
|
+
if (before !== void 0) total += (before.size + label.size) / 2 + gap;
|
|
27
|
+
offsets.push(total);
|
|
28
|
+
}
|
|
29
|
+
return offsets;
|
|
30
|
+
}
|
|
31
|
+
/** The nearest non-decreasing sequence to `targets`: runs that would cross share their mean. */
|
|
32
|
+
function pooled(targets) {
|
|
33
|
+
const blocks = [];
|
|
34
|
+
for (const [i, target] of targets.entries()) {
|
|
35
|
+
let block = {
|
|
36
|
+
sum: target,
|
|
37
|
+
count: 1,
|
|
38
|
+
from: i
|
|
39
|
+
};
|
|
40
|
+
for (let last = blocks.at(-1); last !== void 0 && last.sum / last.count > block.sum / block.count; last = blocks.at(-1)) {
|
|
41
|
+
blocks.pop();
|
|
42
|
+
block = {
|
|
43
|
+
sum: last.sum + block.sum,
|
|
44
|
+
count: last.count + block.count,
|
|
45
|
+
from: last.from
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
blocks.push(block);
|
|
49
|
+
}
|
|
50
|
+
return blocks.flatMap((b) => Array.from({ length: b.count }, () => b.sum / b.count));
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Pushed inside the bounds: down from the top first, then up from the bottom, each keeping its
|
|
54
|
+
* distance from its neighbour. When the labels cannot all fit, the first ones run past `min`:
|
|
55
|
+
* so a caller asks `roomFor` first, and places no more labels than have room.
|
|
56
|
+
*/
|
|
57
|
+
function clamped(placed, sorted, bounds) {
|
|
58
|
+
const out = [...placed];
|
|
59
|
+
const half = (i) => (sorted[i]?.size ?? 0) / 2;
|
|
60
|
+
const apart = (i) => half(i) + half(i + 1) + bounds.gap;
|
|
61
|
+
for (let i = 0; i < out.length; i++) {
|
|
62
|
+
const floor = i === 0 ? bounds.min + half(0) : (out[i - 1] ?? 0) + apart(i - 1);
|
|
63
|
+
out[i] = Math.max(out[i] ?? 0, floor);
|
|
64
|
+
}
|
|
65
|
+
for (let i = out.length - 1; i >= 0; i--) {
|
|
66
|
+
const ceiling = i === out.length - 1 ? bounds.max - half(i) : (out[i + 1] ?? 0) - apart(i);
|
|
67
|
+
out[i] = Math.min(out[i] ?? 0, ceiling);
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
/** How many labels of one size fit between the bounds. */
|
|
72
|
+
function roomFor(size, bounds) {
|
|
73
|
+
return Math.max(0, Math.floor((bounds.max - bounds.min + bounds.gap) / (size + bounds.gap)));
|
|
74
|
+
}
|
|
75
|
+
export { dodge, roomFor };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// src/layout/extent.ts
|
|
2
|
+
/** A text's width as it was measured: each of its runs at its own weight. */
|
|
3
|
+
function textWidth(node, measure) {
|
|
4
|
+
const font = (weight) => ({
|
|
5
|
+
size: node.size,
|
|
6
|
+
...weight === void 0 ? {} : { weight }
|
|
7
|
+
});
|
|
8
|
+
if (node.runs === void 0) return measure(node.text, font(node.weight));
|
|
9
|
+
return node.runs.reduce((sum, run) => sum + measure(run.text, font(run.weight ?? node.weight)), 0);
|
|
10
|
+
}
|
|
11
|
+
/** How far right a mark reaches; nothing for one with no single place (a path, a group). */
|
|
12
|
+
function rightOf(node, measure) {
|
|
13
|
+
if (node.kind === "rect") return node.x + node.width;
|
|
14
|
+
if (node.kind === "line") return Math.max(node.x1, node.x2);
|
|
15
|
+
if (node.kind === "circle") return node.cx + node.r;
|
|
16
|
+
if (node.kind !== "text") return void 0;
|
|
17
|
+
const width = textWidth(node, measure);
|
|
18
|
+
const shift = {
|
|
19
|
+
start: width,
|
|
20
|
+
middle: width / 2,
|
|
21
|
+
end: 0
|
|
22
|
+
}[node.anchor ?? "start"];
|
|
23
|
+
return node.x + shift;
|
|
24
|
+
}
|
|
25
|
+
export { rightOf };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { said } from "../data/format.mjs";
|
|
2
|
+
import { fitText } from "../text/measure.mjs";
|
|
3
|
+
import { LINE, SPACE, TEXT } from "../theme.mjs";
|
|
4
|
+
// src/layout/frame.ts
|
|
5
|
+
/** The room a header's two lines take. */
|
|
6
|
+
const TITLE_LINE = LINE.title;
|
|
7
|
+
const SUBTITLE_LINE = LINE.subtitle;
|
|
8
|
+
/** The room between the header and what is under it: the header's own, wider than any gap inside a plot so the two read apart. */
|
|
9
|
+
const HEADER_GAP = 12;
|
|
10
|
+
/** The room a source takes at the foot: a line of its text, and the gap over it. */
|
|
11
|
+
const SOURCE_ROOM = LINE.source + SPACE.gutter;
|
|
12
|
+
/** How far a header's text is kept off the picture's edge and off the line over it. */
|
|
13
|
+
const EDGE = 2;
|
|
14
|
+
/** Where a plot starts when nothing is over it: far enough down that its top number is drawn whole. */
|
|
15
|
+
const TOP_INSET = 8;
|
|
16
|
+
/** A header's lines as they are drawn: a host's text that is empty is no line, as any text it gives. */
|
|
17
|
+
const linesOf = (header) => ({
|
|
18
|
+
title: said(header.title),
|
|
19
|
+
subtitle: said(header.subtitle),
|
|
20
|
+
source: said(header.source)
|
|
21
|
+
});
|
|
22
|
+
/** How tall a header is, with the room under it; nothing for a chart with none. */
|
|
23
|
+
function headerHeight(header) {
|
|
24
|
+
const { title, subtitle } = linesOf(header);
|
|
25
|
+
const lines = (title === void 0 ? 0 : TITLE_LINE) + (subtitle === void 0 ? 0 : SUBTITLE_LINE);
|
|
26
|
+
return lines === 0 ? 0 : lines + HEADER_GAP;
|
|
27
|
+
}
|
|
28
|
+
/** The room the source line takes at the bottom; nothing for a chart with none. */
|
|
29
|
+
const footHeight = (header) => linesOf(header).source === void 0 ? 0 : SOURCE_ROOM;
|
|
30
|
+
/**
|
|
31
|
+
* The room between the header and the source line, the picture's full width: where a chart with
|
|
32
|
+
* no axis down its side (rows lying down, a grid of panels) lays itself out.
|
|
33
|
+
*/
|
|
34
|
+
function contentBox(env, header) {
|
|
35
|
+
const top = headerHeight(header);
|
|
36
|
+
return {
|
|
37
|
+
x: 0,
|
|
38
|
+
y: top,
|
|
39
|
+
width: env.width,
|
|
40
|
+
height: Math.max(0, env.height - top - footHeight(header))
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/** Whether a plot has room for marks worth reading, across and down. */
|
|
44
|
+
const roomy = (plot) => plot.width >= 24 && plot.height >= 24;
|
|
45
|
+
/**
|
|
46
|
+
* A chart with no room for a plot, or with nothing to draw: its header alone, and still what it
|
|
47
|
+
* would show, in words. Every kind bends the same way when it is given too little room.
|
|
48
|
+
*/
|
|
49
|
+
function bare(env, header, summary) {
|
|
50
|
+
const box = contentBox(env, header);
|
|
51
|
+
return {
|
|
52
|
+
width: env.width,
|
|
53
|
+
height: env.height,
|
|
54
|
+
plot: {
|
|
55
|
+
...box,
|
|
56
|
+
width: Math.max(1, box.width),
|
|
57
|
+
height: Math.max(1, box.height)
|
|
58
|
+
},
|
|
59
|
+
summary,
|
|
60
|
+
nodes: headerNodes(env, header)
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** The title and subtitle at the top left, and the source at the bottom left. */
|
|
64
|
+
function headerNodes(env, header) {
|
|
65
|
+
const line = (text, y, font) => ({
|
|
66
|
+
kind: "text",
|
|
67
|
+
part: "header",
|
|
68
|
+
x: 0,
|
|
69
|
+
y,
|
|
70
|
+
text: fitText(text, env.width, font, env.measure),
|
|
71
|
+
baseline: "hanging",
|
|
72
|
+
...font
|
|
73
|
+
});
|
|
74
|
+
const { title, subtitle, source } = linesOf(header);
|
|
75
|
+
const under = title === void 0 ? 0 : TITLE_LINE;
|
|
76
|
+
const foot = env.height - TEXT.source.size - EDGE;
|
|
77
|
+
return [
|
|
78
|
+
...title === void 0 ? [] : [line(title, EDGE / 2, TEXT.title)],
|
|
79
|
+
...subtitle === void 0 ? [] : [line(subtitle, under + EDGE, TEXT.subtitle)],
|
|
80
|
+
...source === void 0 ? [] : [line(source, foot, TEXT.source)]
|
|
81
|
+
];
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The plot's box: the chart's size less its header, its source line and the margins. Never
|
|
85
|
+
* smaller than a pixel a side, so a chart in no room at all still lays out.
|
|
86
|
+
*/
|
|
87
|
+
function plotBox(env, header, margins) {
|
|
88
|
+
const top = Math.max(TOP_INSET, headerHeight(header) + (margins.top ?? 0));
|
|
89
|
+
const bottom = margins.bottom + footHeight(header);
|
|
90
|
+
return {
|
|
91
|
+
x: margins.left,
|
|
92
|
+
y: top,
|
|
93
|
+
width: Math.max(1, env.width - margins.left - margins.right),
|
|
94
|
+
height: Math.max(1, env.height - top - bottom)
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
export { bare, contentBox, headerHeight, headerNodes, plotBox, roomy };
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { firstFree } from "./boxes.mjs";
|
|
2
|
+
// src/layout/place-labels.ts
|
|
3
|
+
/**
|
|
4
|
+
* Room between a dot's edge and its name: three pixels, clear of the ring round the dot and
|
|
5
|
+
* near enough that the name reads as that dot's.
|
|
6
|
+
*/
|
|
7
|
+
const GAP = 3;
|
|
8
|
+
/** Right, left, above, below, then the four corners. */
|
|
9
|
+
const SPOTS = [
|
|
10
|
+
{
|
|
11
|
+
across: 1,
|
|
12
|
+
down: 0
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
across: -1,
|
|
16
|
+
down: 0
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
across: 0,
|
|
20
|
+
down: -1
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
across: 0,
|
|
24
|
+
down: 1
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
across: 1,
|
|
28
|
+
down: -1
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
across: -1,
|
|
32
|
+
down: -1
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
across: 1,
|
|
36
|
+
down: 1
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
across: -1,
|
|
40
|
+
down: 1
|
|
41
|
+
}
|
|
42
|
+
];
|
|
43
|
+
/** Where a name's text is set in its box: at its left edge right of the dot, and so on. */
|
|
44
|
+
function setAt(box, across) {
|
|
45
|
+
if (across > 0) return {
|
|
46
|
+
x: box.x,
|
|
47
|
+
anchor: "start"
|
|
48
|
+
};
|
|
49
|
+
if (across < 0) return {
|
|
50
|
+
x: box.x + box.width,
|
|
51
|
+
anchor: "end"
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
x: box.x + box.width / 2,
|
|
55
|
+
anchor: "middle"
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/** The box a name takes at one spot around its dot. */
|
|
59
|
+
function boxAt(label, spot) {
|
|
60
|
+
const reach = spot.across !== 0 && spot.down !== 0 ? label.r * Math.SQRT1_2 + GAP : label.r + GAP;
|
|
61
|
+
return {
|
|
62
|
+
x: spot.across === 0 ? label.x - label.width / 2 : spot.across > 0 ? label.x + reach : label.x - reach - label.width,
|
|
63
|
+
y: spot.down === 0 ? label.y - label.height / 2 : spot.down > 0 ? label.y + reach : label.y - reach - label.height,
|
|
64
|
+
width: label.width,
|
|
65
|
+
height: label.height
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/** How far a box is from a dot's edge; nothing when they touch. */
|
|
69
|
+
function apart(box, dot) {
|
|
70
|
+
const across = Math.max(box.x - dot.x, 0, dot.x - (box.x + box.width));
|
|
71
|
+
const down = Math.max(box.y - dot.y, 0, dot.y - (box.y + box.height));
|
|
72
|
+
return Math.max(0, Math.hypot(across, down) - dot.r);
|
|
73
|
+
}
|
|
74
|
+
/** Whether a name's box reads as its own dot's: no other dot is nearer to it. */
|
|
75
|
+
const itsOwn = (box, dot, dots) => dots.every((other) => apart(box, other) >= apart(box, dot));
|
|
76
|
+
/**
|
|
77
|
+
* A place for each name, in the order given, or nothing for a name with no free spot. Every
|
|
78
|
+
* wanted name's dot is also a dot no name may cover, and a name is never set nearer another
|
|
79
|
+
* dot than its own.
|
|
80
|
+
*/
|
|
81
|
+
function placeLabels(wanted, bounds, obstacles = {}) {
|
|
82
|
+
const dots = [...wanted, ...obstacles.dots ?? []];
|
|
83
|
+
const placed = [...obstacles.taken ?? []];
|
|
84
|
+
const result = [];
|
|
85
|
+
for (const label of wanted) {
|
|
86
|
+
const spots = SPOTS.filter((s) => itsOwn(boxAt(label, s), label, dots));
|
|
87
|
+
const spot = firstFree(spots, (s) => boxAt(label, s), {
|
|
88
|
+
bounds,
|
|
89
|
+
taken: placed,
|
|
90
|
+
dots
|
|
91
|
+
});
|
|
92
|
+
const box = spot === void 0 ? void 0 : boxAt(label, spot);
|
|
93
|
+
if (box !== void 0) placed.push(box);
|
|
94
|
+
result.push(spot === void 0 || box === void 0 ? void 0 : {
|
|
95
|
+
...setAt(box, spot.across),
|
|
96
|
+
y: box.y + box.height / 2
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
export { placeLabels };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// src/layout/stack.ts
|
|
2
|
+
/** Each series' span at each place, `[series][place]`; nothing where it has no value. */
|
|
3
|
+
function stacked(series, places) {
|
|
4
|
+
const ends = {
|
|
5
|
+
up: Array.from({ length: places }, () => 0),
|
|
6
|
+
down: Array.from({ length: places }, () => 0)
|
|
7
|
+
};
|
|
8
|
+
return series.map((values) => Array.from({ length: places }, (_v, i) => {
|
|
9
|
+
const value = values[i] ?? null;
|
|
10
|
+
return value === null ? void 0 : pushed(value >= 0 ? ends.up : ends.down, i, value);
|
|
11
|
+
}));
|
|
12
|
+
}
|
|
13
|
+
/** The span a value takes on the stack at a place, the stack growing by it. */
|
|
14
|
+
function pushed(stack, place, value) {
|
|
15
|
+
const from = stack[place] ?? 0;
|
|
16
|
+
stack[place] = from + value;
|
|
17
|
+
return {
|
|
18
|
+
from,
|
|
19
|
+
to: from + value
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** Every height an axis must reach for some series stacked at each place. */
|
|
23
|
+
const stackHeights = (series, places) => stackEnds(stacked(series, places));
|
|
24
|
+
/**
|
|
25
|
+
* How far each stack reaches, for the axis: every span's far end, with the float's last digits
|
|
26
|
+
* rounded off, so shares that add up to a hair over one do not push the axis past 100%.
|
|
27
|
+
*/
|
|
28
|
+
function stackEnds(spans) {
|
|
29
|
+
return spans.flat().flatMap((span) => span === void 0 ? [] : [Number(span.to.toPrecision(12))]);
|
|
30
|
+
}
|
|
31
|
+
export { stackHeights, stacked };
|