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
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "plotilla",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Plots from one spec for AI agents: validation an agent can read, statistics, maps, and ECharts or Plotly output. Pulled out of scribble.tube so any MCP server can plot.
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Plots from one spec for AI agents: validation an agent can read, statistics, maps, and ECharts or Plotly output. Pulled out of scribble.tube so any MCP server can plot.",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"author": "Idin <idin@
|
|
6
|
+
"author": "Idin <idin@idin.net>",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "git+https://github.com/idin/plotilla.git"
|
|
@@ -14,14 +14,16 @@
|
|
|
14
14
|
},
|
|
15
15
|
"type": "module",
|
|
16
16
|
"files": [
|
|
17
|
-
"
|
|
17
|
+
"dist",
|
|
18
18
|
"data",
|
|
19
19
|
"README.md",
|
|
20
20
|
"LICENSE"
|
|
21
21
|
],
|
|
22
22
|
"scripts": {
|
|
23
|
+
"build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.build.json",
|
|
24
|
+
"prepack": "npm run build",
|
|
23
25
|
"test": "vitest run",
|
|
24
|
-
"typecheck": "tsc --noEmit -p tsconfig.json && tsc --noEmit -p tests/tsconfig.json && tsc --noEmit -p scripts/tsconfig.json"
|
|
26
|
+
"typecheck": "tsc --noEmit -p tsconfig.json && tsc --noEmit -p tests/tsconfig.json && tsc --noEmit -p scripts/tsconfig.json && tsc --noEmit -p tsconfig.build.json"
|
|
25
27
|
},
|
|
26
28
|
"devDependencies": {
|
|
27
29
|
"@echarts-x/custom-contour": "^1.2.1",
|
|
@@ -29,6 +31,8 @@
|
|
|
29
31
|
"@types/node": "^26.6.4",
|
|
30
32
|
"@types/topojson-client": "^3.1.5",
|
|
31
33
|
"echarts": "^6.1.0",
|
|
34
|
+
"jsdom": "^29.1.1",
|
|
35
|
+
"plotly.js-dist-min": "4.1.1",
|
|
32
36
|
"topojson-client": "^3.1.0",
|
|
33
37
|
"tsx": "^4.23.15",
|
|
34
38
|
"typescript": "^7.0.2",
|
|
@@ -36,26 +40,27 @@
|
|
|
36
40
|
"world-atlas": "^2.0.2"
|
|
37
41
|
},
|
|
38
42
|
"exports": {
|
|
39
|
-
"./figure/plot_figure": "./
|
|
40
|
-
"./figure/validate_plot_figure": "./
|
|
41
|
-
"./figure/resolve_plot_figure": "./
|
|
42
|
-
"./table/data_table": "./
|
|
43
|
-
"./plots/map_plot": "./
|
|
44
|
-
"./model/plot_model": "./
|
|
45
|
-
"./model/panel_model": "./
|
|
46
|
-
"./model/statistics": "./
|
|
47
|
-
"./colour/palette": "./
|
|
48
|
-
"./colour/colour_scale": "./
|
|
49
|
-
"./map/geojson": "./
|
|
50
|
-
"./map/
|
|
51
|
-
"./map/
|
|
52
|
-
"./
|
|
53
|
-
"./
|
|
54
|
-
"./
|
|
55
|
-
"./
|
|
56
|
-
"./renderers/
|
|
57
|
-
"./renderers/
|
|
58
|
-
"./renderers/
|
|
43
|
+
"./figure/plot_figure": { "types": "./dist/figure/plot_figure.d.ts", "import": "./dist/figure/plot_figure.js" },
|
|
44
|
+
"./figure/validate_plot_figure": { "types": "./dist/figure/validate_plot_figure.d.ts", "import": "./dist/figure/validate_plot_figure.js" },
|
|
45
|
+
"./figure/resolve_plot_figure": { "types": "./dist/figure/resolve_plot_figure.d.ts", "import": "./dist/figure/resolve_plot_figure.js" },
|
|
46
|
+
"./table/data_table": { "types": "./dist/table/data_table.d.ts", "import": "./dist/table/data_table.js" },
|
|
47
|
+
"./plots/map_plot": { "types": "./dist/plots/map_plot.d.ts", "import": "./dist/plots/map_plot.js" },
|
|
48
|
+
"./model/plot_model": { "types": "./dist/model/plot_model.d.ts", "import": "./dist/model/plot_model.js" },
|
|
49
|
+
"./model/panel_model": { "types": "./dist/model/panel_model.d.ts" },
|
|
50
|
+
"./model/statistics": { "types": "./dist/model/statistics.d.ts", "import": "./dist/model/statistics.js" },
|
|
51
|
+
"./colour/palette": { "types": "./dist/colour/palette.d.ts", "import": "./dist/colour/palette.js" },
|
|
52
|
+
"./colour/colour_scale": { "types": "./dist/colour/colour_scale.d.ts", "import": "./dist/colour/colour_scale.js" },
|
|
53
|
+
"./map/geojson": { "types": "./dist/map/geojson.d.ts", "import": "./dist/map/geojson.js" },
|
|
54
|
+
"./map/build_world_outline_across_180": { "types": "./dist/map/build_world_outline_across_180.d.ts", "import": "./dist/map/build_world_outline_across_180.js" },
|
|
55
|
+
"./map/map_layers": { "types": "./dist/map/map_layers.d.ts", "import": "./dist/map/map_layers.js" },
|
|
56
|
+
"./map/fetch_region_outlines": { "types": "./dist/map/fetch_region_outlines.d.ts", "import": "./dist/map/fetch_region_outlines.js" },
|
|
57
|
+
"./model/map_model": { "types": "./dist/model/map_model.d.ts", "import": "./dist/model/map_model.js" },
|
|
58
|
+
"./map/boundary_sets": { "types": "./dist/map/boundary_sets.d.ts", "import": "./dist/map/boundary_sets.js" },
|
|
59
|
+
"./model/panel_layout": { "types": "./dist/model/panel_layout.d.ts", "import": "./dist/model/panel_layout.js" },
|
|
60
|
+
"./renderers/echarts_options": { "types": "./dist/renderers/echarts_options.d.ts", "import": "./dist/renderers/echarts_options.js" },
|
|
61
|
+
"./renderers/echarts_map": { "types": "./dist/renderers/echarts_map.d.ts", "import": "./dist/renderers/echarts_map.js" },
|
|
62
|
+
"./renderers/plotly_options": { "types": "./dist/renderers/plotly_options.d.ts", "import": "./dist/renderers/plotly_options.js" },
|
|
63
|
+
"./renderers/plotly_map": { "types": "./dist/renderers/plotly_map.d.ts", "import": "./dist/renderers/plotly_map.js" },
|
|
59
64
|
"./data/boundaries/*": "./data/boundaries/*"
|
|
60
65
|
},
|
|
61
66
|
"dependencies": {
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Colour scales: how numbers map to colour, shared by charts and maps so
|
|
3
|
-
* `colour_scale` means the same on every plot.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { DEFAULT_COLOUR_SCALE, type ColourScale, type ColourType } from "../fields/colour_fields";
|
|
7
|
-
import { getNumberRange } from "../table/get_number_range";
|
|
8
|
-
|
|
9
|
-
/** Where quantile legends put their labels (0 = lowest, 1 = highest). */
|
|
10
|
-
const QUANTILE_TICKS = [0, 0.25, 0.5, 0.75, 1];
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Write a real value the way legends and hovers show it.
|
|
14
|
-
*
|
|
15
|
-
* @param value - The number.
|
|
16
|
-
* @returns It with thousands separators, or to three significant figures when not whole.
|
|
17
|
-
*/
|
|
18
|
-
export function formatValue(value: number): string {
|
|
19
|
-
return Number.isInteger(value) ? value.toLocaleString("en-US") : value.toPrecision(3);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Turn values into the numbers colour follows, per the layer's colour scale.
|
|
24
|
-
*
|
|
25
|
-
* @param values - The layer's numbers (null where missing).
|
|
26
|
-
* @param options - The scale, the column's name, and where to put notes.
|
|
27
|
-
* @returns The transformed values, and legend ticks in real values when the scale transforms them.
|
|
28
|
-
*/
|
|
29
|
-
export function applyColourScale(
|
|
30
|
-
values: (number | null)[],
|
|
31
|
-
options: { scale: ColourScale | undefined; label: string; notes: string[] },
|
|
32
|
-
): { values: (number | null)[]; ticks: { value: number; text: string }[] | undefined } {
|
|
33
|
-
const scale = options.scale ?? DEFAULT_COLOUR_SCALE;
|
|
34
|
-
if (scale === "linear") return { values, ticks: undefined };
|
|
35
|
-
if (scale === "log") {
|
|
36
|
-
const dropped = values.filter((value) => value !== null && value <= 0).length;
|
|
37
|
-
if (dropped > 0) options.notes.push(`${dropped} value(s) of "${options.label}" are zero or negative, which a log scale cannot show; they are left grey.`);
|
|
38
|
-
const logs = values.map((value) => (value === null || value <= 0 ? null : Math.log10(value)));
|
|
39
|
-
const present = logs.filter((value): value is number => value !== null);
|
|
40
|
-
const ticks: { value: number; text: string }[] = [];
|
|
41
|
-
const logRange = getNumberRange(present);
|
|
42
|
-
for (let power = Math.ceil(logRange.minimum); power <= Math.floor(logRange.maximum); power += 1) ticks.push({ value: power, text: formatValue(10 ** power) });
|
|
43
|
-
return { values: logs, ticks };
|
|
44
|
-
}
|
|
45
|
-
// Quantile: each value becomes its rank from 0 to 1, so colour steps hold equal numbers of places.
|
|
46
|
-
const sorted = values.filter((value): value is number => value !== null).sort((first, second) => first - second);
|
|
47
|
-
// First sorted position of each value, built once; NaN is left out so it ranks -1 like indexOf, which never matches NaN.
|
|
48
|
-
const firstPositions = new Map<number, number>();
|
|
49
|
-
sorted.forEach((value, position) => {
|
|
50
|
-
if (!Number.isNaN(value) && !firstPositions.has(value)) firstPositions.set(value, position);
|
|
51
|
-
});
|
|
52
|
-
const rank = (value: number): number => (sorted.length <= 1 ? 0 : (firstPositions.get(value) ?? -1) / (sorted.length - 1));
|
|
53
|
-
const ticks = QUANTILE_TICKS.map((fraction) => ({ value: fraction, text: formatValue(sorted[Math.round(fraction * (sorted.length - 1))] ?? 0) }));
|
|
54
|
-
return { values: values.map((value) => (value === null ? null : rank(value))), ticks };
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* The text at a colour legend's two ends, top first: the real values when
|
|
59
|
-
* the scale transforms them, else the column's name.
|
|
60
|
-
*
|
|
61
|
-
* @param scale - The panel's colour scale.
|
|
62
|
-
* @returns The top and bottom text.
|
|
63
|
-
*/
|
|
64
|
-
export function buildColourLegendText(scale: { label: string; ticks?: { value: number; text: string }[] }): [string, string] {
|
|
65
|
-
if (scale.ticks === undefined) return [scale.label, ""];
|
|
66
|
-
return [scale.ticks.at(-1)?.text ?? "", scale.ticks[0]?.text ?? ""];
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Decide whether a column colours by a scale (numbers) or by category: what
|
|
71
|
-
* its values imply, unless colour_type says otherwise.
|
|
72
|
-
*
|
|
73
|
-
* @param values - The colour column's values.
|
|
74
|
-
* @param options - The colour_type the agent sent, if any.
|
|
75
|
-
* @returns True for a colour scale.
|
|
76
|
-
*/
|
|
77
|
-
export function decideColourIsContinuous(values: (number | string | null)[], options: { colourType: ColourType | undefined }): boolean {
|
|
78
|
-
if (options.colourType !== undefined) return options.colourType === "continuous";
|
|
79
|
-
return values.every((value) => value === null || typeof value === "number");
|
|
80
|
-
}
|
package/src/colour/palette.ts
DELETED
|
@@ -1,220 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Colours and marks for plots, from the dataviz skill's validated reference
|
|
3
|
-
* palette: its dark column for the dark theme, its light column for light.
|
|
4
|
-
*
|
|
5
|
-
* Rules carried over from that skill:
|
|
6
|
-
* - categorical hues are assigned in this fixed order, never cycled or generated;
|
|
7
|
-
* - more than 8 categories is refused (facet or fold to "Other" instead);
|
|
8
|
-
* - where every pair of categories can sit side by side (scatter), only the
|
|
9
|
-
* first 3 hues stay distinguishable, so past 3 the marker shape varies too;
|
|
10
|
-
* - sequential colour is one hue, light to dark; text never wears series colour.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import { getNumberRange } from "../table/get_number_range";
|
|
14
|
-
|
|
15
|
-
/** The viewer's two themes; each device picks one. */
|
|
16
|
-
export type PlotTheme = "dark" | "light";
|
|
17
|
-
|
|
18
|
-
/** The theme when none is asked for: the viewer is a dark "TV" by default. */
|
|
19
|
-
export const DEFAULT_PLOT_THEME: PlotTheme = "dark";
|
|
20
|
-
|
|
21
|
-
/** Surface, inks and categorical slots per theme (dataviz reference palette; its light and dark columns). */
|
|
22
|
-
export type ThemeInks = {
|
|
23
|
-
surface: string;
|
|
24
|
-
textPrimary: string;
|
|
25
|
-
textSecondary: string;
|
|
26
|
-
gridLine: string;
|
|
27
|
-
/** Categorical slots 1–8, in the validated order. */
|
|
28
|
-
categorical: readonly string[];
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
/** Each theme's inks: surface, text, grid lines and the categorical colours. */
|
|
32
|
-
export const PLOT_THEMES: Readonly<Record<PlotTheme, ThemeInks>> = {
|
|
33
|
-
dark: {
|
|
34
|
-
surface: "#1a1a19",
|
|
35
|
-
textPrimary: "#ffffff",
|
|
36
|
-
textSecondary: "#c3c2b7",
|
|
37
|
-
gridLine: "#383835",
|
|
38
|
-
categorical: ["#3987e5", "#d95926", "#199e70", "#c98500", "#d55181", "#008300", "#9085e9", "#e66767"],
|
|
39
|
-
},
|
|
40
|
-
light: {
|
|
41
|
-
surface: "#fcfcfb",
|
|
42
|
-
textPrimary: "#0b0b0b",
|
|
43
|
-
textSecondary: "#52514e",
|
|
44
|
-
// Recessive grid: the palette's neutral midpoint, a step below the surface.
|
|
45
|
-
gridLine: "#e4e3df",
|
|
46
|
-
categorical: ["#2a78d6", "#eb6834", "#1baf7a", "#eda100", "#e87ba4", "#008300", "#4a3aa7", "#e34948"],
|
|
47
|
-
},
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
/** How many categories colour can tell apart (the palette's slot count). */
|
|
51
|
-
export const MAXIMUM_COLOUR_CATEGORIES = PLOT_THEMES.dark.categorical.length;
|
|
52
|
-
|
|
53
|
-
/** How many categories can share a scatter by colour alone (all-pairs validated). */
|
|
54
|
-
export const ALL_PAIRS_COLOUR_LIMIT = 3;
|
|
55
|
-
|
|
56
|
-
/** The single-hue sequential ramp (blue), light to dark, for continuous colour. */
|
|
57
|
-
export const SEQUENTIAL_COLOURS: readonly string[] = [
|
|
58
|
-
"#cde2fb", "#b7d3f6", "#9ec5f4", "#86b6ef", "#6da7ec", "#5598e7", "#3987e5",
|
|
59
|
-
"#2a78d6", "#256abf", "#1c5cab", "#184f95", "#104281", "#0d366b",
|
|
60
|
-
];
|
|
61
|
-
|
|
62
|
-
/** Line dash styles, in order, for `line_type` categories. */
|
|
63
|
-
export const LINE_DASHES = ["solid", "dashed", "dotted", "dash_dot"] as const;
|
|
64
|
-
/** A line's dash pattern, one of LINE_DASHES. */
|
|
65
|
-
export type LineDash = (typeof LINE_DASHES)[number];
|
|
66
|
-
|
|
67
|
-
/** Marker shapes, in order, for `marker_shape` categories. */
|
|
68
|
-
export const MARKER_SHAPES = ["circle", "square", "triangle", "diamond", "cross", "star"] as const;
|
|
69
|
-
/** A point's marker shape, one of MARKER_SHAPES. */
|
|
70
|
-
export type MarkerShape = (typeof MARKER_SHAPES)[number];
|
|
71
|
-
|
|
72
|
-
/** Mark sizes from the dataviz skill: 2px lines, markers of at least 8px. */
|
|
73
|
-
export const LINE_WIDTH = 2;
|
|
74
|
-
/** A marker's size in pixels: at least 8 px, so it reads at a glance (dataviz skill). */
|
|
75
|
-
export const MARKER_SIZE = 8;
|
|
76
|
-
|
|
77
|
-
/** Marker sizes for a `size` column, in pixels, smallest to largest. */
|
|
78
|
-
export const SIZE_RANGE_PIXELS = { minimum: MARKER_SIZE, maximum: 28 };
|
|
79
|
-
|
|
80
|
-
/** The thinnest and thickest a map line drawn by a width column is, in pixels. */
|
|
81
|
-
export const LINE_WIDTH_RANGE_PIXELS = { minimum: 1.5, maximum: 8 };
|
|
82
|
-
|
|
83
|
-
/** A map line's width in pixels when the layer has no width column. */
|
|
84
|
-
export const DEFAULT_LINE_WIDTH_PIXELS = 2.5;
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Scale a column's numbers linearly onto a pixel range, smallest to the
|
|
88
|
-
* range's minimum and largest to its maximum. A missing value, or a column
|
|
89
|
-
* whose numbers are all equal, gets the fallback.
|
|
90
|
-
*
|
|
91
|
-
* @param values - The numbers, null where missing.
|
|
92
|
-
* @param options - The pixel range, and the size for a value that cannot be placed on it.
|
|
93
|
-
* @returns A pixel size per value.
|
|
94
|
-
*/
|
|
95
|
-
export function scaleToPixels(
|
|
96
|
-
values: readonly (number | null)[],
|
|
97
|
-
options: { range: { minimum: number; maximum: number }; fallback: number },
|
|
98
|
-
): number[] {
|
|
99
|
-
const present = values.filter((value): value is number => value !== null);
|
|
100
|
-
const { minimum: low, maximum: high } = getNumberRange(present);
|
|
101
|
-
const span = options.range.maximum - options.range.minimum;
|
|
102
|
-
return values.map((value) => (value === null || high === low ? options.fallback : options.range.minimum + ((value - low) / (high - low)) * span));
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Pick the colour for the n-th category.
|
|
107
|
-
*
|
|
108
|
-
* @param index - The category's position in first-seen order (0-based).
|
|
109
|
-
* @param options - The theme it is drawn in.
|
|
110
|
-
* @returns Its hex colour.
|
|
111
|
-
*/
|
|
112
|
-
export function pickCategoricalColour(index: number, options: { theme: PlotTheme }): string {
|
|
113
|
-
const colour = PLOT_THEMES[options.theme].categorical[index];
|
|
114
|
-
if (colour === undefined) {
|
|
115
|
-
throw new Error(
|
|
116
|
-
`Colour can tell apart at most ${MAXIMUM_COLOUR_CATEGORIES} categories; got more. ` +
|
|
117
|
-
"Put the rest in an \"Other\" category, or split them across plots in a grid.",
|
|
118
|
-
);
|
|
119
|
-
}
|
|
120
|
-
return colour;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Pick the dash for the n-th line_type category.
|
|
125
|
-
*
|
|
126
|
-
* @param index - The category's position (0-based).
|
|
127
|
-
* @returns The dash style.
|
|
128
|
-
*/
|
|
129
|
-
export function pickLineDash(index: number): LineDash {
|
|
130
|
-
const dash = LINE_DASHES[index];
|
|
131
|
-
if (dash === undefined) {
|
|
132
|
-
throw new Error(`line_type can tell apart at most ${LINE_DASHES.length} categories; got more.`);
|
|
133
|
-
}
|
|
134
|
-
return dash;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Pick the marker shape for the n-th category.
|
|
139
|
-
*
|
|
140
|
-
* @param index - The category's position (0-based).
|
|
141
|
-
* @returns The shape.
|
|
142
|
-
*/
|
|
143
|
-
export function pickMarkerShape(index: number): MarkerShape {
|
|
144
|
-
const shape = MARKER_SHAPES[index];
|
|
145
|
-
if (shape === undefined) {
|
|
146
|
-
throw new Error(`marker_shape can tell apart at most ${MARKER_SHAPES.length} categories; got more.`);
|
|
147
|
-
}
|
|
148
|
-
return shape;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Pick a colour along the sequential ramp.
|
|
153
|
-
*
|
|
154
|
-
* @param fraction - 0 (lightest) to 1 (darkest); clamped.
|
|
155
|
-
* @returns The nearest ramp step.
|
|
156
|
-
*/
|
|
157
|
-
export function pickSequentialColour(fraction: number): string {
|
|
158
|
-
const clamped = Math.min(1, Math.max(0, Number.isFinite(fraction) ? fraction : 0));
|
|
159
|
-
return SEQUENTIAL_COLOURS[Math.round(clamped * (SEQUENTIAL_COLOURS.length - 1))] as string;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/** The radix of hexadecimal digits, for colour channels and hashed names. */
|
|
163
|
-
export const HEXADECIMAL = 16;
|
|
164
|
-
const HEXADECIMAL_COLOUR_PATTERN = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i;
|
|
165
|
-
const CHANNEL_MAXIMUM = 255;
|
|
166
|
-
/** The number of hexadecimal digits in one colour channel of a `#rrggbb` colour. */
|
|
167
|
-
export const CHANNEL_DIGITS = 2;
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Blend a value's colour along a ramp exactly as ECharts' continuous
|
|
171
|
-
* visualMap does (zrender's fastLerp over the ramp's evenly spaced stops,
|
|
172
|
-
* after echarts' clamped linearMap): each channel interpolated between the
|
|
173
|
-
* two nearest stops and rounded. A renderer that paints a colour itself,
|
|
174
|
-
* where a visualMap cannot reach, then matches the colour bar to the byte.
|
|
175
|
-
*
|
|
176
|
-
* @param value - The value to colour.
|
|
177
|
-
* @param options - The scale's range, and the ramp's colours as `#rrggbb`.
|
|
178
|
-
* @returns The colour as `#rrggbb`.
|
|
179
|
-
*/
|
|
180
|
-
export function interpolateRampColour(value: number, options: { minimum: number; maximum: number; colours: readonly string[] }): string {
|
|
181
|
-
const span = options.maximum - options.minimum;
|
|
182
|
-
// As echarts' linearMap: a range of no width maps to the middle.
|
|
183
|
-
const fraction = span === 0 ? 1 / 2 : Math.min(1, Math.max(0, (value - options.minimum) / span));
|
|
184
|
-
const position = fraction * (options.colours.length - 1);
|
|
185
|
-
const left = parseHexadecimalColour(options.colours[Math.floor(position)]);
|
|
186
|
-
const right = parseHexadecimalColour(options.colours[Math.ceil(position)]);
|
|
187
|
-
const step = position - Math.floor(position);
|
|
188
|
-
return `#${left.map((from, channel) => {
|
|
189
|
-
const to = right[channel] ?? from;
|
|
190
|
-
const mixed = Math.min(CHANNEL_MAXIMUM, Math.max(0, Math.round(from + (to - from) * step)));
|
|
191
|
-
return mixed.toString(HEXADECIMAL).padStart(CHANNEL_DIGITS, "0");
|
|
192
|
-
}).join("")}`;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Read a `#rrggbb` colour's channels.
|
|
197
|
-
*
|
|
198
|
-
* @param colour - The colour.
|
|
199
|
-
* @returns Its red, green and blue, 0 to 255.
|
|
200
|
-
*/
|
|
201
|
-
function parseHexadecimalColour(colour: string | undefined): number[] {
|
|
202
|
-
const match = HEXADECIMAL_COLOUR_PATTERN.exec(colour ?? "");
|
|
203
|
-
if (match === null) throw new Error(`Expected a ramp colour written #rrggbb; got ${String(colour)}.`);
|
|
204
|
-
return match.slice(1).map((digits) => Number.parseInt(digits, HEXADECIMAL));
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/** Land, water and borders for maps drawn without street tiles (globe, ECharts), per theme. */
|
|
208
|
-
export const MAP_INKS: Readonly<Record<PlotTheme, { land: string; water: string; border: string }>> = {
|
|
209
|
-
dark: { land: "#2a2b2f", water: "#141518", border: "#4a4b51" },
|
|
210
|
-
light: { land: "#fcfcfb", water: "#e9edf1", border: "#c9c8c2" },
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
/** The font OpenFreeMap serves for map labels; any other font's glyphs 404 on its server. */
|
|
214
|
-
export const MAP_LABEL_FONT = "Noto Sans Regular";
|
|
215
|
-
|
|
216
|
-
/** The street-map style (OpenFreeMap: free, no key; attribution is added by the map library) per theme. */
|
|
217
|
-
export const MAP_STYLE_URLS: Readonly<Record<PlotTheme, string>> = {
|
|
218
|
-
dark: "https://tiles.openfreemap.org/styles/dark",
|
|
219
|
-
light: "https://tiles.openfreemap.org/styles/positron",
|
|
220
|
-
};
|