plotilla 1.0.0 → 1.1.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 +37 -11
- package/data/boundaries/world_countries.geojson +1 -1
- package/dist/colour/colour_scale.d.ts +55 -0
- package/dist/colour/colour_scale.js +80 -0
- package/dist/colour/palette.d.ts +176 -0
- package/dist/colour/palette.js +220 -0
- package/dist/fields/axis_fields.d.ts +56 -0
- package/dist/fields/axis_fields.js +46 -0
- package/dist/fields/colour_fields.d.ts +32 -0
- package/dist/fields/colour_fields.js +31 -0
- package/dist/fields/column_fields.d.ts +52 -0
- package/{src/fields/column_fields.ts → dist/fields/column_fields.js} +15 -29
- package/dist/fields/common_fields.d.ts +9 -0
- package/{src/fields/common_fields.ts → dist/fields/common_fields.js} +3 -6
- package/dist/fields/decide_plot_axes.d.ts +56 -0
- package/dist/fields/decide_plot_axes.js +71 -0
- package/dist/fields/marker_fields.d.ts +8 -0
- package/dist/fields/marker_fields.js +13 -0
- package/dist/fields/read_date_time.d.ts +85 -0
- package/dist/fields/read_date_time.js +199 -0
- package/dist/figure/check_axis_year_formats.d.ts +36 -0
- package/dist/figure/check_axis_year_formats.js +116 -0
- package/dist/figure/check_plot_category_limits.d.ts +128 -0
- package/dist/figure/check_plot_category_limits.js +284 -0
- package/dist/figure/plot_figure.d.ts +861 -0
- package/dist/figure/plot_figure.js +58 -0
- package/dist/figure/resolve_plot_figure.d.ts +28 -0
- package/dist/figure/resolve_plot_figure.js +41 -0
- package/dist/figure/validate_plot_figure.d.ts +50 -0
- package/dist/figure/validate_plot_figure.js +441 -0
- package/dist/map/assign_map_layer_hue_slots.d.ts +89 -0
- package/dist/map/assign_map_layer_hue_slots.js +146 -0
- package/dist/map/boundary_sets.d.ts +65 -0
- package/dist/map/boundary_sets.js +103 -0
- package/dist/map/build_world_outline_across_180.d.ts +21 -0
- package/dist/map/build_world_outline_across_180.js +23 -0
- package/dist/map/check_map_layers.d.ts +16 -0
- package/dist/map/check_map_layers.js +31 -0
- package/dist/map/fetch_region_outlines.d.ts +25 -0
- package/dist/map/fetch_region_outlines.js +39 -0
- package/dist/map/geojson.d.ts +150 -0
- package/dist/map/geojson.js +248 -0
- package/dist/map/join_region_rows.d.ts +40 -0
- package/dist/map/join_region_rows.js +45 -0
- package/dist/map/map_layers.d.ts +229 -0
- package/dist/map/map_layers.js +87 -0
- package/dist/model/find_panel_colour_scale.d.ts +25 -0
- package/dist/model/find_panel_colour_scale.js +27 -0
- package/dist/model/map_model.d.ts +30 -0
- package/dist/model/map_model.js +234 -0
- package/dist/model/panel_layout.d.ts +42 -0
- package/dist/model/panel_layout.js +154 -0
- package/dist/model/panel_model.d.ts +223 -0
- package/dist/model/panel_model.js +7 -0
- package/dist/model/plot_model.d.ts +28 -0
- package/dist/model/plot_model.js +431 -0
- package/dist/model/statistics.d.ts +83 -0
- package/dist/model/statistics.js +107 -0
- package/dist/plots/bar_plot.d.ts +46 -0
- package/dist/plots/bar_plot.js +30 -0
- package/dist/plots/box_plot.d.ts +30 -0
- package/dist/plots/box_plot.js +21 -0
- package/dist/plots/density_plot.d.ts +36 -0
- package/dist/plots/density_plot.js +23 -0
- package/dist/plots/heatmap_plot.d.ts +22 -0
- package/dist/plots/heatmap_plot.js +18 -0
- package/dist/plots/histogram_plot.d.ts +32 -0
- package/dist/plots/histogram_plot.js +24 -0
- package/dist/plots/line_plot.d.ts +44 -0
- package/dist/plots/line_plot.js +25 -0
- package/dist/plots/map_plot.d.ts +92 -0
- package/dist/plots/map_plot.js +35 -0
- package/dist/plots/pie_plot.d.ts +17 -0
- package/dist/plots/pie_plot.js +16 -0
- package/dist/plots/scatter_plot.d.ts +47 -0
- package/dist/plots/scatter_plot.js +27 -0
- package/dist/renderers/date_ticks.d.ts +37 -0
- package/dist/renderers/date_ticks.js +148 -0
- package/dist/renderers/echarts_map.d.ts +47 -0
- package/dist/renderers/echarts_map.js +492 -0
- package/dist/renderers/echarts_options.d.ts +30 -0
- package/dist/renderers/echarts_options.js +449 -0
- package/dist/renderers/echarts_pie_radius.d.ts +52 -0
- package/dist/renderers/echarts_pie_radius.js +158 -0
- package/dist/renderers/escape_html.d.ts +10 -0
- package/dist/renderers/escape_html.js +15 -0
- package/dist/renderers/line_colour_segments.d.ts +39 -0
- package/dist/renderers/line_colour_segments.js +41 -0
- package/dist/renderers/plotly_map.d.ts +48 -0
- package/dist/renderers/plotly_map.js +159 -0
- package/dist/renderers/plotly_options.d.ts +19 -0
- package/dist/renderers/plotly_options.js +345 -0
- package/dist/renderers/year_ticks.d.ts +27 -0
- package/dist/renderers/year_ticks.js +42 -0
- package/dist/table/check_column.d.ts +36 -0
- package/dist/table/check_column.js +46 -0
- package/dist/table/check_values_have_spread.d.ts +30 -0
- package/{src/table/check_values_have_spread.ts → dist/table/check_values_have_spread.js} +16 -17
- package/dist/table/compute_quantile.d.ts +16 -0
- package/dist/table/compute_quantile.js +23 -0
- package/dist/table/data_table.d.ts +59 -0
- package/dist/table/data_table.js +87 -0
- package/dist/table/density_bandwidth.d.ts +64 -0
- package/dist/table/density_bandwidth.js +88 -0
- package/dist/table/find_line_fit.d.ts +30 -0
- package/dist/table/find_line_fit.js +36 -0
- package/dist/table/get_number_range.d.ts +16 -0
- package/{src/table/get_number_range.ts → dist/table/get_number_range.js} +8 -8
- package/dist/table/group_rows.d.ts +40 -0
- package/dist/table/group_rows.js +67 -0
- package/dist/table/list_categories.d.ts +14 -0
- package/{src/table/list_categories.ts → dist/table/list_categories.js} +2 -3
- package/dist/table/read_number.d.ts +12 -0
- package/dist/table/read_number.js +14 -0
- package/package.json +28 -23
- package/src/colour/colour_scale.ts +0 -80
- package/src/colour/palette.ts +0 -220
- package/src/examples/example_figures.ts +0 -320
- package/src/fields/axis_fields.ts +0 -32
- package/src/fields/colour_fields.ts +0 -40
- package/src/fields/decide_plot_axes.ts +0 -77
- package/src/fields/marker_fields.ts +0 -16
- package/src/figure/plot_figure.ts +0 -67
- package/src/figure/resolve_plot_figure.ts +0 -47
- package/src/figure/validate_plot_figure.ts +0 -412
- package/src/map/boundary_sets.ts +0 -116
- package/src/map/check_map_layers.ts +0 -32
- package/src/map/fetch_region_outlines.ts +0 -48
- package/src/map/geojson.ts +0 -174
- package/src/map/map_layers.ts +0 -108
- package/src/model/check_axis_shows_years.ts +0 -58
- package/src/model/map_model.ts +0 -238
- package/src/model/panel_layout.ts +0 -171
- package/src/model/panel_model.ts +0 -85
- package/src/model/plot_model.ts +0 -347
- package/src/model/statistics.ts +0 -134
- package/src/plots/bar_plot.ts +0 -36
- package/src/plots/box_plot.ts +0 -26
- package/src/plots/density_plot.ts +0 -29
- package/src/plots/heatmap_plot.ts +0 -24
- package/src/plots/histogram_plot.ts +0 -29
- package/src/plots/line_plot.ts +0 -29
- package/src/plots/map_plot.ts +0 -46
- package/src/plots/pie_plot.ts +0 -23
- package/src/plots/scatter_plot.ts +0 -32
- package/src/renderers/echarts_map.ts +0 -194
- package/src/renderers/echarts_options.ts +0 -317
- package/src/renderers/plotly_map.ts +0 -170
- package/src/renderers/plotly_options.ts +0 -318
- package/src/table/check_column.ts +0 -34
- package/src/table/compute_quantile.ts +0 -23
- package/src/table/data_table.ts +0 -83
- package/src/table/density_bandwidth.ts +0 -94
- package/src/table/find_line_fit.ts +0 -44
- package/src/table/group_rows.ts +0 -71
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chart model → Plotly figure ({ data, layout }). Plain JSON, passed to
|
|
3
|
+
* `Plotly.newPlot` on the page.
|
|
4
|
+
*/
|
|
5
|
+
import { LINE_WIDTH, MARKER_SIZE, SEQUENTIAL_COLOURS, PLOT_THEMES } from "../colour/palette.js";
|
|
6
|
+
import { formatValue } from "../colour/colour_scale.js";
|
|
7
|
+
import { buildColourBar, buildPlotlyMap } from "./plotly_map.js";
|
|
8
|
+
import { groupLineSegmentsByColour } from "./line_colour_segments.js";
|
|
9
|
+
import { listDateTicks } from "./date_ticks.js";
|
|
10
|
+
import { formatYear, listWholeTicks } from "./year_ticks.js";
|
|
11
|
+
import { computePanelRectangles, MINIMUM_SERIES_FOR_LEGEND } from "../model/panel_layout.js";
|
|
12
|
+
const PLOTLY_DASHES = { solid: "solid", dashed: "dash", dotted: "dot", dash_dot: "dashdot" };
|
|
13
|
+
const PLOTLY_SYMBOLS = {
|
|
14
|
+
circle: "circle", square: "square", triangle: "triangle-up", diamond: "diamond", cross: "cross", star: "star",
|
|
15
|
+
};
|
|
16
|
+
const FILLED_AREA_ALPHA_HEXADECIMAL = "40";
|
|
17
|
+
const PANEL_TITLE_FONT_PIXELS = 13;
|
|
18
|
+
/** How far above its panel a panel title sits, in pixels. */
|
|
19
|
+
const PANEL_TITLE_OFFSET_PIXELS = 4;
|
|
20
|
+
/** A 2px surface gap between neighbouring fills (dataviz skill). */
|
|
21
|
+
const FILL_GAP_PIXELS = 2;
|
|
22
|
+
/** A heatmap cell's hover: where it is, then its real value (the colour may follow a log or rank); no trace name box. */
|
|
23
|
+
const HEATMAP_HOVER_TEMPLATE = "%{x}, %{y}: %{text}<extra></extra>";
|
|
24
|
+
/** Offset groups of stacked panels are named this, then the panel's index. */
|
|
25
|
+
const STACK_OFFSET_GROUP_PREFIX = "stack-";
|
|
26
|
+
const POSITIVE_STACK_SUFFIX = "+";
|
|
27
|
+
const NEGATIVE_STACK_SUFFIX = "-";
|
|
28
|
+
/**
|
|
29
|
+
* A line coloured by a number, for Plotly, which gives a line one colour: one
|
|
30
|
+
* line trace per colour step of `groupLineSegmentsByColour` (its segments
|
|
31
|
+
* split by gaps).
|
|
32
|
+
* One markers trace carries the colour bar and the hover; its markers show
|
|
33
|
+
* only when the series asks for them.
|
|
34
|
+
*
|
|
35
|
+
* @param series - The line, coloured by a number.
|
|
36
|
+
* @param options - Its axes, the panel's colour range and Plotly colour scale, whether this trace shows the scale, and its colour bar.
|
|
37
|
+
* @returns The segment traces, then the markers trace.
|
|
38
|
+
*/
|
|
39
|
+
function buildColouredLineTraces(series, options) {
|
|
40
|
+
const lines = groupLineSegmentsByColour(series.points, { colourRange: options.colourRange, colours: SEQUENTIAL_COLOURS }).map(({ colour, segments }) => ({
|
|
41
|
+
type: "scatter", ...options.axes, name: series.name, mode: "lines",
|
|
42
|
+
x: segments.flatMap(({ start, end }) => [start.x, end.x, null]), y: segments.flatMap(({ start, end }) => [start.y, end.y, null]),
|
|
43
|
+
line: { color: colour, width: LINE_WIDTH, dash: PLOTLY_DASHES[series.dash] },
|
|
44
|
+
hoverinfo: "skip", showlegend: false,
|
|
45
|
+
}));
|
|
46
|
+
const markers = {
|
|
47
|
+
type: "scatter", ...options.axes, name: series.name, mode: "markers",
|
|
48
|
+
x: series.points.map((point) => point.x), y: series.points.map((point) => point.y),
|
|
49
|
+
marker: {
|
|
50
|
+
// Plotly still draws a marker of size 0, so markers the series does not ask for are made transparent.
|
|
51
|
+
size: MARKER_SIZE, opacity: series.showMarkers ? 1 : 0, color: series.points.map((point) => point.colourValue),
|
|
52
|
+
colorscale: options.colourScale, showscale: options.showScale, colorbar: options.colourBar,
|
|
53
|
+
cmin: options.colourRange.minimum, cmax: options.colourRange.maximum,
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
return [...lines, markers];
|
|
57
|
+
}
|
|
58
|
+
/** The figure title's top, and the legend's top under a title, from the figure's top edge, in pixels (inside the header panel_layout reserves). */
|
|
59
|
+
const TITLE_TOP_PIXELS = 12;
|
|
60
|
+
const LEGEND_TOP_BELOW_TITLE_PIXELS = 42;
|
|
61
|
+
function convertColourScale(colours) {
|
|
62
|
+
return colours.map((colour, index) => [index / (colours.length - 1), colour]);
|
|
63
|
+
}
|
|
64
|
+
/** A date axis's tick format, by label form: 2024.01, 2024.01.15, 2024.01.15 08:30, or 2024.01.15 08:30:20. */
|
|
65
|
+
const DATE_TICK_FORMATS = { month: "%Y.%m", day: "%Y.%m.%d", minute: "%Y.%m.%d %H:%M", second: "%Y.%m.%d %H:%M:%S" };
|
|
66
|
+
/**
|
|
67
|
+
* Decide how an axis prints its ticks, by its label form. Plain numbers keep
|
|
68
|
+
* thousands commas, which Plotly leaves off a 4-digit number unless
|
|
69
|
+
* separatethousands is set, and print in full, which it shortens (1.5B)
|
|
70
|
+
* unless exponentformat is "none".
|
|
71
|
+
*
|
|
72
|
+
* Year and two-digit-year ticks are a fixed array of whole years from the
|
|
73
|
+
* model's range. Plotly 4.1.1's automatic ticks have no minimum step on a
|
|
74
|
+
* linear axis (prepTicks divides the range by nticks, and autoTicks rounds
|
|
75
|
+
* that up to 0.2, 0.5, 1, 2, 5... times a power of ten, with no floor at 1),
|
|
76
|
+
* so a zoomed view would tick on half-years and tickformat "d" would print a
|
|
77
|
+
* year twice, or round 2009.5 up to 2010. No Plotly format prints 1990 as 90
|
|
78
|
+
* either. The cost: a zoom between two ticks shows no year label;
|
|
79
|
+
* recomputing ticks on zoom would need a plotly_relayout handler on the host.
|
|
80
|
+
*
|
|
81
|
+
* Dates are printed from ticks this builds too, as Plotly would tick inside a
|
|
82
|
+
* month, day, minute or second and print its label twice.
|
|
83
|
+
*
|
|
84
|
+
* @param axis - The axis.
|
|
85
|
+
* @returns The layout fields that set its tick labels.
|
|
86
|
+
*/
|
|
87
|
+
function buildTickLabels(axis) {
|
|
88
|
+
switch (axis.labelForm) {
|
|
89
|
+
case "number":
|
|
90
|
+
return { separatethousands: true, exponentformat: "none" };
|
|
91
|
+
case "YYYY":
|
|
92
|
+
case "YY": {
|
|
93
|
+
if (axis.numberRange === null)
|
|
94
|
+
throw new Error("Expected a year axis to have numbers to show.");
|
|
95
|
+
const form = axis.labelForm;
|
|
96
|
+
const ticks = listWholeTicks(axis.numberRange);
|
|
97
|
+
return { tickmode: "array", tickvals: ticks, ticktext: ticks.map((tick) => formatYear(tick, { form })) };
|
|
98
|
+
}
|
|
99
|
+
case "month":
|
|
100
|
+
case "day":
|
|
101
|
+
case "minute":
|
|
102
|
+
case "second": {
|
|
103
|
+
const tickformat = DATE_TICK_FORMATS[axis.labelForm];
|
|
104
|
+
if (axis.numberRange === null)
|
|
105
|
+
return { tickformat };
|
|
106
|
+
return { tickformat, tickmode: "array", tickvals: listDateTicks(axis.numberRange, { form: axis.labelForm }) };
|
|
107
|
+
}
|
|
108
|
+
case "category":
|
|
109
|
+
return {};
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function buildAxisLayout(axis, options) {
|
|
113
|
+
const ink = PLOT_THEMES[options.theme];
|
|
114
|
+
return {
|
|
115
|
+
domain: options.domain,
|
|
116
|
+
anchor: options.anchor,
|
|
117
|
+
type: axis.kind === "value" ? (axis.scale === "log" ? "log" : "linear") : axis.kind === "time" ? "date" : "category",
|
|
118
|
+
...(axis.kind === "category" ? { categoryorder: "array", categoryarray: axis.categories } : {}),
|
|
119
|
+
// No automargin: it would shrink every panel to fit one axis's labels; panel_layout has left room for them.
|
|
120
|
+
title: { text: axis.label, font: { color: ink.textSecondary } },
|
|
121
|
+
automargin: false,
|
|
122
|
+
...buildTickLabels(axis),
|
|
123
|
+
tickfont: { color: ink.textSecondary },
|
|
124
|
+
gridcolor: ink.gridLine,
|
|
125
|
+
linecolor: ink.gridLine,
|
|
126
|
+
zeroline: false,
|
|
127
|
+
...(axis.kind === "value" ? { rangemode: axis.includesZero ? "tozero" : "normal" } : {}),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
function buildTraces(series, options) {
|
|
131
|
+
const ink = PLOT_THEMES[options.theme];
|
|
132
|
+
const axes = { xaxis: options.xAxis, yaxis: options.yAxis };
|
|
133
|
+
switch (series.mark) {
|
|
134
|
+
case "line":
|
|
135
|
+
if (series.colour === null && options.colourRange !== null) {
|
|
136
|
+
return buildColouredLineTraces(series, { axes, colourRange: options.colourRange, colourScale: options.colourScale, showScale: options.showScale, colourBar: options.colourBar });
|
|
137
|
+
}
|
|
138
|
+
return [{
|
|
139
|
+
type: "scatter", ...axes, name: series.name, mode: series.showMarkers ? "lines+markers" : "lines",
|
|
140
|
+
x: series.points.map((point) => point.x), y: series.points.map((point) => point.y),
|
|
141
|
+
line: { color: series.colour ?? ink.textSecondary, width: LINE_WIDTH, dash: PLOTLY_DASHES[series.dash] },
|
|
142
|
+
marker: series.colour === null
|
|
143
|
+
? { size: MARKER_SIZE, color: series.points.map((point) => point.colourValue), colorscale: options.colourScale, showscale: options.showScale, colorbar: options.colourBar }
|
|
144
|
+
: { size: MARKER_SIZE, color: series.colour },
|
|
145
|
+
...(series.isFilled ? { fill: "tozeroy", fillcolor: `${series.colour}${FILLED_AREA_ALPHA_HEXADECIMAL}` } : {}),
|
|
146
|
+
}];
|
|
147
|
+
case "scatter": {
|
|
148
|
+
const isContinuous = series.colour === null;
|
|
149
|
+
return [{
|
|
150
|
+
type: "scatter", ...axes, name: series.name, mode: "markers",
|
|
151
|
+
x: series.points.map((point) => point.x), y: series.points.map((point) => point.y),
|
|
152
|
+
marker: {
|
|
153
|
+
symbol: PLOTLY_SYMBOLS[series.shape],
|
|
154
|
+
size: series.points.map((point) => point.sizePixels),
|
|
155
|
+
line: { color: ink.surface, width: FILL_GAP_PIXELS / 2 },
|
|
156
|
+
...(isContinuous
|
|
157
|
+
? { color: series.points.map((point) => point.colourValue), colorscale: options.colourScale, showscale: options.showScale, colorbar: options.colourBar }
|
|
158
|
+
: { color: series.colour }),
|
|
159
|
+
},
|
|
160
|
+
}];
|
|
161
|
+
}
|
|
162
|
+
case "bar": {
|
|
163
|
+
const categories = series.points.map((point) => point.x);
|
|
164
|
+
const values = series.points.map((point) => point.y);
|
|
165
|
+
const isContinuous = series.colour === null;
|
|
166
|
+
const stack = options.barStack;
|
|
167
|
+
const stacking = stack === null ? {} : {
|
|
168
|
+
offsetgroup: stack.offsetGroup,
|
|
169
|
+
base: series.points.map((point) => {
|
|
170
|
+
// Positive and negative bars stack away from zero separately, as Plotly's own stacking does.
|
|
171
|
+
const key = `${point.x}${point.y < 0 ? NEGATIVE_STACK_SUFFIX : POSITIVE_STACK_SUFFIX}`;
|
|
172
|
+
const start = stack.ends.get(key) ?? 0;
|
|
173
|
+
stack.ends.set(key, start + point.y);
|
|
174
|
+
return start;
|
|
175
|
+
}),
|
|
176
|
+
};
|
|
177
|
+
return [{
|
|
178
|
+
type: "bar", ...axes, name: series.name, orientation: series.isHorizontal ? "h" : "v", ...stacking,
|
|
179
|
+
x: series.isHorizontal ? values : categories, y: series.isHorizontal ? categories : values,
|
|
180
|
+
marker: {
|
|
181
|
+
line: { color: ink.surface, width: FILL_GAP_PIXELS / 2 },
|
|
182
|
+
...(isContinuous
|
|
183
|
+
? { color: series.points.map((point) => point.colourValue), colorscale: options.colourScale, showscale: options.showScale, colorbar: options.colourBar }
|
|
184
|
+
: { color: series.colour }),
|
|
185
|
+
},
|
|
186
|
+
}];
|
|
187
|
+
}
|
|
188
|
+
case "box":
|
|
189
|
+
// Precomputed statistics, so Plotly draws the same box as ECharts.
|
|
190
|
+
return [{
|
|
191
|
+
type: "box", ...axes, name: series.name, x: series.boxes.map((box) => box.category),
|
|
192
|
+
q1: series.boxes.map((box) => box.summary.firstQuartile), median: series.boxes.map((box) => box.summary.median),
|
|
193
|
+
q3: series.boxes.map((box) => box.summary.thirdQuartile), lowerfence: series.boxes.map((box) => box.summary.lowerWhisker),
|
|
194
|
+
upperfence: series.boxes.map((box) => box.summary.upperWhisker),
|
|
195
|
+
line: { color: series.colour, width: LINE_WIDTH }, fillcolor: "rgba(0,0,0,0)",
|
|
196
|
+
}, {
|
|
197
|
+
type: "scatter", ...axes, name: series.name, mode: "markers", showlegend: false, marker: { color: series.colour, size: MARKER_SIZE },
|
|
198
|
+
x: series.boxes.flatMap((box) => (series.showMarkers ? box.values : box.summary.outliers).map(() => box.category)),
|
|
199
|
+
y: series.boxes.flatMap((box) => series.showMarkers ? box.values : box.summary.outliers),
|
|
200
|
+
}];
|
|
201
|
+
case "histogram": {
|
|
202
|
+
// An explicit offset places the bar itself, so Plotly's grouping cannot shift it out of its bin (see HistogramSlot).
|
|
203
|
+
const slot = options.histogramSlot;
|
|
204
|
+
if (slot === null)
|
|
205
|
+
throw new Error(`Expected a histogram slot for histogram series "${series.name}"; got null.`);
|
|
206
|
+
const { index, count } = slot;
|
|
207
|
+
return [{
|
|
208
|
+
type: "bar", ...axes, name: series.name,
|
|
209
|
+
x: series.bins.map((bin) => (bin.start + bin.end) / 2), y: series.bins.map((bin) => bin.height),
|
|
210
|
+
offset: series.bins.map((bin) => (bin.end - bin.start) * (index / count - 1 / 2)),
|
|
211
|
+
width: series.bins.map((bin) => (bin.end - bin.start) / count),
|
|
212
|
+
marker: { color: series.colour, line: { color: ink.surface, width: FILL_GAP_PIXELS / 2 } },
|
|
213
|
+
}];
|
|
214
|
+
}
|
|
215
|
+
case "heatmap": {
|
|
216
|
+
const xs = [...new Set(series.cells.map((cell) => cell.x))];
|
|
217
|
+
const ys = [...new Set(series.cells.map((cell) => cell.y))];
|
|
218
|
+
// Nested maps (not a joined string key) so no x or y value can collide with another; the first cell at a position wins, as a scan would.
|
|
219
|
+
const cellsByYThenX = new Map();
|
|
220
|
+
for (const cell of series.cells) {
|
|
221
|
+
const row = cellsByYThenX.get(cell.y) ?? new Map();
|
|
222
|
+
cellsByYThenX.set(cell.y, row);
|
|
223
|
+
if (!row.has(cell.x))
|
|
224
|
+
row.set(cell.x, cell);
|
|
225
|
+
}
|
|
226
|
+
const findCell = (x, y) => cellsByYThenX.get(y)?.get(x);
|
|
227
|
+
// Colour follows the colour value; the hover shows the real one.
|
|
228
|
+
const z = ys.map((y) => xs.map((x) => findCell(x, y)?.colourValue ?? null));
|
|
229
|
+
const text = ys.map((y) => xs.map((x) => {
|
|
230
|
+
const cell = findCell(x, y);
|
|
231
|
+
return cell === undefined ? null : formatValue(cell.value);
|
|
232
|
+
}));
|
|
233
|
+
return [{
|
|
234
|
+
type: "heatmap", ...axes, name: series.name, x: xs, y: ys, z, text, hovertemplate: HEATMAP_HOVER_TEMPLATE,
|
|
235
|
+
colorscale: options.colourScale, showscale: options.showScale, colorbar: options.colourBar,
|
|
236
|
+
}];
|
|
237
|
+
}
|
|
238
|
+
case "pie":
|
|
239
|
+
return [];
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Build the Plotly figure for a chart model.
|
|
244
|
+
*
|
|
245
|
+
* @param model - The chart model.
|
|
246
|
+
* @param options - The size the host draws the figure at, in pixels; build again when it changes.
|
|
247
|
+
* @returns `{ data, layout }` for Plotly.newPlot.
|
|
248
|
+
*/
|
|
249
|
+
export function buildPlotlyFigure(model, options) {
|
|
250
|
+
const ink = PLOT_THEMES[model.theme];
|
|
251
|
+
const { size } = options;
|
|
252
|
+
const rectangles = computePanelRectangles(model, { size });
|
|
253
|
+
const data = [];
|
|
254
|
+
const layout = {
|
|
255
|
+
paper_bgcolor: ink.surface,
|
|
256
|
+
plot_bgcolor: ink.surface,
|
|
257
|
+
font: { color: ink.textPrimary },
|
|
258
|
+
...(model.title === null ? {} : {
|
|
259
|
+
title: { text: model.title, font: { color: ink.textPrimary }, yref: "container", y: 1 - TITLE_TOP_PIXELS / size.height, yanchor: "top" },
|
|
260
|
+
}),
|
|
261
|
+
// In the header the layout reserves above the panels, below the title.
|
|
262
|
+
legend: {
|
|
263
|
+
orientation: "h", x: 0.5, xanchor: "center", yanchor: "top", font: { color: ink.textSecondary },
|
|
264
|
+
y: 1 - (model.title === null ? TITLE_TOP_PIXELS : LEGEND_TOP_BELOW_TITLE_PIXELS) / size.height,
|
|
265
|
+
},
|
|
266
|
+
// No margins: the panel layout has already left room for every label, in pixels.
|
|
267
|
+
margin: { l: 0, r: 0, t: 0, b: 0 },
|
|
268
|
+
// Plotly's barmode covers every panel, so stacking is done per panel instead (see BarStack).
|
|
269
|
+
barmode: "group",
|
|
270
|
+
hovermode: "closest",
|
|
271
|
+
annotations: [],
|
|
272
|
+
};
|
|
273
|
+
// One legend entry per series name, however many panels it appears in.
|
|
274
|
+
const namesInLegend = new Set();
|
|
275
|
+
// A shared axis links to the first panel that has one, as ECharts links every axis it has: never to a pie's or map's missing axis.
|
|
276
|
+
const firstAxisReferences = { x: null, y: null };
|
|
277
|
+
const linkAxis = (direction, options) => {
|
|
278
|
+
const first = firstAxisReferences[direction];
|
|
279
|
+
if (first === null) {
|
|
280
|
+
firstAxisReferences[direction] = options.reference;
|
|
281
|
+
return {};
|
|
282
|
+
}
|
|
283
|
+
return options.isShared ? { matches: first } : {};
|
|
284
|
+
};
|
|
285
|
+
model.panels.forEach((panel, panelIndex) => {
|
|
286
|
+
const rectangle = rectangles[panelIndex];
|
|
287
|
+
if (rectangle === undefined)
|
|
288
|
+
throw new Error(`Expected a rectangle for panel ${panelIndex}; got none.`);
|
|
289
|
+
const xDomain = [rectangle.left, rectangle.left + rectangle.width];
|
|
290
|
+
const yDomain = [1 - rectangle.top - rectangle.height, 1 - rectangle.top];
|
|
291
|
+
if (panel.title !== null) {
|
|
292
|
+
layout.annotations.push({
|
|
293
|
+
text: panel.title, showarrow: false, xref: "paper", yref: "paper", x: rectangle.left, xanchor: "left",
|
|
294
|
+
y: yDomain[1] + PANEL_TITLE_OFFSET_PIXELS / size.height, yanchor: "bottom", font: { color: ink.textPrimary, size: PANEL_TITLE_FONT_PIXELS },
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
const pie = panel.series.find((series) => series.mark === "pie");
|
|
298
|
+
if (pie !== undefined && pie.mark === "pie") {
|
|
299
|
+
data.push({
|
|
300
|
+
type: "pie", name: pie.name, hole: pie.hole, domain: { x: xDomain, y: yDomain },
|
|
301
|
+
labels: pie.slices.map((slice) => slice.label), values: pie.slices.map((slice) => slice.value),
|
|
302
|
+
marker: { colors: pie.slices.map((slice) => slice.colour), line: { color: ink.surface, width: FILL_GAP_PIXELS } },
|
|
303
|
+
textfont: { color: ink.textPrimary },
|
|
304
|
+
});
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
const suffix = panelIndex === 0 ? "" : String(panelIndex + 1);
|
|
308
|
+
if (panel.map !== undefined) {
|
|
309
|
+
const colourScale = panel.colourScale === null ? null : convertColourScale(panel.colourScale.colours);
|
|
310
|
+
const built = buildPlotlyMap({ ...panel, map: panel.map }, { suffix, domain: { x: xDomain, y: yDomain }, theme: model.theme, colourScale });
|
|
311
|
+
layout[built.layoutKey] = built.layoutValue;
|
|
312
|
+
for (const trace of built.traces) {
|
|
313
|
+
const name = String(trace.name ?? "");
|
|
314
|
+
const isFirst = trace.showlegend !== false && model.legendNames.includes(name) && !namesInLegend.has(name);
|
|
315
|
+
if (isFirst)
|
|
316
|
+
namesInLegend.add(name);
|
|
317
|
+
data.push({ ...trace, legendgroup: name, showlegend: isFirst });
|
|
318
|
+
}
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
const xRef = `x${suffix}`;
|
|
322
|
+
const yRef = `y${suffix}`;
|
|
323
|
+
if (panel.xAxis !== null)
|
|
324
|
+
layout[`xaxis${suffix}`] = { ...buildAxisLayout(panel.xAxis, { theme: model.theme, domain: xDomain, anchor: yRef }), ...linkAxis("x", { isShared: model.isXAxisShared, reference: xRef }) };
|
|
325
|
+
if (panel.yAxis !== null)
|
|
326
|
+
layout[`yaxis${suffix}`] = { ...buildAxisLayout(panel.yAxis, { theme: model.theme, domain: yDomain, anchor: xRef }), ...linkAxis("y", { isShared: model.isYAxisShared, reference: yRef }) };
|
|
327
|
+
const colourScale = panel.colourScale === null ? null : convertColourScale(panel.colourScale.colours);
|
|
328
|
+
const isStacked = panel.series.some((series) => series.mark === "bar" && series.isStacked);
|
|
329
|
+
const barStack = isStacked ? { offsetGroup: `${STACK_OFFSET_GROUP_PREFIX}${panelIndex}`, ends: new Map() } : null;
|
|
330
|
+
const histograms = panel.series.filter((series) => series.mark === "histogram");
|
|
331
|
+
panel.series.forEach((series, seriesIndex) => {
|
|
332
|
+
const histogramSlot = series.mark === "histogram" ? { index: histograms.indexOf(series), count: histograms.length } : null;
|
|
333
|
+
const colourRange = panel.colourScale === null ? null : { minimum: panel.colourScale.minimum, maximum: panel.colourScale.maximum };
|
|
334
|
+
const traceOptions = { theme: model.theme, xAxis: xRef, yAxis: yRef, colourScale, colourRange, showScale: seriesIndex === 0 && panel.colourScale !== null, colourBar: buildColourBar(panel.colourScale, { domain: { x: xDomain, y: yDomain } }), barStack, histogramSlot };
|
|
335
|
+
for (const trace of buildTraces(series, traceOptions)) {
|
|
336
|
+
const isFirst = series.name !== "" && !namesInLegend.has(series.name) && trace.showlegend !== false;
|
|
337
|
+
if (isFirst)
|
|
338
|
+
namesInLegend.add(series.name);
|
|
339
|
+
data.push({ ...trace, legendgroup: series.name, showlegend: isFirst });
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
layout.showlegend = model.legendNames.length >= MINIMUM_SERIES_FOR_LEGEND;
|
|
344
|
+
return { data, layout };
|
|
345
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ticks and labels for an axis declared as years, for a renderer that cannot
|
|
3
|
+
* be told to tick only on whole numbers or to print a year by two digits
|
|
4
|
+
* (Plotly). Years are whole, so a tick between two years would print one of
|
|
5
|
+
* them twice.
|
|
6
|
+
*/
|
|
7
|
+
import type { AxisYearFormat } from "../fields/axis_fields.ts";
|
|
8
|
+
/**
|
|
9
|
+
* List whole-number ticks across a range, a round step apart (1, 2, 5, 10, 20, 50...), never less than one.
|
|
10
|
+
*
|
|
11
|
+
* @param range - The smallest and largest number the axis shows.
|
|
12
|
+
* @returns The ticks, ascending.
|
|
13
|
+
*/
|
|
14
|
+
export declare function listWholeTicks(range: {
|
|
15
|
+
minimum: number;
|
|
16
|
+
maximum: number;
|
|
17
|
+
}): number[];
|
|
18
|
+
/**
|
|
19
|
+
* Print a whole number as a year, in full or by its last two digits.
|
|
20
|
+
*
|
|
21
|
+
* @param year - The year.
|
|
22
|
+
* @param options - The declared form.
|
|
23
|
+
* @returns The label: 1990, or 90.
|
|
24
|
+
*/
|
|
25
|
+
export declare function formatYear(year: number, options: {
|
|
26
|
+
form: AxisYearFormat;
|
|
27
|
+
}): string;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ticks and labels for an axis declared as years, for a renderer that cannot
|
|
3
|
+
* be told to tick only on whole numbers or to print a year by two digits
|
|
4
|
+
* (Plotly). Years are whole, so a tick between two years would print one of
|
|
5
|
+
* them twice.
|
|
6
|
+
*/
|
|
7
|
+
/** About how many ticks a year axis gets: as many as a renderer's own number axis shows. */
|
|
8
|
+
const TARGET_TICK_COUNT = 6;
|
|
9
|
+
/** The round steps between ticks, within each power of ten: 1, 2, 5, 10, 20, 50 and so on. */
|
|
10
|
+
const ROUND_STEP_FACTORS = [1, 2, 5, 10];
|
|
11
|
+
const DECIMAL_BASE = 10;
|
|
12
|
+
/** A year by two digits keeps the last two: 1990 as 90, 2005 as 05. */
|
|
13
|
+
const TWO_DIGIT_YEAR_MODULUS = 100;
|
|
14
|
+
const TWO_DIGIT_YEAR_LENGTH = 2;
|
|
15
|
+
/**
|
|
16
|
+
* List whole-number ticks across a range, a round step apart (1, 2, 5, 10, 20, 50...), never less than one.
|
|
17
|
+
*
|
|
18
|
+
* @param range - The smallest and largest number the axis shows.
|
|
19
|
+
* @returns The ticks, ascending.
|
|
20
|
+
*/
|
|
21
|
+
export function listWholeTicks(range) {
|
|
22
|
+
const rough = Math.max(1, (range.maximum - range.minimum) / TARGET_TICK_COUNT);
|
|
23
|
+
const magnitude = DECIMAL_BASE ** Math.floor(Math.log10(rough));
|
|
24
|
+
const factor = ROUND_STEP_FACTORS.find((candidate) => candidate * magnitude >= rough) ?? DECIMAL_BASE;
|
|
25
|
+
const step = factor * magnitude;
|
|
26
|
+
const ticks = [];
|
|
27
|
+
for (let tick = Math.ceil(range.minimum / step) * step; tick <= range.maximum; tick += step)
|
|
28
|
+
ticks.push(tick);
|
|
29
|
+
return ticks;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Print a whole number as a year, in full or by its last two digits.
|
|
33
|
+
*
|
|
34
|
+
* @param year - The year.
|
|
35
|
+
* @param options - The declared form.
|
|
36
|
+
* @returns The label: 1990, or 90.
|
|
37
|
+
*/
|
|
38
|
+
export function formatYear(year, options) {
|
|
39
|
+
if (options.form === "YYYY")
|
|
40
|
+
return String(year);
|
|
41
|
+
return String(Math.abs(year) % TWO_DIGIT_YEAR_MODULUS).padStart(TWO_DIGIT_YEAR_LENGTH, "0");
|
|
42
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type DataTable } from "./data_table.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Check that a field names a column of the table, and that the column holds
|
|
4
|
+
* only numbers when the field needs them.
|
|
5
|
+
*
|
|
6
|
+
* @param table - The table the plot draws from.
|
|
7
|
+
* @param options - The column named, the field naming it and where, for errors, and whether it must be numeric.
|
|
8
|
+
*/
|
|
9
|
+
export declare function checkColumn(table: DataTable, options: {
|
|
10
|
+
column: string;
|
|
11
|
+
field: string;
|
|
12
|
+
where: string;
|
|
13
|
+
isNumeric: boolean;
|
|
14
|
+
}): void;
|
|
15
|
+
/**
|
|
16
|
+
* Whether a column holds numbers (nulls aside), so colour by it is continuous.
|
|
17
|
+
*
|
|
18
|
+
* @param table - The table.
|
|
19
|
+
* @param options - The column.
|
|
20
|
+
* @returns True when every non-null value is a number.
|
|
21
|
+
*/
|
|
22
|
+
export declare function checkColumnIsNumeric(table: DataTable, options: {
|
|
23
|
+
column: string;
|
|
24
|
+
}): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Whether a column holds no values, every cell missing. Validation and the
|
|
27
|
+
* model share it, so a plot validation lets through for its note is the one
|
|
28
|
+
* the model notes.
|
|
29
|
+
*
|
|
30
|
+
* @param table - The table.
|
|
31
|
+
* @param options - The column.
|
|
32
|
+
* @returns True when every value is null.
|
|
33
|
+
*/
|
|
34
|
+
export declare function checkColumnHasNoValues(table: DataTable, options: {
|
|
35
|
+
column: string;
|
|
36
|
+
}): boolean;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { findColumn } from "./data_table.js";
|
|
2
|
+
/**
|
|
3
|
+
* Check that a field names a column of the table, and that the column holds
|
|
4
|
+
* only numbers when the field needs them.
|
|
5
|
+
*
|
|
6
|
+
* @param table - The table the plot draws from.
|
|
7
|
+
* @param options - The column named, the field naming it and where, for errors, and whether it must be numeric.
|
|
8
|
+
*/
|
|
9
|
+
export function checkColumn(table, options) {
|
|
10
|
+
const values = findColumn(table, { column: options.column });
|
|
11
|
+
if (values === null) {
|
|
12
|
+
throw new Error(`${options.where}.${options.field}: names column "${options.column}", which is not in the table. ` +
|
|
13
|
+
`Columns: ${Object.keys(table).join(", ")}.`);
|
|
14
|
+
}
|
|
15
|
+
if (options.isNumeric && values.some((value) => value !== null && typeof value !== "number")) {
|
|
16
|
+
throw new Error(`${options.where}.${options.field}: column "${options.column}" must be numeric.`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Whether a column holds numbers (nulls aside), so colour by it is continuous.
|
|
21
|
+
*
|
|
22
|
+
* @param table - The table.
|
|
23
|
+
* @param options - The column.
|
|
24
|
+
* @returns True when every non-null value is a number.
|
|
25
|
+
*/
|
|
26
|
+
export function checkColumnIsNumeric(table, options) {
|
|
27
|
+
const values = findColumn(table, { column: options.column });
|
|
28
|
+
if (values === null)
|
|
29
|
+
throw new Error(`Column "${options.column}" is not in the table.`);
|
|
30
|
+
return values.every((value) => value === null || typeof value === "number");
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Whether a column holds no values, every cell missing. Validation and the
|
|
34
|
+
* model share it, so a plot validation lets through for its note is the one
|
|
35
|
+
* the model notes.
|
|
36
|
+
*
|
|
37
|
+
* @param table - The table.
|
|
38
|
+
* @param options - The column.
|
|
39
|
+
* @returns True when every value is null.
|
|
40
|
+
*/
|
|
41
|
+
export function checkColumnHasNoValues(table, options) {
|
|
42
|
+
const values = findColumn(table, { column: options.column });
|
|
43
|
+
if (values === null)
|
|
44
|
+
throw new Error(`Column "${options.column}" is not in the table.`);
|
|
45
|
+
return values.every((value) => value === null);
|
|
46
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a column's numbers vary by an amount arithmetic can measure. A
|
|
3
|
+
* density works its curve's width out from this spread, so validation (which
|
|
4
|
+
* refuses a density that cannot be drawn) and the model (which draws it) ask
|
|
5
|
+
* the same function, and never disagree on numbers so close together that
|
|
6
|
+
* their deviations underflow to zero, or so large that their squared deviations overflow.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* The sample standard deviation (divided by n - 1), by Welford's running
|
|
10
|
+
* mean: a sum of values near the largest number overflows, so equal values
|
|
11
|
+
* there would get an infinite deviation; with a running mean, their deviations stay 0.
|
|
12
|
+
*
|
|
13
|
+
* @param values - The numbers.
|
|
14
|
+
* @returns The standard deviation: NaN with fewer than two values, 0 when they
|
|
15
|
+
* do not vary or their deviations are too small to square without underflow,
|
|
16
|
+
* and Infinity or NaN once a squared deviation overflows (a spread above
|
|
17
|
+
* about 1e154, for example [0, 1e200]; [1.7e308, -1.7e308] gives NaN).
|
|
18
|
+
*/
|
|
19
|
+
export declare function computeStandardDeviation(values: readonly number[]): number;
|
|
20
|
+
/**
|
|
21
|
+
* Whether numbers have a spread a density can use: a finite standard
|
|
22
|
+
* deviation above zero. This alone does not decide the bandwidth: Silverman's
|
|
23
|
+
* rule takes the smaller of this and IQR / 1.34, which can round to zero or
|
|
24
|
+
* near it while the standard deviation is large (a middle half a subnormal
|
|
25
|
+
* distance apart), so findBandwidthFault judges the bandwidth itself.
|
|
26
|
+
*
|
|
27
|
+
* @param values - The numbers.
|
|
28
|
+
* @returns True when the spread is finite and above zero.
|
|
29
|
+
*/
|
|
30
|
+
export declare function checkValuesHaveSpread(values: readonly number[]): boolean;
|
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
* the same function, and never disagree on numbers so close together that
|
|
6
6
|
* their deviations underflow to zero, or so large that their squared deviations overflow.
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
8
|
/**
|
|
10
9
|
* The sample standard deviation (divided by n - 1), by Welford's running
|
|
11
10
|
* mean: a sum of values near the largest number overflows, so equal values
|
|
@@ -17,20 +16,20 @@
|
|
|
17
16
|
* and Infinity or NaN once a squared deviation overflows (a spread above
|
|
18
17
|
* about 1e154, for example [0, 1e200]; [1.7e308, -1.7e308] gives NaN).
|
|
19
18
|
*/
|
|
20
|
-
export function computeStandardDeviation(values
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
19
|
+
export function computeStandardDeviation(values) {
|
|
20
|
+
const count = values.length;
|
|
21
|
+
// Dividing by n - 1 gives no NaN for an empty list (0 / -1 is -0), so fewer than two is answered here.
|
|
22
|
+
if (count < 2)
|
|
23
|
+
return Number.NaN;
|
|
24
|
+
let mean = 0;
|
|
25
|
+
let sumOfSquaredDeviations = 0;
|
|
26
|
+
values.forEach((value, index) => {
|
|
27
|
+
const deviation = value - mean;
|
|
28
|
+
mean += deviation / (index + 1);
|
|
29
|
+
sumOfSquaredDeviations += deviation * (value - mean);
|
|
30
|
+
});
|
|
31
|
+
return Math.sqrt(sumOfSquaredDeviations / (count - 1));
|
|
32
32
|
}
|
|
33
|
-
|
|
34
33
|
/**
|
|
35
34
|
* Whether numbers have a spread a density can use: a finite standard
|
|
36
35
|
* deviation above zero. This alone does not decide the bandwidth: Silverman's
|
|
@@ -41,7 +40,7 @@ export function computeStandardDeviation(values: readonly number[]): number {
|
|
|
41
40
|
* @param values - The numbers.
|
|
42
41
|
* @returns True when the spread is finite and above zero.
|
|
43
42
|
*/
|
|
44
|
-
export function checkValuesHaveSpread(values
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
export function checkValuesHaveSpread(values) {
|
|
44
|
+
const standardDeviation = computeStandardDeviation(values);
|
|
45
|
+
return Number.isFinite(standardDeviation) && standardDeviation > 0;
|
|
47
46
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Quantiles of a column's numbers. Below both validation and the model,
|
|
3
|
+
* because Silverman's bandwidth (which both judge) needs the interquartile
|
|
4
|
+
* range, and a box plot needs the quartiles.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* The q-quantile of sorted values, by linear interpolation (the "type 7"
|
|
8
|
+
* definition used by R, NumPy and Excel).
|
|
9
|
+
*
|
|
10
|
+
* @param sorted - Values in ascending order; at least one.
|
|
11
|
+
* @param options - Which quantile, from 0 to 1.
|
|
12
|
+
* @returns The quantile.
|
|
13
|
+
*/
|
|
14
|
+
export declare function computeQuantile(sorted: number[], options: {
|
|
15
|
+
quantile: number;
|
|
16
|
+
}): number;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Quantiles of a column's numbers. Below both validation and the model,
|
|
3
|
+
* because Silverman's bandwidth (which both judge) needs the interquartile
|
|
4
|
+
* range, and a box plot needs the quartiles.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* The q-quantile of sorted values, by linear interpolation (the "type 7"
|
|
8
|
+
* definition used by R, NumPy and Excel).
|
|
9
|
+
*
|
|
10
|
+
* @param sorted - Values in ascending order; at least one.
|
|
11
|
+
* @param options - Which quantile, from 0 to 1.
|
|
12
|
+
* @returns The quantile.
|
|
13
|
+
*/
|
|
14
|
+
export function computeQuantile(sorted, options) {
|
|
15
|
+
if (sorted.length === 0)
|
|
16
|
+
throw new Error("A quantile needs at least one value; got none.");
|
|
17
|
+
const position = (sorted.length - 1) * options.quantile;
|
|
18
|
+
const lower = Math.floor(position);
|
|
19
|
+
const upper = Math.ceil(position);
|
|
20
|
+
const lowerValue = sorted[lower];
|
|
21
|
+
const upperValue = sorted[upper];
|
|
22
|
+
return lowerValue + (upperValue - lowerValue) * (position - lower);
|
|
23
|
+
}
|