plotilla 0.2.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 +30 -25
- 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,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escape text for HTML: a gallery page's headings, and the tooltips a page shows as HTML.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Escape text for HTML, in element text or a quoted attribute alike.
|
|
6
|
+
*
|
|
7
|
+
* @param text - The text.
|
|
8
|
+
* @returns The text with every markup character written as its entity.
|
|
9
|
+
*/
|
|
10
|
+
export declare function escapeHtml(text: string): string;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escape text for HTML: a gallery page's headings, and the tooltips a page shows as HTML.
|
|
3
|
+
*/
|
|
4
|
+
/** Characters HTML reads as markup, and the entity each is written as. */
|
|
5
|
+
const HTML_ENTITIES = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'" };
|
|
6
|
+
const HTML_SPECIAL_CHARACTER_PATTERN = /[&<>"']/g;
|
|
7
|
+
/**
|
|
8
|
+
* Escape text for HTML, in element text or a quoted attribute alike.
|
|
9
|
+
*
|
|
10
|
+
* @param text - The text.
|
|
11
|
+
* @returns The text with every markup character written as its entity.
|
|
12
|
+
*/
|
|
13
|
+
export function escapeHtml(text) {
|
|
14
|
+
return text.replace(HTML_SPECIAL_CHARACTER_PATTERN, (character) => HTML_ENTITIES[character] ?? character);
|
|
15
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A line coloured by a number, split into straight segments that each take
|
|
3
|
+
* one colour. Neither Plotly nor ECharts shades a line's stroke by a third
|
|
4
|
+
* value (ECharts' visual map shades a line only along its x or y), so both
|
|
5
|
+
* renderers draw these segments, one series per colour, and look the same.
|
|
6
|
+
*/
|
|
7
|
+
import type { Value } from "../model/panel_model.ts";
|
|
8
|
+
/** One point of a line coloured by a number. */
|
|
9
|
+
export type ColouredLinePoint = {
|
|
10
|
+
x: Value;
|
|
11
|
+
y: number;
|
|
12
|
+
colourValue: number | null;
|
|
13
|
+
};
|
|
14
|
+
/** The segments drawn in one colour. */
|
|
15
|
+
export type ColouredLineSegments = {
|
|
16
|
+
colour: string;
|
|
17
|
+
segments: {
|
|
18
|
+
start: ColouredLinePoint;
|
|
19
|
+
end: ColouredLinePoint;
|
|
20
|
+
}[];
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Group a line's segments by colour: each pair of neighbouring points is one
|
|
24
|
+
* segment, coloured by the pair's mean value at the nearest step of the
|
|
25
|
+
* colours. A segment whose two values are both missing is left out, so the
|
|
26
|
+
* line has a gap there. Grouping by step keeps the series count at most the
|
|
27
|
+
* number of colours, however long the line.
|
|
28
|
+
*
|
|
29
|
+
* @param points - The line's points, in drawing order.
|
|
30
|
+
* @param options - The colour range the values map onto, and the colours from its minimum to its maximum.
|
|
31
|
+
* @returns The segments of each colour used, from the lowest step to the highest.
|
|
32
|
+
*/
|
|
33
|
+
export declare function groupLineSegmentsByColour(points: readonly ColouredLinePoint[], options: {
|
|
34
|
+
colourRange: {
|
|
35
|
+
minimum: number;
|
|
36
|
+
maximum: number;
|
|
37
|
+
};
|
|
38
|
+
colours: readonly string[];
|
|
39
|
+
}): ColouredLineSegments[];
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A line coloured by a number, split into straight segments that each take
|
|
3
|
+
* one colour. Neither Plotly nor ECharts shades a line's stroke by a third
|
|
4
|
+
* value (ECharts' visual map shades a line only along its x or y), so both
|
|
5
|
+
* renderers draw these segments, one series per colour, and look the same.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Group a line's segments by colour: each pair of neighbouring points is one
|
|
9
|
+
* segment, coloured by the pair's mean value at the nearest step of the
|
|
10
|
+
* colours. A segment whose two values are both missing is left out, so the
|
|
11
|
+
* line has a gap there. Grouping by step keeps the series count at most the
|
|
12
|
+
* number of colours, however long the line.
|
|
13
|
+
*
|
|
14
|
+
* @param points - The line's points, in drawing order.
|
|
15
|
+
* @param options - The colour range the values map onto, and the colours from its minimum to its maximum.
|
|
16
|
+
* @returns The segments of each colour used, from the lowest step to the highest.
|
|
17
|
+
*/
|
|
18
|
+
export function groupLineSegmentsByColour(points, options) {
|
|
19
|
+
const span = options.colourRange.maximum - options.colourRange.minimum;
|
|
20
|
+
const segmentsByStep = new Map();
|
|
21
|
+
points.slice(1).forEach((end, index) => {
|
|
22
|
+
const start = points[index];
|
|
23
|
+
if (start === undefined)
|
|
24
|
+
return;
|
|
25
|
+
const values = [start.colourValue, end.colourValue].filter((value) => value !== null);
|
|
26
|
+
if (values.length === 0)
|
|
27
|
+
return;
|
|
28
|
+
const mean = values.reduce((sum, value) => sum + value, 0) / values.length;
|
|
29
|
+
const fraction = span === 0 ? 0 : (mean - options.colourRange.minimum) / span;
|
|
30
|
+
const step = Math.round(Math.min(1, Math.max(0, fraction)) * (options.colours.length - 1));
|
|
31
|
+
const segments = segmentsByStep.get(step) ?? [];
|
|
32
|
+
segments.push({ start, end });
|
|
33
|
+
segmentsByStep.set(step, segments);
|
|
34
|
+
});
|
|
35
|
+
return [...segmentsByStep.entries()].sort(([first], [second]) => first - second).map(([step, segments]) => {
|
|
36
|
+
const colour = options.colours[step];
|
|
37
|
+
if (colour === undefined)
|
|
38
|
+
throw new Error(`Expected a colour at step ${step} of ${options.colours.length}; got none.`);
|
|
39
|
+
return { colour, segments };
|
|
40
|
+
});
|
|
41
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A map panel → Plotly traces and the layout entry for its subplot.
|
|
3
|
+
*
|
|
4
|
+
* Flat maps use Plotly's tile maps (scattermap, choroplethmap, densitymap)
|
|
5
|
+
* over an OpenFreeMap street map; the globe uses Plotly's geo subplot with an
|
|
6
|
+
* orthographic projection (country outlines, no street tiles).
|
|
7
|
+
*/
|
|
8
|
+
import type { MapModel } from "../model/panel_model.ts";
|
|
9
|
+
import { type PlotTheme } from "../colour/palette.ts";
|
|
10
|
+
import type { PanelModel } from "../model/panel_model.ts";
|
|
11
|
+
/**
|
|
12
|
+
* A panel's Plotly colour bar: beside the panel and as tall as it, so panels
|
|
13
|
+
* in a grid never stack their bars (panel_layout leaves room for it), with
|
|
14
|
+
* ticks in real values when the scale transforms them (log, quantile).
|
|
15
|
+
*
|
|
16
|
+
* @param scale - The panel's colour scale, if any.
|
|
17
|
+
* @param options - The panel's domain.
|
|
18
|
+
* @returns The colorbar to give the panel's coloured traces.
|
|
19
|
+
*/
|
|
20
|
+
export declare function buildColourBar(scale: PanelModel["colourScale"] | undefined, options: {
|
|
21
|
+
domain: {
|
|
22
|
+
x: [number, number];
|
|
23
|
+
y: [number, number];
|
|
24
|
+
};
|
|
25
|
+
}): Record<string, unknown>;
|
|
26
|
+
/**
|
|
27
|
+
* Build a map panel's traces and subplot layout for Plotly.
|
|
28
|
+
*
|
|
29
|
+
* @param panel - A panel with a map.
|
|
30
|
+
* @param options - The subplot suffix ("" or "2", …), its domain, the theme,
|
|
31
|
+
* and the colour scale as Plotly wants it.
|
|
32
|
+
* @returns The traces, and the layout key and value for the subplot.
|
|
33
|
+
*/
|
|
34
|
+
export declare function buildPlotlyMap(panel: PanelModel & {
|
|
35
|
+
map: MapModel;
|
|
36
|
+
}, options: {
|
|
37
|
+
suffix: string;
|
|
38
|
+
domain: {
|
|
39
|
+
x: [number, number];
|
|
40
|
+
y: [number, number];
|
|
41
|
+
};
|
|
42
|
+
theme: PlotTheme;
|
|
43
|
+
colourScale: [number, string][] | null;
|
|
44
|
+
}): {
|
|
45
|
+
traces: Record<string, unknown>[];
|
|
46
|
+
layoutKey: string;
|
|
47
|
+
layoutValue: Record<string, unknown>;
|
|
48
|
+
};
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A map panel → Plotly traces and the layout entry for its subplot.
|
|
3
|
+
*
|
|
4
|
+
* Flat maps use Plotly's tile maps (scattermap, choroplethmap, densitymap)
|
|
5
|
+
* over an OpenFreeMap street map; the globe uses Plotly's geo subplot with an
|
|
6
|
+
* orthographic projection (country outlines, no street tiles).
|
|
7
|
+
*/
|
|
8
|
+
import { pickNoDataColour } from "../model/map_model.js";
|
|
9
|
+
import { MAP_INKS, MAP_LABEL_FONT, MAP_STYLE_URLS, MARKER_SIZE, PLOT_THEMES, pickSequentialColour } from "../colour/palette.js";
|
|
10
|
+
/** How far each point spreads on a density map, in pixels. */
|
|
11
|
+
const DENSITY_RADIUS_PIXELS = 18;
|
|
12
|
+
const REGION_OPACITY = 0.78;
|
|
13
|
+
/** Space between a panel and its colour bar, as a fraction of the figure's width. */
|
|
14
|
+
const COLOUR_BAR_GAP = 0.005;
|
|
15
|
+
/** How thick a colour bar is. */
|
|
16
|
+
const COLOUR_BAR_THICKNESS_PIXELS = 14;
|
|
17
|
+
/**
|
|
18
|
+
* A panel's Plotly colour bar: beside the panel and as tall as it, so panels
|
|
19
|
+
* in a grid never stack their bars (panel_layout leaves room for it), with
|
|
20
|
+
* ticks in real values when the scale transforms them (log, quantile).
|
|
21
|
+
*
|
|
22
|
+
* @param scale - The panel's colour scale, if any.
|
|
23
|
+
* @param options - The panel's domain.
|
|
24
|
+
* @returns The colorbar to give the panel's coloured traces.
|
|
25
|
+
*/
|
|
26
|
+
export function buildColourBar(scale, options) {
|
|
27
|
+
const ticks = scale === null || scale === undefined || scale.ticks === undefined
|
|
28
|
+
? {}
|
|
29
|
+
: { tickvals: scale.ticks.map((tick) => tick.value), ticktext: scale.ticks.map((tick) => tick.text) };
|
|
30
|
+
return {
|
|
31
|
+
title: { text: scale?.label ?? "" },
|
|
32
|
+
x: options.domain.x[1] + COLOUR_BAR_GAP, xanchor: "left",
|
|
33
|
+
y: (options.domain.y[0] + options.domain.y[1]) / 2, yanchor: "middle",
|
|
34
|
+
len: options.domain.y[1] - options.domain.y[0], lenmode: "fraction", thickness: COLOUR_BAR_THICKNESS_PIXELS,
|
|
35
|
+
...ticks,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Build a map panel's traces and subplot layout for Plotly.
|
|
40
|
+
*
|
|
41
|
+
* @param panel - A panel with a map.
|
|
42
|
+
* @param options - The subplot suffix ("" or "2", …), its domain, the theme,
|
|
43
|
+
* and the colour scale as Plotly wants it.
|
|
44
|
+
* @returns The traces, and the layout key and value for the subplot.
|
|
45
|
+
*/
|
|
46
|
+
export function buildPlotlyMap(panel, options) {
|
|
47
|
+
const map = panel.map;
|
|
48
|
+
const ink = PLOT_THEMES[options.theme];
|
|
49
|
+
const isGlobe = map.projection === "globe";
|
|
50
|
+
const subplot = isGlobe ? { geo: `geo${options.suffix}` } : { subplot: `map${options.suffix}` };
|
|
51
|
+
const scale = panel.colourScale;
|
|
52
|
+
const traces = [];
|
|
53
|
+
const colourBar = buildColourBar(scale, { domain: options.domain });
|
|
54
|
+
let hasScaleShown = false;
|
|
55
|
+
const showScaleOnce = () => {
|
|
56
|
+
const show = !hasScaleShown;
|
|
57
|
+
hasScaleShown = true;
|
|
58
|
+
return show;
|
|
59
|
+
};
|
|
60
|
+
for (const layer of map.layers) {
|
|
61
|
+
if (layer.kind === "points") {
|
|
62
|
+
const hasLabels = layer.points.some((point) => point.label !== null);
|
|
63
|
+
const sizes = layer.points.map((point) => point.sizePixels);
|
|
64
|
+
traces.push({
|
|
65
|
+
type: isGlobe ? "scattergeo" : "scattermap", ...subplot, name: layer.name,
|
|
66
|
+
lon: layer.points.map((point) => point.longitude), lat: layer.points.map((point) => point.latitude),
|
|
67
|
+
mode: hasLabels ? "markers+text" : "markers", text: layer.points.map((point) => point.label ?? ""),
|
|
68
|
+
// Street maps draw labels with the tile server's own fonts.
|
|
69
|
+
textposition: "top right", textfont: { color: ink.textPrimary, ...(isGlobe ? {} : { family: MAP_LABEL_FONT }) },
|
|
70
|
+
marker: layer.colour === null
|
|
71
|
+
? { size: sizes, color: layer.points.map((point) => point.colourValue), colorscale: options.colourScale, showscale: showScaleOnce(), cmin: scale?.minimum, cmax: scale?.maximum, colorbar: colourBar }
|
|
72
|
+
: { size: sizes, color: layer.colour },
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
else if (layer.kind === "density") {
|
|
76
|
+
traces.push({
|
|
77
|
+
type: "densitymap", ...subplot, name: layer.name, showlegend: false,
|
|
78
|
+
lon: layer.points.map((point) => point.longitude), lat: layer.points.map((point) => point.latitude),
|
|
79
|
+
z: layer.points.map((point) => point.weight), radius: DENSITY_RADIUS_PIXELS,
|
|
80
|
+
colorscale: options.colourScale ?? [[0, "rgba(0,0,0,0)"], [0.2, pickSequentialColour(0.3)], [1, pickSequentialColour(1)]],
|
|
81
|
+
showscale: false,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
else if (layer.kind === "regions") {
|
|
85
|
+
const common = { ...subplot, geojson: layer.geojson, featureidkey: "properties.plotilla_id", hoverinfo: "text", showlegend: false };
|
|
86
|
+
const regionType = isGlobe ? "choropleth" : "choroplethmap";
|
|
87
|
+
const line = { color: ink.surface, width: 1 };
|
|
88
|
+
const valued = layer.regions.filter((region) => region.value !== null);
|
|
89
|
+
if (valued.length > 0) {
|
|
90
|
+
traces.push({
|
|
91
|
+
type: regionType, ...common, name: layer.name, locations: valued.map((region) => region.id), z: valued.map((region) => region.value),
|
|
92
|
+
text: valued.map((region) => region.label), colorscale: options.colourScale, showscale: showScaleOnce(),
|
|
93
|
+
zmin: scale?.minimum, zmax: scale?.maximum, colorbar: colourBar, marker: { opacity: REGION_OPACITY, line },
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
// Regions coloured by category, and those with no data, each drawn in their fixed colour.
|
|
97
|
+
const byColour = new Map();
|
|
98
|
+
for (const region of layer.regions) {
|
|
99
|
+
if (region.value !== null)
|
|
100
|
+
continue;
|
|
101
|
+
const colour = region.colour ?? pickNoDataColour(options.theme);
|
|
102
|
+
byColour.set(colour, [...(byColour.get(colour) ?? []), region]);
|
|
103
|
+
}
|
|
104
|
+
for (const [colour, regions] of byColour) {
|
|
105
|
+
traces.push({
|
|
106
|
+
type: regionType, ...common, name: layer.name, locations: regions.map((region) => region.id), z: regions.map(() => 0),
|
|
107
|
+
text: regions.map((region) => region.label), colorscale: [[0, colour], [1, colour]], showscale: false,
|
|
108
|
+
marker: { opacity: REGION_OPACITY, line },
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
layer.lines.forEach((line) => {
|
|
114
|
+
const colour = layer.colour ?? (scale === null || line.colourValue === null
|
|
115
|
+
? ink.textSecondary
|
|
116
|
+
: pickSequentialColour((line.colourValue - scale.minimum) / (scale.maximum - scale.minimum || 1)));
|
|
117
|
+
traces.push({
|
|
118
|
+
type: isGlobe ? "scattergeo" : "scattermap", ...subplot, name: layer.name, mode: "lines",
|
|
119
|
+
lon: line.path.map(([longitude]) => longitude), lat: line.path.map(([, latitude]) => latitude),
|
|
120
|
+
line: { color: colour, width: line.widthPixels },
|
|
121
|
+
hoverinfo: "text", text: line.label ?? layer.name,
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
// A line trace has one colour and no colour bar, so lines coloured by a number get one
|
|
125
|
+
// invisible marker per line, at its start, that carries the panel's colour bar.
|
|
126
|
+
if (layer.colour === null && scale !== null) {
|
|
127
|
+
traces.push({
|
|
128
|
+
type: isGlobe ? "scattergeo" : "scattermap", ...subplot, name: layer.name, mode: "markers",
|
|
129
|
+
lon: layer.lines.map((line) => line.path[0]?.[0] ?? null), lat: layer.lines.map((line) => line.path[0]?.[1] ?? null),
|
|
130
|
+
marker: {
|
|
131
|
+
size: MARKER_SIZE, opacity: 0, color: layer.lines.map((line) => line.colourValue),
|
|
132
|
+
colorscale: options.colourScale, showscale: showScaleOnce(), cmin: scale.minimum, cmax: scale.maximum, colorbar: colourBar,
|
|
133
|
+
},
|
|
134
|
+
hoverinfo: "skip", showlegend: false,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (isGlobe) {
|
|
140
|
+
const land = MAP_INKS[options.theme];
|
|
141
|
+
return {
|
|
142
|
+
traces,
|
|
143
|
+
layoutKey: `geo${options.suffix}`,
|
|
144
|
+
layoutValue: {
|
|
145
|
+
domain: options.domain,
|
|
146
|
+
// The globe starts whole, turned to face the data; closer only if the agent asked.
|
|
147
|
+
projection: { type: "orthographic", rotation: { lon: map.centre[0], lat: map.centre[1] }, scale: map.isZoomGiven ? Math.max(1, 2 ** (map.zoom - 1)) : 1 },
|
|
148
|
+
showland: true, landcolor: land.land, showocean: true, oceancolor: land.water,
|
|
149
|
+
showcountries: true, countrycolor: land.border, coastlinecolor: land.border,
|
|
150
|
+
showlakes: false, showframe: false, bgcolor: ink.surface,
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
traces,
|
|
156
|
+
layoutKey: `map${options.suffix}`,
|
|
157
|
+
layoutValue: { domain: options.domain, style: MAP_STYLE_URLS[options.theme], center: { lon: map.centre[0], lat: map.centre[1] }, zoom: map.zoom },
|
|
158
|
+
};
|
|
159
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chart model → Plotly figure ({ data, layout }). Plain JSON, passed to
|
|
3
|
+
* `Plotly.newPlot` on the page.
|
|
4
|
+
*/
|
|
5
|
+
import type { FigureModel } from "../model/panel_model.ts";
|
|
6
|
+
import { type FigureSize } from "../model/panel_layout.ts";
|
|
7
|
+
/**
|
|
8
|
+
* Build the Plotly figure for a chart model.
|
|
9
|
+
*
|
|
10
|
+
* @param model - The chart model.
|
|
11
|
+
* @param options - The size the host draws the figure at, in pixels; build again when it changes.
|
|
12
|
+
* @returns `{ data, layout }` for Plotly.newPlot.
|
|
13
|
+
*/
|
|
14
|
+
export declare function buildPlotlyFigure(model: FigureModel, options: {
|
|
15
|
+
size: FigureSize;
|
|
16
|
+
}): {
|
|
17
|
+
data: Record<string, unknown>[];
|
|
18
|
+
layout: Record<string, unknown>;
|
|
19
|
+
};
|