@altnovatrail/picture-kit 0.0.0-stage → 0.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/LICENSE +201 -0
- package/NOTICE +7 -0
- package/README.md +27 -2
- package/lib/emoji.d.ts +16 -0
- package/lib/emoji.js +64 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +10 -0
- package/lib/kind.d.ts +22 -0
- package/lib/kind.js +2 -0
- package/lib/kinds/balance.d.ts +34 -0
- package/lib/kinds/balance.js +217 -0
- package/lib/kinds/branches.d.ts +33 -0
- package/lib/kinds/branches.js +473 -0
- package/lib/kinds/calendar.d.ts +55 -0
- package/lib/kinds/calendar.js +532 -0
- package/lib/kinds/chart.d.ts +48 -0
- package/lib/kinds/chart.js +645 -0
- package/lib/kinds/choices.d.ts +14 -0
- package/lib/kinds/choices.js +53 -0
- package/lib/kinds/clock.d.ts +36 -0
- package/lib/kinds/clock.js +219 -0
- package/lib/kinds/coins.d.ts +37 -0
- package/lib/kinds/coins.js +550 -0
- package/lib/kinds/column.d.ts +37 -0
- package/lib/kinds/column.js +410 -0
- package/lib/kinds/commands-parts.d.ts +12 -0
- package/lib/kinds/commands-parts.js +24 -0
- package/lib/kinds/commands.d.ts +66 -0
- package/lib/kinds/commands.js +789 -0
- package/lib/kinds/containers.d.ts +52 -0
- package/lib/kinds/containers.js +758 -0
- package/lib/kinds/countfig.d.ts +41 -0
- package/lib/kinds/countfig.js +372 -0
- package/lib/kinds/cubes.d.ts +70 -0
- package/lib/kinds/cubes.js +371 -0
- package/lib/kinds/die.d.ts +32 -0
- package/lib/kinds/die.js +301 -0
- package/lib/kinds/domino.d.ts +37 -0
- package/lib/kinds/domino.js +450 -0
- package/lib/kinds/dotgrid.d.ts +38 -0
- package/lib/kinds/dotgrid.js +411 -0
- package/lib/kinds/figure.d.ts +62 -0
- package/lib/kinds/figure.js +687 -0
- package/lib/kinds/fold-parts.d.ts +73 -0
- package/lib/kinds/fold-parts.js +315 -0
- package/lib/kinds/fold.d.ts +52 -0
- package/lib/kinds/fold.js +650 -0
- package/lib/kinds/fraction.d.ts +50 -0
- package/lib/kinds/fraction.js +568 -0
- package/lib/kinds/gauges.d.ts +53 -0
- package/lib/kinds/gauges.js +880 -0
- package/lib/kinds/gears.d.ts +40 -0
- package/lib/kinds/gears.js +787 -0
- package/lib/kinds/grid.d.ts +45 -0
- package/lib/kinds/grid.js +591 -0
- package/lib/kinds/groups.d.ts +23 -0
- package/lib/kinds/groups.js +335 -0
- package/lib/kinds/index.d.ts +3 -0
- package/lib/kinds/index.js +87 -0
- package/lib/kinds/lanes.d.ts +36 -0
- package/lib/kinds/lanes.js +220 -0
- package/lib/kinds/layers.d.ts +31 -0
- package/lib/kinds/layers.js +419 -0
- package/lib/kinds/line.d.ts +38 -0
- package/lib/kinds/line.js +739 -0
- package/lib/kinds/magic-parts.d.ts +72 -0
- package/lib/kinds/magic-parts.js +186 -0
- package/lib/kinds/magic.d.ts +66 -0
- package/lib/kinds/magic.js +664 -0
- package/lib/kinds/map.d.ts +32 -0
- package/lib/kinds/map.js +370 -0
- package/lib/kinds/matchsticks.d.ts +26 -0
- package/lib/kinds/matchsticks.js +434 -0
- package/lib/kinds/necklace.d.ts +27 -0
- package/lib/kinds/necklace.js +342 -0
- package/lib/kinds/net.d.ts +47 -0
- package/lib/kinds/net.js +241 -0
- package/lib/kinds/overlay.d.ts +50 -0
- package/lib/kinds/overlay.js +519 -0
- package/lib/kinds/pieces.d.ts +41 -0
- package/lib/kinds/pieces.js +420 -0
- package/lib/kinds/pipes.d.ts +46 -0
- package/lib/kinds/pipes.js +657 -0
- package/lib/kinds/plane-parts.d.ts +105 -0
- package/lib/kinds/plane-parts.js +372 -0
- package/lib/kinds/plane.d.ts +35 -0
- package/lib/kinds/plane.js +755 -0
- package/lib/kinds/pyramid.d.ts +26 -0
- package/lib/kinds/pyramid.js +228 -0
- package/lib/kinds/ropes.d.ts +30 -0
- package/lib/kinds/ropes.js +571 -0
- package/lib/kinds/shapes.d.ts +59 -0
- package/lib/kinds/shapes.js +490 -0
- package/lib/kinds/solids.d.ts +78 -0
- package/lib/kinds/solids.js +1058 -0
- package/lib/kinds/tangram.d.ts +52 -0
- package/lib/kinds/tangram.js +713 -0
- package/lib/kinds/target.d.ts +39 -0
- package/lib/kinds/target.js +761 -0
- package/lib/kinds/tiles.d.ts +55 -0
- package/lib/kinds/tiles.js +606 -0
- package/lib/kinds/venn.d.ts +38 -0
- package/lib/kinds/venn.js +710 -0
- package/lib/noto.d.ts +2 -0
- package/lib/noto.js +212 -0
- package/lib/objects.d.ts +7 -0
- package/lib/objects.js +226 -0
- package/lib/pen.d.ts +32 -0
- package/lib/pen.js +60 -0
- package/lib/read.d.ts +9 -0
- package/lib/read.js +49 -0
- package/lib/render.d.ts +24 -0
- package/lib/render.js +68 -0
- package/lib/solve/balance.d.ts +46 -0
- package/lib/solve/balance.js +118 -0
- package/lib/style.d.ts +70 -0
- package/lib/style.js +39 -0
- package/lib/svg.d.ts +22 -0
- package/lib/svg.js +48 -0
- package/lib/things.d.ts +40 -0
- package/lib/things.js +269 -0
- package/package.json +28 -4
|
@@ -0,0 +1,645 @@
|
|
|
1
|
+
import { margin } from "../kind.js";
|
|
2
|
+
import { textWidth } from "../pen.js";
|
|
3
|
+
import { int, isObj, list, oneOf, only, text } from "../read.js";
|
|
4
|
+
import { colorNames, colors, faint, ink, isColor, outline, soft, } from "../style.js";
|
|
5
|
+
import { n } from "../svg.js";
|
|
6
|
+
import { checkThing, count, drawThing, wordOf } from "../things.js";
|
|
7
|
+
/**
|
|
8
|
+
* A chart: a bar chart, its bars standing on a line side by side or lying
|
|
9
|
+
* one under another, read against a scale with a line at every step; or a
|
|
10
|
+
* picture chart, where each bar is a column or a row of icons, one icon
|
|
11
|
+
* standing for a unit and half an icon for half of it, with a key that says
|
|
12
|
+
* so. A bar is named by a word, an object or both, and a missing bar is a
|
|
13
|
+
* "?" in its place. A bar ends on a line of the scale or halfway between two,
|
|
14
|
+
* so that its value can be read exactly.
|
|
15
|
+
*/
|
|
16
|
+
export const chartStyles = ["bars", "pictures"];
|
|
17
|
+
export const keyModes = ["show", "ask", "none"];
|
|
18
|
+
/** The most bars of each style, standing up or lying across. */
|
|
19
|
+
const mostBars = {
|
|
20
|
+
bars: { up: 12, across: 10 },
|
|
21
|
+
pictures: { up: 8, across: 8 },
|
|
22
|
+
};
|
|
23
|
+
/** The most icons in a column or in a row of a picture chart. */
|
|
24
|
+
const mostIcons = { up: 8, across: 10 };
|
|
25
|
+
const mostLines = 12;
|
|
26
|
+
const valueMost = 10000;
|
|
27
|
+
const unitMost = 1000;
|
|
28
|
+
const labelMost = 10;
|
|
29
|
+
const titleMost = 30;
|
|
30
|
+
const tickSize = 15;
|
|
31
|
+
const nameSize = 15;
|
|
32
|
+
const titleSize = 18;
|
|
33
|
+
/** The width the whole picture may take, margins included. */
|
|
34
|
+
const widest = 640;
|
|
35
|
+
/** Icons of a picture chart: their size and the distance between two. */
|
|
36
|
+
const iconSize = 36;
|
|
37
|
+
const iconPitch = 42;
|
|
38
|
+
/** Things that name a bar of a picture chart. */
|
|
39
|
+
const nameThing = 30;
|
|
40
|
+
function range(k) {
|
|
41
|
+
return Array.from({ length: k }, (_, i) => i);
|
|
42
|
+
}
|
|
43
|
+
function styleOf(c) {
|
|
44
|
+
return c.style ?? "bars";
|
|
45
|
+
}
|
|
46
|
+
/** scaleOf is the scale of a bar chart: its step, its top and how many lines it has. */
|
|
47
|
+
export function scaleOf(c) {
|
|
48
|
+
const step = c.step ?? 1;
|
|
49
|
+
const top = Math.max(0, ...c.bars.map((b) => b.value ?? 0));
|
|
50
|
+
const max = c.max ?? Math.max(2, Math.ceil(top / step)) * step;
|
|
51
|
+
return { step, max, lines: Math.round(max / step) };
|
|
52
|
+
}
|
|
53
|
+
/** iconsOf is how many whole icons a value makes, and whether half an icon more. */
|
|
54
|
+
export function iconsOf(value, unit) {
|
|
55
|
+
return { whole: Math.floor(value / unit), half: value % unit !== 0 };
|
|
56
|
+
}
|
|
57
|
+
function placesOf(b, unit) {
|
|
58
|
+
if (b.ask)
|
|
59
|
+
return 1;
|
|
60
|
+
const { whole, half } = iconsOf(b.value ?? 0, unit);
|
|
61
|
+
return whole + (half ? 1 : 0);
|
|
62
|
+
}
|
|
63
|
+
function keyOf(c) {
|
|
64
|
+
return c.key ?? ((c.unit ?? 1) > 1 ? "show" : "none");
|
|
65
|
+
}
|
|
66
|
+
// --- sizes, shared by check and draw ------------------------------------------
|
|
67
|
+
/** slotUp is the width of the place of a standing bar: wide for few bars, narrower for many. */
|
|
68
|
+
function slotUp(k) {
|
|
69
|
+
return Math.max(46, Math.min(100, Math.floor(540 / k)));
|
|
70
|
+
}
|
|
71
|
+
/** spacingUp is the distance between two lines of a standing chart's scale. */
|
|
72
|
+
function spacingUp(lines) {
|
|
73
|
+
return Math.max(24, Math.min(56, Math.floor(300 / lines)));
|
|
74
|
+
}
|
|
75
|
+
/** rowAcross is the height of the row of a lying bar. */
|
|
76
|
+
function rowAcross(k) {
|
|
77
|
+
return Math.max(40, Math.min(56, Math.floor(320 / k)));
|
|
78
|
+
}
|
|
79
|
+
function thingAcross(k) {
|
|
80
|
+
return Math.min(32, rowAcross(k) - 10);
|
|
81
|
+
}
|
|
82
|
+
/** namesWidth is the width of the column of names left of lying bars or rows of icons. */
|
|
83
|
+
function namesWidth(bars, thing) {
|
|
84
|
+
const things = bars.some((b) => b.thing !== undefined);
|
|
85
|
+
const words = Math.max(0, ...bars.map((b) => (b.label ? textWidth(b.label, nameSize) : 0)));
|
|
86
|
+
return (things ? thing + (words > 0 ? 8 : 0) : 0) + words;
|
|
87
|
+
}
|
|
88
|
+
/** across is where the value axis of lying bars stands and how far apart its lines are. */
|
|
89
|
+
function across(c) {
|
|
90
|
+
const k = c.bars.length;
|
|
91
|
+
const x0 = namesWidth(c.bars, thingAcross(k)) + 14;
|
|
92
|
+
const room = widest - 2 * margin - x0 - 20;
|
|
93
|
+
const { lines } = scaleOf(c);
|
|
94
|
+
return { x0, spacing: Math.min(60, Math.floor(room / lines)), room };
|
|
95
|
+
}
|
|
96
|
+
/** columnUp is the width of a column of icons, wide enough for its name. */
|
|
97
|
+
function columnUp(c) {
|
|
98
|
+
const words = Math.max(0, ...c.bars.map((b) => (b.label ? textWidth(b.label, nameSize) : 0)));
|
|
99
|
+
return Math.max(62, Math.ceil(words) + 16);
|
|
100
|
+
}
|
|
101
|
+
// --- checking ---------------------------------------------------------------
|
|
102
|
+
function checkPicture(value, path, problems) {
|
|
103
|
+
const before = problems.length;
|
|
104
|
+
checkThing(value, path, problems);
|
|
105
|
+
if (problems.length > before)
|
|
106
|
+
return;
|
|
107
|
+
if (isObj(value) && (value.ask === true || value.empty === true))
|
|
108
|
+
problems.push(`${path}: must be an object or a drawn thing, not ask or empty; a missing bar is "ask": true on the bar`);
|
|
109
|
+
}
|
|
110
|
+
function checkBar(b, path, style, problems) {
|
|
111
|
+
if (!isObj(b)) {
|
|
112
|
+
problems.push(`${path}: must be a bar: a label or a thing, and a value or "ask": true`);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
only(b, ["label", "thing", "value", "ask", "color"], path, problems);
|
|
116
|
+
if (b.label === undefined && b.thing === undefined)
|
|
117
|
+
problems.push(`${path}: give a label, a thing or both, to name the bar`);
|
|
118
|
+
if (b.label !== undefined)
|
|
119
|
+
text(b.label, `${path}.label`, problems, labelMost);
|
|
120
|
+
if (b.thing !== undefined)
|
|
121
|
+
checkPicture(b.thing, `${path}.thing`, problems);
|
|
122
|
+
if (b.ask !== undefined && b.ask !== true)
|
|
123
|
+
problems.push(`${path}.ask: must be true, or left out`);
|
|
124
|
+
if (b.ask === true) {
|
|
125
|
+
if (b.value !== undefined)
|
|
126
|
+
problems.push(`${path}.value: a missing bar shows no value; leave value out`);
|
|
127
|
+
}
|
|
128
|
+
else if (b.value === undefined)
|
|
129
|
+
problems.push(`${path}.value: give the bar's value, a whole number, or "ask": true`);
|
|
130
|
+
else
|
|
131
|
+
int(b.value, `${path}.value`, problems, 0, valueMost);
|
|
132
|
+
if (b.color !== undefined) {
|
|
133
|
+
if (style !== "bars")
|
|
134
|
+
problems.push(`${path}.color: only a chart of style "bars" has colours`);
|
|
135
|
+
else if (!isColor(b.color))
|
|
136
|
+
problems.push(`${path}.color: must be a colour: ${colorNames.join(", ")}`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
function checkBars(c, scene, problems) {
|
|
140
|
+
const k = c.bars.length;
|
|
141
|
+
const before = problems.length;
|
|
142
|
+
const step = scene.step === undefined ? 1 : int(scene.step, "step", problems, 1, valueMost);
|
|
143
|
+
if (problems.length > before)
|
|
144
|
+
return;
|
|
145
|
+
const shown = c.bars.filter((b) => !b.ask).map((b) => b.value);
|
|
146
|
+
const top = Math.max(0, ...shown);
|
|
147
|
+
let max;
|
|
148
|
+
if (scene.max !== undefined) {
|
|
149
|
+
max = int(scene.max, "max", problems, 1, valueMost);
|
|
150
|
+
if (problems.length > before)
|
|
151
|
+
return;
|
|
152
|
+
if (max % step !== 0) {
|
|
153
|
+
problems.push(`max: must be a multiple of the step, ${step}, so that the top is a line`);
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const lines = max / step;
|
|
157
|
+
if (lines < 2)
|
|
158
|
+
problems.push(`max: the scale needs at least 2 lines: max at least ${2 * step}`);
|
|
159
|
+
if (lines > mostLines)
|
|
160
|
+
problems.push(`max: ${max} with step ${step} makes ${lines} lines; at most ${mostLines}: raise step or lower max`);
|
|
161
|
+
c.bars.forEach((b, i) => {
|
|
162
|
+
if (!b.ask && b.value > max)
|
|
163
|
+
problems.push(`bars[${i}].value: ${b.value} is above the top of the scale, ${max}`);
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
const lines = Math.max(2, Math.ceil(top / step));
|
|
168
|
+
if (lines > mostLines) {
|
|
169
|
+
problems.push(`step: the scale up to ${top} needs ${lines} lines with step ${step}; at most ${mostLines}: use a step of at least ${Math.ceil(top / mostLines)}`);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
max = lines * step;
|
|
173
|
+
}
|
|
174
|
+
// A bar ends on a line, or halfway between two when the half of a step is whole.
|
|
175
|
+
const grain = step % 2 === 0 ? step / 2 : step;
|
|
176
|
+
c.bars.forEach((b, i) => {
|
|
177
|
+
if (!b.ask && b.value % grain !== 0)
|
|
178
|
+
problems.push(`bars[${i}].value: ${b.value} must be a multiple of ${grain}, so that the bar ends on a line of the scale${grain < step ? " or halfway between two" : ""}`);
|
|
179
|
+
});
|
|
180
|
+
if (problems.length > before)
|
|
181
|
+
return;
|
|
182
|
+
if (c.horizontal === true) {
|
|
183
|
+
const { spacing, room } = across(c);
|
|
184
|
+
const need = Math.ceil(textWidth(String(max), tickSize)) + 10;
|
|
185
|
+
if (spacing < need)
|
|
186
|
+
problems.push(`max: the numbers of the scale do not fit along the bottom; use at most ${Math.max(2, Math.floor(room / need))} lines: a larger step`);
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
const slot = slotUp(k);
|
|
190
|
+
c.bars.forEach((b, i) => {
|
|
191
|
+
if (b.label && textWidth(b.label, nameSize) > slot - 6)
|
|
192
|
+
problems.push(`bars[${i}].label: "${b.label}" is too wide under a bar when there are ${k} bars; keep it to about ${Math.floor((slot - 6) / 9)} letters, or use fewer bars`);
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
function checkPictures(c, scene, problems) {
|
|
196
|
+
const before = problems.length;
|
|
197
|
+
const unit = scene.unit === undefined ? 1 : int(scene.unit, "unit", problems, 1, unitMost);
|
|
198
|
+
if (scene.icon !== undefined)
|
|
199
|
+
checkPicture(scene.icon, "icon", problems);
|
|
200
|
+
const key = scene.key === undefined ? undefined : oneOf(scene.key, keyModes, "key", problems);
|
|
201
|
+
if (problems.length > before)
|
|
202
|
+
return;
|
|
203
|
+
if (scene.icon === undefined) {
|
|
204
|
+
if (unit > 1)
|
|
205
|
+
problems.push(`icon: with a unit of ${unit} every bar shows one icon, the one in the key: give icon`);
|
|
206
|
+
else if (key === "show" || key === "ask")
|
|
207
|
+
problems.push("icon: the key shows the icon of every bar: give icon, or leave key out");
|
|
208
|
+
else
|
|
209
|
+
c.bars.forEach((b, i) => {
|
|
210
|
+
if (b.thing === undefined)
|
|
211
|
+
problems.push(`bars[${i}].thing: without an icon a bar is drawn with its own thing: give thing, or give icon`);
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
const grain = unit % 2 === 0 ? unit / 2 : unit;
|
|
215
|
+
const way = c.horizontal === true ? "across" : "up";
|
|
216
|
+
c.bars.forEach((b, i) => {
|
|
217
|
+
if (b.ask)
|
|
218
|
+
return;
|
|
219
|
+
const v = b.value;
|
|
220
|
+
if (v % grain !== 0) {
|
|
221
|
+
problems.push(`bars[${i}].value: ${v} is not a whole number of icons${grain < unit ? " or a half more" : ""}; with a unit of ${unit} it must be a multiple of ${grain}`);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const places = placesOf(b, unit);
|
|
225
|
+
if (places > mostIcons[way])
|
|
226
|
+
problems.push(`bars[${i}].value: ${v} needs ${places} icons; at most ${mostIcons[way]} fit in a ${way === "up" ? "column" : "row"}: use a larger unit`);
|
|
227
|
+
});
|
|
228
|
+
if (problems.length > before || way === "across")
|
|
229
|
+
return;
|
|
230
|
+
const k = c.bars.length;
|
|
231
|
+
const most = Math.floor((widest - 2 * margin) / k);
|
|
232
|
+
c.bars.forEach((b, i) => {
|
|
233
|
+
if (b.label && textWidth(b.label, nameSize) + 16 > most)
|
|
234
|
+
problems.push(`bars[${i}].label: "${b.label}" is too wide for a column when there are ${k}; keep it to about ${Math.floor((most - 16) / 9)} letters, or use fewer bars`);
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
// --- drawing ------------------------------------------------------------------
|
|
238
|
+
/** halfIcon draws the left half of an icon, cut along its middle. */
|
|
239
|
+
function halfIcon(pen, icon, cx, cy, size) {
|
|
240
|
+
const inner = pen.sub();
|
|
241
|
+
drawThing(inner, icon, size / 2, size / 2, size);
|
|
242
|
+
pen.add(`<svg class="half" x="${n(cx - size / 2)}" y="${n(cy - size / 2)}" width="${n(size / 2)}" height="${n(size)}" overflow="hidden">${inner.markup()}</svg>`);
|
|
243
|
+
}
|
|
244
|
+
/** nameAcross writes a bar's name, its thing and then its word, from x, middle on y. */
|
|
245
|
+
function nameAcross(pen, b, x, y, thing, things) {
|
|
246
|
+
if (b.thing !== undefined)
|
|
247
|
+
drawThing(pen, b.thing, x + thing / 2, y, thing);
|
|
248
|
+
if (b.label)
|
|
249
|
+
pen.text(x + (things ? thing + 8 : 0), y, b.label, { size: nameSize, anchor: "start" });
|
|
250
|
+
}
|
|
251
|
+
/** barsUp draws standing bars and their scale, its top left at 0, 0. */
|
|
252
|
+
function barsUp(pen, c) {
|
|
253
|
+
const k = c.bars.length;
|
|
254
|
+
const { step, max, lines } = scaleOf(c);
|
|
255
|
+
const slot = slotUp(k);
|
|
256
|
+
const width = Math.round(slot * 0.58);
|
|
257
|
+
const spacing = spacingUp(lines);
|
|
258
|
+
const tickW = Math.max(...range(lines + 1).map((j) => textWidth(String(j * step), tickSize)));
|
|
259
|
+
const x0 = Math.ceil(tickW) + 14;
|
|
260
|
+
const top = 12;
|
|
261
|
+
const bottom = top + lines * spacing;
|
|
262
|
+
const right = x0 + k * slot;
|
|
263
|
+
for (let j = 1; j <= lines; j++)
|
|
264
|
+
pen.el("path", {
|
|
265
|
+
d: `M${n(x0)} ${n(bottom - j * spacing)}H${n(right + 6)}`,
|
|
266
|
+
stroke: faint,
|
|
267
|
+
"stroke-width": 1.5,
|
|
268
|
+
class: "line",
|
|
269
|
+
});
|
|
270
|
+
c.bars.forEach((b, i) => {
|
|
271
|
+
const cx = x0 + (i + 0.5) * slot;
|
|
272
|
+
if (b.ask) {
|
|
273
|
+
drawThing(pen, { ask: true }, cx, bottom - 24, Math.min(44, slot - 6));
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
if (!b.value)
|
|
277
|
+
return;
|
|
278
|
+
const h = (b.value / max) * lines * spacing;
|
|
279
|
+
pen.el("rect", {
|
|
280
|
+
class: "bar",
|
|
281
|
+
x: cx - width / 2,
|
|
282
|
+
y: bottom - h,
|
|
283
|
+
width,
|
|
284
|
+
height: h,
|
|
285
|
+
fill: colors[b.color ?? c.color ?? "blue"].fill,
|
|
286
|
+
stroke: ink,
|
|
287
|
+
"stroke-width": outline,
|
|
288
|
+
"stroke-linejoin": "round",
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
for (let j = 0; j <= lines; j++) {
|
|
292
|
+
const y = bottom - j * spacing;
|
|
293
|
+
pen.el("path", { d: `M${n(x0 - 6)} ${n(y)}H${n(x0)}`, stroke: ink, "stroke-width": 2 });
|
|
294
|
+
pen.text(x0 - 10, y, String(j * step), { size: tickSize, anchor: "end", weight: 600 });
|
|
295
|
+
}
|
|
296
|
+
pen.el("path", {
|
|
297
|
+
d: `M${n(x0)} ${n(top - 10)}V${n(bottom)}H${n(right + 10)}`,
|
|
298
|
+
fill: "none",
|
|
299
|
+
stroke: ink,
|
|
300
|
+
"stroke-width": outline,
|
|
301
|
+
"stroke-linejoin": "round",
|
|
302
|
+
"stroke-linecap": "round",
|
|
303
|
+
});
|
|
304
|
+
const thing = Math.min(36, slot - 12);
|
|
305
|
+
const things = c.bars.some((b) => b.thing !== undefined);
|
|
306
|
+
const labels = c.bars.some((b) => b.label);
|
|
307
|
+
let y = bottom + 10;
|
|
308
|
+
c.bars.forEach((b, i) => {
|
|
309
|
+
const cx = x0 + (i + 0.5) * slot;
|
|
310
|
+
if (b.thing !== undefined)
|
|
311
|
+
drawThing(pen, b.thing, cx, y + thing / 2, thing);
|
|
312
|
+
if (b.label)
|
|
313
|
+
pen.text(cx, y + (things ? thing + 6 : 0) + 9, b.label, { size: nameSize });
|
|
314
|
+
});
|
|
315
|
+
if (things)
|
|
316
|
+
y += thing + 6;
|
|
317
|
+
if (labels)
|
|
318
|
+
y += 20;
|
|
319
|
+
return { width: right + 12, height: y };
|
|
320
|
+
}
|
|
321
|
+
/** barsAcross draws lying bars, their names at the left and their scale below. */
|
|
322
|
+
function barsAcross(pen, c) {
|
|
323
|
+
const k = c.bars.length;
|
|
324
|
+
const { step, max, lines } = scaleOf(c);
|
|
325
|
+
const row = rowAcross(k);
|
|
326
|
+
const thick = Math.round(row * 0.6);
|
|
327
|
+
const thing = thingAcross(k);
|
|
328
|
+
const things = c.bars.some((b) => b.thing !== undefined);
|
|
329
|
+
const { x0, spacing } = across(c);
|
|
330
|
+
const top = 8;
|
|
331
|
+
const bottom = top + k * row;
|
|
332
|
+
const right = x0 + lines * spacing;
|
|
333
|
+
for (let j = 1; j <= lines; j++)
|
|
334
|
+
pen.el("path", {
|
|
335
|
+
d: `M${n(x0 + j * spacing)} ${n(top - 6)}V${n(bottom)}`,
|
|
336
|
+
stroke: faint,
|
|
337
|
+
"stroke-width": 1.5,
|
|
338
|
+
class: "line",
|
|
339
|
+
});
|
|
340
|
+
c.bars.forEach((b, i) => {
|
|
341
|
+
const cy = top + (i + 0.5) * row;
|
|
342
|
+
nameAcross(pen, b, 0, cy, thing, things);
|
|
343
|
+
if (b.ask) {
|
|
344
|
+
drawThing(pen, { ask: true }, x0 + 26, cy, Math.min(44, row - 4));
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
if (!b.value)
|
|
348
|
+
return;
|
|
349
|
+
pen.el("rect", {
|
|
350
|
+
class: "bar",
|
|
351
|
+
x: x0,
|
|
352
|
+
y: cy - thick / 2,
|
|
353
|
+
width: (b.value / max) * lines * spacing,
|
|
354
|
+
height: thick,
|
|
355
|
+
fill: colors[b.color ?? c.color ?? "blue"].fill,
|
|
356
|
+
stroke: ink,
|
|
357
|
+
"stroke-width": outline,
|
|
358
|
+
"stroke-linejoin": "round",
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
for (let j = 0; j <= lines; j++) {
|
|
362
|
+
const x = x0 + j * spacing;
|
|
363
|
+
pen.el("path", { d: `M${n(x)} ${n(bottom)}V${n(bottom + 6)}`, stroke: ink, "stroke-width": 2 });
|
|
364
|
+
pen.text(x, bottom + 18, String(j * step), { size: tickSize, weight: 600 });
|
|
365
|
+
}
|
|
366
|
+
pen.el("path", {
|
|
367
|
+
d: `M${n(x0)} ${n(top - 8)}V${n(bottom)}H${n(right + 10)}`,
|
|
368
|
+
fill: "none",
|
|
369
|
+
stroke: ink,
|
|
370
|
+
"stroke-width": outline,
|
|
371
|
+
"stroke-linejoin": "round",
|
|
372
|
+
"stroke-linecap": "round",
|
|
373
|
+
});
|
|
374
|
+
const half = textWidth(String(max), tickSize) / 2;
|
|
375
|
+
return { width: right + Math.max(12, half + 2), height: bottom + 28 };
|
|
376
|
+
}
|
|
377
|
+
/** icons draws a bar of a picture chart: whole icons, then half of one, from the first place on. */
|
|
378
|
+
function icons(pen, icon, b, unit, at) {
|
|
379
|
+
if (b.ask) {
|
|
380
|
+
const p = at(0);
|
|
381
|
+
drawThing(pen, { ask: true }, p.x, p.y, iconSize + 4);
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
const { whole, half } = iconsOf(b.value, unit);
|
|
385
|
+
for (let i = 0; i < whole; i++) {
|
|
386
|
+
const p = at(i);
|
|
387
|
+
drawThing(pen, icon, p.x, p.y, iconSize);
|
|
388
|
+
}
|
|
389
|
+
if (half) {
|
|
390
|
+
const p = at(whole);
|
|
391
|
+
halfIcon(pen, icon, p.x, p.y, iconSize);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
/** bandPath is a band of a box of width w and height h, round at the corners it shares with the box. */
|
|
395
|
+
function bandPath(b, w, h, r) {
|
|
396
|
+
const tl = b.x0 === 0 && b.y0 === 0;
|
|
397
|
+
const tr = b.x1 === w && b.y0 === 0;
|
|
398
|
+
const br = b.x1 === w && b.y1 === h;
|
|
399
|
+
const bl = b.x0 === 0 && b.y1 === h;
|
|
400
|
+
let d = `M${n(b.x0 + (tl ? r : 0))} ${n(b.y0)}H${n(b.x1 - (tr ? r : 0))}`;
|
|
401
|
+
if (tr)
|
|
402
|
+
d += `A${r} ${r} 0 0 1 ${n(b.x1)} ${n(b.y0 + r)}`;
|
|
403
|
+
d += `V${n(b.y1 - (br ? r : 0))}`;
|
|
404
|
+
if (br)
|
|
405
|
+
d += `A${r} ${r} 0 0 1 ${n(b.x1 - r)} ${n(b.y1)}`;
|
|
406
|
+
d += `H${n(b.x0 + (bl ? r : 0))}`;
|
|
407
|
+
if (bl)
|
|
408
|
+
d += `A${r} ${r} 0 0 1 ${n(b.x0)} ${n(b.y1 - r)}`;
|
|
409
|
+
d += `V${n(b.y0 + (tl ? r : 0))}`;
|
|
410
|
+
if (tl)
|
|
411
|
+
d += `A${r} ${r} 0 0 1 ${n(b.x0 + r)} ${n(b.y0)}`;
|
|
412
|
+
return `${d}Z`;
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* frame draws the box of a picture chart: white, its names on a soft band
|
|
416
|
+
* set off by a full outline, so that a name drawn as a thing is not counted
|
|
417
|
+
* with the icons, and thin lines between the bars.
|
|
418
|
+
*/
|
|
419
|
+
function frame(pen, width, height, band, splits) {
|
|
420
|
+
const r = 10;
|
|
421
|
+
pen.el("rect", { x: 0, y: 0, width, height, rx: r, fill: "#ffffff" });
|
|
422
|
+
pen.el("path", { d: bandPath(band, width, height, r), fill: soft });
|
|
423
|
+
if (splits)
|
|
424
|
+
pen.el("path", { d: splits, stroke: ink, "stroke-width": 1.5, fill: "none" });
|
|
425
|
+
const divider = band.x1 < width ? `M${n(band.x1)} 0V${n(height)}` : `M0 ${n(band.y0)}H${n(width)}`;
|
|
426
|
+
pen.el("path", { d: divider, stroke: ink, "stroke-width": outline });
|
|
427
|
+
pen.el("rect", {
|
|
428
|
+
x: 0,
|
|
429
|
+
y: 0,
|
|
430
|
+
width,
|
|
431
|
+
height,
|
|
432
|
+
rx: r,
|
|
433
|
+
fill: "none",
|
|
434
|
+
stroke: ink,
|
|
435
|
+
"stroke-width": outline,
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
/** picturesAcross draws a picture chart in rows: names at the left, icons to the right. */
|
|
439
|
+
function picturesAcross(pen, c) {
|
|
440
|
+
const k = c.bars.length;
|
|
441
|
+
const unit = c.unit ?? 1;
|
|
442
|
+
const row = 52;
|
|
443
|
+
const things = c.bars.some((b) => b.thing !== undefined);
|
|
444
|
+
const names = namesWidth(c.bars, nameThing) + 24;
|
|
445
|
+
const places = Math.max(3, ...c.bars.map((b) => placesOf(b, unit)));
|
|
446
|
+
const width = names + places * iconPitch + 14;
|
|
447
|
+
const height = k * row;
|
|
448
|
+
let splits = "";
|
|
449
|
+
for (let i = 1; i < k; i++)
|
|
450
|
+
splits += `M0 ${n(i * row)}H${n(width)}`;
|
|
451
|
+
frame(pen, width, height, { x0: 0, y0: 0, x1: names, y1: height }, splits);
|
|
452
|
+
c.bars.forEach((b, i) => {
|
|
453
|
+
const cy = (i + 0.5) * row;
|
|
454
|
+
nameAcross(pen, b, 12, cy, nameThing, things);
|
|
455
|
+
icons(pen, c.icon ?? b.thing, b, unit, (p) => ({
|
|
456
|
+
x: names + 8 + (p + 0.5) * iconPitch,
|
|
457
|
+
y: cy,
|
|
458
|
+
}));
|
|
459
|
+
});
|
|
460
|
+
return { width, height };
|
|
461
|
+
}
|
|
462
|
+
/** picturesUp draws a picture chart in columns: icons from the bottom up, names below. */
|
|
463
|
+
function picturesUp(pen, c) {
|
|
464
|
+
const k = c.bars.length;
|
|
465
|
+
const unit = c.unit ?? 1;
|
|
466
|
+
const column = columnUp(c);
|
|
467
|
+
const things = c.bars.some((b) => b.thing !== undefined);
|
|
468
|
+
const labels = c.bars.some((b) => b.label);
|
|
469
|
+
const places = Math.max(3, ...c.bars.map((b) => placesOf(b, unit)));
|
|
470
|
+
const iconsH = places * iconPitch + 12;
|
|
471
|
+
const namesH = 10 + (things ? nameThing + 4 : 0) + (labels ? 22 : 0);
|
|
472
|
+
const width = k * column;
|
|
473
|
+
const height = iconsH + namesH;
|
|
474
|
+
let splits = "";
|
|
475
|
+
for (let i = 1; i < k; i++)
|
|
476
|
+
splits += `M${n(i * column)} 0V${n(height)}`;
|
|
477
|
+
frame(pen, width, height, { x0: 0, y0: iconsH, x1: width, y1: height }, splits);
|
|
478
|
+
c.bars.forEach((b, i) => {
|
|
479
|
+
const cx = (i + 0.5) * column;
|
|
480
|
+
let y = iconsH + 6;
|
|
481
|
+
if (b.thing !== undefined)
|
|
482
|
+
drawThing(pen, b.thing, cx, y + nameThing / 2, nameThing);
|
|
483
|
+
if (things)
|
|
484
|
+
y += nameThing + 4;
|
|
485
|
+
if (b.label)
|
|
486
|
+
pen.text(cx, y + 10, b.label, { size: nameSize });
|
|
487
|
+
icons(pen, c.icon ?? b.thing, b, unit, (p) => ({
|
|
488
|
+
x: cx,
|
|
489
|
+
y: iconsH - 6 - (p + 0.5) * iconPitch,
|
|
490
|
+
}));
|
|
491
|
+
});
|
|
492
|
+
return { width, height };
|
|
493
|
+
}
|
|
494
|
+
/** key draws "icon = unit", or "icon = ?", in a box; x is its right end. */
|
|
495
|
+
function key(pen, c, right, y) {
|
|
496
|
+
const asked = keyOf(c) === "ask";
|
|
497
|
+
const words = asked ? "=" : `= ${c.unit ?? 1}`;
|
|
498
|
+
const w = 14 + 34 + 8 + textWidth(words, 20) + (asked ? 8 + 34 : 0) + 14;
|
|
499
|
+
const x = right - w;
|
|
500
|
+
pen.el("rect", {
|
|
501
|
+
x,
|
|
502
|
+
y,
|
|
503
|
+
width: w,
|
|
504
|
+
height: 46,
|
|
505
|
+
rx: 10,
|
|
506
|
+
fill: "#ffffff",
|
|
507
|
+
stroke: ink,
|
|
508
|
+
"stroke-width": 2,
|
|
509
|
+
class: "key",
|
|
510
|
+
});
|
|
511
|
+
drawThing(pen, c.icon, x + 14 + 17, y + 23, 34);
|
|
512
|
+
pen.text(x + 14 + 34 + 8, y + 23, words, { size: 20, anchor: "start" });
|
|
513
|
+
if (asked)
|
|
514
|
+
drawThing(pen, { ask: true }, x + w - 14 - 17, y + 23, 36);
|
|
515
|
+
return { width: w, height: 46 };
|
|
516
|
+
}
|
|
517
|
+
// --- words ------------------------------------------------------------------
|
|
518
|
+
function nameOf(b) {
|
|
519
|
+
if (b.label && b.thing !== undefined)
|
|
520
|
+
return `"${b.label}" (${wordOf(b.thing)})`;
|
|
521
|
+
if (b.label)
|
|
522
|
+
return `"${b.label}"`;
|
|
523
|
+
return wordOf(b.thing, 2);
|
|
524
|
+
}
|
|
525
|
+
function an(words) {
|
|
526
|
+
return `${/^[aeiou]/.test(words) ? "an" : "a"} ${words}`;
|
|
527
|
+
}
|
|
528
|
+
function sayBar(b, step, color) {
|
|
529
|
+
if (b.ask)
|
|
530
|
+
return "the bar is missing, a question mark stands in its place";
|
|
531
|
+
const v = b.value;
|
|
532
|
+
const bar = color ? `the ${color} bar` : "the bar";
|
|
533
|
+
if (v === 0)
|
|
534
|
+
return "no bar, it is at 0";
|
|
535
|
+
if (v % step === 0)
|
|
536
|
+
return `${bar} reaches ${v}`;
|
|
537
|
+
return `${bar} ends halfway between ${v - step / 2} and ${v + step / 2}`;
|
|
538
|
+
}
|
|
539
|
+
function sayIcons(b, icon, unit) {
|
|
540
|
+
if (b.ask)
|
|
541
|
+
return "a question mark instead of pictures";
|
|
542
|
+
const { whole, half } = iconsOf(b.value, unit);
|
|
543
|
+
if (whole === 0 && !half)
|
|
544
|
+
return "no pictures";
|
|
545
|
+
const parts = [];
|
|
546
|
+
if (whole > 0)
|
|
547
|
+
parts.push(count(icon, whole));
|
|
548
|
+
if (half)
|
|
549
|
+
parts.push(`half ${an(wordOf(icon))}`);
|
|
550
|
+
return parts.join(" and ");
|
|
551
|
+
}
|
|
552
|
+
export const chart = {
|
|
553
|
+
kind: "chart",
|
|
554
|
+
members: [
|
|
555
|
+
"kind",
|
|
556
|
+
"style",
|
|
557
|
+
"horizontal",
|
|
558
|
+
"bars",
|
|
559
|
+
"step",
|
|
560
|
+
"max",
|
|
561
|
+
"color",
|
|
562
|
+
"icon",
|
|
563
|
+
"unit",
|
|
564
|
+
"key",
|
|
565
|
+
"title",
|
|
566
|
+
],
|
|
567
|
+
check(scene, problems) {
|
|
568
|
+
only(scene, this.members, "scene", problems);
|
|
569
|
+
const before = problems.length;
|
|
570
|
+
const style = scene.style === undefined ? "bars" : oneOf(scene.style, chartStyles, "style", problems);
|
|
571
|
+
if (scene.horizontal !== undefined && typeof scene.horizontal !== "boolean")
|
|
572
|
+
problems.push("horizontal: must be true or false");
|
|
573
|
+
if (scene.title !== undefined)
|
|
574
|
+
text(scene.title, "title", problems, titleMost);
|
|
575
|
+
const others = style === "bars" ? ["icon", "unit", "key"] : ["step", "max", "color"];
|
|
576
|
+
for (const m of others)
|
|
577
|
+
if (scene[m] !== undefined)
|
|
578
|
+
problems.push(`${m}: only a chart of style "${style === "bars" ? "pictures" : "bars"}" has ${m}`);
|
|
579
|
+
if (style === "bars" && scene.color !== undefined && !isColor(scene.color))
|
|
580
|
+
problems.push(`color: must be a colour: ${colorNames.join(", ")}`);
|
|
581
|
+
const most = mostBars[style][scene.horizontal === true ? "across" : "up"];
|
|
582
|
+
const bars = list(scene.bars, "bars", problems, 2, most);
|
|
583
|
+
bars.forEach((b, i) => {
|
|
584
|
+
checkBar(b, `bars[${i}]`, style, problems);
|
|
585
|
+
});
|
|
586
|
+
if (problems.length > before)
|
|
587
|
+
return;
|
|
588
|
+
const c = scene;
|
|
589
|
+
if (c.bars.every((b) => b.ask))
|
|
590
|
+
problems.push("bars: at least one bar must show its value");
|
|
591
|
+
else if (style === "bars")
|
|
592
|
+
checkBars(c, scene, problems);
|
|
593
|
+
else
|
|
594
|
+
checkPictures(c, scene, problems);
|
|
595
|
+
},
|
|
596
|
+
draw(scene, pen) {
|
|
597
|
+
const c = scene;
|
|
598
|
+
const body = pen.sub();
|
|
599
|
+
const up = c.horizontal !== true;
|
|
600
|
+
const drawn = styleOf(c) === "bars"
|
|
601
|
+
? up
|
|
602
|
+
? barsUp(body, c)
|
|
603
|
+
: barsAcross(body, c)
|
|
604
|
+
: up
|
|
605
|
+
? picturesUp(body, c)
|
|
606
|
+
: picturesAcross(body, c);
|
|
607
|
+
const titleH = c.title ? 34 : 0;
|
|
608
|
+
const titleW = c.title ? textWidth(c.title, titleSize) : 0;
|
|
609
|
+
const showKey = styleOf(c) === "pictures" && keyOf(c) !== "none";
|
|
610
|
+
const inner = Math.max(drawn.width, titleW);
|
|
611
|
+
const width = inner + 2 * margin;
|
|
612
|
+
const dx = margin + (inner - drawn.width) / 2;
|
|
613
|
+
pen.place(body, dx, margin + titleH);
|
|
614
|
+
if (c.title)
|
|
615
|
+
pen.text(width / 2, margin + 12, c.title, { size: titleSize });
|
|
616
|
+
let height = margin + titleH + drawn.height + margin;
|
|
617
|
+
if (showKey) {
|
|
618
|
+
const k = key(pen, c, dx + drawn.width, margin + titleH + drawn.height + 12);
|
|
619
|
+
height += k.height + 12;
|
|
620
|
+
}
|
|
621
|
+
return { width, height };
|
|
622
|
+
},
|
|
623
|
+
describe(scene) {
|
|
624
|
+
const c = scene;
|
|
625
|
+
const k = c.bars.length;
|
|
626
|
+
const up = c.horizontal !== true;
|
|
627
|
+
const titled = c.title ? ` titled "${c.title}"` : "";
|
|
628
|
+
const order = up ? "From the left" : "From the top";
|
|
629
|
+
if (styleOf(c) === "bars") {
|
|
630
|
+
const { step, max } = scaleOf(c);
|
|
631
|
+
const shades = new Set(c.bars.filter((b) => !b.ask).map((b) => b.color ?? c.color ?? "blue"));
|
|
632
|
+
const one = shades.size === 1 ? `${colors[[...shades][0]].word} ` : "";
|
|
633
|
+
const head = `A bar chart${titled} with ${k} ${one}bars ${up ? "standing side by side on a line" : "lying one under another, starting from a line"}. The scale ${up ? "at the left" : "along the bottom"} goes from 0 to ${max}, with a line at every ${step}.`;
|
|
634
|
+
const bars = c.bars.map((b) => `${nameOf(b)}: ${sayBar(b, step, shades.size > 1 ? colors[b.color ?? c.color ?? "blue"].word : undefined)}`);
|
|
635
|
+
return `${head} ${order}: ${bars.join("; ")}.`;
|
|
636
|
+
}
|
|
637
|
+
const unit = c.unit ?? 1;
|
|
638
|
+
const keyMode = keyOf(c);
|
|
639
|
+
const keyWords = keyMode === "none"
|
|
640
|
+
? ""
|
|
641
|
+
: ` A key below says: one ${wordOf(c.icon)} stands for ${keyMode === "ask" ? "?" : unit}.`;
|
|
642
|
+
const rows = c.bars.map((b) => `${nameOf(b)}: ${sayIcons(b, c.icon ?? b.thing, unit)}`);
|
|
643
|
+
return `A picture chart${titled} in ${k} ${up ? "columns, the pictures stacked from the bottom" : "rows, the pictures in a line from the left"}.${keyWords} ${order}: ${rows.join("; ")}.`;
|
|
644
|
+
},
|
|
645
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Kind } from "../kind.ts";
|
|
2
|
+
import { type Obj } from "../read.ts";
|
|
3
|
+
import { type Thing } from "../things.ts";
|
|
4
|
+
/**
|
|
5
|
+
* Choices: the options of a question drawn in a row, (A) to (F), each a thing
|
|
6
|
+
* or a whole scene of another kind, scaled to one cell. The format may keep
|
|
7
|
+
* each option as its own picture; this kind draws them together, as a booklet
|
|
8
|
+
* does.
|
|
9
|
+
*/
|
|
10
|
+
export type ChoicesScene = {
|
|
11
|
+
kind: "choices";
|
|
12
|
+
options: (Thing | Obj)[];
|
|
13
|
+
};
|
|
14
|
+
export declare const choices: Kind;
|