@link-foundation/ui-components 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/CONFIGURATIONS.md +74 -0
- package/ICONS.md +76 -0
- package/LICENSE +24 -0
- package/README.md +1499 -2
- package/bin/lino-network.mjs +71 -0
- package/dist/icons/ATTRIBUTION.md +602 -0
- package/dist/icons/NOTICE.lino +1261 -0
- package/dist/icons/brand.js +31 -0
- package/dist/icons/brand.svg +3 -0
- package/dist/icons/licenses/brand/DISCLAIMER.md +51 -0
- package/dist/icons/licenses/brand/LICENSE.md +30 -0
- package/dist/icons/licenses/lucide/LICENSE +43 -0
- package/dist/icons/licenses/material-symbols/LICENSE +202 -0
- package/dist/icons/licenses/material-symbols/NOTICE +12 -0
- package/dist/icons/licenses/tabler/LICENSE +21 -0
- package/dist/icons/lucide.js +681 -0
- package/dist/icons/lucide.svg +34 -0
- package/dist/icons/material-symbols.js +681 -0
- package/dist/icons/material-symbols.svg +34 -0
- package/dist/icons/own.js +521 -0
- package/dist/icons/own.svg +34 -0
- package/dist/icons/tabler.js +681 -0
- package/dist/icons/tabler.svg +34 -0
- package/dist/network-layout-NOTICE.md +6 -0
- package/dist/network-layout-d3-dispatch-LICENSE.txt +13 -0
- package/dist/network-layout-d3-force-LICENSE.txt +13 -0
- package/dist/network-layout-d3-quadtree-LICENSE.txt +13 -0
- package/dist/network-layout-d3-timer-LICENSE.txt +13 -0
- package/dist/network-layout-dagre-LICENSE.txt +19 -0
- package/dist/network-layout.worker.mjs +2 -0
- package/package.json +246 -4
- package/src/a11y/announce.js +88 -0
- package/src/a11y/bind-machine.js +181 -0
- package/src/a11y/focus.js +169 -0
- package/src/a11y/index.d.ts +89 -0
- package/src/a11y/index.js +19 -0
- package/src/a11y/keys.js +182 -0
- package/src/a11y/roving.js +167 -0
- package/src/a11y/typeahead.js +82 -0
- package/src/behavior/actions.js +235 -0
- package/src/behavior/diagnostics.js +73 -0
- package/src/behavior/dom.d.ts +38 -0
- package/src/behavior/dom.js +420 -0
- package/src/behavior/index.d.ts +211 -0
- package/src/behavior/index.js +29 -0
- package/src/behavior/interpret.js +454 -0
- package/src/behavior/machines/disclosure.lino +46 -0
- package/src/behavior/machines/index.js +91 -0
- package/src/behavior/machines/toggle.lino +25 -0
- package/src/behavior/parse-machine.js +367 -0
- package/src/charts/README.md +78 -0
- package/src/charts/activity-layout.js +350 -0
- package/src/charts/activity-model.js +130 -0
- package/src/charts/activity-validation.js +31 -0
- package/src/charts/chart-model.js +667 -0
- package/src/charts/chart.js +518 -0
- package/src/charts/echarts-adapter.d.ts +23 -0
- package/src/charts/echarts-adapter.js +240 -0
- package/src/charts/element.js +330 -0
- package/src/charts/errors.js +62 -0
- package/src/charts/gauge.js +27 -0
- package/src/charts/index.d.ts +344 -0
- package/src/charts/index.js +59 -0
- package/src/charts/layout.js +542 -0
- package/src/charts/lino.js +158 -0
- package/src/charts/mark-options.js +78 -0
- package/src/charts/read.js +101 -0
- package/src/charts/renderer-features.js +8 -0
- package/src/charts/scales.js +223 -0
- package/src/charts/small-model.js +223 -0
- package/src/charts/small-svg.js +269 -0
- package/src/charts/sparkline.js +27 -0
- package/src/charts/stat-tile.js +37 -0
- package/src/charts/summary.js +193 -0
- package/src/charts/svg-renderer.js +226 -0
- package/src/charts/svg.js +69 -0
- package/src/charts/tag-cloud.js +129 -0
- package/src/charts/tokens.js +171 -0
- package/src/charts/tooltip.js +47 -0
- package/src/charts/values.js +146 -0
- package/src/chat/README.md +142 -0
- package/src/chat/actions.js +75 -0
- package/src/chat/chat-contract.js +95 -0
- package/src/chat/chat-list.js +42 -0
- package/src/chat/chat-model.js +125 -0
- package/src/chat/chat.css +228 -0
- package/src/chat/composer.js +131 -0
- package/src/chat/day-separator.js +12 -0
- package/src/chat/dom.js +33 -0
- package/src/chat/follow.js +49 -0
- package/src/chat/index.d.ts +142 -0
- package/src/chat/index.js +13 -0
- package/src/chat/message-bubble.js +191 -0
- package/src/chat/message-stream.js +331 -0
- package/src/chat/reasoning-trace.js +70 -0
- package/src/chat/selection.js +39 -0
- package/src/chat/skins/bubbles.lino +2 -0
- package/src/chat/skins/cards.lino +4 -0
- package/src/chat/skins/compact.lino +4 -0
- package/src/chat/skins/signal.lino +25 -0
- package/src/chat/skins/t3.lino +25 -0
- package/src/chat/skins/telegram.lino +31 -0
- package/src/chat/skins/whatsapp.lino +25 -0
- package/src/chat/stream-keys.js +57 -0
- package/src/chat/stream-window.js +159 -0
- package/src/chat/vocabulary.js +19 -0
- package/src/controls/accordion.js +160 -0
- package/src/controls/avatar.js +80 -0
- package/src/controls/badge.js +48 -0
- package/src/controls/button-template.js +32 -0
- package/src/controls/button.js +97 -0
- package/src/controls/catalog.lino +84 -0
- package/src/controls/checkbox.js +112 -0
- package/src/controls/chip.js +140 -0
- package/src/controls/choices.js +45 -0
- package/src/controls/color-field.js +217 -0
- package/src/controls/color-popover.js +135 -0
- package/src/controls/color-rgb.js +80 -0
- package/src/controls/color.js +67 -0
- package/src/controls/combobox.js +192 -0
- package/src/controls/dom.js +261 -0
- package/src/controls/icon-button.js +33 -0
- package/src/controls/index.d.ts +163 -0
- package/src/controls/index.js +182 -0
- package/src/controls/kinds-text.js +358 -0
- package/src/controls/kinds.lino +350 -0
- package/src/controls/label.js +52 -0
- package/src/controls/link.js +101 -0
- package/src/controls/listbox.js +144 -0
- package/src/controls/machines/button.lino +20 -0
- package/src/controls/machines/checkbox.lino +17 -0
- package/src/controls/machines/chip.lino +20 -0
- package/src/controls/machines/color-field.lino +23 -0
- package/src/controls/machines/combobox.lino +82 -0
- package/src/controls/machines/icon-button.lino +20 -0
- package/src/controls/machines/index.js +586 -0
- package/src/controls/machines/number-field.lino +71 -0
- package/src/controls/machines/radio-group.lino +17 -0
- package/src/controls/machines/select.lino +122 -0
- package/src/controls/machines/slider.lino +99 -0
- package/src/controls/machines/switch.lino +19 -0
- package/src/controls/machines/text-area.lino +26 -0
- package/src/controls/machines/text-field.lino +26 -0
- package/src/controls/number-field.js +239 -0
- package/src/controls/numeric-keys.js +125 -0
- package/src/controls/option.js +54 -0
- package/src/controls/progress.js +76 -0
- package/src/controls/radio-group.js +234 -0
- package/src/controls/select.js +229 -0
- package/src/controls/slider.js +234 -0
- package/src/controls/stories.js +172 -0
- package/src/controls/styles.css +1060 -0
- package/src/controls/switch.js +95 -0
- package/src/controls/text-area.js +39 -0
- package/src/controls/text-field.js +131 -0
- package/src/controls/vocabulary.js +164 -0
- package/src/data/data.css +95 -0
- package/src/data/index.d.ts +83 -0
- package/src/data/index.js +10 -0
- package/src/data/keyboard.js +189 -0
- package/src/data/list.js +215 -0
- package/src/data/render-data.js +116 -0
- package/src/data/selection-dom.js +39 -0
- package/src/data/selection-model.js +43 -0
- package/src/data/sort-model.js +42 -0
- package/src/data/source.js +45 -0
- package/src/data/table.js +160 -0
- package/src/data/tree-model.js +41 -0
- package/src/data/tree.js +55 -0
- package/src/data/viewport.js +216 -0
- package/src/data/virtual-range.js +92 -0
- package/src/data/vocabulary.js +67 -0
- package/src/diagrams/diagram-model.js +511 -0
- package/src/diagrams/diagram.js +204 -0
- package/src/diagrams/index.d.ts +225 -0
- package/src/diagrams/index.js +48 -0
- package/src/diagrams/layout-dagre.js +164 -0
- package/src/diagrams/layout-plugins.js +131 -0
- package/src/diagrams/mermaid-export.js +232 -0
- package/src/diagrams/mermaid-view.js +229 -0
- package/src/diagrams/svg-diagram.js +215 -0
- package/src/editor/code-editor.js +206 -0
- package/src/editor/documents-model.js +170 -0
- package/src/editor/documents-persistence.js +85 -0
- package/src/editor/editor-engine.js +22 -0
- package/src/editor/editor-ui.js +203 -0
- package/src/editor/editor.css +106 -0
- package/src/editor/engines/codemirror.js +227 -0
- package/src/editor/engines/monaco.js +219 -0
- package/src/editor/index.d.ts +111 -0
- package/src/editor/index.js +14 -0
- package/src/editor/lino-highlight.js +106 -0
- package/src/editor/lino-structure.js +85 -0
- package/src/editor/validate-lino.js +43 -0
- package/src/embed/README.md +161 -0
- package/src/embed/controller.js +223 -0
- package/src/embed/element.js +141 -0
- package/src/embed/index.d.ts +75 -0
- package/src/embed/index.js +9 -0
- package/src/embed/providers/telegram.d.ts +3 -0
- package/src/embed/providers/telegram.js +38 -0
- package/src/embed/providers/vk.d.ts +3 -0
- package/src/embed/providers/vk.js +42 -0
- package/src/embed/providers/x.d.ts +3 -0
- package/src/embed/providers/x.js +31 -0
- package/src/embed/registry.js +35 -0
- package/src/embed/script.js +73 -0
- package/src/embed/view.js +122 -0
- package/src/icons/adapters/lucide.d.ts +15 -0
- package/src/icons/adapters/lucide.js +142 -0
- package/src/icons/adapters/material-symbols.d.ts +13 -0
- package/src/icons/adapters/material-symbols.js +113 -0
- package/src/icons/adapters/shared.d.ts +38 -0
- package/src/icons/adapters/shared.js +157 -0
- package/src/icons/adapters/simple-icons.d.ts +9 -0
- package/src/icons/adapters/simple-icons.js +38 -0
- package/src/icons/adapters/tabler.d.ts +9 -0
- package/src/icons/adapters/tabler.js +86 -0
- package/src/icons/icon-registry.js +215 -0
- package/src/icons/icon.js +356 -0
- package/src/icons/index.d.ts +123 -0
- package/src/icons/index.js +30 -0
- package/src/icons/names.js +51 -0
- package/src/icons/own-icons.js +521 -0
- package/src/icons/presentation.js +55 -0
- package/src/icons/svg.js +136 -0
- package/src/index.d.ts +14 -0
- package/src/index.js +40 -0
- package/src/layout/README.md +102 -0
- package/src/layout/app-shell.js +150 -0
- package/src/layout/catalog.lino +27 -0
- package/src/layout/dock-model.js +576 -0
- package/src/layout/dock-view.js +312 -0
- package/src/layout/dock.js +352 -0
- package/src/layout/dom.js +312 -0
- package/src/layout/grid.js +192 -0
- package/src/layout/index.d.ts +267 -0
- package/src/layout/index.js +60 -0
- package/src/layout/layer-manager.js +120 -0
- package/src/layout/layout.css +760 -0
- package/src/layout/menu.js +141 -0
- package/src/layout/nav-strip.js +174 -0
- package/src/layout/page-links.js +166 -0
- package/src/layout/panel.js +26 -0
- package/src/layout/pref-switches.js +209 -0
- package/src/layout/split-model.js +270 -0
- package/src/layout/split.js +430 -0
- package/src/layout/stack.js +86 -0
- package/src/layout/surfaces.js +119 -0
- package/src/layout/tab-group.js +151 -0
- package/src/layout/tab-strip.js +277 -0
- package/src/layout/vocabulary.js +229 -0
- package/src/layout/window-frame.js +222 -0
- package/src/layout/window.js +146 -0
- package/src/links-view/animator.js +144 -0
- package/src/links-view/configuration-classes.js +150 -0
- package/src/links-view/configuration-model.js +191 -0
- package/src/links-view/configuration-notations.js +230 -0
- package/src/links-view/configurations.d.ts +73 -0
- package/src/links-view/configurations.js +33 -0
- package/src/links-view/create-layout-runner.js +79 -0
- package/src/links-view/doublet-curves.js +239 -0
- package/src/links-view/doublet-layout.js +148 -0
- package/src/links-view/doublet-markers.js +109 -0
- package/src/links-view/doublet-options.js +369 -0
- package/src/links-view/doublet-svg.js +156 -0
- package/src/links-view/doublet.js +230 -0
- package/src/links-view/export.js +146 -0
- package/src/links-view/geometry.js +409 -0
- package/src/links-view/index.d.ts +1029 -0
- package/src/links-view/index.js +140 -0
- package/src/links-view/labels.js +28 -0
- package/src/links-view/layout-worker-entry.mjs +2 -0
- package/src/links-view/layout-worker.js +28 -0
- package/src/links-view/layouts/circle.js +7 -0
- package/src/links-view/layouts/force.js +76 -0
- package/src/links-view/layouts/grid.js +7 -0
- package/src/links-view/layouts/hex.js +8 -0
- package/src/links-view/layouts/index.js +15 -0
- package/src/links-view/layouts/layered.js +29 -0
- package/src/links-view/layouts/regular.js +27 -0
- package/src/links-view/layouts/spiral.js +10 -0
- package/src/links-view/link-view.js +494 -0
- package/src/links-view/links-gallery.js +306 -0
- package/src/links-view/links-network.js +496 -0
- package/src/links-view/lino-network-source.js +122 -0
- package/src/links-view/lino-network.js +233 -0
- package/src/links-view/lino-to-network.js +252 -0
- package/src/links-view/markers.js +192 -0
- package/src/links-view/model.js +233 -0
- package/src/links-view/network-editor/dom.js +36 -0
- package/src/links-view/network-editor/editor.js +202 -0
- package/src/links-view/network-editor/model.js +163 -0
- package/src/links-view/network-editor/options.js +265 -0
- package/src/links-view/network-editor/panels.js +423 -0
- package/src/links-view/network-editor/presets.js +76 -0
- package/src/links-view/network-editor/provider.js +93 -0
- package/src/links-view/network-editor/remap.js +45 -0
- package/src/links-view/network-editor/schema.js +224 -0
- package/src/links-view/network-editor/share.js +232 -0
- package/src/links-view/network-editor/styles.js +19 -0
- package/src/links-view/network-editor/url-sync.js +131 -0
- package/src/links-view/network-interaction.js +228 -0
- package/src/links-view/network-model.js +85 -0
- package/src/links-view/network-renderer.js +259 -0
- package/src/links-view/network-scene.js +245 -0
- package/src/links-view/network-source.js +82 -0
- package/src/links-view/network-vocabulary.js +18 -0
- package/src/links-view/presentation.js +199 -0
- package/src/links-view/render-canvas.js +102 -0
- package/src/links-view/render-svg.js +118 -0
- package/src/links-view/scene.js +233 -0
- package/src/links-view/solver-job.js +106 -0
- package/src/links-view/tokens.js +114 -0
- package/src/links-view/zoom-pan.js +203 -0
- package/src/overlays/catalog.lino +89 -0
- package/src/overlays/context-menu.js +110 -0
- package/src/overlays/dialog.js +117 -0
- package/src/overlays/dom.js +168 -0
- package/src/overlays/drawer.js +8 -0
- package/src/overlays/index.d.ts +80 -0
- package/src/overlays/index.js +25 -0
- package/src/overlays/kinds.lino +137 -0
- package/src/overlays/list-popover.js +129 -0
- package/src/overlays/machines/index.js +71 -0
- package/src/overlays/machines/surface.lino +11 -0
- package/src/overlays/machines/tabs.lino +7 -0
- package/src/overlays/machines/toast.lino +16 -0
- package/src/overlays/machines/tooltip.lino +29 -0
- package/src/overlays/menu.js +240 -0
- package/src/overlays/popover.js +194 -0
- package/src/overlays/position.js +84 -0
- package/src/overlays/radial-geometry.js +38 -0
- package/src/overlays/radial-menu.js +50 -0
- package/src/overlays/styles.css +155 -0
- package/src/overlays/tabs.js +125 -0
- package/src/overlays/toast-queue.js +105 -0
- package/src/overlays/toast.js +163 -0
- package/src/overlays/tooltip.js +110 -0
- package/src/overlays/vocabulary.js +3 -0
- package/src/settings/conditions.js +44 -0
- package/src/settings/control-for.js +62 -0
- package/src/settings/field-control.js +201 -0
- package/src/settings/index.d.ts +201 -0
- package/src/settings/index.js +3 -0
- package/src/settings/messages.js +112 -0
- package/src/settings/miniature.js +26 -0
- package/src/settings/panel-actions.js +234 -0
- package/src/settings/panel-model.js +49 -0
- package/src/settings/panel-refresh.js +56 -0
- package/src/settings/panel-rows.js +200 -0
- package/src/settings/preset-cards.js +43 -0
- package/src/settings/saved-looks.js +227 -0
- package/src/settings/section-layout.js +95 -0
- package/src/settings/section-tabs.js +175 -0
- package/src/settings/settings-panel.js +192 -0
- package/src/settings/settings.css +178 -0
- package/stories/accordion.lino +14 -0
- package/stories/alert-dialog.lino +4 -0
- package/stories/app-bar.lino +10 -0
- package/stories/avatar.lino +7 -0
- package/stories/badge.lino +12 -0
- package/stories/button.lino +17 -0
- package/stories/card.lino +10 -0
- package/stories/checkbox.lino +14 -0
- package/stories/chip.lino +19 -0
- package/stories/color-field.lino +9 -0
- package/stories/combobox.lino +10 -0
- package/stories/container.lino +11 -0
- package/stories/context-menu.lino +15 -0
- package/stories/dialog.lino +16 -0
- package/stories/drawer.lino +12 -0
- package/stories/icon-button.lino +13 -0
- package/stories/label.lino +7 -0
- package/stories/link-cards.lino +9 -0
- package/stories/link.lino +9 -0
- package/stories/menu-item.lino +6 -0
- package/stories/menu-separator.lino +7 -0
- package/stories/menu.lino +30 -0
- package/stories/nav-strip.lino +11 -0
- package/stories/number-field.lino +16 -0
- package/stories/option.lino +10 -0
- package/stories/paper.lino +11 -0
- package/stories/popover.lino +4 -0
- package/stories/pref-switches.lino +4 -0
- package/stories/progress.lino +14 -0
- package/stories/radial-menu.lino +19 -0
- package/stories/radio-group.lino +22 -0
- package/stories/radio.lino +10 -0
- package/stories/section-nav.lino +9 -0
- package/stories/select.lino +16 -0
- package/stories/slider.lino +14 -0
- package/stories/switch.lino +10 -0
- package/stories/tab-panel.lino +7 -0
- package/stories/tab.lino +7 -0
- package/stories/tabs.lino +25 -0
- package/stories/text-area.lino +12 -0
- package/stories/text-field.lino +23 -0
- package/stories/titled-section.lino +7 -0
- package/stories/toast.lino +3 -0
- package/stories/toaster.lino +2 -0
- package/stories/tooltip.lino +3 -0
|
@@ -0,0 +1,667 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The chart dialect: a chart written in Links Notation with the grammar of
|
|
3
|
+
* Vega-Lite (data, mark, encoding), read into a normalised spec and written
|
|
4
|
+
* back. Pure: no DOM, no clock, no randomness.
|
|
5
|
+
*
|
|
6
|
+
* ```lino
|
|
7
|
+
* (sales: chart
|
|
8
|
+
* (title 'Monthly sales')
|
|
9
|
+
* (description 'Sales grew from 12 to 30 between January and June')
|
|
10
|
+
* (data
|
|
11
|
+
* (row (month 2026-01) (value 12))
|
|
12
|
+
* (row (month 2026-02) (value 18)))
|
|
13
|
+
* (mark line)
|
|
14
|
+
* (encoding
|
|
15
|
+
* (x (field month) (type temporal))
|
|
16
|
+
* (y (field value) (type quantitative))))
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* The small charts `gauge`, `stat-tile` and `sparkline` are read by
|
|
20
|
+
* `small-model.js`; `chartFromLinks` accepts every kind.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import {
|
|
24
|
+
ACTIVITY_PROPERTIES,
|
|
25
|
+
activityNodes,
|
|
26
|
+
readActivity,
|
|
27
|
+
readScale,
|
|
28
|
+
} from './activity-model.js';
|
|
29
|
+
import { validateActivity } from './activity-validation.js';
|
|
30
|
+
import { readTagCloud, tagCloudNode } from './tag-cloud.js';
|
|
31
|
+
import { ChartSpecError, at, fail } from './errors.js';
|
|
32
|
+
import {
|
|
33
|
+
formatNested,
|
|
34
|
+
headName,
|
|
35
|
+
isAtom,
|
|
36
|
+
parseLino,
|
|
37
|
+
prop,
|
|
38
|
+
unfoldLinks,
|
|
39
|
+
} from './lino.js';
|
|
40
|
+
import {
|
|
41
|
+
booleanOf,
|
|
42
|
+
numberOf,
|
|
43
|
+
optionalText,
|
|
44
|
+
properties,
|
|
45
|
+
single,
|
|
46
|
+
} from './read.js';
|
|
47
|
+
import { SMALL_KINDS, readSmallChart, smallChartNode } from './small-model.js';
|
|
48
|
+
import { isCanonicalNumber, parseTemporal, toNumber } from './values.js';
|
|
49
|
+
|
|
50
|
+
export { ChartSpecError };
|
|
51
|
+
|
|
52
|
+
/** The marks of the chart dialect (Vega-Lite mark types). */
|
|
53
|
+
export const MARKS = Object.freeze([
|
|
54
|
+
'line',
|
|
55
|
+
'area',
|
|
56
|
+
'bar',
|
|
57
|
+
'point',
|
|
58
|
+
'arc',
|
|
59
|
+
'calendar',
|
|
60
|
+
'range',
|
|
61
|
+
'composition',
|
|
62
|
+
]);
|
|
63
|
+
|
|
64
|
+
/** The field types (Vega-Lite measurement types). */
|
|
65
|
+
export const FIELD_TYPES = Object.freeze([
|
|
66
|
+
'quantitative',
|
|
67
|
+
'temporal',
|
|
68
|
+
'ordinal',
|
|
69
|
+
'nominal',
|
|
70
|
+
]);
|
|
71
|
+
|
|
72
|
+
/** The encoding channels. `theta` is the Vega-Lite angle of an `arc`. */
|
|
73
|
+
export const CHANNELS = Object.freeze([
|
|
74
|
+
'x',
|
|
75
|
+
'y',
|
|
76
|
+
'theta',
|
|
77
|
+
'x2',
|
|
78
|
+
'color',
|
|
79
|
+
'size',
|
|
80
|
+
'tooltip',
|
|
81
|
+
]);
|
|
82
|
+
|
|
83
|
+
/** The renderers: our SVG renderer, or the optional ECharts adapter. */
|
|
84
|
+
export const RENDERERS = Object.freeze(['svg', 'echarts']);
|
|
85
|
+
|
|
86
|
+
/** Every kind `chartFromLinks` reads. */
|
|
87
|
+
export const CHART_KINDS = Object.freeze([
|
|
88
|
+
'chart',
|
|
89
|
+
'tag-cloud',
|
|
90
|
+
...SMALL_KINDS,
|
|
91
|
+
]);
|
|
92
|
+
|
|
93
|
+
const INTERPOLATIONS = ['linear', 'monotone', 'step'];
|
|
94
|
+
|
|
95
|
+
/** The channels each mark needs and allows. */
|
|
96
|
+
const MARK_CHANNELS = {
|
|
97
|
+
calendar: { required: ['x', 'y'], allowed: ['x', 'y', 'tooltip'] },
|
|
98
|
+
range: {
|
|
99
|
+
required: ['x', 'x2', 'y'],
|
|
100
|
+
allowed: ['x', 'x2', 'y', 'color', 'tooltip'],
|
|
101
|
+
},
|
|
102
|
+
composition: {
|
|
103
|
+
required: ['theta', 'color'],
|
|
104
|
+
allowed: ['theta', 'color', 'tooltip'],
|
|
105
|
+
},
|
|
106
|
+
line: { required: ['x', 'y'], allowed: ['x', 'y', 'color', 'tooltip'] },
|
|
107
|
+
area: { required: ['x', 'y'], allowed: ['x', 'y', 'color', 'tooltip'] },
|
|
108
|
+
bar: { required: ['x', 'y'], allowed: ['x', 'y', 'color', 'tooltip'] },
|
|
109
|
+
point: {
|
|
110
|
+
required: ['x', 'y'],
|
|
111
|
+
allowed: ['x', 'y', 'color', 'size', 'tooltip'],
|
|
112
|
+
},
|
|
113
|
+
arc: { required: ['theta'], allowed: ['theta', 'color', 'tooltip'] },
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @typedef {'quantitative'|'temporal'|'ordinal'|'nominal'} FieldType
|
|
118
|
+
*
|
|
119
|
+
* @typedef {object} FieldDef
|
|
120
|
+
* @property {string} field the data field
|
|
121
|
+
* @property {FieldType} type
|
|
122
|
+
* @property {string|null} title the axis or legend title (default: field)
|
|
123
|
+
*
|
|
124
|
+
* @typedef {object} MarkDef
|
|
125
|
+
* @property {'line'|'area'|'bar'|'point'|'arc'|'calendar'|'range'|'composition'} type
|
|
126
|
+
* @property {'linear'|'monotone'|'step'} interpolate line and area curve
|
|
127
|
+
* @property {boolean} point draw a point on every value of a line or area
|
|
128
|
+
* @property {number} innerRadius the hole of an arc (a donut), in pixels
|
|
129
|
+
*
|
|
130
|
+
* @typedef {object} Encoding
|
|
131
|
+
* @property {FieldDef} [x]
|
|
132
|
+
* @property {FieldDef} [y]
|
|
133
|
+
* @property {FieldDef} [theta]
|
|
134
|
+
* @property {FieldDef} [x2]
|
|
135
|
+
* @property {FieldDef} [color]
|
|
136
|
+
* @property {FieldDef} [size]
|
|
137
|
+
* @property {FieldDef[]} [tooltip]
|
|
138
|
+
*
|
|
139
|
+
* @typedef {object} ChartSpec
|
|
140
|
+
* @property {'chart'} kind
|
|
141
|
+
* @property {string|null} id
|
|
142
|
+
* @property {string|null} title
|
|
143
|
+
* @property {string|null} description
|
|
144
|
+
* @property {'svg'|'echarts'} renderer
|
|
145
|
+
* @property {number|null} width
|
|
146
|
+
* @property {number|null} height
|
|
147
|
+
* @property {MarkDef} mark
|
|
148
|
+
* @property {{ values: Record<string, string|number>[] }} data typed rows
|
|
149
|
+
* @property {Encoding} encoding
|
|
150
|
+
*/
|
|
151
|
+
|
|
152
|
+
/** The mark of `(mark line)` or `(mark line (interpolate monotone))`. */
|
|
153
|
+
function readMark(entry) {
|
|
154
|
+
const { node, path } = entry;
|
|
155
|
+
const [, type] = node.values;
|
|
156
|
+
if (type === undefined || !isAtom(type)) {
|
|
157
|
+
fail('missing-mark', '(mark) needs a mark type', node, path);
|
|
158
|
+
}
|
|
159
|
+
if (!MARKS.includes(type.id)) {
|
|
160
|
+
fail(
|
|
161
|
+
'unknown-mark',
|
|
162
|
+
`unknown mark "${type.id}" (expected ${MARKS.join(', ')})`,
|
|
163
|
+
node,
|
|
164
|
+
path
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
const mark = {
|
|
168
|
+
type: type.id,
|
|
169
|
+
interpolate: 'linear',
|
|
170
|
+
point: false,
|
|
171
|
+
innerRadius: 0,
|
|
172
|
+
};
|
|
173
|
+
const props = properties(node, path, 2, [
|
|
174
|
+
'interpolate',
|
|
175
|
+
'point',
|
|
176
|
+
'inner-radius',
|
|
177
|
+
]);
|
|
178
|
+
const interpolate = props.get('interpolate')?.[0];
|
|
179
|
+
if (interpolate !== undefined) {
|
|
180
|
+
mark.interpolate = single(
|
|
181
|
+
interpolate.node,
|
|
182
|
+
interpolate.path,
|
|
183
|
+
'interpolate'
|
|
184
|
+
);
|
|
185
|
+
if (!INTERPOLATIONS.includes(mark.interpolate)) {
|
|
186
|
+
fail(
|
|
187
|
+
'invalid-value',
|
|
188
|
+
`unknown interpolation "${mark.interpolate}"`,
|
|
189
|
+
interpolate.node,
|
|
190
|
+
interpolate.path
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
const point = props.get('point')?.[0];
|
|
195
|
+
if (point !== undefined) {
|
|
196
|
+
mark.point = booleanOf(point.node, point.path, 'point');
|
|
197
|
+
}
|
|
198
|
+
const inner = props.get('inner-radius')?.[0];
|
|
199
|
+
if (inner !== undefined) {
|
|
200
|
+
mark.innerRadius = numberOf(inner.node, inner.path, 'inner-radius');
|
|
201
|
+
}
|
|
202
|
+
return mark;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** One `(row (field value) ...)` of the data, as raw texts. */
|
|
206
|
+
function readRow(node, path) {
|
|
207
|
+
if (headName(node) !== 'row') {
|
|
208
|
+
fail('invalid-data', 'expected (row (field value) ...)', node, path);
|
|
209
|
+
}
|
|
210
|
+
const row = {};
|
|
211
|
+
const where = {};
|
|
212
|
+
node.values.slice(1).forEach((cell, i) => {
|
|
213
|
+
const cellPath = [...path, i + 1];
|
|
214
|
+
const name = headName(cell);
|
|
215
|
+
if (name === null) {
|
|
216
|
+
fail('invalid-data', 'expected (field value)', cell, cellPath);
|
|
217
|
+
}
|
|
218
|
+
if (Object.hasOwn(row, name)) {
|
|
219
|
+
fail(
|
|
220
|
+
'duplicate-property',
|
|
221
|
+
`field "${name}" is given twice`,
|
|
222
|
+
cell,
|
|
223
|
+
cellPath
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
row[name] = single(cell, cellPath, name);
|
|
227
|
+
where[name] = { node: cell, path: cellPath };
|
|
228
|
+
});
|
|
229
|
+
return { row, where };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** The rows of `(data (row ...) ...)` with the position of every cell. */
|
|
233
|
+
function readData(entry) {
|
|
234
|
+
const rows = entry.node.values
|
|
235
|
+
.slice(1)
|
|
236
|
+
.map((row, i) => readRow(row, [...entry.path, i + 1]));
|
|
237
|
+
if (rows.length === 0) {
|
|
238
|
+
fail(
|
|
239
|
+
'empty-data',
|
|
240
|
+
'(data) needs at least one (row ...)',
|
|
241
|
+
entry.node,
|
|
242
|
+
entry.path
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
return rows;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** A field definition `((field f) (type t) (title 'T'))` after its head. */
|
|
249
|
+
function readFieldDef(node, path, from, name) {
|
|
250
|
+
const props = properties(node, path, from, [
|
|
251
|
+
'field',
|
|
252
|
+
'type',
|
|
253
|
+
'title',
|
|
254
|
+
'scale',
|
|
255
|
+
]);
|
|
256
|
+
const field = optionalText(props, 'field');
|
|
257
|
+
if (field === null) {
|
|
258
|
+
fail('missing-field', `(${name}) needs (field NAME)`, node, path);
|
|
259
|
+
}
|
|
260
|
+
const type = optionalText(props, 'type');
|
|
261
|
+
if (type !== null && !FIELD_TYPES.includes(type)) {
|
|
262
|
+
const entry = props.get('type')[0];
|
|
263
|
+
fail(
|
|
264
|
+
'unknown-type',
|
|
265
|
+
`unknown type "${type}" (expected ${FIELD_TYPES.join(', ')})`,
|
|
266
|
+
entry.node,
|
|
267
|
+
entry.path
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
return {
|
|
271
|
+
def: {
|
|
272
|
+
field,
|
|
273
|
+
type,
|
|
274
|
+
title: optionalText(props, 'title'),
|
|
275
|
+
...(props.has('scale')
|
|
276
|
+
? { scale: readScale(props.get('scale')[0]) }
|
|
277
|
+
: {}),
|
|
278
|
+
},
|
|
279
|
+
where: { node, path, field: props.get('field')[0] },
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* The tooltip: `(tooltip (field f) (type t))` for one field, or
|
|
285
|
+
* `(tooltip ((field a) (type t)) ((field b)))` for several.
|
|
286
|
+
*/
|
|
287
|
+
function readTooltip(node, path) {
|
|
288
|
+
const [, first] = node.values;
|
|
289
|
+
if (first !== undefined && headName(first) === null && !isAtom(first)) {
|
|
290
|
+
return node.values.slice(1).map((item, i) => {
|
|
291
|
+
const where = [...path, i + 1];
|
|
292
|
+
return readFieldDef(item, where, 0, 'tooltip');
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
return [readFieldDef(node, path, 1, 'tooltip')];
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** The channels of `(encoding (x ...) (y ...) ...)`. */
|
|
299
|
+
function readEncoding(entry) {
|
|
300
|
+
const props = properties(entry.node, entry.path, 1, CHANNELS);
|
|
301
|
+
const channels = {};
|
|
302
|
+
for (const [name, [{ node, path }]] of props) {
|
|
303
|
+
channels[name] =
|
|
304
|
+
name === 'tooltip'
|
|
305
|
+
? readTooltip(node, path)
|
|
306
|
+
: readFieldDef(node, path, 1, name);
|
|
307
|
+
}
|
|
308
|
+
return channels;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Whether every value of a field is a canonical number. */
|
|
312
|
+
const allNumbers = (rows, field) =>
|
|
313
|
+
rows.every(({ row }) => isCanonicalNumber(row[field]));
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Check each encoded field against the data and give it its type: a
|
|
317
|
+
* missing field is an error, and a field without `(type)` is
|
|
318
|
+
* `quantitative` when every value is a number, `nominal` otherwise.
|
|
319
|
+
*/
|
|
320
|
+
function typeFields(channels, rows, mark) {
|
|
321
|
+
const types = new Map();
|
|
322
|
+
for (const item of Object.values(channels).flat()) {
|
|
323
|
+
const { def, where } = item;
|
|
324
|
+
const missing = rows.find(({ row }) => !Object.hasOwn(row, def.field));
|
|
325
|
+
if (
|
|
326
|
+
missing !== undefined &&
|
|
327
|
+
!(mark.type === 'range' && item === channels.x2)
|
|
328
|
+
) {
|
|
329
|
+
fail(
|
|
330
|
+
'missing-field',
|
|
331
|
+
`field "${def.field}" is missing from a data row`,
|
|
332
|
+
where.field.node,
|
|
333
|
+
where.field.path
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
def.type ??= allNumbers(rows, def.field) ? 'quantitative' : 'nominal';
|
|
337
|
+
if (def.scale && def.type !== 'quantitative') {
|
|
338
|
+
fail(
|
|
339
|
+
'invalid-value',
|
|
340
|
+
'log scales require quantitative fields',
|
|
341
|
+
where.node,
|
|
342
|
+
where.path
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
const known = types.get(def.field);
|
|
346
|
+
// A field has one type: the strongest one asked for wins.
|
|
347
|
+
if (known === undefined || def.type === 'quantitative') {
|
|
348
|
+
types.set(def.field, def.type);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return types;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** A typed cell, or a ChartSpecError at that cell. */
|
|
355
|
+
function typedValue(text, type, cell) {
|
|
356
|
+
if (type === 'quantitative') {
|
|
357
|
+
const number = toNumber(text);
|
|
358
|
+
if (number === null) {
|
|
359
|
+
fail('invalid-value', `"${text}" is not a number`, cell.node, cell.path);
|
|
360
|
+
}
|
|
361
|
+
return number;
|
|
362
|
+
}
|
|
363
|
+
if (type === 'temporal' && parseTemporal(text) === null) {
|
|
364
|
+
fail('invalid-value', `"${text}" is not a date`, cell.node, cell.path);
|
|
365
|
+
}
|
|
366
|
+
if (type === undefined) {
|
|
367
|
+
return isCanonicalNumber(text) ? Number(text) : text;
|
|
368
|
+
}
|
|
369
|
+
return text;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** The typed rows: numbers for quantitative fields, text otherwise. */
|
|
373
|
+
function typeRows(rows, types, ongoingField) {
|
|
374
|
+
return rows.map(({ row, where }) => {
|
|
375
|
+
const typed = {};
|
|
376
|
+
for (const [name, text] of Object.entries(row)) {
|
|
377
|
+
typed[name] =
|
|
378
|
+
name === ongoingField && text === 'ongoing'
|
|
379
|
+
? text
|
|
380
|
+
: typedValue(text, types.get(name), where[name]);
|
|
381
|
+
}
|
|
382
|
+
return typed;
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/** Check the channels a mark needs and allows. */
|
|
387
|
+
function checkChannels(mark, channels, markEntry, encodingEntry) {
|
|
388
|
+
const { required, allowed } = MARK_CHANNELS[mark.type];
|
|
389
|
+
for (const name of Object.keys(channels)) {
|
|
390
|
+
if (!allowed.includes(name)) {
|
|
391
|
+
const { where } = [channels[name]].flat()[0];
|
|
392
|
+
fail(
|
|
393
|
+
'unknown-channel',
|
|
394
|
+
`a ${mark.type} mark has no "${name}" channel`,
|
|
395
|
+
where.node,
|
|
396
|
+
where.path
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
for (const name of required) {
|
|
401
|
+
if (channels[name] === undefined) {
|
|
402
|
+
const entry = encodingEntry ?? markEntry;
|
|
403
|
+
fail(
|
|
404
|
+
'missing-channel',
|
|
405
|
+
`a ${mark.type} mark needs the "${name}" channel`,
|
|
406
|
+
entry.node,
|
|
407
|
+
entry.path
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/** The normalised encoding: field definitions in channel order. */
|
|
414
|
+
function encodingOf(channels) {
|
|
415
|
+
const encoding = {};
|
|
416
|
+
for (const name of CHANNELS) {
|
|
417
|
+
const value = channels[name];
|
|
418
|
+
if (value !== undefined) {
|
|
419
|
+
encoding[name] = Array.isArray(value)
|
|
420
|
+
? value.map((item) => item.def)
|
|
421
|
+
: value.def;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
return encoding;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/** A positive pixel size property. */
|
|
428
|
+
function sizeOf(props, name) {
|
|
429
|
+
const entry = props.get(name)?.[0];
|
|
430
|
+
if (entry === undefined) {
|
|
431
|
+
return null;
|
|
432
|
+
}
|
|
433
|
+
const value = numberOf(entry.node, entry.path, name);
|
|
434
|
+
if (value <= 0) {
|
|
435
|
+
fail('invalid-value', `(${name}) must be positive`, entry.node, entry.path);
|
|
436
|
+
}
|
|
437
|
+
return value;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
const CHART_PROPERTIES = [
|
|
441
|
+
...ACTIVITY_PROPERTIES,
|
|
442
|
+
'title',
|
|
443
|
+
'description',
|
|
444
|
+
'data',
|
|
445
|
+
'mark',
|
|
446
|
+
'encoding',
|
|
447
|
+
'renderer',
|
|
448
|
+
'width',
|
|
449
|
+
'height',
|
|
450
|
+
];
|
|
451
|
+
|
|
452
|
+
/** Read one `chart` node (already unfolded). */
|
|
453
|
+
function readChart(node, path) {
|
|
454
|
+
const props = properties(node, path, 1, CHART_PROPERTIES);
|
|
455
|
+
const markEntry = props.get('mark')?.[0];
|
|
456
|
+
if (markEntry === undefined) {
|
|
457
|
+
fail('missing-mark', 'a chart needs (mark TYPE)', node, path);
|
|
458
|
+
}
|
|
459
|
+
const mark = readMark(markEntry);
|
|
460
|
+
const dataEntry = props.get('data')?.[0];
|
|
461
|
+
if (dataEntry === undefined) {
|
|
462
|
+
fail('empty-data', 'a chart needs (data (row ...) ...)', node, path);
|
|
463
|
+
}
|
|
464
|
+
const rows = readData(dataEntry);
|
|
465
|
+
const encodingEntry = props.get('encoding')?.[0];
|
|
466
|
+
const channels = encodingEntry ? readEncoding(encodingEntry) : {};
|
|
467
|
+
checkChannels(mark, channels, markEntry, encodingEntry);
|
|
468
|
+
const types = typeFields(channels, rows, mark);
|
|
469
|
+
const renderer = optionalText(props, 'renderer') ?? 'svg';
|
|
470
|
+
if (!RENDERERS.includes(renderer)) {
|
|
471
|
+
const entry = props.get('renderer')[0];
|
|
472
|
+
fail(
|
|
473
|
+
'invalid-value',
|
|
474
|
+
`unknown renderer "${renderer}"`,
|
|
475
|
+
entry.node,
|
|
476
|
+
entry.path
|
|
477
|
+
);
|
|
478
|
+
}
|
|
479
|
+
return validateActivity(
|
|
480
|
+
{
|
|
481
|
+
kind: 'chart',
|
|
482
|
+
...readActivity(props),
|
|
483
|
+
id: node.id,
|
|
484
|
+
title: optionalText(props, 'title'),
|
|
485
|
+
description: optionalText(props, 'description'),
|
|
486
|
+
renderer,
|
|
487
|
+
width: sizeOf(props, 'width'),
|
|
488
|
+
height: sizeOf(props, 'height'),
|
|
489
|
+
mark,
|
|
490
|
+
data: {
|
|
491
|
+
values: typeRows(
|
|
492
|
+
rows,
|
|
493
|
+
types,
|
|
494
|
+
mark.type === 'range' ? channels.x2.def.field : undefined
|
|
495
|
+
),
|
|
496
|
+
},
|
|
497
|
+
encoding: encodingOf(channels),
|
|
498
|
+
},
|
|
499
|
+
node,
|
|
500
|
+
path
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/** The kind of a top-level node (`chart`, `gauge`, ...), or `null`. */
|
|
505
|
+
function kindOf(node) {
|
|
506
|
+
const [first] = node.values;
|
|
507
|
+
return first !== undefined && isAtom(first) && CHART_KINDS.includes(first.id)
|
|
508
|
+
? first.id
|
|
509
|
+
: null;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* Every chart of a list of links (a document may hold other elements).
|
|
514
|
+
* @param {readonly import('links-notation').Link[]} links
|
|
515
|
+
* @returns {(ChartSpec|import('./small-model.js').SmallChartSpec)[]}
|
|
516
|
+
* @throws {ChartSpecError} with the position of the bad link
|
|
517
|
+
*/
|
|
518
|
+
export function chartsFromLinks(links) {
|
|
519
|
+
if (!Array.isArray(links)) {
|
|
520
|
+
throw new ChartSpecError(
|
|
521
|
+
'not-a-chart',
|
|
522
|
+
'expected an array of links',
|
|
523
|
+
at([])
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
return unfoldLinks(links).flatMap((node, i) => {
|
|
527
|
+
const kind = kindOf(node);
|
|
528
|
+
if (kind === null) {
|
|
529
|
+
return [];
|
|
530
|
+
}
|
|
531
|
+
return [
|
|
532
|
+
kind === 'chart'
|
|
533
|
+
? readChart(node, [i])
|
|
534
|
+
: kind === 'tag-cloud'
|
|
535
|
+
? readTagCloud(node, [i])
|
|
536
|
+
: readSmallChart(node, [i], kind),
|
|
537
|
+
];
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* The first chart of a list of links, as a normalised spec with resolved
|
|
543
|
+
* data and encodings.
|
|
544
|
+
* @param {readonly import('links-notation').Link[]} links
|
|
545
|
+
* @returns {ChartSpec|import('./small-model.js').SmallChartSpec}
|
|
546
|
+
* @throws {ChartSpecError} with the position of the bad link
|
|
547
|
+
*/
|
|
548
|
+
export function chartFromLinks(links) {
|
|
549
|
+
const [chart] = chartsFromLinks(links);
|
|
550
|
+
if (chart === undefined) {
|
|
551
|
+
throw new ChartSpecError(
|
|
552
|
+
'not-a-chart',
|
|
553
|
+
`no (NAME: chart ...) link (expected one of ${CHART_KINDS.join(', ')})`,
|
|
554
|
+
at([0])
|
|
555
|
+
);
|
|
556
|
+
}
|
|
557
|
+
return chart;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* Parse LiNo text with the official parser and read its first chart.
|
|
562
|
+
* @param {string} text
|
|
563
|
+
*/
|
|
564
|
+
export const parseChart = (text) => chartFromLinks(parseLino(text));
|
|
565
|
+
|
|
566
|
+
/** The LiNo node of a field definition (after its head). */
|
|
567
|
+
function fieldNodes(def) {
|
|
568
|
+
return [
|
|
569
|
+
prop('field', def.field),
|
|
570
|
+
prop('type', def.type),
|
|
571
|
+
...(def.scale
|
|
572
|
+
? [
|
|
573
|
+
prop(
|
|
574
|
+
'scale',
|
|
575
|
+
prop('type', def.scale.type),
|
|
576
|
+
prop('base', String(def.scale.base ?? 10))
|
|
577
|
+
),
|
|
578
|
+
]
|
|
579
|
+
: []),
|
|
580
|
+
...(def.title === null || def.title === undefined
|
|
581
|
+
? []
|
|
582
|
+
: [prop('title', def.title)]),
|
|
583
|
+
];
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/** The `(mark ...)` node of a mark definition. */
|
|
587
|
+
function markNode(mark) {
|
|
588
|
+
const options = [];
|
|
589
|
+
if (mark.interpolate !== undefined && mark.interpolate !== 'linear') {
|
|
590
|
+
options.push(prop('interpolate', mark.interpolate));
|
|
591
|
+
}
|
|
592
|
+
if (mark.point === true) {
|
|
593
|
+
options.push(prop('point', 'true'));
|
|
594
|
+
}
|
|
595
|
+
if (mark.innerRadius) {
|
|
596
|
+
options.push(prop('inner-radius', String(mark.innerRadius)));
|
|
597
|
+
}
|
|
598
|
+
return prop('mark', mark.type, ...options);
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/** The `(encoding ...)` node. */
|
|
602
|
+
function encodingNode(encoding) {
|
|
603
|
+
const channels = CHANNELS.filter((name) => encoding[name] !== undefined).map(
|
|
604
|
+
(name) => {
|
|
605
|
+
const value = encoding[name];
|
|
606
|
+
if (name !== 'tooltip') {
|
|
607
|
+
return prop(name, ...fieldNodes(value));
|
|
608
|
+
}
|
|
609
|
+
return value.length === 1
|
|
610
|
+
? prop('tooltip', ...fieldNodes(value[0]))
|
|
611
|
+
: prop(
|
|
612
|
+
'tooltip',
|
|
613
|
+
...value.map((def) => ({ id: null, values: fieldNodes(def) }))
|
|
614
|
+
);
|
|
615
|
+
}
|
|
616
|
+
);
|
|
617
|
+
return prop('encoding', ...channels);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/** The node of a chart spec. */
|
|
621
|
+
function chartNode(spec) {
|
|
622
|
+
const values = [{ id: 'chart', values: [] }, ...activityNodes(spec)];
|
|
623
|
+
for (const name of ['title', 'description']) {
|
|
624
|
+
if (spec[name] !== null && spec[name] !== undefined) {
|
|
625
|
+
values.push(prop(name, spec[name]));
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
if (spec.renderer !== undefined && spec.renderer !== 'svg') {
|
|
629
|
+
values.push(prop('renderer', spec.renderer));
|
|
630
|
+
}
|
|
631
|
+
for (const name of ['width', 'height']) {
|
|
632
|
+
if (spec[name] !== null && spec[name] !== undefined) {
|
|
633
|
+
values.push(prop(name, String(spec[name])));
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
values.push(
|
|
637
|
+
prop(
|
|
638
|
+
'data',
|
|
639
|
+
...spec.data.values.map((row) =>
|
|
640
|
+
prop(
|
|
641
|
+
'row',
|
|
642
|
+
...Object.entries(row).map(([name, value]) =>
|
|
643
|
+
prop(name, String(value))
|
|
644
|
+
)
|
|
645
|
+
)
|
|
646
|
+
)
|
|
647
|
+
),
|
|
648
|
+
markNode(typeof spec.mark === 'string' ? { type: spec.mark } : spec.mark),
|
|
649
|
+
encodingNode(spec.encoding ?? {})
|
|
650
|
+
);
|
|
651
|
+
return { id: spec.id ?? null, values };
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
/**
|
|
655
|
+
* Write a spec (of any kind) as LiNo text, in the nested form.
|
|
656
|
+
* @param {ChartSpec|import('./small-model.js').SmallChartSpec} spec
|
|
657
|
+
* @returns {string} LiNo text
|
|
658
|
+
*/
|
|
659
|
+
export function chartToLino(spec) {
|
|
660
|
+
const node =
|
|
661
|
+
spec.kind === undefined || spec.kind === 'chart'
|
|
662
|
+
? chartNode(spec)
|
|
663
|
+
: spec.kind === 'tag-cloud'
|
|
664
|
+
? tagCloudNode(spec)
|
|
665
|
+
: smallChartNode(spec);
|
|
666
|
+
return formatNested(node);
|
|
667
|
+
}
|