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,26 @@
|
|
|
1
|
+
import { Env, Header, Reference, TimeTable } from "../data/types.mjs";
|
|
2
|
+
import { Scene } from "../scene/types.mjs";
|
|
3
|
+
import { Curve } from "../marks/lines.mjs";
|
|
4
|
+
/**
|
|
5
|
+
* What small multiples are drawn with. They take the notes a line chart takes, all but its
|
|
6
|
+
* marked moments (`events`), left out on purpose: a panel has no room for their names. The
|
|
7
|
+
* levels (`references`) are drawn in every panel, and named where a panel names its scale.
|
|
8
|
+
*/
|
|
9
|
+
export type MultiplesOptions = Header & {
|
|
10
|
+
/** The levels every panel is read against: a goal, an average. */
|
|
11
|
+
references?: readonly Reference[];
|
|
12
|
+
curve?: Curve;
|
|
13
|
+
/** One value axis for every panel (the default), or each its own. */
|
|
14
|
+
shared?: boolean;
|
|
15
|
+
/** How many panels across; from the width when left out. */
|
|
16
|
+
across?: number;
|
|
17
|
+
/** Keep zero on the axis even when the values sit far above it. */
|
|
18
|
+
zero?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* The palette place of the colour every panel wears; a series' own `slot` is not read. A
|
|
21
|
+
* series that brings its own `color` keeps it, and Other with none is grey.
|
|
22
|
+
*/
|
|
23
|
+
slot?: number;
|
|
24
|
+
};
|
|
25
|
+
/** Small multiples of series over time: one panel each, on one axis unless told otherwise. */
|
|
26
|
+
export declare function multiplesChart(given: TimeTable, options: MultiplesOptions, env: Env): Scene;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { panelTable } from "../data/clean.mjs";
|
|
2
|
+
import { bare, headerNodes } from "../layout/frame.mjs";
|
|
3
|
+
import { moreLine } from "../marks/more.mjs";
|
|
4
|
+
import { seriesPaint } from "../theme.mjs";
|
|
5
|
+
import { layoutOf } from "./multiples-layout.mjs";
|
|
6
|
+
import { panelNodes } from "./multiples-marks.mjs";
|
|
7
|
+
import { describeMultiples, hoverOf, wordingOf } from "./multiples-words.mjs";
|
|
8
|
+
// src/kinds/multiples.ts
|
|
9
|
+
/** Small multiples of series over time: one panel each, on one axis unless told otherwise. */
|
|
10
|
+
function multiplesChart(given, options, env) {
|
|
11
|
+
const table = panelTable(given);
|
|
12
|
+
const { panels, plot, hidden, moreY } = layoutOf(table, options, env);
|
|
13
|
+
const summary = describeMultiples(table, {
|
|
14
|
+
...wordingOf(table, options, env),
|
|
15
|
+
shown: panels.length
|
|
16
|
+
});
|
|
17
|
+
if (panels.length === 0) return bare(env, options, summary);
|
|
18
|
+
const at = {
|
|
19
|
+
x: table.x,
|
|
20
|
+
curve: options.curve ?? "straight",
|
|
21
|
+
references: options.references ?? [],
|
|
22
|
+
own: seriesPaint(options.slot ?? 0),
|
|
23
|
+
env
|
|
24
|
+
};
|
|
25
|
+
return {
|
|
26
|
+
width: env.width,
|
|
27
|
+
height: env.height,
|
|
28
|
+
plot,
|
|
29
|
+
summary,
|
|
30
|
+
nodes: [
|
|
31
|
+
...headerNodes(env, options),
|
|
32
|
+
...panels.flatMap((panel) => panelNodes(panel, at)),
|
|
33
|
+
...moreLine(hidden, {
|
|
34
|
+
y: moreY,
|
|
35
|
+
width: env.width,
|
|
36
|
+
env
|
|
37
|
+
})
|
|
38
|
+
],
|
|
39
|
+
hover: hoverOf(table, panels, at)
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export { multiplesChart };
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { contentBox } from "../layout/frame.mjs";
|
|
2
|
+
import { widest } from "../text/measure.mjs";
|
|
3
|
+
import { SPACE, TEXT } from "../theme.mjs";
|
|
4
|
+
import { rowBar, rowName, rowStop, rowValue, rowsMore, rowsZero } from "./row-marks.mjs";
|
|
5
|
+
// src/kinds/row-chart.ts
|
|
6
|
+
/** A row is this tall at most and at least; a bar this thick at most, with this much between two. */
|
|
7
|
+
const ROW = {
|
|
8
|
+
most: 34,
|
|
9
|
+
least: 20,
|
|
10
|
+
thick: 18,
|
|
11
|
+
between: 8
|
|
12
|
+
};
|
|
13
|
+
/** The most of the width the names take, and the least the marks keep whatever the numbers ask. */
|
|
14
|
+
const NAMES = .38;
|
|
15
|
+
const LEAST_MARKS = 40;
|
|
16
|
+
/** Where the rows named by some labels go. */
|
|
17
|
+
function rowFrame(labels, rule, env) {
|
|
18
|
+
const box = contentBox(env, rule.header);
|
|
19
|
+
const top = box.y + (rule.above ?? 0);
|
|
20
|
+
const room = Math.max(0, box.height - (rule.above ?? 0) - (rule.below ?? 0));
|
|
21
|
+
const tallest = rule.most ?? ROW.most;
|
|
22
|
+
const rowHeight = Math.max(ROW.least, Math.min(tallest, room / Math.max(1, labels.length)));
|
|
23
|
+
const fit = Math.floor(room / rowHeight);
|
|
24
|
+
const shown = labels.length <= fit ? labels.length : Math.max(0, fit - 1);
|
|
25
|
+
const words = [...rule.labelsOf?.(shown) ?? labels.slice(0, shown), ...rule.also ?? []];
|
|
26
|
+
const names = Math.min(env.width * NAMES, Math.ceil(widest(words, TEXT.name, env.measure)));
|
|
27
|
+
return {
|
|
28
|
+
names,
|
|
29
|
+
left: names + SPACE.names,
|
|
30
|
+
top,
|
|
31
|
+
rowHeight,
|
|
32
|
+
fit,
|
|
33
|
+
shown,
|
|
34
|
+
middle: (slot) => top + (slot + .5) * rowHeight,
|
|
35
|
+
band: (slot) => ({
|
|
36
|
+
x: 0,
|
|
37
|
+
y: top + slot * rowHeight,
|
|
38
|
+
width: env.width,
|
|
39
|
+
height: rowHeight
|
|
40
|
+
})
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/** How thick a row's bar is: the row less the room between two bars, never more than a bar's most. */
|
|
44
|
+
const barThick = (rowHeight, between = ROW.between) => Math.max(1, Math.min(ROW.thick, rowHeight - between));
|
|
45
|
+
/**
|
|
46
|
+
* Where the marks run: from the names to the right edge, less the room kept past their ends for
|
|
47
|
+
* the numbers written there, on the left too when a mark can run left of zero. The numbers
|
|
48
|
+
* never squeeze the marks under a least width: numbers with no room are left out, and the
|
|
49
|
+
* marks run the whole stretch.
|
|
50
|
+
*/
|
|
51
|
+
function reachOf(frame, texts, at) {
|
|
52
|
+
const sides = at.bothSides === true ? 2 : 1;
|
|
53
|
+
const pad = SPACE.value + 1;
|
|
54
|
+
const cap = (at.env.width - frame.left - pad * sides - LEAST_MARKS) / sides;
|
|
55
|
+
const wanted = Math.ceil(widest(texts, TEXT.value, at.env.measure));
|
|
56
|
+
const fits = wanted <= cap;
|
|
57
|
+
const room = fits ? Math.max(0, wanted) : 0;
|
|
58
|
+
const from = frame.left + (sides === 2 ? room + pad : 0);
|
|
59
|
+
return {
|
|
60
|
+
from,
|
|
61
|
+
to: Math.max(from + 1, at.env.width - room - pad),
|
|
62
|
+
fits
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/** The plot of a row chart: between the marks' ends, as tall as the slots drawn. */
|
|
66
|
+
const rowPlot = (frame, reach, slots) => ({
|
|
67
|
+
x: reach.from,
|
|
68
|
+
y: frame.top,
|
|
69
|
+
width: reach.to - reach.from,
|
|
70
|
+
height: Math.max(1, slots * frame.rowHeight)
|
|
71
|
+
});
|
|
72
|
+
/**
|
|
73
|
+
* Which rows are drawn when not all fit: the first that fit, except that a row the chart is
|
|
74
|
+
* about (one in focus) is never the one left out while another could be. In the rows' order.
|
|
75
|
+
*/
|
|
76
|
+
function keptRows(rows, shown, matters) {
|
|
77
|
+
if (rows.length <= shown) return [...rows];
|
|
78
|
+
const must = new Set(rows.filter((row) => matters(row)).slice(0, shown));
|
|
79
|
+
const rest = new Set(rows.filter((row) => !must.has(row)).slice(0, shown - must.size));
|
|
80
|
+
return rows.filter((row) => must.has(row) || rest.has(row));
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The rows drawn, each in its slot, and the slot of the line that counts the rest. When rows
|
|
84
|
+
* are left out, the line stands where the first of them would have been, and a row kept from
|
|
85
|
+
* further down (one the chart is about, a closing total) is drawn under it: the reader sees
|
|
86
|
+
* where the rows were cut, and never takes a row from far down the list for the next in order.
|
|
87
|
+
*/
|
|
88
|
+
function slotted(rows, kept) {
|
|
89
|
+
const cut = rows.findIndex((row) => !kept.includes(row));
|
|
90
|
+
if (cut === -1) return {
|
|
91
|
+
kept: kept.map((row, slot) => ({
|
|
92
|
+
row,
|
|
93
|
+
slot
|
|
94
|
+
})),
|
|
95
|
+
moreSlot: kept.length
|
|
96
|
+
};
|
|
97
|
+
return {
|
|
98
|
+
kept: kept.map((row, i) => ({
|
|
99
|
+
row,
|
|
100
|
+
slot: i < cut ? i : i + 1
|
|
101
|
+
})),
|
|
102
|
+
moreSlot: cut
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
/** How many slots some rows drawn take, the line that counts the rest among them: down to the last row's. */
|
|
106
|
+
const slotsOf = (kept) => (kept.at(-1)?.slot ?? -1) + 1;
|
|
107
|
+
/**
|
|
108
|
+
* Whether a row chart has anything to draw and room to draw it: some row has a value, a row
|
|
109
|
+
* fits down, and the marks have room across. Without, the chart is its header alone.
|
|
110
|
+
*/
|
|
111
|
+
const hasRoom = (frame, reach, drawn) => drawn && frame.shown > 0 && reach.to - reach.from >= 24;
|
|
112
|
+
export { ROW, barThick, hasRoom, keptRows, reachOf, rowBar, rowFrame, rowName, rowPlot, rowStop, rowValue, rowsMore, rowsZero, slotsOf, slotted };
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { most } from "../data/range.mjs";
|
|
2
|
+
import { rightOf } from "../layout/extent.mjs";
|
|
3
|
+
import { zeroLine } from "../marks/axes.mjs";
|
|
4
|
+
import { moreLine } from "../marks/more.mjs";
|
|
5
|
+
import { fitText } from "../text/measure.mjs";
|
|
6
|
+
import { SPACE, TEXT, roundedEnd } from "../theme.mjs";
|
|
7
|
+
// src/kinds/row-marks.ts
|
|
8
|
+
/** A row's name: at the left of its row, cut to the names' width. */
|
|
9
|
+
function rowName(label, at) {
|
|
10
|
+
return {
|
|
11
|
+
kind: "text",
|
|
12
|
+
part: "name",
|
|
13
|
+
key: at.key,
|
|
14
|
+
x: 0,
|
|
15
|
+
y: at.frame.middle(at.slot),
|
|
16
|
+
text: fitText(label, at.frame.names, TEXT.name, at.env.measure),
|
|
17
|
+
...TEXT.name,
|
|
18
|
+
baseline: "middle"
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The number of a row's mark: past the end it belongs to. Left out when it has no room there,
|
|
23
|
+
* never cut, never over a name and never across the zero line.
|
|
24
|
+
*/
|
|
25
|
+
function rowValue(text, at) {
|
|
26
|
+
const font = at.text ?? TEXT.value;
|
|
27
|
+
const wide = at.env.measure(text, font);
|
|
28
|
+
const after = {
|
|
29
|
+
x: at.right + SPACE.value,
|
|
30
|
+
anchor: "start"
|
|
31
|
+
};
|
|
32
|
+
const before = {
|
|
33
|
+
x: at.left - SPACE.value,
|
|
34
|
+
anchor: "end"
|
|
35
|
+
};
|
|
36
|
+
const struck = (from, to) => at.zero !== void 0 && from < at.zero && to > at.zero;
|
|
37
|
+
const fits = (side) => side.anchor === "start" ? side.x + wide <= at.env.width && !struck(side.x, side.x + wide) : side.x - wide >= at.frame.left && !struck(side.x - wide, side.x);
|
|
38
|
+
const own = at.toRight ? after : before;
|
|
39
|
+
const other = at.toRight ? before : after;
|
|
40
|
+
const side = (at.floats === true ? [own, other] : [own]).find((s) => fits(s));
|
|
41
|
+
if (side === void 0) return [];
|
|
42
|
+
return [{
|
|
43
|
+
kind: "text",
|
|
44
|
+
part: "value",
|
|
45
|
+
...side,
|
|
46
|
+
y: at.y,
|
|
47
|
+
text,
|
|
48
|
+
...font,
|
|
49
|
+
baseline: "middle"
|
|
50
|
+
}];
|
|
51
|
+
}
|
|
52
|
+
/** A row's bar: from one place to another along the row, rounded at the end its value is at. */
|
|
53
|
+
function rowBar(at) {
|
|
54
|
+
return {
|
|
55
|
+
kind: "rect",
|
|
56
|
+
part: "bar",
|
|
57
|
+
key: at.key,
|
|
58
|
+
x: Math.min(at.from, at.to),
|
|
59
|
+
y: at.mid - at.thick / 2,
|
|
60
|
+
width: Math.max(1, Math.abs(at.to - at.from)),
|
|
61
|
+
height: at.thick,
|
|
62
|
+
fill: at.fill,
|
|
63
|
+
radius: roundedEnd(at.to >= at.from ? "right" : "left"),
|
|
64
|
+
...at.opacity === void 0 ? {} : { opacity: at.opacity }
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The zero line of a row chart: upright, down each run of slots drawn next to each other, so it
|
|
69
|
+
* never crosses the line that counts the rows left out. Every chart of bars from zero has it.
|
|
70
|
+
*/
|
|
71
|
+
function rowsZero(slots, at) {
|
|
72
|
+
const runs = [];
|
|
73
|
+
for (const slot of slots) {
|
|
74
|
+
const last = runs.at(-1);
|
|
75
|
+
if (last?.to === slot) last.to = slot + 1;
|
|
76
|
+
else runs.push({
|
|
77
|
+
from: slot,
|
|
78
|
+
to: slot + 1
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
const y = (slot) => at.frame.top + slot * at.frame.rowHeight;
|
|
82
|
+
return runs.flatMap((run) => zeroLine({
|
|
83
|
+
min: 0,
|
|
84
|
+
max: 0
|
|
85
|
+
}, {
|
|
86
|
+
plot: at.plot,
|
|
87
|
+
place: at.x,
|
|
88
|
+
upright: true,
|
|
89
|
+
span: {
|
|
90
|
+
from: y(run.from),
|
|
91
|
+
to: y(run.to)
|
|
92
|
+
}
|
|
93
|
+
}));
|
|
94
|
+
}
|
|
95
|
+
/** What a pointer finds on a row: the whole row lit, its name, and what the kind says of it. */
|
|
96
|
+
function rowStop(title, at) {
|
|
97
|
+
const { frame } = at;
|
|
98
|
+
return {
|
|
99
|
+
key: at.key,
|
|
100
|
+
x: frame.left,
|
|
101
|
+
y: frame.middle(at.slot),
|
|
102
|
+
title,
|
|
103
|
+
band: frame.band(at.slot),
|
|
104
|
+
end: most(at.nodes.flatMap((node) => rightOf(node, at.env.measure) ?? []), frame.left),
|
|
105
|
+
items: at.items
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* The line that counts the rows left out, in the slot after the last row drawn unless a place
|
|
110
|
+
* is given. Nothing when every row is drawn, or when there is no room for even one.
|
|
111
|
+
*/
|
|
112
|
+
function rowsMore(left, at) {
|
|
113
|
+
if (at.frame.fit < 1) return [];
|
|
114
|
+
const y = at.y ?? at.frame.middle(at.slot ?? at.frame.shown);
|
|
115
|
+
return moreLine(left, {
|
|
116
|
+
y,
|
|
117
|
+
width: at.env.width,
|
|
118
|
+
env: at.env
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
export { rowBar, rowName, rowStop, rowValue, rowsMore, rowsZero };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Named } from "../data/rows.mjs";
|
|
2
|
+
import { Tint, Value } from "../data/types.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* A point to place. The colour it asks for (`Tint`: its own `color`, else its palette `slot`,
|
|
5
|
+
* as a series' and a ranked bar's) is its group's: the dots of a group share one colour, the
|
|
6
|
+
* one asked for by the first of its points that asks. A point in no group wears the chart's
|
|
7
|
+
* own colour, or grey beside groups.
|
|
8
|
+
*/
|
|
9
|
+
export type ScatterPoint = Named & Tint & {
|
|
10
|
+
x: Value;
|
|
11
|
+
y: Value;
|
|
12
|
+
/** The value the dot's area shows, when the chart is given a size measure. */
|
|
13
|
+
size?: Value;
|
|
14
|
+
/** The dots of a group share a colour, and the groups are named in a legend. */
|
|
15
|
+
group?: string;
|
|
16
|
+
};
|
|
17
|
+
/** Which dots are named: the notable ones, all of them, or those listed by key. */
|
|
18
|
+
export type ScatterNamed = "notable" | "all" | readonly string[];
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { drawable } from "../data/clean.mjs";
|
|
2
|
+
import { keyOf } from "../data/rows.mjs";
|
|
3
|
+
import { quantile } from "d3-array";
|
|
4
|
+
import { scaleSqrt } from "d3-scale";
|
|
5
|
+
// src/kinds/scatter-data.ts
|
|
6
|
+
/** The points that can be drawn: a point missing either value, or with one not finite, has no place. */
|
|
7
|
+
function cleaned(points) {
|
|
8
|
+
return points.flatMap(({ x, y, size, ...rest }) => {
|
|
9
|
+
const across = drawable(x);
|
|
10
|
+
const up = drawable(y);
|
|
11
|
+
const sized = drawable(size);
|
|
12
|
+
if (across === null || up === null) return [];
|
|
13
|
+
const key = keyOf(rest);
|
|
14
|
+
return [{
|
|
15
|
+
...rest,
|
|
16
|
+
key,
|
|
17
|
+
x: across,
|
|
18
|
+
y: up,
|
|
19
|
+
...sized === null ? {} : { size: sized }
|
|
20
|
+
}];
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/** The smallest a sized dot is drawn: a size of zero. */
|
|
24
|
+
const SMALLEST = 3;
|
|
25
|
+
/** Sizes above this share of all sizes are drawn as large as the largest allowed. */
|
|
26
|
+
const CAP = .95;
|
|
27
|
+
/** The most dots named when the chart picks which to name. */
|
|
28
|
+
const NOTABLE = 8;
|
|
29
|
+
/** Whether a point asks for a colour: one of its own, or a place in the palette. */
|
|
30
|
+
const tinted = (p) => p.color !== void 0 || p.slot !== void 0;
|
|
31
|
+
/**
|
|
32
|
+
* The groups the points fall into, in the order each is first met, each with the colour its
|
|
33
|
+
* points ask for: the first point of the group that asks for one decides it, its own colour
|
|
34
|
+
* winning over its slot (which still holds its place in the palette, as a series' does); a
|
|
35
|
+
* group none of whose points asks takes the next free place.
|
|
36
|
+
*/
|
|
37
|
+
function groupsOf(points) {
|
|
38
|
+
return [...new Set(points.flatMap((p) => p.group === void 0 ? [] : [p.group]))].map((name) => {
|
|
39
|
+
const asks = points.find((p) => p.group === name && tinted(p));
|
|
40
|
+
return {
|
|
41
|
+
name,
|
|
42
|
+
...asks?.color === void 0 ? {} : { color: asks.color },
|
|
43
|
+
...asks?.slot === void 0 ? {} : { slot: asks.slot }
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/** A size, never below zero; nothing for a point with none. */
|
|
48
|
+
const sizeOf = (point) => point.size === void 0 ? void 0 : Math.max(0, point.size);
|
|
49
|
+
/**
|
|
50
|
+
* Whether the dots can show sizes: some point has one, and the widest a dot may be is far
|
|
51
|
+
* enough past the smallest to be seen.
|
|
52
|
+
*/
|
|
53
|
+
const sizesShow = (points, biggest) => biggest >= 6 && points.some((p) => p.size !== void 0);
|
|
54
|
+
/**
|
|
55
|
+
* Each point's radius. With sizes, a dot's area is its value: the smallest radius at zero up to
|
|
56
|
+
* the widest allowed at the 95th percentile of the sizes, so one huge value does not shrink every
|
|
57
|
+
* other dot; a point with no size is the plain dot, drawn hollow (`sizeless`), never the
|
|
58
|
+
* smallest size. Without sizes, every dot is the plain dot.
|
|
59
|
+
*/
|
|
60
|
+
function radiiOf(points, at) {
|
|
61
|
+
if (!at.sized) return points.map(() => 4);
|
|
62
|
+
const sizes = points.flatMap((p) => sizeOf(p) ?? []).toSorted((a, b) => a - b);
|
|
63
|
+
const cap = quantile(sizes, CAP) ?? 0;
|
|
64
|
+
const scale = scaleSqrt().domain([0, cap > 0 ? cap : 1]).range([SMALLEST, at.biggest]).clamp(true);
|
|
65
|
+
return points.map((p) => {
|
|
66
|
+
const size = sizeOf(p);
|
|
67
|
+
return size === void 0 ? 4 : scale(size);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Whether a point's dot is drawn hollow: in a sized chart, a point with no size. Its place is
|
|
72
|
+
* known and its size is not, so it is drawn as a value that is not whole, as a hollow dot is in
|
|
73
|
+
* every kind (a period still running): a filled dot of any width would read as some size.
|
|
74
|
+
*/
|
|
75
|
+
const sizeless = (point, sized) => sized && point.size === void 0;
|
|
76
|
+
/** Index of the point that is best by a score, the first among equals; nothing for no points. */
|
|
77
|
+
function best(points, score) {
|
|
78
|
+
let found = -1;
|
|
79
|
+
for (const [i, p] of points.entries()) if (found === -1 || score(p) > score(points[found] ?? p)) found = i;
|
|
80
|
+
return found === -1 ? [] : [found];
|
|
81
|
+
}
|
|
82
|
+
/** How far a point stands out: its distance from the middle of all, in each axis's own spread. */
|
|
83
|
+
function standing(points) {
|
|
84
|
+
const xs = points.map((p) => p.x).toSorted((a, b) => a - b);
|
|
85
|
+
const ys = points.map((p) => p.y).toSorted((a, b) => a - b);
|
|
86
|
+
const spread = (sorted) => (sorted.at(-1) ?? 0) - (sorted[0] ?? 0) || 1;
|
|
87
|
+
const mid = {
|
|
88
|
+
x: quantile(xs, .5) ?? 0,
|
|
89
|
+
y: quantile(ys, .5) ?? 0
|
|
90
|
+
};
|
|
91
|
+
return (p) => Math.hypot((p.x - mid.x) / spread(xs), (p.y - mid.y) / spread(ys));
|
|
92
|
+
}
|
|
93
|
+
/** The point that is highest, furthest right, lowest and furthest left, each once. */
|
|
94
|
+
const extremesOf = (points) => [
|
|
95
|
+
...best(points, (p) => p.y),
|
|
96
|
+
...best(points, (p) => p.x),
|
|
97
|
+
...best(points, (p) => -p.y),
|
|
98
|
+
...best(points, (p) => -p.x)
|
|
99
|
+
];
|
|
100
|
+
/** The notable points (the extremes and the four largest), then every other by how far out it is. */
|
|
101
|
+
function byImportance(points) {
|
|
102
|
+
const far = standing(points);
|
|
103
|
+
const scores = points.map((p) => far(p));
|
|
104
|
+
const sizes = points.map((p) => sizeOf(p) ?? -1);
|
|
105
|
+
const places = points.map((_p, i) => i);
|
|
106
|
+
const largest = places.filter((i) => (sizes[i] ?? -1) >= 0).toSorted((a, b) => (sizes[b] ?? 0) - (sizes[a] ?? 0) || a - b).slice(0, 4);
|
|
107
|
+
const rest = places.toSorted((a, b) => (scores[b] ?? 0) - (scores[a] ?? 0) || a - b);
|
|
108
|
+
const notable = [.../* @__PURE__ */ new Set([...extremesOf(points), ...largest])].slice(0, NOTABLE);
|
|
109
|
+
return {
|
|
110
|
+
notable,
|
|
111
|
+
all: [.../* @__PURE__ */ new Set([...notable, ...rest])]
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/** Which points are named, by their place: most important first, or in the order listed. */
|
|
115
|
+
function chosenOf(points, named) {
|
|
116
|
+
if (typeof named !== "string") return [...new Set(named.flatMap((key) => {
|
|
117
|
+
const i = points.findIndex((p) => keyOf(p) === key);
|
|
118
|
+
return i === -1 ? [] : [i];
|
|
119
|
+
}))];
|
|
120
|
+
const order = byImportance(points);
|
|
121
|
+
return named === "all" ? order.all : order.notable;
|
|
122
|
+
}
|
|
123
|
+
export { chosenOf, cleaned, groupsOf, radiiOf, sizeless, sizesShow };
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { plotBox, roomy } from "../layout/frame.mjs";
|
|
2
|
+
import { X_ROOM, gutterOf, tickTexts } from "../marks/axes.mjs";
|
|
3
|
+
import { dotReach, widestReach } from "../marks/dot.mjs";
|
|
4
|
+
import { cappedLegend, firstLegend, underHeader } from "../marks/legend.mjs";
|
|
5
|
+
import { placeOn, tickCount, ticksAcross, valueAxis } from "../scale/value.mjs";
|
|
6
|
+
import { LINE, SPACE, TEXT, paintAt, paintsOf } from "../theme.mjs";
|
|
7
|
+
import { groupsOf, radiiOf, sizeless, sizesShow } from "./scatter-data.mjs";
|
|
8
|
+
// src/kinds/scatter-layout.ts
|
|
9
|
+
/**
|
|
10
|
+
* The y axis's name takes a line over the plot, set off from it as a source line is; the x
|
|
11
|
+
* axis's a line under its numbers.
|
|
12
|
+
*/
|
|
13
|
+
const NAME_ROOM = LINE.tick + SPACE.gutter;
|
|
14
|
+
const BOTTOM = X_ROOM + LINE.tick;
|
|
15
|
+
/** A sized dot's widest, in a plot tall enough for it. */
|
|
16
|
+
const LARGEST = 18;
|
|
17
|
+
/** An axis over some values whose dots sit near zero or far from it. */
|
|
18
|
+
const axisOf = (values, measure, count) => valueAxis(values, {
|
|
19
|
+
zero: "near",
|
|
20
|
+
count,
|
|
21
|
+
reads: measure
|
|
22
|
+
});
|
|
23
|
+
/** Every value an axis must reach: the dots' and the reference lines' that are numbers. */
|
|
24
|
+
const reach = (values, references) => [...values, ...(references ?? []).map((r) => r.value).filter((v) => Number.isFinite(v))];
|
|
25
|
+
/** The circle no name may reach into: the dot and the half of its stroke outside it. */
|
|
26
|
+
const clearOf = (d) => ({
|
|
27
|
+
x: d.x,
|
|
28
|
+
y: d.y,
|
|
29
|
+
r: dotReach(d.r, d.hollow)
|
|
30
|
+
});
|
|
31
|
+
/**
|
|
32
|
+
* Each group with its colour: the one its points ask for (`groupsOf`), else the next free place
|
|
33
|
+
* in the palette, by the palette rule a chart's series take theirs by; and whether any group
|
|
34
|
+
* asks for a colour.
|
|
35
|
+
*/
|
|
36
|
+
function paintedGroups(placed) {
|
|
37
|
+
const tints = groupsOf(placed);
|
|
38
|
+
const paints = paintsOf(tints);
|
|
39
|
+
return {
|
|
40
|
+
groups: tints.map((g, i) => ({
|
|
41
|
+
name: g.name,
|
|
42
|
+
color: paintAt(paints, i)
|
|
43
|
+
})),
|
|
44
|
+
asked: tints.some((g) => g.color !== void 0 || g.slot !== void 0)
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** The legend over the plot in some rows, when the dots fall into more than one group and it fits. */
|
|
48
|
+
function legendOf(groups, at) {
|
|
49
|
+
const { env } = at;
|
|
50
|
+
if (groups.length < 2) return void 0;
|
|
51
|
+
const items = groups.map((g) => ({
|
|
52
|
+
key: g.name,
|
|
53
|
+
label: g.name,
|
|
54
|
+
color: g.color,
|
|
55
|
+
shape: "dot"
|
|
56
|
+
}));
|
|
57
|
+
return cappedLegend(items, {
|
|
58
|
+
...underHeader(at.header, env),
|
|
59
|
+
rows: at.rows,
|
|
60
|
+
env
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The groups told apart and their legend: in the house's rows, else one row, whichever first
|
|
65
|
+
* leaves the plot room. With room for neither, no group is told apart. One group that holds
|
|
66
|
+
* every dot needs no name, and paints them only when it asks for a colour: asking for none, the
|
|
67
|
+
* dots wear the chart's own. One that holds some would part them by a colour nothing names.
|
|
68
|
+
*/
|
|
69
|
+
function namedGroups(placed, rule, env) {
|
|
70
|
+
const { groups, asked } = paintedGroups(placed);
|
|
71
|
+
const whole = placed.every((p) => p.group !== void 0);
|
|
72
|
+
if (groups.length < 2) return {
|
|
73
|
+
groups: whole && asked ? groups : [],
|
|
74
|
+
legend: void 0
|
|
75
|
+
};
|
|
76
|
+
const legend = firstLegend((rows) => legendOf(groups, {
|
|
77
|
+
header: rule,
|
|
78
|
+
rows,
|
|
79
|
+
env
|
|
80
|
+
}), (made) => made !== void 0 && roomy(plotRoom(env, {
|
|
81
|
+
header: rule,
|
|
82
|
+
top: made.height
|
|
83
|
+
})));
|
|
84
|
+
return legend === void 0 ? {
|
|
85
|
+
groups: [],
|
|
86
|
+
legend
|
|
87
|
+
} : {
|
|
88
|
+
groups,
|
|
89
|
+
legend
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The room a value inset leaves on one side of an axis: the widest dot's reach, filled or
|
|
94
|
+
* hollow, so a dot at an end is drawn whole.
|
|
95
|
+
*/
|
|
96
|
+
const insetOf = (biggest) => widestReach(biggest);
|
|
97
|
+
/**
|
|
98
|
+
* The widest a dot may be in a plot: as asked, but never so wide that its inset takes more than
|
|
99
|
+
* a quarter of either side, so at least half of each axis is left to tell values apart. An
|
|
100
|
+
* inset is a radius and the stroke past it (`insetOf(0)`).
|
|
101
|
+
*/
|
|
102
|
+
const widestIn = (plot, biggest) => Math.max(4, Math.min(biggest, Math.min(plot.width, plot.height) / 4 - insetOf(0)));
|
|
103
|
+
/** The least room kept at the plot's right: the picture's edge never touches the last hairline. */
|
|
104
|
+
const EDGE_AIR = 4;
|
|
105
|
+
/** The plot's box with room above it for a legend this tall, before the axes' numbers. */
|
|
106
|
+
const plotRoom = (env, at) => plotBox(env, at.header, {
|
|
107
|
+
top: at.top + NAME_ROOM,
|
|
108
|
+
bottom: BOTTOM,
|
|
109
|
+
left: at.left ?? 0,
|
|
110
|
+
right: at.right ?? 0
|
|
111
|
+
});
|
|
112
|
+
/** Both axes, and the plot between them: the y axis's numbers at the left, the x axis's half-ends inside. */
|
|
113
|
+
function axesOf(placed, rule, at) {
|
|
114
|
+
const { env, top } = at;
|
|
115
|
+
const refs = rule.references ?? {};
|
|
116
|
+
const tall = plotRoom(env, {
|
|
117
|
+
header: rule,
|
|
118
|
+
top
|
|
119
|
+
}).height;
|
|
120
|
+
const yAxis = axisOf(reach(placed.map((p) => p.y), refs.y), rule.y, tickCount(tall));
|
|
121
|
+
const gutter = gutterOf(yAxis, rule.y, env.measure);
|
|
122
|
+
const wide = plotRoom(env, {
|
|
123
|
+
header: rule,
|
|
124
|
+
top,
|
|
125
|
+
left: gutter
|
|
126
|
+
}).width;
|
|
127
|
+
const xAxis = axisOf(reach(placed.map((p) => p.x), refs.x), rule.x, ticksAcross(wide));
|
|
128
|
+
const xText = tickTexts(xAxis, rule.x);
|
|
129
|
+
const half = (value) => value === void 0 ? 0 : env.measure(xText(value), TEXT.tick) / 2;
|
|
130
|
+
const left = Math.max(gutter, half(xAxis.ticks[0]) - at.inset);
|
|
131
|
+
const right = Math.max(EDGE_AIR, half(xAxis.ticks.at(-1)) - at.inset + 1);
|
|
132
|
+
return {
|
|
133
|
+
xAxis,
|
|
134
|
+
yAxis,
|
|
135
|
+
plot: plotRoom(env, {
|
|
136
|
+
header: rule,
|
|
137
|
+
top,
|
|
138
|
+
left: Math.ceil(left),
|
|
139
|
+
right
|
|
140
|
+
})
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Where a scatter's parts go: both axes, the plot, each dot, and the legend when the dots fall
|
|
145
|
+
* into groups. Groups the legend has no room to name are not told apart by colour.
|
|
146
|
+
*/
|
|
147
|
+
function layoutOf(placed, rule, env) {
|
|
148
|
+
const { groups, legend } = namedGroups(placed, rule, env);
|
|
149
|
+
const top = legend?.height ?? 0;
|
|
150
|
+
const tall = plotRoom(env, {
|
|
151
|
+
header: rule,
|
|
152
|
+
top
|
|
153
|
+
}).height;
|
|
154
|
+
const asked = rule.size === void 0 ? 4 : Math.max(4, Math.min(LARGEST, tall / 8));
|
|
155
|
+
const { xAxis, yAxis, plot } = axesOf(placed, rule, {
|
|
156
|
+
env,
|
|
157
|
+
top,
|
|
158
|
+
inset: insetOf(asked)
|
|
159
|
+
});
|
|
160
|
+
const biggest = widestIn(plot, asked);
|
|
161
|
+
const sized = rule.size !== void 0 && sizesShow(placed, biggest);
|
|
162
|
+
const inset = insetOf(biggest);
|
|
163
|
+
const x = placeOn(xAxis, plot.x + inset, plot.x + plot.width - inset);
|
|
164
|
+
const y = placeOn(yAxis, plot.y + plot.height - inset, plot.y + inset);
|
|
165
|
+
const radii = radiiOf(placed, {
|
|
166
|
+
sized,
|
|
167
|
+
biggest
|
|
168
|
+
});
|
|
169
|
+
return {
|
|
170
|
+
plot,
|
|
171
|
+
xAxis,
|
|
172
|
+
yAxis,
|
|
173
|
+
x,
|
|
174
|
+
y,
|
|
175
|
+
sized,
|
|
176
|
+
groups,
|
|
177
|
+
legend,
|
|
178
|
+
dots: placed.map((point, index) => ({
|
|
179
|
+
point,
|
|
180
|
+
key: point.key,
|
|
181
|
+
index,
|
|
182
|
+
x: x(point.x),
|
|
183
|
+
y: y(point.y),
|
|
184
|
+
r: radii[index] ?? 4,
|
|
185
|
+
hollow: sizeless(point, sized)
|
|
186
|
+
}))
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
export { NAME_ROOM, clearOf, layoutOf };
|