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,86 @@
|
|
|
1
|
+
import { said, topAsWritten } from "../data/format.mjs";
|
|
2
|
+
import { keyOf } from "../data/rows.mjs";
|
|
3
|
+
import { counted, noValue, shownOf, summary } from "../describe.mjs";
|
|
4
|
+
import { colorOf } from "./grid-marks.mjs";
|
|
5
|
+
// src/kinds/grid-words.ts
|
|
6
|
+
/** A cell's value as the host writes it, where it gave its own text for it. */
|
|
7
|
+
const writtenAt = (data, at) => said(data.texts?.[at.row]?.[at.column]);
|
|
8
|
+
/**
|
|
9
|
+
* What a pointer finds on a place in the grid: its row and column, as the data names them, and
|
|
10
|
+
* its value; with no value there, its name and nothing else, as a row with no value has a stop
|
|
11
|
+
* in every chart of rows, so a keyboard and a table reach it too.
|
|
12
|
+
*/
|
|
13
|
+
function stopOf(place, data, at) {
|
|
14
|
+
const row = data.rows[place.row]?.label ?? "";
|
|
15
|
+
const column = data.columns[place.column];
|
|
16
|
+
const middle = {
|
|
17
|
+
x: place.box.x + place.box.width / 2,
|
|
18
|
+
y: place.box.y + place.box.height / 2
|
|
19
|
+
};
|
|
20
|
+
const value = "value" in place ? place : void 0;
|
|
21
|
+
return {
|
|
22
|
+
key: place.key,
|
|
23
|
+
cell: [place.row, place.column],
|
|
24
|
+
...middle,
|
|
25
|
+
title: `${row} · ${column?.label ?? ""}`,
|
|
26
|
+
...column?.note === void 0 ? {} : { note: column.note },
|
|
27
|
+
band: place.box,
|
|
28
|
+
items: value === void 0 ? [] : [{
|
|
29
|
+
key: "value",
|
|
30
|
+
label: at.measure.label,
|
|
31
|
+
value: writtenAt(data, place) ?? at.measure.text(value.value),
|
|
32
|
+
color: colorOf(value, at),
|
|
33
|
+
...middle
|
|
34
|
+
}]
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/** Every place of the rows drawn, a cell or an empty one, in the grid's order: row by row. */
|
|
38
|
+
const stopsOf = (layout, data, at) => [...layout.filled, ...layout.empty].toSorted((a, b) => a.row - b.row || a.column - b.column).map((place) => stopOf(place, data, at));
|
|
39
|
+
/** The cell with the highest value, named as a pointer finds it; nothing when the top two read alike. */
|
|
40
|
+
function highestOf(data, measure) {
|
|
41
|
+
const cells = data.rows.flatMap((row, r) => data.columns.flatMap((column, c) => {
|
|
42
|
+
const value = data.values[r]?.[c] ?? null;
|
|
43
|
+
return value === null ? [] : [{
|
|
44
|
+
row,
|
|
45
|
+
column,
|
|
46
|
+
value,
|
|
47
|
+
at: {
|
|
48
|
+
row: r,
|
|
49
|
+
column: c
|
|
50
|
+
}
|
|
51
|
+
}];
|
|
52
|
+
}));
|
|
53
|
+
const top = topAsWritten(cells, (cell) => cell.value, measure.text);
|
|
54
|
+
if (top === void 0) return void 0;
|
|
55
|
+
const written = writtenAt(data, top.at) ?? measure.text(top.value);
|
|
56
|
+
return `Highest: ${top.row.label} · ${top.column.label}, ${written}`;
|
|
57
|
+
}
|
|
58
|
+
/** The grid in words: what it is of, its rows and how many have room, its columns, its highest cell, and how many cells are empty. */
|
|
59
|
+
function describeGrid(data, at) {
|
|
60
|
+
const cells = data.rows.flatMap((_row, r) => data.columns.map((_column, c) => data.values[r]?.[c] ?? null));
|
|
61
|
+
return summary([
|
|
62
|
+
`Grid of ${at.measure.label}`,
|
|
63
|
+
shownOf(data.rows.length, at.shown, "row"),
|
|
64
|
+
counted(data.columns.length, "column")
|
|
65
|
+
], [
|
|
66
|
+
data.mergeNote,
|
|
67
|
+
highestOf(data, at.measure),
|
|
68
|
+
noValue(cells.filter((v) => v === null).length, "cell")
|
|
69
|
+
]);
|
|
70
|
+
}
|
|
71
|
+
/** How the grid's one-cell stops are read as a table and traversed by keyboard. */
|
|
72
|
+
function matrixOf(data, rows, heading) {
|
|
73
|
+
return {
|
|
74
|
+
heading,
|
|
75
|
+
rowMajor: true,
|
|
76
|
+
rows: rows.map((row) => ({
|
|
77
|
+
key: keyOf(row),
|
|
78
|
+
label: row.label
|
|
79
|
+
})),
|
|
80
|
+
columns: data.columns.map((column) => ({
|
|
81
|
+
key: keyOf(column),
|
|
82
|
+
label: [column.label, column.note].filter(Boolean).join(" · ")
|
|
83
|
+
}))
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
export { describeGrid, matrixOf, stopsOf };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Env, Header, Measure, TimeX } from "../data/types.mjs";
|
|
2
|
+
import { Scene } from "../scene/types.mjs";
|
|
3
|
+
import { GridData } from "./grid-data.mjs";
|
|
4
|
+
export type GridOptions = Header & {
|
|
5
|
+
measure: Measure;
|
|
6
|
+
/** When each column is a time bucket, adjacent periods share cells at narrow widths. */
|
|
7
|
+
time?: TimeX;
|
|
8
|
+
/** Use two hues around zero when the range contains gains and losses. */
|
|
9
|
+
diverging?: boolean;
|
|
10
|
+
/** A second column beside the row names (a cohort's size), and the word over it. */
|
|
11
|
+
sizes?: readonly string[];
|
|
12
|
+
sizeTitle?: string;
|
|
13
|
+
/** The word over the row names. */
|
|
14
|
+
rowTitle?: string;
|
|
15
|
+
/** The values the shade runs between; the data's own lowest and highest when left out. */
|
|
16
|
+
range?: readonly [number, number];
|
|
17
|
+
/** The palette place of the grid's one colour. */
|
|
18
|
+
slot?: number;
|
|
19
|
+
};
|
|
20
|
+
/** A heat grid of one measure by two fields. */
|
|
21
|
+
export declare function gridChart(given: GridData, options: GridOptions, env: Env): Scene;
|
|
22
|
+
export type { GridData };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { bare, headerNodes } from "../layout/frame.mjs";
|
|
2
|
+
import { seriesPaint } from "../theme.mjs";
|
|
3
|
+
import { rowsMore } from "./row-marks.mjs";
|
|
4
|
+
import { cleaned, colorsOf } from "./grid-data.mjs";
|
|
5
|
+
import { layoutOf } from "./grid-layout.mjs";
|
|
6
|
+
import { cellNodes, emptyNodes, heads, sides } from "./grid-marks.mjs";
|
|
7
|
+
import { fitTimeGrid } from "./grid-time.mjs";
|
|
8
|
+
import { describeGrid, matrixOf, stopsOf } from "./grid-words.mjs";
|
|
9
|
+
import { hasRoom } from "./row-chart.mjs";
|
|
10
|
+
// src/kinds/grid.ts
|
|
11
|
+
/** A heat grid of one measure by two fields. */
|
|
12
|
+
function gridChart(given, options, env) {
|
|
13
|
+
const clean = cleaned(given);
|
|
14
|
+
const data = options.time === void 0 ? clean : fitTimeGrid(clean, options.time, env);
|
|
15
|
+
const layout = layoutOf(data, options, env);
|
|
16
|
+
const { measure } = options;
|
|
17
|
+
const { frame, plot } = layout;
|
|
18
|
+
const roomy = hasRoom(frame, layout.across, layout.drawn);
|
|
19
|
+
const summary = describeGrid(data, {
|
|
20
|
+
measure,
|
|
21
|
+
shown: roomy ? layout.kept.length : 0
|
|
22
|
+
});
|
|
23
|
+
if (!roomy) return bare(env, options, summary);
|
|
24
|
+
const at = {
|
|
25
|
+
layout,
|
|
26
|
+
measure,
|
|
27
|
+
...colorsOf(data, options.range, options.diverging),
|
|
28
|
+
negative: seriesPaint((options.slot ?? 0) + 1),
|
|
29
|
+
own: seriesPaint(options.slot ?? 0),
|
|
30
|
+
env
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
width: env.width,
|
|
34
|
+
height: env.height,
|
|
35
|
+
plot,
|
|
36
|
+
summary,
|
|
37
|
+
nodes: [
|
|
38
|
+
...headerNodes(env, options),
|
|
39
|
+
...heads(data, options, at),
|
|
40
|
+
...sides(options, at),
|
|
41
|
+
...emptyNodes(layout),
|
|
42
|
+
...layout.filled.flatMap((cell) => cellNodes(cell, at)),
|
|
43
|
+
...rowsMore(data.rows.length - layout.kept.length, {
|
|
44
|
+
frame,
|
|
45
|
+
env
|
|
46
|
+
})
|
|
47
|
+
],
|
|
48
|
+
hover: {
|
|
49
|
+
snap: "nearest",
|
|
50
|
+
stops: stopsOf(layout, data, at),
|
|
51
|
+
matrix: matrixOf(data, layout.kept, measure.label)
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
export { gridChart };
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { drawable } from "../data/clean.mjs";
|
|
2
|
+
import { measure } from "../data/format.mjs";
|
|
3
|
+
import { least, most } from "../data/range.mjs";
|
|
4
|
+
import { bin, thresholdFreedmanDiaconis, thresholdSturges } from "d3-array";
|
|
5
|
+
import { scaleLinear } from "d3-scale";
|
|
6
|
+
// src/kinds/histogram-data.ts
|
|
7
|
+
/** A bar is at least this wide: the histogram's own floor, so a bin is never a sliver. */
|
|
8
|
+
const THINNEST = 6;
|
|
9
|
+
/** The values that can be drawn: missing and non-finite values are left out. */
|
|
10
|
+
const finiteOf = (values) => values.flatMap((v) => {
|
|
11
|
+
const value = drawable(v);
|
|
12
|
+
return value === null ? [] : [value];
|
|
13
|
+
});
|
|
14
|
+
/** About how many bins the data asks for: Freedman–Diaconis, or Sturges when the middle half is flat. */
|
|
15
|
+
function wanted(values, low, high) {
|
|
16
|
+
const spread = thresholdFreedmanDiaconis(values, low, high);
|
|
17
|
+
return Number.isFinite(spread) && spread > 1 ? spread : thresholdSturges(values);
|
|
18
|
+
}
|
|
19
|
+
/** A spread too fine for D3 to split reliably into round bins can still have distinct labels. */
|
|
20
|
+
function tooCloseToSplit(low, high) {
|
|
21
|
+
const finest = Math.max(1e-300, Number.EPSILON * 1e3 * Math.max(Math.abs(low), Math.abs(high)));
|
|
22
|
+
return high - low <= finest;
|
|
23
|
+
}
|
|
24
|
+
/** Whether both ends represent the same reading in the measure's own words. */
|
|
25
|
+
const readAlike = (low, high, text) => low === high || text(low) === text(high);
|
|
26
|
+
/** Whether the bins are one bin of one value: what a histogram of values all read alike is. */
|
|
27
|
+
const onePoint = (bins, text) => {
|
|
28
|
+
const [only] = bins;
|
|
29
|
+
return bins.length === 1 && only !== void 0 && readAlike(only.from, only.to, text);
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Whether every bin is at least a bar's least width across a width: a round step whose ends
|
|
33
|
+
* cannot be rounded (a scale gives up rounding some spreads) leaves a sliver at an end. The
|
|
34
|
+
* width is shared with every value the run across reaches (`reach`: a level marked past the
|
|
35
|
+
* bins), so bins beside a far level are held to the part of the width they are left.
|
|
36
|
+
*/
|
|
37
|
+
const wide = (edges, at) => {
|
|
38
|
+
const ends = [
|
|
39
|
+
edges[0] ?? 0,
|
|
40
|
+
edges.at(-1) ?? 0,
|
|
41
|
+
...at.reach ?? []
|
|
42
|
+
];
|
|
43
|
+
const span = most(ends) - least(ends);
|
|
44
|
+
return edges.slice(1).every((edge, i) => (edge - (edges[i] ?? edge)) / span * at.width >= THINNEST);
|
|
45
|
+
};
|
|
46
|
+
/** Whether no two edges read alike: each bin's edges can be told apart in words. */
|
|
47
|
+
const apart = (edges, text) => new Set(edges.map((e) => text(e))).size === edges.length;
|
|
48
|
+
/**
|
|
49
|
+
* Whether some edges make bins the width has room for, none a sliver, each read apart; and, for
|
|
50
|
+
* a measure of whole things, every edge a whole number: no bin of seats runs from 1 to 1.2.
|
|
51
|
+
*/
|
|
52
|
+
const fitting = (edges, at) => edges.length - 1 <= at.room && wide(edges, at) && apart(edges, at.text) && (at.whole !== true || edges.every((edge) => Number.isInteger(edge)));
|
|
53
|
+
/**
|
|
54
|
+
* The values in bins on round edges: as many as asked for (or the data asks for), no more than
|
|
55
|
+
* the width has room for, none finer than the measure writes, and none on a fraction of a whole
|
|
56
|
+
* thing. A round step can give nearly
|
|
57
|
+
* twice the bins asked for, or edges that read alike, so fewer are asked for until the edges
|
|
58
|
+
* fit and read apart. Values that read alike are one bin, from the least to the greatest.
|
|
59
|
+
*/
|
|
60
|
+
function binsOf(values, at) {
|
|
61
|
+
if (values.length === 0) return [];
|
|
62
|
+
const low = least(values);
|
|
63
|
+
const high = most(values);
|
|
64
|
+
const whole = [{
|
|
65
|
+
from: low,
|
|
66
|
+
to: high,
|
|
67
|
+
count: values.length
|
|
68
|
+
}];
|
|
69
|
+
if (tooCloseToSplit(low, high) || readAlike(low, high, at.text)) return whole;
|
|
70
|
+
const room = Math.max(1, Math.floor(at.width / THINNEST));
|
|
71
|
+
const asked = at.bins !== void 0 && Number.isFinite(at.bins) ? at.bins : wanted(values, low, high);
|
|
72
|
+
for (let ask = Math.max(1, Math.min(room, Math.round(asked))); ask >= 1; ask--) {
|
|
73
|
+
const scale = scaleLinear().domain([low, high]).nice(ask);
|
|
74
|
+
const [from = low, to = high] = scale.domain();
|
|
75
|
+
const thresholds = scale.ticks(ask).filter((t) => t > from && t < to);
|
|
76
|
+
const edges = [
|
|
77
|
+
Math.min(from, low),
|
|
78
|
+
...thresholds,
|
|
79
|
+
Math.max(to, high)
|
|
80
|
+
];
|
|
81
|
+
if (!fitting(edges, {
|
|
82
|
+
room,
|
|
83
|
+
...at
|
|
84
|
+
})) continue;
|
|
85
|
+
return bin().domain([Math.min(from, low), Math.max(to, high)]).thresholds(thresholds)([...values]).map((b) => ({
|
|
86
|
+
from: b.x0 ?? from,
|
|
87
|
+
to: b.x1 ?? to,
|
|
88
|
+
count: b.length
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
return whole;
|
|
92
|
+
}
|
|
93
|
+
/** The bins' edges, each once, in order: one value's one bin (`point`) has one. */
|
|
94
|
+
const edgesOf = (bins, point) => {
|
|
95
|
+
const [head] = bins;
|
|
96
|
+
if (head === void 0) return [];
|
|
97
|
+
return point ? [head.from] : [head.from, ...bins.map((b) => b.to)];
|
|
98
|
+
};
|
|
99
|
+
/** The levels a host marks that are numbers: the run across reaches each, as any axis reaches its levels. */
|
|
100
|
+
const levelsOf = (references) => (references ?? []).map((r) => r.value).filter((v) => Number.isFinite(v));
|
|
101
|
+
/**
|
|
102
|
+
* The run of values across the plot: from the first edge to the last, and out to any level
|
|
103
|
+
* marked past them, so a goal the values fall short of is drawn where it stands. One value's
|
|
104
|
+
* bin is set in the middle, the run a step either side of it: a step that reaches the furthest
|
|
105
|
+
* level.
|
|
106
|
+
*/
|
|
107
|
+
function spanAcross(bins, at) {
|
|
108
|
+
const edges = edgesOf(bins, at.point);
|
|
109
|
+
const min = edges[0] ?? 0;
|
|
110
|
+
const max = edges.at(-1) ?? 1;
|
|
111
|
+
if (!at.point) return {
|
|
112
|
+
min: least([min, ...at.levels]),
|
|
113
|
+
max: most([max, ...at.levels])
|
|
114
|
+
};
|
|
115
|
+
const step = most([
|
|
116
|
+
1,
|
|
117
|
+
Math.abs(min),
|
|
118
|
+
...at.levels.map((level) => Math.abs(level - min))
|
|
119
|
+
]);
|
|
120
|
+
return {
|
|
121
|
+
min: least([min - step, ...at.levels]),
|
|
122
|
+
max: most([max + step, ...at.levels])
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Whether a bin overlaps a range, its lower end first (as `finiteRange` reads one): they share
|
|
127
|
+
* more than an edge, or one is a point in the other.
|
|
128
|
+
*/
|
|
129
|
+
function overlaps(one, range, last) {
|
|
130
|
+
const [low, high] = range;
|
|
131
|
+
if (low === high) return one.from <= low && (low < one.to || last && low === one.to);
|
|
132
|
+
if (one.from === one.to) return low <= one.from && one.from <= high;
|
|
133
|
+
return one.from < high && one.to > low;
|
|
134
|
+
}
|
|
135
|
+
/** The bins a range takes; every bin when there is no range. */
|
|
136
|
+
const taken = (bins, range) => new Set(bins.filter((b, i) => range === void 0 || overlaps(b, range, i === bins.length - 1)));
|
|
137
|
+
/** How a count reads, on the axis and in a tooltip: whole numbers, in the reader's locale. */
|
|
138
|
+
const countReads = (env) => measure("", {
|
|
139
|
+
whole: true,
|
|
140
|
+
...env.locale === void 0 ? {} : { locale: env.locale }
|
|
141
|
+
});
|
|
142
|
+
export { binsOf, countReads, edgesOf, finiteOf, levelsOf, onePoint, spanAcross, taken };
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { X_ROOM, scaled, tickTexts } from "../marks/axes.mjs";
|
|
2
|
+
import { NOTE_LINE } from "../marks/notes.mjs";
|
|
3
|
+
import { placeOn } from "../scale/value.mjs";
|
|
4
|
+
import { spaced } from "../text/measure.mjs";
|
|
5
|
+
import { SPACE, TEXT } from "../theme.mjs";
|
|
6
|
+
import { binsOf, countReads, edgesOf, levelsOf, onePoint, spanAcross } from "./histogram-data.mjs";
|
|
7
|
+
import { valuePlot } from "./value-plot.mjs";
|
|
8
|
+
// src/kinds/histogram-layout.ts
|
|
9
|
+
/** Room at the right, so the last edge's name, centred on the last edge, stays near it. */
|
|
10
|
+
const RIGHT = 8;
|
|
11
|
+
/**
|
|
12
|
+
* How a bin's edges read under the bars: the measure's short form, or its full form when the short form would
|
|
13
|
+
* name two edges alike (1K, 1K for 1,000 and 1,005), by the same rule as an axis's ticks.
|
|
14
|
+
*/
|
|
15
|
+
const edgeText = (edges, measure) => tickTexts({
|
|
16
|
+
min: edges[0] ?? 0,
|
|
17
|
+
max: edges.at(-1) ?? 1,
|
|
18
|
+
ticks: edges
|
|
19
|
+
}, measure);
|
|
20
|
+
/** The plot and its count axis for some bins. */
|
|
21
|
+
function plotFor(bins, options, env) {
|
|
22
|
+
const lane = (options.references?.length ?? 0) > 0 ? NOTE_LINE : 0;
|
|
23
|
+
return {
|
|
24
|
+
...valuePlot(env, {
|
|
25
|
+
header: options,
|
|
26
|
+
values: bins.map((b) => b.count),
|
|
27
|
+
zero: "always",
|
|
28
|
+
reads: countReads(env),
|
|
29
|
+
margins: {
|
|
30
|
+
top: lane,
|
|
31
|
+
right: RIGHT,
|
|
32
|
+
bottom: X_ROOM
|
|
33
|
+
}
|
|
34
|
+
}),
|
|
35
|
+
lane
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The bins of the drawable values for the width the count axis leaves, the plot, its two
|
|
40
|
+
* scales, and the count axis's grid.
|
|
41
|
+
*/
|
|
42
|
+
function layoutOf(values, options, env) {
|
|
43
|
+
const levels = levelsOf(options.references);
|
|
44
|
+
const asked = {
|
|
45
|
+
text: options.measure.text,
|
|
46
|
+
whole: options.measure.whole,
|
|
47
|
+
reach: levels,
|
|
48
|
+
...options.bins === void 0 ? {} : { bins: options.bins }
|
|
49
|
+
};
|
|
50
|
+
const first = plotFor(binsOf(values, {
|
|
51
|
+
width: env.width,
|
|
52
|
+
...asked
|
|
53
|
+
}), options, env);
|
|
54
|
+
const bins = binsOf(values, {
|
|
55
|
+
width: first.plot.width,
|
|
56
|
+
...asked
|
|
57
|
+
});
|
|
58
|
+
const point = onePoint(bins, options.measure.text);
|
|
59
|
+
const { plot, axis, place, grid, lane } = plotFor(bins, options, env);
|
|
60
|
+
const span = spanAcross(bins, {
|
|
61
|
+
point,
|
|
62
|
+
levels
|
|
63
|
+
});
|
|
64
|
+
return {
|
|
65
|
+
values,
|
|
66
|
+
bins,
|
|
67
|
+
point,
|
|
68
|
+
plot,
|
|
69
|
+
axis,
|
|
70
|
+
span,
|
|
71
|
+
x: placeOn({
|
|
72
|
+
...span,
|
|
73
|
+
ticks: []
|
|
74
|
+
}, plot.x, plot.x + plot.width),
|
|
75
|
+
y: place,
|
|
76
|
+
grid,
|
|
77
|
+
lane,
|
|
78
|
+
edges: edgesOf(bins, point)
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/** Where a bin's bar runs across, left to right: one value's bin takes the plot. */
|
|
82
|
+
function runOf(bin, layout) {
|
|
83
|
+
const { plot } = layout;
|
|
84
|
+
if (layout.point) return {
|
|
85
|
+
left: plot.x,
|
|
86
|
+
right: plot.x + plot.width
|
|
87
|
+
};
|
|
88
|
+
return {
|
|
89
|
+
left: layout.x(bin.from),
|
|
90
|
+
right: layout.x(bin.to)
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/** The bins' edge names to try: every one, else every 2nd, 5th, 10th... */
|
|
94
|
+
const STEPS = [
|
|
95
|
+
1,
|
|
96
|
+
2,
|
|
97
|
+
5,
|
|
98
|
+
10,
|
|
99
|
+
20,
|
|
100
|
+
50,
|
|
101
|
+
100,
|
|
102
|
+
200,
|
|
103
|
+
500
|
|
104
|
+
];
|
|
105
|
+
/**
|
|
106
|
+
* Every `step`th edge's name from the first, and the last edge's, its middle moved so the whole
|
|
107
|
+
* name stays inside the picture; the `step`th name before the last is left out when it would
|
|
108
|
+
* crowd the last. The edges read apart in the words they are named in (`binsOf` keeps only
|
|
109
|
+
* edges that read apart in full, and `tickTexts` writes them short only when the short form does
|
|
110
|
+
* too), so no name repeats the one before it.
|
|
111
|
+
*/
|
|
112
|
+
function named(layout, step, at) {
|
|
113
|
+
const { env } = at;
|
|
114
|
+
const last = layout.edges.length - 1;
|
|
115
|
+
const names = layout.edges.flatMap((edge, i) => {
|
|
116
|
+
if (i % step !== 0 && i !== last) return [];
|
|
117
|
+
const text = at.text(edge);
|
|
118
|
+
const width = env.measure(text, TEXT.tick);
|
|
119
|
+
return [{
|
|
120
|
+
x: Math.min(Math.max(layout.x(edge), width / 2), env.width - width / 2),
|
|
121
|
+
text,
|
|
122
|
+
width
|
|
123
|
+
}];
|
|
124
|
+
});
|
|
125
|
+
const end = names.slice(-2);
|
|
126
|
+
return names.length > 2 && !spaced(end, SPACE.ticks) ? [...names.slice(0, -2), ...end.slice(1)] : names;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The edges named under the bars: every one, else every 2nd, 5th, 10th..., the first set whose
|
|
130
|
+
* names stand apart, the last edge always among them. A name wider than the picture is left
|
|
131
|
+
* out. Bins of a run of values are named as an axis is, by two numbers or more, or by none: one
|
|
132
|
+
* number alone under several bars would not say which edge it stands on, nor how far a bin
|
|
133
|
+
* runs. One value's one bin is named by its value alone.
|
|
134
|
+
*/
|
|
135
|
+
function edgeNames(layout, at) {
|
|
136
|
+
const text = edgeText(layout.edges, at.measure);
|
|
137
|
+
const { env } = at;
|
|
138
|
+
const kept = (STEPS.filter((s) => s < Math.max(2, layout.edges.length)).map((s) => named(layout, s, {
|
|
139
|
+
text,
|
|
140
|
+
env
|
|
141
|
+
})).find((list) => spaced(list, SPACE.ticks)) ?? []).filter((n) => n.width <= env.width);
|
|
142
|
+
return layout.point ? kept : scaled(kept);
|
|
143
|
+
}
|
|
144
|
+
export { edgeNames, layoutOf, runOf };
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { pairKey } from "../data/rows.mjs";
|
|
2
|
+
import { OTHER } from "../scene/paint.mjs";
|
|
3
|
+
import { tickUnder, zeroLine } from "../marks/axes.mjs";
|
|
4
|
+
import { marked, uprightNotes } from "../marks/notes.mjs";
|
|
5
|
+
import { MARK, roundedEnd } from "../theme.mjs";
|
|
6
|
+
import { edgeNames, runOf } from "./histogram-layout.mjs";
|
|
7
|
+
// src/kinds/histogram-marks.ts
|
|
8
|
+
/** A bin's colour: the chart's own when the range keeps it, else grey context. */
|
|
9
|
+
const colorOf = (bin, at) => at.lit.has(bin) ? at.own : OTHER;
|
|
10
|
+
/** A bin's identity, on its bar and its stop: its two edges, which no two bins share. */
|
|
11
|
+
const binKey = (bin) => pairKey(String(bin.from), String(bin.to));
|
|
12
|
+
/**
|
|
13
|
+
* A bar for each bin with values: touching its neighbours, parted by a hair, rounded on top. A
|
|
14
|
+
* bin with values is never drawn as nothing: its bar is at least the house's least across and
|
|
15
|
+
* up, however narrow its bin or small its count beside the tallest.
|
|
16
|
+
*/
|
|
17
|
+
function barNodes(at) {
|
|
18
|
+
const { layout } = at;
|
|
19
|
+
const bottom = layout.plot.y + layout.plot.height;
|
|
20
|
+
return layout.bins.flatMap((b) => {
|
|
21
|
+
if (b.count === 0) return [];
|
|
22
|
+
const { left, right } = runOf(b, layout);
|
|
23
|
+
const width = Math.max(MARK.least, right - left - MARK.hair);
|
|
24
|
+
const height = Math.max(MARK.least, bottom - layout.y(b.count));
|
|
25
|
+
return [{
|
|
26
|
+
kind: "rect",
|
|
27
|
+
part: "bar",
|
|
28
|
+
key: binKey(b),
|
|
29
|
+
x: (left + right - width) / 2,
|
|
30
|
+
y: bottom - height,
|
|
31
|
+
width,
|
|
32
|
+
height,
|
|
33
|
+
fill: colorOf(b, at),
|
|
34
|
+
radius: roundedEnd("up")
|
|
35
|
+
}];
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
/** The box of a text set from its right end at its middle: an axis's number. */
|
|
39
|
+
function numberBox(node, env) {
|
|
40
|
+
if (node.kind !== "text") return [];
|
|
41
|
+
const width = env.measure(node.text, { size: node.size });
|
|
42
|
+
return [{
|
|
43
|
+
x: node.x - width,
|
|
44
|
+
y: node.y - node.size / 2,
|
|
45
|
+
width,
|
|
46
|
+
height: node.size
|
|
47
|
+
}];
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The references: an upright dashed line at each value (the run across reaches them all), named at its top in the
|
|
51
|
+
* lane over the plot. A name may run past the plot over the axis's numbers' side, never past the
|
|
52
|
+
* picture and never over a number; one with no room is left out and its line stays.
|
|
53
|
+
*/
|
|
54
|
+
function noteMarks(references, at) {
|
|
55
|
+
const { env, layout } = at;
|
|
56
|
+
const room = {
|
|
57
|
+
plot: layout.plot,
|
|
58
|
+
measure: env.measure,
|
|
59
|
+
lane: layout.lane,
|
|
60
|
+
sides: {
|
|
61
|
+
min: 0,
|
|
62
|
+
max: env.width
|
|
63
|
+
},
|
|
64
|
+
taken: layout.grid.flatMap((node) => numberBox(node, env))
|
|
65
|
+
};
|
|
66
|
+
const place = {
|
|
67
|
+
place: layout.x,
|
|
68
|
+
text: at.measure.text
|
|
69
|
+
};
|
|
70
|
+
return marked(uprightNotes(references ?? [], place, room), room);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Zero across the run of values, standing upright under the bars, when it lies inside the run:
|
|
74
|
+
* as in any chart whose values run along the bottom. At an end of the run, the first or last
|
|
75
|
+
* bar's own edge stands there.
|
|
76
|
+
*/
|
|
77
|
+
function zeroAcross(layout) {
|
|
78
|
+
const { min, max } = layout.span;
|
|
79
|
+
if (layout.point || !(min < 0 && max > 0)) return [];
|
|
80
|
+
return zeroLine({
|
|
81
|
+
min,
|
|
82
|
+
max
|
|
83
|
+
}, {
|
|
84
|
+
plot: layout.plot,
|
|
85
|
+
place: layout.x,
|
|
86
|
+
upright: true
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Every mark of a histogram, in drawing order: the grid, edge names, zero across the bins,
|
|
91
|
+
* bars, the count's zero, references.
|
|
92
|
+
*/
|
|
93
|
+
function histogramNodes(at, options) {
|
|
94
|
+
const { layout } = at;
|
|
95
|
+
const { plot, axis, y } = layout;
|
|
96
|
+
const notes = noteMarks(options.references, at);
|
|
97
|
+
return [
|
|
98
|
+
...layout.grid,
|
|
99
|
+
...edgeNames(layout, at).map((name) => tickUnder(name, plot)),
|
|
100
|
+
...zeroAcross(layout),
|
|
101
|
+
...barNodes(at),
|
|
102
|
+
...zeroLine(axis, {
|
|
103
|
+
plot,
|
|
104
|
+
place: y
|
|
105
|
+
}),
|
|
106
|
+
...notes.lines,
|
|
107
|
+
...notes.names
|
|
108
|
+
];
|
|
109
|
+
}
|
|
110
|
+
export { binKey, colorOf, histogramNodes };
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { least, most } from "../data/range.mjs";
|
|
2
|
+
import { wordsOf } from "../data/words.mjs";
|
|
3
|
+
import { counted, noValue, summary } from "../describe.mjs";
|
|
4
|
+
import { countReads } from "./histogram-data.mjs";
|
|
5
|
+
import { runOf } from "./histogram-layout.mjs";
|
|
6
|
+
import { binKey, colorOf } from "./histogram-marks.mjs";
|
|
7
|
+
import { median } from "d3-array";
|
|
8
|
+
// src/kinds/histogram-words.ts
|
|
9
|
+
/** A stop for each bin: its range, its count, and its share of all values, keyed as its bar. */
|
|
10
|
+
function stopsOf(at) {
|
|
11
|
+
const { layout, env } = at;
|
|
12
|
+
const { plot, bins } = layout;
|
|
13
|
+
const count = countReads(env);
|
|
14
|
+
const total = bins.reduce((sum, b) => sum + b.count, 0);
|
|
15
|
+
const text = at.measure.text;
|
|
16
|
+
const words = wordsOf(env);
|
|
17
|
+
const label = words.histogram.count;
|
|
18
|
+
return bins.map((b) => {
|
|
19
|
+
const { left, right } = runOf(b, layout);
|
|
20
|
+
const x = (left + right) / 2;
|
|
21
|
+
const y = layout.y(b.count);
|
|
22
|
+
const item = {
|
|
23
|
+
key: "count",
|
|
24
|
+
label,
|
|
25
|
+
value: count.text(b.count),
|
|
26
|
+
color: colorOf(b, at),
|
|
27
|
+
x,
|
|
28
|
+
y,
|
|
29
|
+
aside: at.percent(b.count / Math.max(1, total))
|
|
30
|
+
};
|
|
31
|
+
const band = {
|
|
32
|
+
x: left,
|
|
33
|
+
y: plot.y,
|
|
34
|
+
width: right - left,
|
|
35
|
+
height: plot.height
|
|
36
|
+
};
|
|
37
|
+
const from = text(b.from);
|
|
38
|
+
const title = layout.point ? from : words.histogram.range(from, text(b.to));
|
|
39
|
+
return {
|
|
40
|
+
key: binKey(b),
|
|
41
|
+
x,
|
|
42
|
+
y,
|
|
43
|
+
title,
|
|
44
|
+
band,
|
|
45
|
+
items: [item]
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/** "Highlighted: 400 ms to 600 ms, 12% of values", over the bins the range colours. */
|
|
50
|
+
function rangeSaid(bins, at) {
|
|
51
|
+
const { range, text } = at;
|
|
52
|
+
const chosen = bins.filter((b) => at.lit.has(b));
|
|
53
|
+
const first = chosen[0];
|
|
54
|
+
const last = chosen.at(-1);
|
|
55
|
+
if (first === void 0 || last === void 0) {
|
|
56
|
+
const [low, high] = range;
|
|
57
|
+
return `Highlighted: ${text(low)} to ${text(high)}, no values`;
|
|
58
|
+
}
|
|
59
|
+
const total = bins.reduce((sum, b) => sum + b.count, 0);
|
|
60
|
+
const share = chosen.reduce((sum, b) => sum + b.count, 0) / Math.max(1, total);
|
|
61
|
+
return `Highlighted: ${text(first.from)} to ${text(last.to)}, ${at.percent(share)} of values`;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Where the values lie: their median and their range; or, when they all read as one value (one
|
|
65
|
+
* value, or several the measure writes alike), that value alone, as a chart over time says a
|
|
66
|
+
* series of one value was that value, with no range from it to itself.
|
|
67
|
+
*/
|
|
68
|
+
function spread(values, at) {
|
|
69
|
+
const low = least(values);
|
|
70
|
+
if (at.point) return [`${values.length === 1 ? "Value" : "All values"}: ${at.text(low)}`];
|
|
71
|
+
return [`Median: ${at.text(median(values) ?? low)}`, `Range: ${at.text(low)} to ${at.text(most(values))}`];
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* What the chart shows, in words: how many values it was given and in how many bins it drew
|
|
75
|
+
* them (`shown`, none when the chart is its header alone), where they lie, the highlighted
|
|
76
|
+
* share, and how many had no value to draw.
|
|
77
|
+
*/
|
|
78
|
+
function describeHistogram(data, at) {
|
|
79
|
+
const { measure, percent } = at;
|
|
80
|
+
const { given, lit } = data;
|
|
81
|
+
const { values, bins } = data.layout;
|
|
82
|
+
const text = measure.text;
|
|
83
|
+
const opening = [
|
|
84
|
+
`Histogram of ${measure.label}`,
|
|
85
|
+
counted(given.length, "value"),
|
|
86
|
+
at.shown > 0 ? counted(at.shown, "bin") : void 0
|
|
87
|
+
];
|
|
88
|
+
const missing = noValue(given.length - values.length, "value");
|
|
89
|
+
if (values.length === 0) return summary(opening, [missing]);
|
|
90
|
+
return summary(opening, [
|
|
91
|
+
...spread(values, {
|
|
92
|
+
point: data.layout.point,
|
|
93
|
+
text
|
|
94
|
+
}),
|
|
95
|
+
at.highlight === void 0 ? void 0 : rangeSaid(bins, {
|
|
96
|
+
range: at.highlight,
|
|
97
|
+
lit,
|
|
98
|
+
text,
|
|
99
|
+
percent
|
|
100
|
+
}),
|
|
101
|
+
missing
|
|
102
|
+
]);
|
|
103
|
+
}
|
|
104
|
+
export { describeHistogram, stopsOf };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Env, Header, Measure, Reference, Value } from "../data/types.mjs";
|
|
2
|
+
import { Scene } from "../scene/types.mjs";
|
|
3
|
+
export type HistogramOptions = Header & {
|
|
4
|
+
measure: Measure;
|
|
5
|
+
/** About how many bins; from the data's spread when left out. */
|
|
6
|
+
bins?: number;
|
|
7
|
+
/** Values to mark: a median, a mean, a goal. */
|
|
8
|
+
references?: readonly Reference[];
|
|
9
|
+
/** The range to keep in colour, from and to; a range with an end that is not a number is none. */
|
|
10
|
+
highlight?: readonly [number, number];
|
|
11
|
+
/** The palette place of the bars' colour. */
|
|
12
|
+
slot?: number;
|
|
13
|
+
};
|
|
14
|
+
/** A histogram of some values: how many fall in each bin of one measure. */
|
|
15
|
+
export declare function histogramChart(given: readonly Value[], options: HistogramOptions, env: Env): Scene;
|