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,449 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chart model → ECharts option object. Plain JSON (no functions), so it can
|
|
3
|
+
* be sent to the page as-is and passed to `chart.setOption`.
|
|
4
|
+
*/
|
|
5
|
+
import { buildColourLegendText } from "../colour/colour_scale.js";
|
|
6
|
+
import { LINE_WIDTH, MARKER_SIZE, SEQUENTIAL_COLOURS, PLOT_THEMES } from "../colour/palette.js";
|
|
7
|
+
import { buildEChartsMap } from "./echarts_map.js";
|
|
8
|
+
import { findPieRadiusPixels, PIE_LABEL_LAYOUT } from "./echarts_pie_radius.js";
|
|
9
|
+
import { groupLineSegmentsByColour } from "./line_colour_segments.js";
|
|
10
|
+
import { convertDateTimeToMilliseconds, readDateTime } from "../fields/read_date_time.js";
|
|
11
|
+
import { listDateTickMilliseconds } from "./date_ticks.js";
|
|
12
|
+
import { AXIS_LABEL_CHARACTER_PIXELS, computePanelRectangles, MINIMUM_SERIES_FOR_LEGEND } from "../model/panel_layout.js";
|
|
13
|
+
const PERCENT = 100;
|
|
14
|
+
/** A pie sits in the middle of its box (its panel). */
|
|
15
|
+
const PIE_CENTRE_IN_BOX = "50%";
|
|
16
|
+
/** ECharts dash for "dash_dot": 8px dash, 4px gap, 2px dot, 4px gap. */
|
|
17
|
+
const DASH_DOT_PATTERN = [8, 4, 2, 4];
|
|
18
|
+
const FILLED_AREA_OPACITY = 0.25;
|
|
19
|
+
/** Rounded data ends, per the dataviz skill's mark specs. */
|
|
20
|
+
const BAR_CORNER_RADIUS_PIXELS = 4;
|
|
21
|
+
/** A 2px surface gap between neighbouring fills (dataviz skill). */
|
|
22
|
+
const FILL_GAP_PIXELS = 2;
|
|
23
|
+
/** An x-axis title sits this far below the axis, clear of its one row of labels. */
|
|
24
|
+
const X_AXIS_NAME_GAP_PIXELS = 28;
|
|
25
|
+
/** …plus this much clear space. */
|
|
26
|
+
const Y_AXIS_NAME_CLEARANCE_PIXELS = 18;
|
|
27
|
+
const PANEL_TITLE_FONT_PIXELS = 13;
|
|
28
|
+
/** How far above its panel a panel title sits, in pixels. */
|
|
29
|
+
const PANEL_TITLE_OFFSET_PIXELS = 20;
|
|
30
|
+
/** How far right of its panel a colour scale sits, in pixels. */
|
|
31
|
+
const COLOUR_SCALE_OFFSET_PIXELS = 50;
|
|
32
|
+
const COLOUR_SCALE_HEIGHT_PIXELS = 90;
|
|
33
|
+
/** The legend sits below the figure title when there is one. */
|
|
34
|
+
const LEGEND_BELOW_TITLE_PIXELS = 28;
|
|
35
|
+
/**
|
|
36
|
+
* A histogram's bars sit at their bin centres on a value axis, where ECharts
|
|
37
|
+
* makes the band as wide as the smallest gap between bar positions: one bin.
|
|
38
|
+
* No gap inside the band or between colour groups, so the groups tile each
|
|
39
|
+
* bin edge to edge, and a bin's bars cover exactly its range at any zoom.
|
|
40
|
+
*/
|
|
41
|
+
const HISTOGRAM_BAR_GAP = "0%";
|
|
42
|
+
/**
|
|
43
|
+
* A single bin has no gap to measure, so its edges stand in as empty positions
|
|
44
|
+
* half a bin from its centre: the band is then half a bin, and a gap of minus
|
|
45
|
+
* one band widens the bars back to the whole bin. ECharts measures the gap over
|
|
46
|
+
* every bar series on the axis, so the edges sit in a companion series that
|
|
47
|
+
* the tooltip and axis pointer skip, stacked with the bin so it adds no column.
|
|
48
|
+
*/
|
|
49
|
+
const SINGLE_BIN_BAND_GAP = "-100%";
|
|
50
|
+
/** Histogram points are [centre, height, start, end]: x reads all three, so a bin stays while any of it is in a zoom window. */
|
|
51
|
+
const HISTOGRAM_X_DIMENSIONS = [0, 2, 3];
|
|
52
|
+
const HISTOGRAM_HEIGHT_DIMENSION = 1;
|
|
53
|
+
/** Keeps any point with one of its x dimensions in the window; for one x dimension it is ECharts' default "filter". */
|
|
54
|
+
const ZOOM_FILTER_MODE = "weakFilter";
|
|
55
|
+
/** Scatter and bar points are [x, y, colourValue]: the colour value is at index 2. */
|
|
56
|
+
const COLOUR_VALUE_DIMENSION = 2;
|
|
57
|
+
/** Heatmap cells are [x, y, colourValue, value]: the real value, for the tooltip, is at index 3. */
|
|
58
|
+
const HEATMAP_REAL_VALUE_DIMENSION = 3;
|
|
59
|
+
/** Density points are [longitude, latitude, weight]: the weight is at index 2. */
|
|
60
|
+
const DENSITY_WEIGHT_DIMENSION = 2;
|
|
61
|
+
/** Where there is no weight, the heat fades to nothing rather than tinting the whole map. */
|
|
62
|
+
const DENSITY_EMPTY_COLOUR = "rgba(0, 0, 0, 0)";
|
|
63
|
+
const ECHARTS_SYMBOLS = {
|
|
64
|
+
circle: "circle",
|
|
65
|
+
square: "rect",
|
|
66
|
+
triangle: "triangle",
|
|
67
|
+
diamond: "diamond",
|
|
68
|
+
cross: "path://M4,0h2v4h4v2h-4v4h-2v-4h-4v-2h4z",
|
|
69
|
+
star: "path://M5,0l1.2,3.6h3.8l-3.1,2.3l1.2,3.7l-3.1-2.3l-3.1,2.3l1.2-3.7l-3.1-2.3h3.8z",
|
|
70
|
+
};
|
|
71
|
+
function convertDash(dash) {
|
|
72
|
+
return dash === "dash_dot" ? DASH_DOT_PATTERN : dash;
|
|
73
|
+
}
|
|
74
|
+
function percent(fraction) {
|
|
75
|
+
return `${(fraction * PERCENT).toFixed(2)}%`;
|
|
76
|
+
}
|
|
77
|
+
function formatBinEdge(value) {
|
|
78
|
+
return Number.isInteger(value) ? String(value) : value.toPrecision(3);
|
|
79
|
+
}
|
|
80
|
+
function buildAxis(axis, options) {
|
|
81
|
+
const ink = PLOT_THEMES[options.theme];
|
|
82
|
+
const type = axis.kind === "value" && axis.scale === "log" ? "log" : axis.kind;
|
|
83
|
+
const dateTicks = findDateTicks(axis);
|
|
84
|
+
return {
|
|
85
|
+
gridIndex: options.gridIndex,
|
|
86
|
+
type,
|
|
87
|
+
name: axis.label,
|
|
88
|
+
nameLocation: "middle",
|
|
89
|
+
nameGap: options.direction === "x"
|
|
90
|
+
? X_AXIS_NAME_GAP_PIXELS
|
|
91
|
+
: axis.longestLabelCharacters * AXIS_LABEL_CHARACTER_PIXELS + Y_AXIS_NAME_CLEARANCE_PIXELS,
|
|
92
|
+
nameTextStyle: { color: ink.textSecondary },
|
|
93
|
+
axisLabel: { color: ink.textSecondary, ...LABEL_FORM_AXIS_LABELS[axis.labelForm], ...(dateTicks === null ? {} : { customValues: dateTicks }) },
|
|
94
|
+
...LABEL_FORM_AXES[axis.labelForm],
|
|
95
|
+
...(dateTicks === null ? {} : { axisTick: { customValues: dateTicks } }),
|
|
96
|
+
axisLine: { lineStyle: { color: ink.gridLine } },
|
|
97
|
+
splitLine: { lineStyle: { color: ink.gridLine } },
|
|
98
|
+
...(type === "category" ? { data: axis.categories } : {}),
|
|
99
|
+
// "scale" lets the axis leave out zero; bars and densities must keep it.
|
|
100
|
+
...(type === "value" ? { scale: !axis.includesZero } : {}),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function buildSeries(series, options) {
|
|
104
|
+
const ink = PLOT_THEMES[options.theme];
|
|
105
|
+
const axes = { xAxisIndex: options.index, yAxisIndex: options.index };
|
|
106
|
+
switch (series.mark) {
|
|
107
|
+
case "line":
|
|
108
|
+
return [{
|
|
109
|
+
type: "line", ...axes, name: series.name, data: series.points.map((point) => [point.x, point.y, point.colourValue]),
|
|
110
|
+
showSymbol: series.showMarkers, symbolSize: MARKER_SIZE,
|
|
111
|
+
...(series.colour === null ? {} : { itemStyle: { color: series.colour } }),
|
|
112
|
+
lineStyle: { width: LINE_WIDTH, type: convertDash(series.dash), ...(series.colour === null ? {} : { color: series.colour }) },
|
|
113
|
+
...(series.isFilled ? { areaStyle: { opacity: FILLED_AREA_OPACITY } } : {}),
|
|
114
|
+
}];
|
|
115
|
+
case "scatter": {
|
|
116
|
+
return [{
|
|
117
|
+
type: "scatter", ...axes, name: series.name, symbol: ECHARTS_SYMBOLS[series.shape],
|
|
118
|
+
data: series.points.map((point) => ({
|
|
119
|
+
value: [point.x, point.y, point.colourValue],
|
|
120
|
+
symbolSize: point.sizePixels,
|
|
121
|
+
})),
|
|
122
|
+
itemStyle: { borderColor: ink.surface, borderWidth: FILL_GAP_PIXELS / 2, ...(series.colour === null ? {} : { color: series.colour }) },
|
|
123
|
+
}];
|
|
124
|
+
}
|
|
125
|
+
case "bar":
|
|
126
|
+
return [{
|
|
127
|
+
type: "bar", ...axes, name: series.name,
|
|
128
|
+
...(series.isStacked ? { stack: `panel-${options.panelIndex}` } : {}),
|
|
129
|
+
data: series.points.map((point) => ({ value: series.isHorizontal ? [point.y, point.x, point.colourValue] : [point.x, point.y, point.colourValue] })),
|
|
130
|
+
itemStyle: { borderRadius: BAR_CORNER_RADIUS_PIXELS, borderColor: ink.surface, borderWidth: FILL_GAP_PIXELS / 2, ...(series.colour === null ? {} : { color: series.colour }) },
|
|
131
|
+
}];
|
|
132
|
+
case "box":
|
|
133
|
+
return [
|
|
134
|
+
{
|
|
135
|
+
type: "boxplot", ...axes, name: series.name, itemStyle: { color: "transparent", borderColor: series.colour, borderWidth: LINE_WIDTH },
|
|
136
|
+
data: series.boxes.map((box) => ({
|
|
137
|
+
name: box.category,
|
|
138
|
+
value: [box.summary.lowerWhisker, box.summary.firstQuartile, box.summary.median, box.summary.thirdQuartile, box.summary.upperWhisker],
|
|
139
|
+
})),
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
type: "scatter", ...axes, name: series.name, symbolSize: MARKER_SIZE, itemStyle: { color: series.colour },
|
|
143
|
+
data: series.boxes.flatMap((box) => (series.showMarkers ? box.values : box.summary.outliers).map((value) => [box.category, value])),
|
|
144
|
+
},
|
|
145
|
+
];
|
|
146
|
+
case "histogram": {
|
|
147
|
+
const isSingleBin = series.bins.length === 1;
|
|
148
|
+
const gaps = { barGap: HISTOGRAM_BAR_GAP, barCategoryGap: isSingleBin ? SINGLE_BIN_BAND_GAP : HISTOGRAM_BAR_GAP };
|
|
149
|
+
const stack = isSingleBin ? { stack: `panel-${options.panelIndex}-histogram-${series.name}` } : {};
|
|
150
|
+
const bins = {
|
|
151
|
+
type: "bar", ...axes, name: series.name, ...gaps, ...stack,
|
|
152
|
+
// Named by its range, so the tooltip reads "3–12" rather than the bin's centre.
|
|
153
|
+
data: series.bins.map((bin) => ({ name: `${formatBinEdge(bin.start)}–${formatBinEdge(bin.end)}`, value: [(bin.start + bin.end) / 2, bin.height, bin.start, bin.end] })),
|
|
154
|
+
encode: { x: HISTOGRAM_X_DIMENSIONS, y: HISTOGRAM_HEIGHT_DIMENSION, tooltip: HISTOGRAM_HEIGHT_DIMENSION },
|
|
155
|
+
itemStyle: { color: series.colour, borderColor: ink.surface, borderWidth: FILL_GAP_PIXELS / 2, borderRadius: [BAR_CORNER_RADIUS_PIXELS, BAR_CORNER_RADIUS_PIXELS, 0, 0] },
|
|
156
|
+
};
|
|
157
|
+
if (!isSingleBin)
|
|
158
|
+
return [bins];
|
|
159
|
+
const edges = {
|
|
160
|
+
type: "bar", ...axes, name: series.name, ...gaps, ...stack, silent: true, tooltip: { show: false },
|
|
161
|
+
data: series.bins.flatMap((bin) => [[bin.start, null], [bin.end, null]]),
|
|
162
|
+
};
|
|
163
|
+
return [bins, edges];
|
|
164
|
+
}
|
|
165
|
+
case "heatmap":
|
|
166
|
+
// Colour follows the colour value; the tooltip shows the real one.
|
|
167
|
+
return [{
|
|
168
|
+
type: "heatmap", ...axes, name: series.name, encode: { x: 0, y: 1, tooltip: HEATMAP_REAL_VALUE_DIMENSION },
|
|
169
|
+
data: series.cells.map((cell) => [cell.x, cell.y, cell.colourValue, cell.value]),
|
|
170
|
+
}];
|
|
171
|
+
case "pie":
|
|
172
|
+
return [];
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* A line coloured by a number. ECharts' visual map shades a line's stroke only
|
|
177
|
+
* along its x or y, so the stroke is drawn as `groupLineSegmentsByColour`'s
|
|
178
|
+
* segments, one silent line series per colour (gaps split by a missing y), as
|
|
179
|
+
* Plotly draws it. One scatter series carries the markers, coloured by the
|
|
180
|
+
* panel's colour scale, and the tooltip; its markers show only when the
|
|
181
|
+
* series asks for them.
|
|
182
|
+
*
|
|
183
|
+
* @param series - The line, coloured by a number.
|
|
184
|
+
* @param options - Its axes' index, and the panel's colour scale.
|
|
185
|
+
* @returns The segment series, and the markers series for the colour scale to drive.
|
|
186
|
+
*/
|
|
187
|
+
function buildColouredLineSeries(series, options) {
|
|
188
|
+
const axes = { xAxisIndex: options.index, yAxisIndex: options.index };
|
|
189
|
+
const colourRange = { minimum: options.colourScale.minimum, maximum: options.colourScale.maximum };
|
|
190
|
+
const segments = groupLineSegmentsByColour(series.points, { colourRange, colours: options.colourScale.colours }).map(({ colour, segments: pairs }) => ({
|
|
191
|
+
type: "line", ...axes, name: series.name, silent: true, tooltip: { show: false }, showSymbol: false, emphasis: { disabled: true },
|
|
192
|
+
data: pairs.flatMap(({ start, end }) => [[start.x, start.y], [end.x, end.y], [end.x, null]]),
|
|
193
|
+
lineStyle: { width: LINE_WIDTH, type: convertDash(series.dash), color: colour },
|
|
194
|
+
itemStyle: { color: colour },
|
|
195
|
+
}));
|
|
196
|
+
const markers = {
|
|
197
|
+
type: "scatter", ...axes, name: series.name, symbolSize: series.showMarkers ? MARKER_SIZE : 0,
|
|
198
|
+
data: series.points.map((point) => [point.x, point.y, point.colourValue]),
|
|
199
|
+
};
|
|
200
|
+
return { segments, markers };
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Keys the ECharts option carries for the page that draws it, beyond ECharts'
|
|
204
|
+
* own: the maps to register before drawing (name → GeoJSON, or "world" for
|
|
205
|
+
* the world outline the page loads), a notice for the person, and on an axis
|
|
206
|
+
* label, that its numbers are years to print in full or by two digits. The
|
|
207
|
+
* page acts on each and removes it before handing the option to ECharts.
|
|
208
|
+
*/
|
|
209
|
+
export const MAPS_OPTION_KEY = "plotillaMaps";
|
|
210
|
+
/** The option key holding a notice the page shows the person, then removes. */
|
|
211
|
+
export const NOTICE_OPTION_KEY = "plotillaNotice";
|
|
212
|
+
/** The axis-label key telling the page to print each number as it is, a year in full (1990, not 1,990), then removed. */
|
|
213
|
+
export const PLAIN_NUMBERS_LABEL_KEY = "plotillaPlainNumbers";
|
|
214
|
+
/** The axis-label key telling the page to print each number by its last two digits, a year (1990 as 90, 2005 as 05), then removed. */
|
|
215
|
+
export const TWO_DIGIT_YEARS_LABEL_KEY = "plotillaTwoDigitYears";
|
|
216
|
+
/**
|
|
217
|
+
* What each label form adds to an axis's label. ECharts already prints plain
|
|
218
|
+
* numbers with thousands commas, decimals kept and never shortened. Years
|
|
219
|
+
* need a formatter, and a function cannot travel in the option, so a key
|
|
220
|
+
* asks the page to swap one in: a string template would not do, since
|
|
221
|
+
* ECharts applies its thousands separator even to those.
|
|
222
|
+
*/
|
|
223
|
+
const LABEL_FORM_AXIS_LABELS = {
|
|
224
|
+
number: {},
|
|
225
|
+
YYYY: { [PLAIN_NUMBERS_LABEL_KEY]: true },
|
|
226
|
+
YY: { [TWO_DIGIT_YEARS_LABEL_KEY]: true },
|
|
227
|
+
month: { formatter: "{yyyy}.{MM}" },
|
|
228
|
+
day: { formatter: "{yyyy}.{MM}.{dd}" },
|
|
229
|
+
minute: { formatter: "{yyyy}.{MM}.{dd} {HH}:{mm}" },
|
|
230
|
+
second: { formatter: "{yyyy}.{MM}.{dd} {HH}:{mm}:{ss}" },
|
|
231
|
+
category: {},
|
|
232
|
+
};
|
|
233
|
+
/**
|
|
234
|
+
* What each label form adds to the axis itself: years tick only on whole
|
|
235
|
+
* ones, so no label repeats. Dates are ticked where `listDateTickMilliseconds`
|
|
236
|
+
* puts them instead (see `findDateTicks`).
|
|
237
|
+
*/
|
|
238
|
+
const LABEL_FORM_AXES = {
|
|
239
|
+
number: {},
|
|
240
|
+
YYYY: { minInterval: 1 },
|
|
241
|
+
YY: { minInterval: 1 },
|
|
242
|
+
month: {},
|
|
243
|
+
day: {},
|
|
244
|
+
minute: {},
|
|
245
|
+
second: {},
|
|
246
|
+
category: {},
|
|
247
|
+
};
|
|
248
|
+
/**
|
|
249
|
+
* Find where a date axis ticks: the ticks Plotly gets, so both renderers
|
|
250
|
+
* print the same labels. Left to itself ECharts ticks more finely than the
|
|
251
|
+
* label prints (repeating a label) and adds unround labels at the ends.
|
|
252
|
+
*
|
|
253
|
+
* @param axis - The axis.
|
|
254
|
+
* @returns The ticks in milliseconds, its wall clock as UTC; null for an axis that is not a date axis or has no dates.
|
|
255
|
+
*/
|
|
256
|
+
function findDateTicks(axis) {
|
|
257
|
+
switch (axis.labelForm) {
|
|
258
|
+
case "month":
|
|
259
|
+
case "day":
|
|
260
|
+
case "minute":
|
|
261
|
+
case "second":
|
|
262
|
+
return axis.numberRange === null ? null : listDateTickMilliseconds(axis.numberRange, { form: axis.labelForm });
|
|
263
|
+
case "number":
|
|
264
|
+
case "YYYY":
|
|
265
|
+
case "YY":
|
|
266
|
+
case "category":
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Place a series on a time axis by its wall clock read as UTC, as the model
|
|
272
|
+
* reads it, rather than letting ECharts read the text: ECharts would turn
|
|
273
|
+
* a written offset, or the machine's zone, into a different instant.
|
|
274
|
+
* The option's `useUTC` then prints that same wall clock.
|
|
275
|
+
*
|
|
276
|
+
* @param series - The series, its x values as the column wrote them.
|
|
277
|
+
* @returns The series, its date x values in milliseconds.
|
|
278
|
+
*/
|
|
279
|
+
function convertTimeSeries(series) {
|
|
280
|
+
const convert = (x) => (typeof x === "string" ? convertDateTimeToMilliseconds(readDateTime(x)) : x);
|
|
281
|
+
switch (series.mark) {
|
|
282
|
+
case "line":
|
|
283
|
+
return { ...series, points: series.points.map((point) => ({ ...point, x: convert(point.x) })) };
|
|
284
|
+
case "scatter":
|
|
285
|
+
return { ...series, points: series.points.map((point) => ({ ...point, x: convert(point.x) })) };
|
|
286
|
+
default:
|
|
287
|
+
return series;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Build the ECharts option for a figure.
|
|
292
|
+
*
|
|
293
|
+
* @param model - The chart model.
|
|
294
|
+
* @param options - The size the host draws the figure at, in pixels; build again when it changes.
|
|
295
|
+
* @returns The option object.
|
|
296
|
+
*/
|
|
297
|
+
export function buildEChartsOption(model, options) {
|
|
298
|
+
const ink = PLOT_THEMES[model.theme];
|
|
299
|
+
const { size } = options;
|
|
300
|
+
const rectangles = computePanelRectangles(model, { size });
|
|
301
|
+
const grids = [];
|
|
302
|
+
const xAxes = [];
|
|
303
|
+
const yAxes = [];
|
|
304
|
+
const allSeries = [];
|
|
305
|
+
const titles = model.title === null ? [] : [{ text: model.title, left: "center", top: 0, textStyle: { color: ink.textPrimary } }];
|
|
306
|
+
const visualMaps = [];
|
|
307
|
+
const geos = [];
|
|
308
|
+
let maps = {};
|
|
309
|
+
const notices = [];
|
|
310
|
+
let axisIndex = 0;
|
|
311
|
+
model.panels.forEach((panel, panelIndex) => {
|
|
312
|
+
const rectangle = rectangles[panelIndex];
|
|
313
|
+
if (rectangle === undefined)
|
|
314
|
+
throw new Error(`Expected a rectangle for panel ${panelIndex}; got none.`);
|
|
315
|
+
if (panel.title !== null) {
|
|
316
|
+
titles.push({ text: panel.title, left: percent(rectangle.left), top: percent(rectangle.top - PANEL_TITLE_OFFSET_PIXELS / size.height), textStyle: { color: ink.textPrimary, fontSize: PANEL_TITLE_FONT_PIXELS } });
|
|
317
|
+
}
|
|
318
|
+
if (panel.map !== undefined) {
|
|
319
|
+
const parts = buildEChartsMap({ ...panel, map: panel.map }, { panelIndex, geoIndex: geos.length, rectangle, size, theme: model.theme });
|
|
320
|
+
const offset = allSeries.length;
|
|
321
|
+
geos.push(parts.geo);
|
|
322
|
+
allSeries.push(...parts.series);
|
|
323
|
+
maps = { ...maps, ...parts.maps };
|
|
324
|
+
if (parts.notice !== null)
|
|
325
|
+
notices.push(parts.notice);
|
|
326
|
+
// Heat runs from nothing to the heaviest place, apart from any colour scale the panel shows.
|
|
327
|
+
for (const density of parts.densitySeries) {
|
|
328
|
+
visualMaps.push({
|
|
329
|
+
type: "continuous", show: false, min: 0, max: density.maximumWeight, dimension: DENSITY_WEIGHT_DIMENSION,
|
|
330
|
+
seriesIndex: [density.index + offset], inRange: { color: [DENSITY_EMPTY_COLOUR, ...SEQUENTIAL_COLOURS] },
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
if (panel.colourScale !== null && parts.scaledSeries.length > 0) {
|
|
334
|
+
visualMaps.push({
|
|
335
|
+
type: "continuous", min: panel.colourScale.minimum, max: panel.colourScale.maximum,
|
|
336
|
+
seriesIndex: parts.scaledSeries.map((index) => index + offset),
|
|
337
|
+
// Real values at the ends, even when colour follows a log or rank.
|
|
338
|
+
inRange: { color: [...panel.colourScale.colours] },
|
|
339
|
+
text: buildColourLegendText(panel.colourScale),
|
|
340
|
+
textStyle: { color: ink.textSecondary }, calculable: false,
|
|
341
|
+
// In the room beside the map that the panel layout keeps for the colour scale, as for a chart's:
|
|
342
|
+
// a bar drawn over the geo catches the hovers of the land under it.
|
|
343
|
+
orient: "vertical", right: percent(1 - rectangle.left - rectangle.width - COLOUR_SCALE_OFFSET_PIXELS / size.width), top: percent(rectangle.top), itemHeight: COLOUR_SCALE_HEIGHT_PIXELS,
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
const pie = panel.series.find((series) => series.mark === "pie");
|
|
349
|
+
if (pie !== undefined && pie.mark === "pie") {
|
|
350
|
+
const radiusPixels = findPieRadiusPixels(pie.slices, { rectangle, size });
|
|
351
|
+
const shared = {
|
|
352
|
+
type: "pie", name: pie.name,
|
|
353
|
+
itemStyle: { borderColor: ink.surface, borderWidth: FILL_GAP_PIXELS },
|
|
354
|
+
data: pie.slices.map((slice) => ({ name: slice.label, value: slice.value, itemStyle: { color: slice.colour } })),
|
|
355
|
+
};
|
|
356
|
+
if (radiusPixels === undefined) {
|
|
357
|
+
// Its labels cannot all fit whole inside its panel: trimming them to the panel would cut names that the whole
|
|
358
|
+
// chart leaves room for, so the pie keeps the percentage radius and the whole chart as its box.
|
|
359
|
+
const radius = Math.min(rectangle.width, rectangle.height) / 2;
|
|
360
|
+
allSeries.push({
|
|
361
|
+
...shared,
|
|
362
|
+
center: [percent(rectangle.left + rectangle.width / 2), percent(rectangle.top + rectangle.height / 2)],
|
|
363
|
+
radius: [percent(radius * pie.hole), percent(radius)],
|
|
364
|
+
label: { color: ink.textSecondary },
|
|
365
|
+
});
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
allSeries.push({
|
|
369
|
+
...shared,
|
|
370
|
+
// The panel is the pie's box: ECharts slides and trims its labels to stay inside it, so they never reach a neighbouring panel.
|
|
371
|
+
left: percent(rectangle.left), top: percent(rectangle.top), width: percent(rectangle.width), height: percent(rectangle.height),
|
|
372
|
+
center: [PIE_CENTRE_IN_BOX, PIE_CENTRE_IN_BOX],
|
|
373
|
+
radius: [radiusPixels * pie.hole, radiusPixels],
|
|
374
|
+
label: { ...PIE_LABEL_LAYOUT.label, color: ink.textSecondary }, labelLine: PIE_LABEL_LAYOUT.labelLine,
|
|
375
|
+
});
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
grids.push({ left: percent(rectangle.left), top: percent(rectangle.top), width: percent(rectangle.width), height: percent(rectangle.height) });
|
|
379
|
+
if (panel.xAxis !== null) {
|
|
380
|
+
const xAxis = buildAxis(panel.xAxis, { theme: model.theme, gridIndex: axisIndex, direction: "x" });
|
|
381
|
+
// A bin is a range, not a point: ECharts snaps a value axis's pointer to the
|
|
382
|
+
// nearest bar's x (the bin's centre) and hands that value to linked axes, so
|
|
383
|
+
// the pointer follows the mouse instead. The tooltip still shows the bin under it.
|
|
384
|
+
const hasHistogram = panel.series.some((series) => series.mark === "histogram");
|
|
385
|
+
xAxes.push(hasHistogram ? { ...xAxis, axisPointer: { snap: false } } : xAxis);
|
|
386
|
+
}
|
|
387
|
+
if (panel.yAxis !== null)
|
|
388
|
+
yAxes.push(buildAxis(panel.yAxis, { theme: model.theme, gridIndex: axisIndex, direction: "y" }));
|
|
389
|
+
const coloured = [];
|
|
390
|
+
const isTimeX = panel.xAxis !== null && panel.xAxis.kind === "time";
|
|
391
|
+
for (const modelSeries of panel.series) {
|
|
392
|
+
const series = isTimeX ? convertTimeSeries(modelSeries) : modelSeries;
|
|
393
|
+
if (series.mark === "line" && series.colour === null && panel.colourScale !== null) {
|
|
394
|
+
const { segments, markers } = buildColouredLineSeries(series, { index: axisIndex, colourScale: panel.colourScale });
|
|
395
|
+
allSeries.push(...segments);
|
|
396
|
+
coloured.push(allSeries.length);
|
|
397
|
+
allSeries.push(markers);
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
const built = buildSeries(series, { theme: model.theme, index: axisIndex, panelIndex });
|
|
401
|
+
// The colour scale drives only series coloured by a number (and heatmaps), never a trend line beside them.
|
|
402
|
+
const isScaled = series.mark === "heatmap" || ("colour" in series && series.colour === null);
|
|
403
|
+
if (isScaled)
|
|
404
|
+
coloured.push(...built.map((_, offset) => allSeries.length + offset));
|
|
405
|
+
allSeries.push(...built);
|
|
406
|
+
}
|
|
407
|
+
if (panel.colourScale !== null) {
|
|
408
|
+
visualMaps.push({
|
|
409
|
+
type: "continuous", min: panel.colourScale.minimum, max: panel.colourScale.maximum, dimension: COLOUR_VALUE_DIMENSION,
|
|
410
|
+
// Handles would show transformed numbers on a log or rank scale, so only a linear scale gets them.
|
|
411
|
+
seriesIndex: coloured, calculable: panel.colourScale.ticks === undefined,
|
|
412
|
+
inRange: { color: [...panel.colourScale.colours] }, text: buildColourLegendText(panel.colourScale), textStyle: { color: ink.textSecondary },
|
|
413
|
+
orient: "vertical", right: percent(1 - rectangle.left - rectangle.width - COLOUR_SCALE_OFFSET_PIXELS / size.width), top: percent(rectangle.top), itemHeight: COLOUR_SCALE_HEIGHT_PIXELS,
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
axisIndex += 1;
|
|
417
|
+
});
|
|
418
|
+
const hasLines = model.panels.some((panel) => panel.series.some((series) => series.mark === "line"));
|
|
419
|
+
const hasItemOnly = model.panels.some((panel) => panel.series.some((series) => series.mark === "pie" || series.mark === "heatmap" || series.mark === "box"));
|
|
420
|
+
return {
|
|
421
|
+
// Dates are placed by their wall clock read as UTC, so they print in UTC too, whatever the machine's zone.
|
|
422
|
+
useUTC: true,
|
|
423
|
+
backgroundColor: ink.surface,
|
|
424
|
+
textStyle: { color: ink.textPrimary },
|
|
425
|
+
title: titles,
|
|
426
|
+
legend: model.legendNames.length >= MINIMUM_SERIES_FOR_LEGEND
|
|
427
|
+
? { data: model.legendNames, top: model.title === null ? 0 : LEGEND_BELOW_TITLE_PIXELS, textStyle: { color: ink.textSecondary } }
|
|
428
|
+
: { show: false },
|
|
429
|
+
tooltip: hasLines && !hasItemOnly ? { trigger: "axis", axisPointer: { type: "cross" } } : { trigger: "item" },
|
|
430
|
+
...(model.isXAxisShared ? { axisPointer: { link: [{ xAxisIndex: "all" }] } } : {}),
|
|
431
|
+
// Shared axes zoom together: one inside zoom drives every x axis, or every y axis.
|
|
432
|
+
...(model.isXAxisShared || model.isYAxisShared ? {
|
|
433
|
+
dataZoom: [
|
|
434
|
+
...(model.isXAxisShared ? [{ type: "inside", xAxisIndex: xAxes.map((_, index) => index), filterMode: ZOOM_FILTER_MODE }] : []),
|
|
435
|
+
...(model.isYAxisShared ? [{ type: "inside", yAxisIndex: yAxes.map((_, index) => index) }] : []),
|
|
436
|
+
],
|
|
437
|
+
} : {}),
|
|
438
|
+
grid: grids,
|
|
439
|
+
xAxis: xAxes,
|
|
440
|
+
yAxis: yAxes,
|
|
441
|
+
visualMap: visualMaps,
|
|
442
|
+
...(geos.length === 0 ? {} : { geo: geos }),
|
|
443
|
+
series: allSeries,
|
|
444
|
+
// For the page, which cannot receive functions or register maps through JSON:
|
|
445
|
+
// outlines to register before drawing, and anything to tell the person.
|
|
446
|
+
...(Object.keys(maps).length === 0 ? {} : { [MAPS_OPTION_KEY]: maps }),
|
|
447
|
+
...(notices.length === 0 ? {} : { [NOTICE_OPTION_KEY]: notices.join(" ") }),
|
|
448
|
+
};
|
|
449
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How big an ECharts pie is drawn: as large as its panel allows while every
|
|
3
|
+
* slice's outside label stays whole inside the panel, and never below a floor:
|
|
4
|
+
* the percentage radius the pie had before it was given its panel as a box. A
|
|
5
|
+
* pie whose labels do not fit even at the floor is drawn as it was before,
|
|
6
|
+
* with its labels free to spread across the whole chart. ECharts does not
|
|
7
|
+
* shrink a pie for its labels; it shrinks the labels instead, to nothing when
|
|
8
|
+
* there is no room, so the pie must leave the room itself.
|
|
9
|
+
*
|
|
10
|
+
* The measure follows ECharts 6.1's pie label layout
|
|
11
|
+
* (`chart/pie/labelLayout.js`): a label sits at the end of a line that runs
|
|
12
|
+
* `length` out from its slice's middle and then `length2` sideways, plus a
|
|
13
|
+
* gap. When labels on one side overlap, or reach past the top or bottom of
|
|
14
|
+
* the pie's box, ECharts slides them apart vertically and then moves them
|
|
15
|
+
* along an ellipse through the label lines' ends, which can carry a label
|
|
16
|
+
* sideways past where its own slice would put it.
|
|
17
|
+
*/
|
|
18
|
+
import { type FigureSize, type PanelRectangle } from "../model/panel_layout.ts";
|
|
19
|
+
/** The label and label line settings `findPieRadiusPixels` assumes, for the pie series. */
|
|
20
|
+
export declare const PIE_LABEL_LAYOUT: {
|
|
21
|
+
label: {
|
|
22
|
+
fontSize: number;
|
|
23
|
+
lineHeight: number;
|
|
24
|
+
distanceToLabelLine: number;
|
|
25
|
+
bleedMargin: number;
|
|
26
|
+
};
|
|
27
|
+
labelLine: {
|
|
28
|
+
length: number;
|
|
29
|
+
length2: number;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Find the largest outer radius at which a pie fills its panel and every
|
|
34
|
+
* label still fits whole inside the panel, no smaller than
|
|
35
|
+
* `measureSmallestPieRadiusPixels`.
|
|
36
|
+
*
|
|
37
|
+
* Each label is checked at its own slice's middle angle. Only a side whose
|
|
38
|
+
* labels ECharts slides apart is checked at the farthest a slid label can land.
|
|
39
|
+
*
|
|
40
|
+
* @param slices - The slices, in drawing order: each one's label and value.
|
|
41
|
+
* @param options - The panel's rectangle, as fractions of the figure, and the figure's size in pixels.
|
|
42
|
+
* @returns The outer radius in pixels, or `undefined` when the labels cannot all fit inside the panel even at the
|
|
43
|
+
* smallest radius. The caller then draws the pie at that percentage radius with the whole chart as its box, because
|
|
44
|
+
* trimming the labels to the panel would cut names that the whole chart leaves room for.
|
|
45
|
+
*/
|
|
46
|
+
export declare function findPieRadiusPixels(slices: readonly {
|
|
47
|
+
label: string;
|
|
48
|
+
value: number;
|
|
49
|
+
}[], options: {
|
|
50
|
+
rectangle: PanelRectangle;
|
|
51
|
+
size: FigureSize;
|
|
52
|
+
}): number | undefined;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How big an ECharts pie is drawn: as large as its panel allows while every
|
|
3
|
+
* slice's outside label stays whole inside the panel, and never below a floor:
|
|
4
|
+
* the percentage radius the pie had before it was given its panel as a box. A
|
|
5
|
+
* pie whose labels do not fit even at the floor is drawn as it was before,
|
|
6
|
+
* with its labels free to spread across the whole chart. ECharts does not
|
|
7
|
+
* shrink a pie for its labels; it shrinks the labels instead, to nothing when
|
|
8
|
+
* there is no room, so the pie must leave the room itself.
|
|
9
|
+
*
|
|
10
|
+
* The measure follows ECharts 6.1's pie label layout
|
|
11
|
+
* (`chart/pie/labelLayout.js`): a label sits at the end of a line that runs
|
|
12
|
+
* `length` out from its slice's middle and then `length2` sideways, plus a
|
|
13
|
+
* gap. When labels on one side overlap, or reach past the top or bottom of
|
|
14
|
+
* the pie's box, ECharts slides them apart vertically and then moves them
|
|
15
|
+
* along an ellipse through the label lines' ends, which can carry a label
|
|
16
|
+
* sideways past where its own slice would put it.
|
|
17
|
+
*/
|
|
18
|
+
import { AXIS_LABEL_CHARACTER_PIXELS } from "../model/panel_layout.js";
|
|
19
|
+
/** ECharts' default label font; the character width estimate is for fonts of about this size. */
|
|
20
|
+
const PIE_LABEL_FONT_PIXELS = 12;
|
|
21
|
+
const PIE_LABEL_LINE_HEIGHT_PIXELS = 16;
|
|
22
|
+
/** ECharts measures a label's box with one pixel above and below its text. */
|
|
23
|
+
const PIE_LABEL_VERTICAL_MARGIN_PIXELS = 1;
|
|
24
|
+
/** The label line's first leg, out from the slice, and its second, flat leg. */
|
|
25
|
+
const PIE_LABEL_LINE_FIRST_PIXELS = 15;
|
|
26
|
+
const PIE_LABEL_LINE_SECOND_PIXELS = 30;
|
|
27
|
+
/** Gap between the label line's end and the label. */
|
|
28
|
+
const PIE_LABEL_DISTANCE_PIXELS = 5;
|
|
29
|
+
/**
|
|
30
|
+
* Space ECharts keeps between a label and the edge of the pie's box (its
|
|
31
|
+
* panel). ECharts' own default for a box this size is 10; the figure's edge
|
|
32
|
+
* padding and the gaps between panels already keep labels clear of whatever
|
|
33
|
+
* lies beyond, so the pie keeps those pixels instead.
|
|
34
|
+
*/
|
|
35
|
+
const PIE_LABEL_BLEED_MARGIN_PIXELS = 2;
|
|
36
|
+
/** ECharts starts the first slice at 12 o'clock and goes clockwise. */
|
|
37
|
+
const PIE_START_ANGLE_DEGREES = 90;
|
|
38
|
+
const FULL_TURN_DEGREES = 360;
|
|
39
|
+
const DEGREES_TO_RADIANS = Math.PI / 180;
|
|
40
|
+
/** How far the radius steps down while searching for one at which every label fits. */
|
|
41
|
+
const RADIUS_STEP_PIXELS = 1;
|
|
42
|
+
/** Half a label's box height, as ECharts measures it. */
|
|
43
|
+
const PIE_LABEL_HALF_HEIGHT_PIXELS = PIE_LABEL_LINE_HEIGHT_PIXELS / 2 + PIE_LABEL_VERTICAL_MARGIN_PIXELS;
|
|
44
|
+
/** The label and label line settings `findPieRadiusPixels` assumes, for the pie series. */
|
|
45
|
+
export const PIE_LABEL_LAYOUT = {
|
|
46
|
+
label: {
|
|
47
|
+
fontSize: PIE_LABEL_FONT_PIXELS, lineHeight: PIE_LABEL_LINE_HEIGHT_PIXELS,
|
|
48
|
+
distanceToLabelLine: PIE_LABEL_DISTANCE_PIXELS, bleedMargin: PIE_LABEL_BLEED_MARGIN_PIXELS,
|
|
49
|
+
},
|
|
50
|
+
labelLine: { length: PIE_LABEL_LINE_FIRST_PIXELS, length2: PIE_LABEL_LINE_SECOND_PIXELS },
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Check whether ECharts slides one side's labels apart: when two overlap, or
|
|
54
|
+
* one reaches past the panel's top or bottom.
|
|
55
|
+
*
|
|
56
|
+
* @param labels - That side's labels.
|
|
57
|
+
* @param options - The labels' reach from the centre (radius plus the line's first leg), and the panel's room.
|
|
58
|
+
* @returns Whether ECharts moves them.
|
|
59
|
+
*/
|
|
60
|
+
function checkLabelsSlide(labels, options) {
|
|
61
|
+
// ECharts never slides a lone label (labelLayoutHelper.js, shiftLayoutOnXY).
|
|
62
|
+
if (labels.length < 2)
|
|
63
|
+
return false;
|
|
64
|
+
const middles = labels.map((label) => label.down * options.reach).sort((first, second) => first - second);
|
|
65
|
+
return middles.some((middle, index) => {
|
|
66
|
+
const previous = middles[index - 1];
|
|
67
|
+
const overlaps = previous !== undefined && middle - previous < 2 * PIE_LABEL_HALF_HEIGHT_PIXELS;
|
|
68
|
+
return overlaps || Math.abs(middle) + PIE_LABEL_HALF_HEIGHT_PIXELS > options.room.vertical;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Check that every label on one side fits inside the panel: sideways, and up
|
|
73
|
+
* or down when ECharts leaves the labels where their slices put them.
|
|
74
|
+
*
|
|
75
|
+
* @param labels - That side's labels.
|
|
76
|
+
* @param options - The labels' reach from the centre, and the panel's room.
|
|
77
|
+
* @returns Whether every label fits.
|
|
78
|
+
*/
|
|
79
|
+
function checkSideFits(labels, options) {
|
|
80
|
+
if (labels.length === 0)
|
|
81
|
+
return true;
|
|
82
|
+
const { reach, room } = options;
|
|
83
|
+
// A slid label lands on an ellipse through the label lines' ends, at most
|
|
84
|
+
// the reach sideways, unless ECharts takes the ellipse's vertical axis to be
|
|
85
|
+
// the reach itself (labelLayout.js, rB = rA). It does only when a label's
|
|
86
|
+
// anchor already sits a reach or more sideways: reach·|cos θ| plus the
|
|
87
|
+
// label distance, so a slice within about acos(1 - distance/reach) of
|
|
88
|
+
// horizontal. A label then pushed to dy from the centre lands
|
|
89
|
+
// reach·sqrt(|1 - dy²/reach²|) sideways, past the reach once dy passes √2·reach.
|
|
90
|
+
const isAxisTheReach = labels.some((label) => reach * Math.abs(label.sideways) + PIE_LABEL_DISTANCE_PIXELS >= reach);
|
|
91
|
+
const farthestDown = room.vertical - PIE_LABEL_HALF_HEIGHT_PIXELS;
|
|
92
|
+
const slidReach = isAxisTheReach ? reach * Math.sqrt(Math.max(1, (farthestDown * farthestDown) / (reach * reach) - 1)) : reach;
|
|
93
|
+
const isSlid = checkLabelsSlide(labels, { reach, room });
|
|
94
|
+
// Unslid labels stay where their slices put them, so each must fit up or down too.
|
|
95
|
+
const isWithinHeight = isSlid || labels.every((label) => Math.abs(label.down) * reach + PIE_LABEL_HALF_HEIGHT_PIXELS <= room.vertical);
|
|
96
|
+
if (!isWithinHeight)
|
|
97
|
+
return false;
|
|
98
|
+
const tail = PIE_LABEL_LINE_SECOND_PIXELS + PIE_LABEL_DISTANCE_PIXELS + PIE_LABEL_BLEED_MARGIN_PIXELS;
|
|
99
|
+
return labels.every((label) => (isSlid ? slidReach : reach * Math.abs(label.sideways)) + tail + label.width <= room.sideways);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Measure the smallest radius a pie is drawn at in its panel: a quarter of
|
|
103
|
+
* the panel's smaller fraction of the figure, times the chart's smaller side.
|
|
104
|
+
* That is half the panel's smaller fraction read as a percentage radius, as
|
|
105
|
+
* ECharts reads one (against half the chart's smaller side): the radius every
|
|
106
|
+
* pie had before it was given its panel as a box. It never exceeds half the
|
|
107
|
+
* panel's smaller side.
|
|
108
|
+
*
|
|
109
|
+
* @param options - The panel's rectangle, as fractions of the figure, and the figure's size in pixels.
|
|
110
|
+
* @returns The radius in pixels.
|
|
111
|
+
*/
|
|
112
|
+
function measureSmallestPieRadiusPixels(options) {
|
|
113
|
+
const { rectangle, size } = options;
|
|
114
|
+
return (Math.min(rectangle.width, rectangle.height) / 2) * (Math.min(size.width, size.height) / 2);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Find the largest outer radius at which a pie fills its panel and every
|
|
118
|
+
* label still fits whole inside the panel, no smaller than
|
|
119
|
+
* `measureSmallestPieRadiusPixels`.
|
|
120
|
+
*
|
|
121
|
+
* Each label is checked at its own slice's middle angle. Only a side whose
|
|
122
|
+
* labels ECharts slides apart is checked at the farthest a slid label can land.
|
|
123
|
+
*
|
|
124
|
+
* @param slices - The slices, in drawing order: each one's label and value.
|
|
125
|
+
* @param options - The panel's rectangle, as fractions of the figure, and the figure's size in pixels.
|
|
126
|
+
* @returns The outer radius in pixels, or `undefined` when the labels cannot all fit inside the panel even at the
|
|
127
|
+
* smallest radius. The caller then draws the pie at that percentage radius with the whole chart as its box, because
|
|
128
|
+
* trimming the labels to the panel would cut names that the whole chart leaves room for.
|
|
129
|
+
*/
|
|
130
|
+
export function findPieRadiusPixels(slices, options) {
|
|
131
|
+
const room = {
|
|
132
|
+
sideways: (options.rectangle.width * options.size.width) / 2,
|
|
133
|
+
vertical: (options.rectangle.height * options.size.height) / 2,
|
|
134
|
+
};
|
|
135
|
+
const fillingRadius = Math.min(room.sideways, room.vertical);
|
|
136
|
+
const smallestRadius = measureSmallestPieRadiusPixels(options);
|
|
137
|
+
const total = slices.reduce((sum, slice) => sum + slice.value, 0);
|
|
138
|
+
if (slices.length === 0 || total <= 0)
|
|
139
|
+
return fillingRadius;
|
|
140
|
+
let before = 0;
|
|
141
|
+
const labels = slices.map((slice) => {
|
|
142
|
+
const middle = (PIE_START_ANGLE_DEGREES - (FULL_TURN_DEGREES * (before + slice.value / 2)) / total) * DEGREES_TO_RADIANS;
|
|
143
|
+
before += slice.value;
|
|
144
|
+
return { width: slice.label.length * AXIS_LABEL_CHARACTER_PIXELS, sideways: Math.cos(middle), down: -Math.sin(middle) };
|
|
145
|
+
});
|
|
146
|
+
// ECharts puts a label pointing straight up or down on the right.
|
|
147
|
+
const left = labels.filter((label) => label.sideways < 0);
|
|
148
|
+
const right = labels.filter((label) => label.sideways >= 0);
|
|
149
|
+
const checkFits = (radius) => {
|
|
150
|
+
const reach = radius + PIE_LABEL_LINE_FIRST_PIXELS;
|
|
151
|
+
return checkSideFits(left, { reach, room }) && checkSideFits(right, { reach, room });
|
|
152
|
+
};
|
|
153
|
+
for (let radius = fillingRadius; radius > smallestRadius; radius -= RADIUS_STEP_PIXELS) {
|
|
154
|
+
if (checkFits(radius))
|
|
155
|
+
return radius;
|
|
156
|
+
}
|
|
157
|
+
return checkFits(smallestRadius) ? smallestRadius : undefined;
|
|
158
|
+
}
|