@motifx-community/ui 0.1.0-beta.14 → 0.1.0-beta.16
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/CHANGELOG.md +113 -0
- package/README.md +2 -2
- package/dist/components/diagram/Diagram.d.ts +607 -0
- package/dist/components/diagram/DiagramAutoPan.d.ts +17 -0
- package/dist/components/diagram/DiagramBends.d.ts +70 -0
- package/dist/components/diagram/DiagramDragging.d.ts +66 -0
- package/dist/components/diagram/DiagramEdgeView.d.ts +78 -0
- package/dist/components/diagram/DiagramGrid.d.ts +13 -0
- package/dist/components/diagram/DiagramHistory.d.ts +16 -0
- package/dist/components/diagram/DiagramLinks.d.ts +177 -0
- package/dist/components/diagram/DiagramMap.d.ts +62 -0
- package/dist/components/diagram/DiagramNodeView.d.ts +143 -0
- package/dist/components/diagram/DiagramOverview.d.ts +20 -0
- package/dist/components/diagram/DiagramPanning.d.ts +34 -0
- package/dist/components/diagram/diagram-alignment.d.ts +2 -0
- package/dist/components/diagram/diagram-auto-pan-host.d.ts +8 -0
- package/dist/components/diagram/diagram-background.d.ts +2 -0
- package/dist/components/diagram/diagram-bends-host.d.ts +17 -0
- package/dist/components/diagram/diagram-card-kind.d.ts +42 -0
- package/dist/components/diagram/diagram-card-node.d.ts +11 -0
- package/dist/components/diagram/diagram-center-options.d.ts +6 -0
- package/dist/components/diagram/diagram-change.d.ts +11 -0
- package/dist/components/diagram/diagram-class-member.d.ts +13 -0
- package/dist/components/diagram/diagram-class-node.d.ts +12 -0
- package/dist/components/diagram/diagram-column-checked-event-args.d.ts +9 -0
- package/dist/components/diagram/diagram-connected-event-args.d.ts +6 -0
- package/dist/components/diagram/diagram-connecting-event-args.d.ts +9 -0
- package/dist/components/diagram/diagram-connection.d.ts +7 -0
- package/dist/components/diagram/diagram-data.d.ts +12 -0
- package/dist/components/diagram/diagram-deleted-event-args.d.ts +8 -0
- package/dist/components/diagram/diagram-deleting-event-args.d.ts +9 -0
- package/dist/components/diagram/diagram-dragging-host.d.ts +47 -0
- package/dist/components/diagram/diagram-drawn-node.d.ts +10 -0
- package/dist/components/diagram/diagram-edge-end.d.ts +2 -0
- package/dist/components/diagram/diagram-edge-path.d.ts +5 -0
- package/dist/components/diagram/diagram-edge-reshaped-event-args.d.ts +8 -0
- package/dist/components/diagram/diagram-edge-reshaping-event-args.d.ts +6 -0
- package/dist/components/diagram/diagram-edge.d.ts +49 -0
- package/dist/components/diagram/diagram-edit.d.ts +10 -0
- package/dist/components/diagram/diagram-export-options.d.ts +14 -0
- package/dist/components/diagram/diagram-export.d.ts +28 -0
- package/dist/components/diagram/diagram-fit-options.d.ts +12 -0
- package/dist/components/diagram/diagram-history-changed-event-args.d.ts +14 -0
- package/dist/components/diagram/diagram-history-step.d.ts +9 -0
- package/dist/components/diagram/diagram-kinds.d.ts +9 -0
- package/dist/components/diagram/diagram-layered.d.ts +39 -0
- package/dist/components/diagram/diagram-layout-direction.d.ts +2 -0
- package/dist/components/diagram/diagram-layout-options.d.ts +24 -0
- package/dist/components/diagram/diagram-layout-type.d.ts +5 -0
- package/dist/components/diagram/diagram-layout.d.ts +33 -0
- package/dist/components/diagram/diagram-line-look.d.ts +6 -0
- package/dist/components/diagram/diagram-line.d.ts +11 -0
- package/dist/components/diagram/diagram-links-host.d.ts +45 -0
- package/dist/components/diagram/diagram-links-options.d.ts +7 -0
- package/dist/components/diagram/diagram-map-props.d.ts +10 -0
- package/dist/components/diagram/diagram-map-target.d.ts +27 -0
- package/dist/components/diagram/diagram-marker.d.ts +6 -0
- package/dist/components/diagram/diagram-mini-map-options.d.ts +13 -0
- package/dist/components/diagram/diagram-mini-map-position.d.ts +2 -0
- package/dist/components/diagram/diagram-node-context.d.ts +26 -0
- package/dist/components/diagram/diagram-node-host.d.ts +23 -0
- package/dist/components/diagram/diagram-node-kind.d.ts +43 -0
- package/dist/components/diagram/diagram-node-move.d.ts +11 -0
- package/dist/components/diagram/diagram-node-resized-event-args.d.ts +8 -0
- package/dist/components/diagram/diagram-node-resizing-event-args.d.ts +6 -0
- package/dist/components/diagram/diagram-node-size.d.ts +8 -0
- package/dist/components/diagram/diagram-node-status.d.ts +2 -0
- package/dist/components/diagram/diagram-node-toggled-event-args.d.ts +7 -0
- package/dist/components/diagram/diagram-node.d.ts +56 -0
- package/dist/components/diagram/diagram-nodes-moved-event-args.d.ts +6 -0
- package/dist/components/diagram/diagram-nodes-moving-event-args.d.ts +6 -0
- package/dist/components/diagram/diagram-panning-host.d.ts +19 -0
- package/dist/components/diagram/diagram-pasted-event-args.d.ts +7 -0
- package/dist/components/diagram/diagram-pasting-event-args.d.ts +11 -0
- package/dist/components/diagram/diagram-path-ends.d.ts +11 -0
- package/dist/components/diagram/diagram-path-result.d.ts +6 -0
- package/dist/components/diagram/diagram-paths.d.ts +35 -0
- package/dist/components/diagram/diagram-point.d.ts +5 -0
- package/dist/components/diagram/diagram-port-place.d.ts +9 -0
- package/dist/components/diagram/diagram-port.d.ts +27 -0
- package/dist/components/diagram/diagram-props.d.ts +159 -0
- package/dist/components/diagram/diagram-reconnected-event-args.d.ts +10 -0
- package/dist/components/diagram/diagram-reconnecting-event-args.d.ts +11 -0
- package/dist/components/diagram/diagram-rect.d.ts +7 -0
- package/dist/components/diagram/diagram-resize-side.d.ts +2 -0
- package/dist/components/diagram/diagram-route.d.ts +10 -0
- package/dist/components/diagram/diagram-selection-changed-event-args.d.ts +9 -0
- package/dist/components/diagram/diagram-selection.d.ts +5 -0
- package/dist/components/diagram/diagram-side.d.ts +2 -0
- package/dist/components/diagram/diagram-table-column.d.ts +13 -0
- package/dist/components/diagram/diagram-table-node.d.ts +12 -0
- package/dist/components/diagram/diagram-tree.d.ts +14 -0
- package/dist/components/diagram/diagram-viewport-changed-event-args.d.ts +8 -0
- package/dist/components/diagram/diagram-viewport-options.d.ts +5 -0
- package/dist/components/diagram/diagram-viewport.d.ts +6 -0
- package/dist/components/diagram/index.d.ts +65 -0
- package/dist/components/diagram/swallow-next-click.d.ts +5 -0
- package/dist/components/expressioneditor/ExpressionEditor.d.ts +111 -0
- package/dist/components/expressioneditor/expression-editor-props.d.ts +24 -0
- package/dist/components/expressioneditor/index.d.ts +2 -0
- package/dist/components/formlayout/FormLayout.d.ts +10 -3
- package/dist/components/formlayout/form-editor-type.d.ts +1 -1
- package/dist/components/formlayout/form-item.d.ts +4 -1
- package/dist/components/formlayout/form-layout-props.d.ts +1 -1
- package/dist/components/formlayout/index.d.ts +1 -0
- package/dist/components/formlayout/schema-items.d.ts +26 -0
- package/dist/components/pivotgrid/model/PivotAxisWindow.d.ts +53 -1
- package/dist/components/pivotgrid/model/PivotCellWindow.d.ts +2 -0
- package/dist/components/pivotgrid/model/PivotWindows.d.ts +3 -6
- package/dist/components/toolbar/Toolbar.d.ts +5 -3
- package/dist/core/control.d.ts +2 -0
- package/dist/core/keyboard.d.ts +5 -0
- package/dist/core/measure/index.d.ts +2 -0
- package/dist/core/measure/picture.d.ts +27 -0
- package/dist/core/shortcut-options.d.ts +5 -2
- package/dist/core/ui-strings.d.ts +191 -0
- package/dist/generated/tokens.d.ts +1615 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +11501 -705
- package/dist/index.js.map +1 -1
- package/dist/locales/en.json +180 -1
- package/dist/locales/tr.json +180 -1
- package/dist/styles.css +2262 -3
- package/dist/tokens/index.d.ts +1615 -0
- package/dist/tokens/index.js +1615 -0
- package/dist/tokens/index.js.map +1 -1
- package/dist/{tr-BGbF9uCs.js → tr-4GNbFdQh.js} +361 -3
- package/dist/{tr-BGbF9uCs.js.map → tr-4GNbFdQh.js.map} +1 -1
- package/docs/accordion.md +1 -1
- package/docs/actionsheet.md +1 -1
- package/docs/appbar.md +1 -1
- package/docs/appmenu-api.md +1 -1
- package/docs/appmenu-programmatic.md +1 -1
- package/docs/appmenu-search.md +1 -1
- package/docs/appmenu.md +1 -1
- package/docs/avatar.md +1 -1
- package/docs/badge.md +1 -1
- package/docs/bar-gauge-angles.md +1 -1
- package/docs/bar-gauge-data.md +1 -1
- package/docs/bar-gauge-labels.md +1 -1
- package/docs/bar-gauge.md +1 -1
- package/docs/barcode.md +1 -1
- package/docs/bottomnavigation.md +1 -1
- package/docs/bottomsheet.md +1 -1
- package/docs/breadcrumb.md +1 -1
- package/docs/button.md +1 -1
- package/docs/buttongroup.md +1 -1
- package/docs/buttons.md +1 -1
- package/docs/calendar.md +1 -1
- package/docs/card.md +1 -1
- package/docs/chart-bars.md +1 -1
- package/docs/chart-candles.md +1 -1
- package/docs/chart-funnel.md +1 -1
- package/docs/chart-heatmap.md +1 -1
- package/docs/chart-lines.md +1 -1
- package/docs/chart-live.md +1 -1
- package/docs/chart-pie.md +1 -1
- package/docs/chart-radar.md +1 -1
- package/docs/chart-ranges.md +1 -1
- package/docs/chart-references.md +1 -1
- package/docs/chart-scatter.md +1 -1
- package/docs/chart-waterfall.md +1 -1
- package/docs/chart-zoom.md +1 -1
- package/docs/chart.md +1 -1
- package/docs/chat.md +1 -1
- package/docs/checkbox.md +1 -1
- package/docs/chip.md +1 -1
- package/docs/choice.md +1 -1
- package/docs/colorbox.md +1 -1
- package/docs/combobox.md +1 -1
- package/docs/commands.md +1 -1
- package/docs/control.md +1 -1
- package/docs/data.md +2 -2
- package/docs/datagrid-api.md +1 -1
- package/docs/datagrid-bands.md +1 -1
- package/docs/datagrid-drag-select.md +1 -1
- package/docs/datagrid-drag.md +1 -1
- package/docs/datagrid-editing.md +1 -1
- package/docs/datagrid-filtering.md +1 -1
- package/docs/datagrid-forms.md +1 -1
- package/docs/datagrid-grouping.md +1 -1
- package/docs/datagrid-layout.md +1 -1
- package/docs/datagrid-lookups.md +1 -1
- package/docs/datagrid-master-detail.md +1 -1
- package/docs/datagrid-scrolling.md +1 -1
- package/docs/datagrid-selection.md +1 -1
- package/docs/datagrid.md +1 -1
- package/docs/datalayer-api.md +491 -0
- package/docs/datalayer-changes.md +501 -0
- package/docs/datalayer-odata.md +998 -0
- package/docs/datalayer-pivot.md +850 -0
- package/docs/datalayer-queries.md +516 -0
- package/docs/datalayer-rest.md +529 -0
- package/docs/datalayer-soap.md +576 -0
- package/docs/datalayer-source.md +464 -0
- package/docs/datalayer.md +264 -0
- package/docs/datebox.md +1 -1
- package/docs/daterangebox.md +1 -1
- package/docs/diagram-api.md +200 -0
- package/docs/diagram-commands.md +79 -0
- package/docs/diagram-connections.md +157 -0
- package/docs/diagram-custom-nodes.md +325 -0
- package/docs/diagram-designer.md +816 -0
- package/docs/diagram-editing.md +62 -0
- package/docs/diagram-flow.md +157 -0
- package/docs/diagram-keyboard.md +58 -0
- package/docs/diagram-layout.md +354 -0
- package/docs/diagram-nodes.md +206 -0
- package/docs/diagram-tables.md +212 -0
- package/docs/diagram.md +217 -0
- package/docs/display.md +1 -1
- package/docs/draggable.md +1 -1
- package/docs/drawer.md +1 -1
- package/docs/dropdown.md +1 -1
- package/docs/editors.md +1 -1
- package/docs/expressioneditor.md +469 -0
- package/docs/expressions.md +431 -0
- package/docs/feedback.md +1 -1
- package/docs/fileuploader.md +1 -1
- package/docs/filterbuilder.md +1 -1
- package/docs/formlayout-schema.md +143 -0
- package/docs/formlayout.md +16 -4
- package/docs/foundations.md +1 -1
- package/docs/gallery.md +1 -1
- package/docs/gauge-base.md +1 -1
- package/docs/gauge-circular.md +1 -1
- package/docs/gauge-dashboards.md +1 -1
- package/docs/gauge-linear.md +1 -1
- package/docs/gauge-markers.md +1 -1
- package/docs/gauge-marks.md +1 -1
- package/docs/gauge-ranges.md +1 -1
- package/docs/gauge-scales.md +1 -1
- package/docs/gauge.md +1 -1
- package/docs/home.md +1 -1
- package/docs/keyboard.md +2 -2
- package/docs/language.md +1 -1
- package/docs/layout.md +1 -1
- package/docs/listbox.md +1 -1
- package/docs/loadpanel.md +1 -1
- package/docs/lookup.md +1 -1
- package/docs/markdown.md +1 -1
- package/docs/menus-api.md +1 -1
- package/docs/menus-context.md +1 -1
- package/docs/menus-customization.md +1 -1
- package/docs/menus-merging.md +1 -1
- package/docs/menus-programmatic.md +1 -1
- package/docs/menus.md +1 -1
- package/docs/multiview.md +1 -1
- package/docs/navigation.md +1 -1
- package/docs/numberbox.md +1 -1
- package/docs/otpinput.md +1 -1
- package/docs/pagination.md +1 -1
- package/docs/pivotgrid-api.md +1 -1
- package/docs/pivotgrid-chart.md +1 -1
- package/docs/pivotgrid-drilldown.md +1 -1
- package/docs/pivotgrid-export.md +1 -1
- package/docs/pivotgrid-fields.md +1 -1
- package/docs/pivotgrid-keyboard.md +1 -1
- package/docs/pivotgrid-layout.md +1 -1
- package/docs/pivotgrid-panel.md +1 -1
- package/docs/pivotgrid-remote.md +122 -44
- package/docs/pivotgrid-sorting.md +3 -3
- package/docs/pivotgrid-state.md +1 -1
- package/docs/pivotgrid-summaries.md +1 -1
- package/docs/pivotgrid.md +1 -1
- package/docs/popover.md +1 -1
- package/docs/progress.md +1 -1
- package/docs/qrcode.md +1 -1
- package/docs/radiogroup.md +1 -1
- package/docs/rating.md +1 -1
- package/docs/resizable.md +1 -1
- package/docs/ribbonmenu-api.md +1 -1
- package/docs/ribbonmenu-commands.md +1 -1
- package/docs/ribbonmenu-customization.md +1 -1
- package/docs/ribbonmenu-display.md +1 -1
- package/docs/ribbonmenu-layouts.md +1 -1
- package/docs/ribbonmenu-programmatic.md +1 -1
- package/docs/ribbonmenu.md +1 -1
- package/docs/scheduler-agenda.md +1 -1
- package/docs/scheduler-big-data.md +1 -1
- package/docs/scheduler-editing.md +1 -1
- package/docs/scheduler-form.md +1 -1
- package/docs/scheduler-icalendar.md +1 -1
- package/docs/scheduler-mobile.md +1 -1
- package/docs/scheduler-recurrence.md +1 -1
- package/docs/scheduler-resources.md +1 -1
- package/docs/scheduler-timeline.md +1 -1
- package/docs/scheduler.md +1 -1
- package/docs/scrollview.md +1 -1
- package/docs/shortcuteditor.md +1 -1
- package/docs/showcase-flowstudio.md +61 -0
- package/docs/showcase-home.md +1 -1
- package/docs/showcase-mail.md +1 -1
- package/docs/showcase.md +1 -1
- package/docs/signature.md +1 -1
- package/docs/skeleton.md +1 -1
- package/docs/slider.md +1 -1
- package/docs/sortable.md +1 -1
- package/docs/sparkline.md +1 -1
- package/docs/speeddial.md +1 -1
- package/docs/splitter.md +1 -1
- package/docs/stepper.md +1 -1
- package/docs/switch.md +1 -1
- package/docs/tabdocs-dock.md +1 -1
- package/docs/tabdocs-drag.md +1 -1
- package/docs/tabdocs-menus.md +1 -1
- package/docs/tabdocs-mobile.md +1 -1
- package/docs/tabdocs-tabs.md +1 -1
- package/docs/tabdocs-windows.md +1 -1
- package/docs/tabdocs-workspace.md +1 -1
- package/docs/tabdocs.md +1 -1
- package/docs/tabs.md +1 -1
- package/docs/tagbox.md +1 -1
- package/docs/textarea.md +1 -1
- package/docs/textbox.md +1 -1
- package/docs/tileview.md +1 -1
- package/docs/timeline.md +1 -1
- package/docs/toast.md +1 -1
- package/docs/tokens.md +324 -1
- package/docs/toolbar-api.md +1 -1
- package/docs/toolbar-customization.md +1 -1
- package/docs/toolbar.md +1 -1
- package/docs/tooltip.md +1 -1
- package/docs/treeview.md +1 -1
- package/docs/validationsummary.md +1 -1
- package/docs/window.md +1 -1
- package/docs/wizard.md +1 -1
- package/llms-full.txt +10142 -462
- package/llms.txt +32 -1
- package/package.json +2 -2
|
@@ -0,0 +1,850 @@
|
|
|
1
|
+
# Data layer: Pivots and loading in parts
|
|
2
|
+
|
|
3
|
+
> Data layer · @motifx-community/data 0.1.0-beta.3
|
|
4
|
+
|
|
5
|
+
This page describes how a pivot is loaded through a data adapter, whole or [in parts](#parts) as a view scrolls, and what a server answers for each protocol. The [PivotGrid](pivotgrid.md) component issues these loads by itself ([remote data](pivotgrid-remote.md)); this page is for code that loads pivots directly, and for the backend that answers it.
|
|
6
|
+
|
|
7
|
+
## Loading a pivot
|
|
8
|
+
|
|
9
|
+
`loadPivot` on any adapter resolves to the members of both axes and the measures of their crossings. `rows` and `columns` list the levels of the axes, `measures` the values computed for every crossing and every total, and `filter` keeps rows out before anything is grouped ([filters as data](datalayer-queries.md)). On a [`DataSource`](datalayer-source.md), `loadPivot` runs over the current query: the source's filter and search are joined with the pivot's own filter, and the source's `params` and `expand` apply unless the pivot gives its own. The rows the source holds stay as they are.
|
|
10
|
+
|
|
11
|
+
```tsx
|
|
12
|
+
const p = await source.loadPivot({
|
|
13
|
+
filter: data.where('amount', '>', 100),
|
|
14
|
+
rows: [{ field: 'region' }, { field: 'city' }],
|
|
15
|
+
columns: [{ field: 'date', interval: 'year' }],
|
|
16
|
+
measures: [{ type: 'sum', field: 'amount' }, { type: 'count' }],
|
|
17
|
+
expandedRows: [['Aegean']],
|
|
18
|
+
});
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`expandedRows` and `expandedColumns` name the members whose children are computed, each by its path: the keys from the top level down. `true`, the default, computes every level whole. A member is shown only when every member above it is expanded and shown, and the top members always are; a path whose own parents are not listed therefore reaches nothing.
|
|
22
|
+
|
|
23
|
+
## Reading the result
|
|
24
|
+
|
|
25
|
+
`p.rows` and `p.columns` are the roots of the axes. A root stands for every row, so its values are the grand totals, and its `children` are the top members. A member carries its `key` (the value of its level, `null` for empty values), its `path` from the top level down, its `count` of rows and, when it is expanded, its `children`, ordered by the level's `dir` with the empty member last. `p.values(row, column)` returns the measures where a row member and a column member cross, in the order of `measures`, or `undefined` when no row falls in both. Totals are computed from their rows, not from the values below them: the average of a total is the average of all its rows.
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
/** A sale of the first example: a region, a city, a day and an amount. */
|
|
29
|
+
export interface Sale {
|
|
30
|
+
id: number;
|
|
31
|
+
region: string;
|
|
32
|
+
city: string;
|
|
33
|
+
date: string;
|
|
34
|
+
amount: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const PLACES = [
|
|
38
|
+
{ region: 'Marmara', cities: ['İstanbul', 'Bursa', 'Kocaeli'] },
|
|
39
|
+
{ region: 'Aegean', cities: ['İzmir', 'Denizli', 'Muğla'] },
|
|
40
|
+
{ region: 'Mediterranean', cities: ['Antalya', 'Mersin', 'Adana'] },
|
|
41
|
+
] as const;
|
|
42
|
+
|
|
43
|
+
/** `n` sales over three regions and the years 2024 to 2026, the same ones every time. */
|
|
44
|
+
export function makeSales(n = 405): Sale[] {
|
|
45
|
+
return Array.from({ length: n }, (_, i) => {
|
|
46
|
+
const place = PLACES[i % PLACES.length];
|
|
47
|
+
const year = 2024 + (Math.floor(i / 9) % 3);
|
|
48
|
+
const month = String(((i * 5) % 12) + 1).padStart(2, '0');
|
|
49
|
+
const day = String(((i * 11) % 28) + 1).padStart(2, '0');
|
|
50
|
+
return {
|
|
51
|
+
id: i + 1,
|
|
52
|
+
region: place.region,
|
|
53
|
+
city: place.cities[Math.floor(i / 3) % place.cities.length],
|
|
54
|
+
date: `${year}-${month}-${day}`,
|
|
55
|
+
amount: ((i * 7919) % 90_000) / 100,
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
The sales are rows in an [`ArrayAdapter`](datalayer.md), so the pivot is grouped in memory. Open adds a region's path to `expandedRows` and loads again, which computes that region's cities. Every number in the table is one `p.values(row, column)` read: the Total column reads against `p.columns` itself, and the first line against `p.rows`.
|
|
62
|
+
|
|
63
|
+
## Levels, intervals and order
|
|
64
|
+
|
|
65
|
+
A level groups the rows that share a value of its `field`; a dotted path reads nested values (`'customer.country'`). Values are told apart the way [grouping](datalayer-queries.md) tells them apart: text without case in the adapter's locale, declared `date` fields by day; a member's key is the value of the first row that has it. The same field may stand on several levels with different intervals, a year above a month.
|
|
66
|
+
|
|
67
|
+
`interval` groups a field's values further. Date parts are read in local time: `'year'` (2024), `'quarter'` (1–4), `'month'` (1–12), `'day'` (the day of the month, 1–31), `'dayOfWeek'` (0 Sunday to 6 Saturday) and `'date'` (the calendar day, a `Date` at local midnight). A number groups numbers in ranges of that width, each member keyed by the start of its range: with `100` the keys are 0, 100, 200, and a value on a boundary belongs to the range it starts. `dir` orders a level's members by their keys, ascending by default; empty values form one member, which comes last either way. `value` reads the level's value with a function instead of `field`: such a level is computed in memory only.
|
|
68
|
+
|
|
69
|
+
## Measures
|
|
70
|
+
|
|
71
|
+
A measure is a summary computed for every crossing and total: `type` is `'count'`, `'countDistinct'`, `'sum'`, `'min'`, `'max'` or `'avg'` over `field`, as [summaries](datalayer-queries.md) compute them. `skipEmpty` (default `true`) leaves empty values (`null`, `undefined`, `''`, `NaN`) out; with `false`, `sum` and `avg` count them as 0, in memory only: a backend computes its own way. A measure may read its value with a function (`value`), and `{ type: 'custom', start, add, result }` is a summary of your own: each crossing starts a state with `start`, takes its rows in through `add` and ends with `result`. Both run in memory only: a pivot that uses them loads the rows and groups them in memory even where the backend computes pivots.
|
|
72
|
+
|
|
73
|
+
```tsx
|
|
74
|
+
// The median of amount: a custom measure, computed in memory.
|
|
75
|
+
const median: data.PivotCustomMeasure<Sale, number[]> = {
|
|
76
|
+
type: 'custom',
|
|
77
|
+
field: 'amount',
|
|
78
|
+
start: () => [],
|
|
79
|
+
add: (list, value) => (typeof value === 'number' ? [...list, value] : list),
|
|
80
|
+
result: (list) => {
|
|
81
|
+
const sorted = [...list].sort((a, b) => a - b);
|
|
82
|
+
return sorted.length ? sorted[(sorted.length - 1) >> 1] : null;
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
const p = await source.loadPivot({ rows: [{ field: 'region' }], measures: [median] });
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Keeping members
|
|
89
|
+
|
|
90
|
+
`keep` keeps rows by their members, whether the field stands on an axis or not. Each entry reads `field` through `intervals`, the way the axes' levels read theirs, and `paths` names members from the first interval down: with `intervals: ['year', 'quarter']`, `[2024]` is a year and `[2024, 2]` its second quarter. `type` is `'include'` (the default) or `'exclude'`, `null` in a path stands for empty values, and keys are told apart the way members are. An entry's `filter` counts the rows it passes among the members named: they are kept with `'include'` and left out with `'exclude'`, so one entry carries members and conditions together, the way a pivot header filter offers both. Every entry of `keep` applies.
|
|
91
|
+
|
|
92
|
+
```tsx
|
|
93
|
+
keep: [
|
|
94
|
+
{ field: 'date', intervals: ['year', 'quarter'], paths: [[2024, 1], [2024, 2]] },
|
|
95
|
+
{ field: 'region', paths: [['Marmara'], [null]], type: 'exclude' },
|
|
96
|
+
{ field: 'date', intervals: ['year'], paths: [[2024]], filter: data.where('amount', '>', 1000) },
|
|
97
|
+
]
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Where the pivot is computed
|
|
101
|
+
|
|
102
|
+
A backend that computes pivots is asked for the groups of the pivot; `capabilities.pivot` says whether it does. The [`ODataAdapter`](datalayer-odata.md) computes pivots with its `aggregation` option, the [`RestAdapter`](datalayer-rest.md) with a `pivot` endpoint, the [`SoapAdapter`](datalayer-soap.md) with a `pivot` operation; an [`ArrayAdapter`](datalayer.md) always groups in memory. Where the pivot needs memory (a level, measure or `keep` entry with `value`, a custom measure, and for OData an interval the service cannot compute), the rows that `filter` passes are loaded with `load` and grouped in memory; `queryPivot` is that grouping, and `adapter.computesPivot(options)` tells which way a pivot goes. The memory engine computes over the rows loaded exactly what a backend is asked for, so the result reads the same either way.
|
|
103
|
+
|
|
104
|
+
## Loading in parts
|
|
105
|
+
|
|
106
|
+
A view that shows a large pivot does not read the whole result: it reads the members and values it shows, as it scrolls. Four calls carry that, on every adapter and on a `DataSource`, where they run over the current query as `loadPivot` does. A backend with `capabilities.pivotWindow` computes them: OData with `aggregation`, REST with `pivot` unless it says `window: false`, SOAP with a `pivot` operation likewise. Otherwise, and where a part needs memory, the rows are loaded and grouped in memory, as for `loadPivot`. [PivotGrid](pivotgrid-remote.md) issues these calls by itself.
|
|
107
|
+
|
|
108
|
+
`loadPivotMembers` returns members of one level of an axis. Each slice asks for a part of one parent's children: from `skip` on, `take` of them, with the number of all of them (`total`); every parent stands on the same level. The members come in the order `loadPivot` gives them (the level's `dir`, empty values last), or ordered by a measure with `sortBySummary`: the measure's place in `measures`, read at the other axis's total or in one member of the other axis named by `levels` and `path`; members without a value go last, and members of the same value keep the order of their values. Each member carries its `key`, `path`, `count` and `values`, the measures at the other axis's total. `childCounts` adds each member's `childCount` on the next level; `descendants` counts the members below each member, so many levels down. `find` names members of the level by their paths, and `found` gives each one's place among its parent's children, -1 for one with no rows: where a view places a member shown open that the parts it loaded do not hold.
|
|
109
|
+
|
|
110
|
+
```tsx
|
|
111
|
+
// The 101st to 200th cities of the Aegean by their amount, largest last; and how many there are.
|
|
112
|
+
const m = await source.loadPivotMembers({
|
|
113
|
+
levels: [{ field: 'region' }, { field: 'city' }],
|
|
114
|
+
slices: [{ parent: ['Aegean'], skip: 100, take: 100 }],
|
|
115
|
+
measures: [{ type: 'sum', field: 'amount' }],
|
|
116
|
+
sortBySummary: { measure: 0 },
|
|
117
|
+
});
|
|
118
|
+
m.slices[0].total; // every city of the Aegean
|
|
119
|
+
m.slices[0].members; // [{ key, path, count, values }], values at the other axis's total
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
With `nonEmpty`, members whose measures are all empty are left out of `members`, `total`, `childCount` and `found` (-1 for a member left out). `data.allEmpty(values)` is the test: there is a value and every one is empty. A `count`, `countDistinct` or `sum` always has a value (0 for no rows), so with one of them, or without measures, no member is left out; the empty member (key `null`) is left out only as any other is, by its measures.
|
|
123
|
+
|
|
124
|
+
`loadPivotCounts` returns the number of children of members: `paths` names each member by its keys from the top, `[]` counts the top level's members, and the result's `childCount(path)` gives each number, 0 for a member with no rows. Its `measures` are read only with `nonEmpty`: the children counted are then those `loadPivotMembers` would keep.
|
|
125
|
+
|
|
126
|
+
`loadPivotBlocks` counts what stands below the children of `parents`, a block of `size` children at a time (one block per parent without `size`): per block, `depth + 1` numbers, the block's children first, then the members on each level below them, the children taken in the order `loadPivotMembers` gives them and without those `nonEmpty` leaves out. A view that opens a level by default places every line this way without reading every member; `descendants` of `loadPivotMembers` gives the same numbers for the members of a part.
|
|
127
|
+
|
|
128
|
+
`loadPivotCells` returns the measures where row members and column members cross: every path of `rowPaths` with every path of `columnPaths`, each member at its own depth, `[]` for a total. The result's `values(rowPath, columnPath)` gives the measures in order, `undefined` where no row falls in both, and `count(rowPath, columnPath)` the number of rows in both. `running` asks for running totals: each a measure added up over a member's siblings on one axis, from the first sibling up to the member itself, the siblings in the order `loadPivotMembers` gives them (`orders` names an order by a measure per level; `null` keeps a level's own order). The result's `running(rowPath, columnPath)` gives one value per running total, `null` where none of the siblings has a value. A backend that answers without running totals has them added up by the adapter, which reads each parent's children and their values first.
|
|
129
|
+
|
|
130
|
+
`capabilities.pivotBlocks` says whether the backend answers `loadPivotBlocks` and the `descendants` of `loadPivotMembers`: OData with `aggregation`, REST and SOAP where their `pivot` says `blocks: true`. Without it those two are worked out in memory over the loaded rows, and a view such as the PivotGrid reads the members of a level open by default whole instead of asking for blocks.
|
|
131
|
+
|
|
132
|
+
## REST: the pivot endpoint
|
|
133
|
+
|
|
134
|
+
`pivot: { url }` on a [`RestAdapter`](datalayer-rest.md) names one endpoint that computes pivots. `loadPivot` sends it a `POST` (`method` changes that) whose JSON body `defaultPivotBody` writes; an operation's `params` go in the query string. The body always carries every field, a missing option written as its default:
|
|
135
|
+
|
|
136
|
+
```tsx
|
|
137
|
+
{
|
|
138
|
+
"filter": { "field": "amount", "op": ">", "value": 100 },
|
|
139
|
+
"keep": [{ "field": "date", "intervals": ["year"], "paths": [[2024]], "type": "include" }],
|
|
140
|
+
"rows": [{ "field": "region" }, { "field": "city" }],
|
|
141
|
+
"columns": [{ "field": "date", "interval": "year" }],
|
|
142
|
+
"measures": [{ "type": "sum", "field": "amount" }, { "type": "count" }],
|
|
143
|
+
"expandedRows": [["Aegean"]],
|
|
144
|
+
"expandedColumns": true
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
The endpoint answers with the groups: a JSON array, or an object whose `groups` holds it. A group is one crossing: the keys of the row member and of the column member from the top level down, the number of rows in the crossing, and the measures in order. The answer holds the groups of every pair of depths: for every row depth from 0 (the total of the axis) to the number of row levels, and every column depth likewise, the rows grouped by the levels down to those depths. From depth 2 a group is sent only under a member that is expanded and shown itself; with the body above, the cities of the Aegean alone. The members of an axis are read from the groups where the other axis is at its total, so those groups decide which members exist; a group under a member they do not name is ignored. Keys and values arrive as JSON and are read by the level and the declared `fields`: date parts and ranges as numbers, `'date'` intervals and declared dates as dates, declared numbers as numbers; `count` and `sum` as numbers, `avg` as a number or `null`.
|
|
149
|
+
|
|
150
|
+
```tsx
|
|
151
|
+
[
|
|
152
|
+
{ "rows": [], "columns": [], "count": 180, "values": [95400.0, 180] },
|
|
153
|
+
{ "rows": [], "columns": [2024], "count": 90, "values": [46100.0, 90] },
|
|
154
|
+
{ "rows": ["Aegean"], "columns": [], "count": 60, "values": [31200.0, 60] },
|
|
155
|
+
{ "rows": ["Aegean"], "columns": [2024], "count": 30, "values": [15400.0, 30] },
|
|
156
|
+
{ "rows": ["Aegean", "İzmir"], "columns": [], "count": 21, "values": [9800.0, 21] },
|
|
157
|
+
{ "rows": ["Aegean", "İzmir"], "columns": [2024], "count": 11, "values": [5200.0, 11] }
|
|
158
|
+
]
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
The same endpoint answers the parts a view loads as it scrolls, the body marked with `kind`: `'members'`, `'counts'`, `'cells'` or `'blocks'`. `defaultPivotPartBody` writes them, with the levels, measures and `keep` written as in the whole-pivot body; `writePart` and `readPart` replace the writing and the reading. `window: false` declares an endpoint that answers whole pivots only: a view then loads the pivot whole. `blocks: true` declares that the endpoint answers `kind: 'blocks'` and the `descendants` of a members body (`capabilities.pivotBlocks`).
|
|
162
|
+
|
|
163
|
+
### The members body
|
|
164
|
+
|
|
165
|
+
```tsx
|
|
166
|
+
{
|
|
167
|
+
"kind": "members",
|
|
168
|
+
"filter": null,
|
|
169
|
+
"keep": [],
|
|
170
|
+
"levels": [{ "field": "region" }, { "field": "city" }],
|
|
171
|
+
"slices": [{ "parent": ["Aegean"], "skip": 100, "take": 100 }],
|
|
172
|
+
"measures": [{ "type": "sum", "field": "amount" }],
|
|
173
|
+
"sortBySummary": { "measure": 0, "levels": [], "path": [] },
|
|
174
|
+
"childCounts": false,
|
|
175
|
+
"find": [],
|
|
176
|
+
"nonEmpty": false
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
`take` is left out when every child from `skip` on is wanted; `sortBySummary` is `null` for the levels' own order, and its `path` names a member of the other axis by its `levels`; `descendants`, a number of levels, is added where it is asked for. The answer gives one slice per slice asked, in order:
|
|
181
|
+
|
|
182
|
+
```tsx
|
|
183
|
+
{ "slices": [{ "total": 143, "members": [{ "key": "City 102", "count": 7, "values": [1053.0] }] }] }
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
A member carries `childCount` when `childCounts` was asked, and `descendants`, a number per level asked, when `descendants` was. The answer carries `found`, one place per member of `find` (-1 for one with no rows), whenever `find` was not empty. A slice without `total` counts the members sent as all of them. With `nonEmpty: true` the members whose measures are all empty are left out of the whole answer: `members`, `total`, `childCount` and `found` alike.
|
|
187
|
+
|
|
188
|
+
### The counts body
|
|
189
|
+
|
|
190
|
+
```tsx
|
|
191
|
+
{
|
|
192
|
+
"kind": "counts",
|
|
193
|
+
"filter": null,
|
|
194
|
+
"keep": [],
|
|
195
|
+
"levels": [{ "field": "region" }, { "field": "city" }],
|
|
196
|
+
"paths": [[], ["Aegean"]],
|
|
197
|
+
"measures": [],
|
|
198
|
+
"nonEmpty": false
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
It is answered with the number of children of each member, on the level below it; a path the answer does not hold counts as 0:
|
|
203
|
+
|
|
204
|
+
```tsx
|
|
205
|
+
{ "counts": [{ "path": [], "count": 6 }, { "path": ["Aegean"], "count": 30 }] }
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### The cells body
|
|
209
|
+
|
|
210
|
+
```tsx
|
|
211
|
+
{
|
|
212
|
+
"kind": "cells",
|
|
213
|
+
"filter": null,
|
|
214
|
+
"keep": [],
|
|
215
|
+
"rows": [{ "field": "region" }, { "field": "city" }],
|
|
216
|
+
"columns": [{ "field": "date", "interval": "year" }],
|
|
217
|
+
"measures": [{ "type": "sum", "field": "amount" }],
|
|
218
|
+
"rowPaths": [["Aegean"], ["Aegean", "City 012"]],
|
|
219
|
+
"columnPaths": [[], [2024]],
|
|
220
|
+
"running": [{ "measure": 0, "axis": "rows", "orders": [null, null] }]
|
|
221
|
+
}
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
It is answered as a whole pivot is, with the groups of every row path and column path asked. `running` is sent only where running totals are asked for; the groups of such a body may each carry a `running` array (one value per running total, `null` for none), and the crossings no row falls in may then come too, with `count` 0, for their running totals alone. An answer without running totals is also read: the adapter then adds them up itself, with further members and cells requests.
|
|
225
|
+
|
|
226
|
+
### The blocks body
|
|
227
|
+
|
|
228
|
+
```tsx
|
|
229
|
+
{
|
|
230
|
+
"kind": "blocks",
|
|
231
|
+
"filter": null,
|
|
232
|
+
"keep": [],
|
|
233
|
+
"levels": [{ "field": "region" }, { "field": "city" }, { "field": "date", "interval": "year" }],
|
|
234
|
+
"parents": [["Aegean"]],
|
|
235
|
+
"size": 100,
|
|
236
|
+
"depth": 1,
|
|
237
|
+
"measures": [],
|
|
238
|
+
"sortBySummary": null,
|
|
239
|
+
"nonEmpty": false
|
|
240
|
+
}
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
`size` is `null` for one block per parent. It is answered with one entry per parent, in order; each block is `depth + 1` numbers, the block's children first, then the members on each level below them, and a missing `total` counts the blocks' first numbers:
|
|
244
|
+
|
|
245
|
+
```tsx
|
|
246
|
+
{ "blocks": [{ "total": 143, "counts": [[100, 211], [43, 90]] }] }
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
| Option | Default | Description |
|
|
250
|
+
| --- | --- | --- |
|
|
251
|
+
| `url` | — | The endpoint that computes pivots. |
|
|
252
|
+
| `method` | `'POST'` | The method of every pivot request. |
|
|
253
|
+
| `write` | `defaultPivotBody` | Writes the body of a whole pivot from its options. |
|
|
254
|
+
| `read` | `defaultReadPivot` | Reads the groups of the answer: the body when it is an array, or its `groups`. |
|
|
255
|
+
| `window` | `true` | `false` declares an endpoint that answers whole pivots only: a view then loads the pivot whole. |
|
|
256
|
+
| `blocks` | `false` | The endpoint answers `kind: 'blocks'` and the `descendants` of a members body (`capabilities.pivotBlocks`). |
|
|
257
|
+
| `writePart` | `defaultPivotPartBody` | Writes the body of a part from its kind and options. |
|
|
258
|
+
| `readPart` | `defaultReadPivotPart` | Reads a part's answer: for `'cells'` as `read` reads a pivot, the rest as they are. |
|
|
259
|
+
|
|
260
|
+
```tsx
|
|
261
|
+
import { data } from '@motifx-community/data';
|
|
262
|
+
|
|
263
|
+
/** A sale the endpoint holds. */
|
|
264
|
+
export interface SaleRow {
|
|
265
|
+
id: number;
|
|
266
|
+
region: string;
|
|
267
|
+
city: string;
|
|
268
|
+
date: string;
|
|
269
|
+
amount: number;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const REGIONS = ['Marmara', 'Aegean', 'Mediterranean', 'Central Anatolia', 'Black Sea', 'Eastern Anatolia'];
|
|
273
|
+
const CITIES = Array.from({ length: 180 }, (_, c) => `City ${String(c + 1).padStart(3, '0')}`);
|
|
274
|
+
const DAYS = Array.from({ length: 730 }, (_, d) => {
|
|
275
|
+
const day = new Date(2024, 0, 1 + d);
|
|
276
|
+
return `${day.getFullYear()}-${String(day.getMonth() + 1).padStart(2, '0')}-${String(day.getDate()).padStart(2, '0')}`;
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
/** The rows the endpoint holds: 6,000 sales of 180 cities in 6 regions over two years. */
|
|
280
|
+
export const salesRows: SaleRow[] = Array.from({ length: 6000 }, (_, i) => {
|
|
281
|
+
const city = (i * 31) % CITIES.length;
|
|
282
|
+
return { id: i + 1, region: REGIONS[city % REGIONS.length], city: CITIES[city], date: DAYS[(i * 13) % DAYS.length], amount: ((i * 7919) % 90_000) / 100 };
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
// How the engine reads the rows: the locale of texts, and that `date` holds dates (they arrive in JSON as text).
|
|
286
|
+
const ctx: data.EvaluateContext = { locale: 'en-US', fields: { date: 'date' } };
|
|
287
|
+
|
|
288
|
+
/** The endpoint: each body the adapter sends, answered with the memory engine over the rows, as a server would answer it. */
|
|
289
|
+
export function answerPivot(body: Record<string, unknown>): unknown {
|
|
290
|
+
switch (body.kind) {
|
|
291
|
+
case 'members':
|
|
292
|
+
return data.pivotMembers(salesRows, body as unknown as data.PivotMembersOptions<SaleRow>, ctx);
|
|
293
|
+
case 'counts': {
|
|
294
|
+
const o = body as unknown as data.PivotCountsOptions<SaleRow>;
|
|
295
|
+
const counts = data.pivotCounts(salesRows, o, ctx);
|
|
296
|
+
return { counts: o.paths.map((path) => ({ path, count: counts.childCount(path) })) };
|
|
297
|
+
}
|
|
298
|
+
case 'cells':
|
|
299
|
+
return data.pivotCellGroups(salesRows, body as unknown as data.PivotCellsOptions<SaleRow>, ctx);
|
|
300
|
+
case 'blocks':
|
|
301
|
+
return data.pivotBlocks(salesRows, body as unknown as data.PivotBlocksOptions<SaleRow>, ctx);
|
|
302
|
+
default:
|
|
303
|
+
// No `kind`: the body of a whole pivot.
|
|
304
|
+
return data.pivotGroups(salesRows, body as unknown as data.PivotLoadOptions<SaleRow>, ctx);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
The endpoint above runs in the page: the adapter's `fetch` hands each body to the data layer's memory engine over 6,000 sales and answers the JSON a server would. The log under the pivot names each body's `kind` as the view scrolls and members open. The [PivotGrid remote page](pivotgrid-remote.md) shows a larger endpoint of the same shape, a million rows on a server in a worker.
|
|
310
|
+
|
|
311
|
+
## OData: what the adapter sends
|
|
312
|
+
|
|
313
|
+
The [`ODataAdapter`](datalayer-odata.md) computes pivots on any service that takes `$apply` (the `aggregation` option). There is no pivot body to implement: the adapter writes standard OData Data Aggregation requests, so a service that supports `$apply` answers pivots without code of its own. For a whole pivot it sends one `$apply` per pair of depths of the axes: the pivot's filter and `keep` become `filter(…)` steps, the members whose children a depth holds become a filter too, and the levels down to the depths are grouped with the count and the measures:
|
|
314
|
+
|
|
315
|
+
```tsx
|
|
316
|
+
$apply=filter(amount gt 100)
|
|
317
|
+
/compute(year(date) as _c0)
|
|
318
|
+
/filter(region eq 'Aegean')
|
|
319
|
+
/groupby((region,city,_c0),aggregate($count as _n,amount with sum as _m0))
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
A level with an `interval` is computed into the request with `compute` and grouped under its alias, as above; that needs `aggregation: { compute: true }`, a service that takes `compute` (`year()`, `month()`, `day()`, `date()`, `floor()` for number ranges), and the service's clock reads the dates. Without it a pivot with an interval is grouped in memory, and one with `'dayOfWeek'` always is: OData has no function for it. `keep` needs no `compute`: it becomes a filter over the same functions, `filter(year(date) eq 2024)`, a quarter written as `(month(date) add 2) div 3`.
|
|
323
|
+
|
|
324
|
+
`loadPivotMembers` lists a parent's children with the same steps and pages them on the service with `$orderby`, `$skip`, `$top` and `$count=true`. A service orders empty values its own way, so the empty member never reaches `$orderby`: it is read apart (`filter(city eq null)/aggregate(…)`) and takes its place among the members in the adapter. Ordered by a measure, the members come in groups the measure's value tells apart (for a sum: above, at and below zero; for another measure: with and without a value), each group counted first and paged after. With `nonEmpty`, every read filters the members on their measures after it aggregates them:
|
|
325
|
+
|
|
326
|
+
```tsx
|
|
327
|
+
$apply=filter(region eq 'Aegean')/filter(city ne null)
|
|
328
|
+
/groupby((region,city),aggregate($count as _n,amount with avg as _m0))/filter(_m0 ne null)
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
`sortBySummary` with a `path` orders by a measure in one member of the other axis. By default every member of the parent is read and ordered in the adapter. With `aggregation: { conditional: true }`, a service that takes `case` in `compute` (OData 4.01), that order is counted and paged on the service like the others: the measure is fed only that member's rows through a computed `case` value, and members without a row in that member go last whatever the direction:
|
|
332
|
+
|
|
333
|
+
```tsx
|
|
334
|
+
$apply=filter(region eq 'Aegean')
|
|
335
|
+
/compute(case(year(date) eq 2025:amount) as _v,case(year(date) eq 2025:1) as _h)
|
|
336
|
+
/filter(city ne null)
|
|
337
|
+
/groupby((region,city),aggregate($count as _n,amount with sum as _m0,_v with sum as _s,_h with sum as _t))
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
No standard `$apply` sums a block of groups, so `loadPivotBlocks` is read as counts per child, keys and counts alone, two reads per parent and level (the empty child apart), and the blocks are summed in the adapter:
|
|
341
|
+
|
|
342
|
+
```tsx
|
|
343
|
+
$apply=filter(region eq 'Aegean')/filter(city ne null)
|
|
344
|
+
/compute(year(date) as _r2)
|
|
345
|
+
/groupby((region,city,_r2))/groupby((region,city),aggregate($count as _c))
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
A pivot takes several reads. `aggregation: { batch: true }` sends them in one `$batch` request; `aggregation: { pageSize: 5000 }` asks for a read of many groups in an order in pages of that many rows, the first page telling how many there are. Without `pageSize`, the pages a service cuts an answer into (`@odata.nextLink`) are followed.
|
|
349
|
+
|
|
350
|
+
| Option | Default | Description |
|
|
351
|
+
| --- | --- | --- |
|
|
352
|
+
| `compute` | `false` | The service takes `compute`: levels with an interval are computed there (`year()`, `month()`, `day()`, `date()`, `floor()`), by the service's clock. `'dayOfWeek'` has no OData function and is grouped in memory either way. |
|
|
353
|
+
| `conditional` | `false` | The service takes `case` in `compute` (OData 4.01): an order by a measure in a member of the other axis is counted and paged there. |
|
|
354
|
+
| `pageSize` | none | The rows one answer to `$apply` may hold: a read of many groups in an order is asked for in pages of it, with `$skip` and `$top`. Without it the pages the service cuts an answer into are followed. |
|
|
355
|
+
| `batch` | `false` | Sends a pivot's reads in one `$batch` request, to the `batch` option's `url` when it names one. |
|
|
356
|
+
|
|
357
|
+
## SOAP: the pivot operation
|
|
358
|
+
|
|
359
|
+
On a [`SoapAdapter`](datalayer-soap.md), one service operation computes pivots, whole and in parts, with the same bodies and answers as a REST pivot endpoint. By default the operation is called with two parameters, `kind` and `body`: `kind` is `'whole'`, `'members'`, `'counts'`, `'cells'` or `'blocks'`, and `body` is the JSON text of the body a REST endpoint gets for it. The operation returns the JSON text of the REST answer; JSON travels as plain text because XML carries no types. A WCF contract implements it as one operation that takes two strings and returns one.
|
|
360
|
+
|
|
361
|
+
```tsx
|
|
362
|
+
const orders = new data.SoapAdapter<Order, number>({
|
|
363
|
+
url: 'https://example.com/OrderService.svc',
|
|
364
|
+
contract: 'IOrderService',
|
|
365
|
+
key: 'id',
|
|
366
|
+
fields: { date: 'date', amount: 'number' },
|
|
367
|
+
operations: {
|
|
368
|
+
load: { name: 'GetOrders' },
|
|
369
|
+
pivot: { name: 'Pivot', blocks: true },
|
|
370
|
+
},
|
|
371
|
+
});
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
| Option | Default | Description |
|
|
375
|
+
| --- | --- | --- |
|
|
376
|
+
| `name` | — | The operation's name. |
|
|
377
|
+
| `action` | the namespace, the contract and the name | The SOAP action sent with the call. |
|
|
378
|
+
| `args` | `{ kind, body }` | The arguments by parameter name, in the operation's parameter order; `body` is the JSON text of the REST body. |
|
|
379
|
+
| `read` | the result parsed as JSON | Reads the result as the REST defaults read an answer. |
|
|
380
|
+
| `window` | `true` | `false` for an operation that answers whole pivots only: a view then loads the pivot whole. |
|
|
381
|
+
| `blocks` | `false` | The operation answers `kind: 'blocks'` and members' `descendants` (`capabilities.pivotBlocks`). |
|
|
382
|
+
|
|
383
|
+
## A server that holds the rows
|
|
384
|
+
|
|
385
|
+
A server that keeps its rows in a JavaScript process answers every body with the data layer's own engine. Five functions compute the answers over an array of rows, each taking the body as its options and an `EvaluateContext` that gives the locale and the declared field types; declare the types, because JSON carries dates as text. `data.pivotGroups(rows, body, ctx)` gives the groups of a whole pivot; `data.pivotMembers(rows, body, ctx)` the answer of a members body; `data.pivotCounts(rows, body, ctx)` the numbers of a counts body, read with `childCount(path)`; `data.pivotCellGroups(rows, body, ctx)` the groups of a cells body, running totals included; `data.pivotBlocks(rows, body, ctx)` the answer of a blocks body.
|
|
386
|
+
|
|
387
|
+
```tsx
|
|
388
|
+
import { data } from '@motifx-community/data';
|
|
389
|
+
|
|
390
|
+
const ctx: data.EvaluateContext = { locale: 'en-US', fields: { date: 'date' } };
|
|
391
|
+
|
|
392
|
+
// Answers the JSON body of POST /orders/pivot over the rows the server holds.
|
|
393
|
+
function answerPivot(rows: Order[], body: any): unknown {
|
|
394
|
+
switch (body.kind) {
|
|
395
|
+
case 'members':
|
|
396
|
+
return data.pivotMembers(rows, body, ctx);
|
|
397
|
+
case 'counts': {
|
|
398
|
+
const counts = data.pivotCounts(rows, body, ctx);
|
|
399
|
+
return { counts: body.paths.map((path: unknown[]) => ({ path, count: counts.childCount(path) })) };
|
|
400
|
+
}
|
|
401
|
+
case 'cells':
|
|
402
|
+
return data.pivotCellGroups(rows, body, ctx);
|
|
403
|
+
case 'blocks':
|
|
404
|
+
return data.pivotBlocks(rows, body, ctx);
|
|
405
|
+
default:
|
|
406
|
+
return data.pivotGroups(rows, body, ctx); // no kind: a whole pivot
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
The second example on this page serves exactly this handler through the adapter's `fetch`. A server need not compute over every row either: the worker of the [PivotGrid remote page](pivotgrid-remote.md) feeds the same functions only the rows an index finds under the parents asked for, as a database would.
|
|
412
|
+
|
|
413
|
+
## API reference
|
|
414
|
+
|
|
415
|
+
Every option object below also takes the shared load options:
|
|
416
|
+
|
|
417
|
+
| Option | Default | Description |
|
|
418
|
+
| --- | --- | --- |
|
|
419
|
+
| `filter` | none | A [filter](datalayer-queries.md) the rows must pass before anything is grouped; `null` stands for none. |
|
|
420
|
+
| `keep` | none | Keeps rows by their members ([Keeping members](#keep)); every entry applies. |
|
|
421
|
+
| `expand` | none | Related data the rows need when they are grouped in memory; [`ODataAdapter`](datalayer-odata.md) sends it as `$expand`. |
|
|
422
|
+
| `params` | none | Passed to the backend as they are; an HTTP adapter sends them in the query string. |
|
|
423
|
+
| `signal` | none | An `AbortSignal` that cancels the load. |
|
|
424
|
+
|
|
425
|
+
### PivotLoadOptions
|
|
426
|
+
|
|
427
|
+
| Option | Default | Description |
|
|
428
|
+
| --- | --- | --- |
|
|
429
|
+
| `rows` | `[]` | The levels of the row axis, the largest first. |
|
|
430
|
+
| `columns` | `[]` | The levels of the column axis, the largest first. |
|
|
431
|
+
| `measures` | `[]` | The values computed for every crossing and every total, in order. |
|
|
432
|
+
| `expandedRows` | `true` | The row members whose children are computed, each by its path; `true` computes every level whole. |
|
|
433
|
+
| `expandedColumns` | `true` | The column members likewise. |
|
|
434
|
+
|
|
435
|
+
### PivotLoadResult and PivotMember
|
|
436
|
+
|
|
437
|
+
| Field | Description |
|
|
438
|
+
| --- | --- |
|
|
439
|
+
| `rows` | The root of the row axis: the grand total, with the top row members as its `children`. |
|
|
440
|
+
| `columns` | The root of the column axis. |
|
|
441
|
+
| `values(row, column)` | The measures where a row member and a column member cross, in the order of `measures`; `undefined` when no row falls in both. |
|
|
442
|
+
|
|
443
|
+
| Field | Description |
|
|
444
|
+
| --- | --- |
|
|
445
|
+
| `key` | The value of the level (of its interval); `null` for empty values. The root of an axis has none (`undefined`). |
|
|
446
|
+
| `path` | The keys from the top level down to the member; empty for the root. |
|
|
447
|
+
| `count` | The number of rows the member holds. |
|
|
448
|
+
| `children` | The members of the next level, in order, when the member is expanded; the root always is. |
|
|
449
|
+
|
|
450
|
+
### PivotLevel
|
|
451
|
+
|
|
452
|
+
| Option | Default | Description |
|
|
453
|
+
| --- | --- | --- |
|
|
454
|
+
| `field` | — | The field whose values group the rows; a dotted path reads nested values (`'customer.country'`). |
|
|
455
|
+
| `interval` | none | Groups the values further: a date part (`'year'`, `'quarter'`, `'month'`, `'day'`, `'dayOfWeek'`, `'date'`, read in local time), or a number, the width of the ranges that group numbers. |
|
|
456
|
+
| `dir` | `'asc'` | Orders the level's members by their keys; empty values form one member, the last either way. |
|
|
457
|
+
| `value` | none | Reads the value with a function instead of `field`; such a level is grouped in memory only. |
|
|
458
|
+
|
|
459
|
+
### PivotMeasure: PivotSummary and PivotCustomMeasure
|
|
460
|
+
|
|
461
|
+
| Option | Default | Description |
|
|
462
|
+
| --- | --- | --- |
|
|
463
|
+
| `type` | — | `'count'`, `'countDistinct'`, `'sum'`, `'min'`, `'max'` or `'avg'`; `'count'` needs no field. |
|
|
464
|
+
| `field` | none | The field whose values are summarized. |
|
|
465
|
+
| `skipEmpty` | `true` | Leaves empty values (`null`, `undefined`, `''`, `NaN`) out; with `false`, `sum` and `avg` count them as 0. Applied in memory only: a backend computes its own way. |
|
|
466
|
+
| `value` | none | Reads the value with a function instead of `field`; computed in memory only. |
|
|
467
|
+
|
|
468
|
+
| Option | Default | Description |
|
|
469
|
+
| --- | --- | --- |
|
|
470
|
+
| `type` | — | `'custom'`. |
|
|
471
|
+
| `field` | none | The field whose value `add` receives; without it, and without `value`, `add` receives `undefined` and the row. |
|
|
472
|
+
| `value` | none | Reads the value with a function instead of `field`. |
|
|
473
|
+
| `start()` | — | Returns the state a crossing starts with. |
|
|
474
|
+
| `add(state, value, row)` | — | Takes one row into the state and returns the state to keep. |
|
|
475
|
+
| `result(state)` | — | Returns the measure's value from the last state. |
|
|
476
|
+
|
|
477
|
+
### PivotKeep
|
|
478
|
+
|
|
479
|
+
| Option | Default | Description |
|
|
480
|
+
| --- | --- | --- |
|
|
481
|
+
| `field` | — | The field the members are read from; it need not stand on an axis. |
|
|
482
|
+
| `intervals` | none | The intervals the paths run through, the largest first; without them the values are read as they are. |
|
|
483
|
+
| `value` | none | Reads the value with a function instead of `field`; kept in memory only. |
|
|
484
|
+
| `paths` | — | The members kept or left out, each a path of keys from the first interval down; `null` stands for empty values. |
|
|
485
|
+
| `type` | `'include'` | `'include'` keeps the rows of the members named, `'exclude'` keeps every other row. |
|
|
486
|
+
| `filter` | none | A row this filter passes counts as one of the members too: kept with `'include'`, left out with `'exclude'`. |
|
|
487
|
+
|
|
488
|
+
### PivotMembersOptions
|
|
489
|
+
|
|
490
|
+
| Option | Default | Description |
|
|
491
|
+
| --- | --- | --- |
|
|
492
|
+
| `levels` | — | The levels of the axis, the largest first; the members returned stand on the level below the slices' parents. |
|
|
493
|
+
| `slices` | — | The parts wanted, each `{ parent, skip, take }`: the children of `parent` (its keys from the top; `[]` for the top level) from `skip` on, `take` of them, all of them without `take`. Every parent stands on the same level. |
|
|
494
|
+
| `measures` | `[]` | Computed for each member at the other axis's total, in order. |
|
|
495
|
+
| `sortBySummary` | none | Orders the members by a measure instead of by their values (`PivotSummarySort`). |
|
|
496
|
+
| `childCounts` | `false` | Also counts each member's children on the next level (`childCount`). |
|
|
497
|
+
| `descendants` | none | Also counts the members below each member, on this many levels down (`descendants` of each member); it needs `capabilities.pivotBlocks`. |
|
|
498
|
+
| `find` | none | Members of the level whose places among their parent's children are wanted, each by its keys from the top (`found`). |
|
|
499
|
+
| `nonEmpty` | `false` | Leaves out the members whose measures are all empty: of `members`, `total`, `childCount` and `found` alike. |
|
|
500
|
+
|
|
501
|
+
### PivotMembersResult and PivotSliceMember
|
|
502
|
+
|
|
503
|
+
| Field | Description |
|
|
504
|
+
| --- | --- |
|
|
505
|
+
| `slices` | One per slice asked, in order: the slice's `parent`, the `total` of all the parent's children, and the `members` of the part. |
|
|
506
|
+
| `found` | The place of each member `find` named among its parent's children, in order; -1 for one with no rows. |
|
|
507
|
+
|
|
508
|
+
| Field | Description |
|
|
509
|
+
| --- | --- |
|
|
510
|
+
| `key` | The value of the level; `null` for empty values. |
|
|
511
|
+
| `path` | The keys from the top level down to the member. |
|
|
512
|
+
| `count` | The number of rows the member holds. |
|
|
513
|
+
| `values` | The measures of the member at the other axis's total, in order. |
|
|
514
|
+
| `childCount` | The number of the member's children on the next level, with `childCounts`. |
|
|
515
|
+
| `descendants` | The number of members below it on each level down, as many as `descendants` asked for; its children first. |
|
|
516
|
+
|
|
517
|
+
### PivotSummarySort
|
|
518
|
+
|
|
519
|
+
| Option | Default | Description |
|
|
520
|
+
| --- | --- | --- |
|
|
521
|
+
| `measure` | — | The measure ordered by, by its place in `measures`; the level's `dir` turns the order around. Members without a value go last either way, and members of the same value keep the order of their values. |
|
|
522
|
+
| `levels` | none | The levels of the other axis, the largest first, for `path`. |
|
|
523
|
+
| `path` | none | The member of the other axis the measure is read in, by its keys from the top; without it, at its total. |
|
|
524
|
+
|
|
525
|
+
### PivotCountsOptions and PivotCountsResult
|
|
526
|
+
|
|
527
|
+
| Option | Default | Description |
|
|
528
|
+
| --- | --- | --- |
|
|
529
|
+
| `levels` | — | The levels of the axis, the largest first. |
|
|
530
|
+
| `paths` | — | The members whose children are counted, each by its keys from the top; `[]` counts the top level's members. |
|
|
531
|
+
| `measures` | `[]` | Computed for each child at the other axis's total; read only with `nonEmpty`. |
|
|
532
|
+
| `nonEmpty` | `false` | Counts only the children whose measures are not all empty, the members `loadPivotMembers` would keep. |
|
|
533
|
+
|
|
534
|
+
| Field | Description |
|
|
535
|
+
| --- | --- |
|
|
536
|
+
| `childCount(path)` | The number of a member's children, the member named by its path as the request named it; 0 for one with no rows. |
|
|
537
|
+
|
|
538
|
+
### PivotBlocksOptions and PivotParentBlocks
|
|
539
|
+
|
|
540
|
+
| Option | Default | Description |
|
|
541
|
+
| --- | --- | --- |
|
|
542
|
+
| `levels` | — | The levels of the axis, the largest first; the children counted stand on the level below the parents. |
|
|
543
|
+
| `parents` | — | The members whose children are counted, by their keys from the top; `[]` for the top level's members. Every parent stands on the same level. |
|
|
544
|
+
| `size` | none | The children of a block; without it every parent's children are one block. |
|
|
545
|
+
| `depth` | — | The levels counted below the children: 0 counts the children alone, 1 their children too, and so on. |
|
|
546
|
+
| `measures` | `[]` | Computed for each member, for `sortBySummary` and `nonEmpty`. |
|
|
547
|
+
| `sortBySummary` | none | Orders the children as `loadPivotMembers` orders them. |
|
|
548
|
+
| `nonEmpty` | `false` | Counts only the members whose measures are not all empty, on every level. |
|
|
549
|
+
|
|
550
|
+
| Field | Description |
|
|
551
|
+
| --- | --- |
|
|
552
|
+
| `parent` | The parent, by its keys as the request named it. |
|
|
553
|
+
| `total` | The number of its children. |
|
|
554
|
+
| `counts` | Per block of children, in order: `depth + 1` numbers, the block's children first, then the members on each level below them. |
|
|
555
|
+
|
|
556
|
+
### PivotCellsOptions and PivotCellsResult
|
|
557
|
+
|
|
558
|
+
| Option | Default | Description |
|
|
559
|
+
| --- | --- | --- |
|
|
560
|
+
| `rows` | — | The levels of the row axis, the largest first. |
|
|
561
|
+
| `columns` | — | The levels of the column axis. |
|
|
562
|
+
| `measures` | — | The measures of every crossing, in order. |
|
|
563
|
+
| `rowPaths` | — | The row members, each by its keys from the top; `[]` is the total of the rows. |
|
|
564
|
+
| `columnPaths` | — | The column members likewise. |
|
|
565
|
+
| `running` | none | Running totals to return with the measures: each a measure added up over a member's siblings on an axis, from the first up to the member itself (`PivotRunningTotal`). |
|
|
566
|
+
|
|
567
|
+
| Option | Default | Description |
|
|
568
|
+
| --- | --- | --- |
|
|
569
|
+
| `measure` | — | The measure added up, by its place in `measures`. |
|
|
570
|
+
| `axis` | — | `'rows'` or `'columns'`: the axis whose members' siblings are added up. |
|
|
571
|
+
| `orders` | none | How the siblings on each level of the axis are ordered by a measure, as `loadPivotMembers` orders them; `null` keeps a level's own order. |
|
|
572
|
+
|
|
573
|
+
| Field | Description |
|
|
574
|
+
| --- | --- |
|
|
575
|
+
| `values(rowPath, columnPath)` | The measures where the members cross, in order; `undefined` when no row falls in both. |
|
|
576
|
+
| `count(rowPath, columnPath)` | The number of rows in both. |
|
|
577
|
+
| `running(rowPath, columnPath)` | The running totals asked for, one per entry of `running`, `null` where none of the siblings has a value; the method itself is there only when running totals were asked for. |
|
|
578
|
+
|
|
579
|
+
### PivotGroup
|
|
580
|
+
|
|
581
|
+
| Field | Description |
|
|
582
|
+
| --- | --- |
|
|
583
|
+
| `rows` | The keys of the row member, from the top level down; empty for the total of the axis. |
|
|
584
|
+
| `columns` | The keys of the column member likewise. |
|
|
585
|
+
| `count` | The number of rows in the crossing. |
|
|
586
|
+
| `values` | The measures of the crossing, in order. |
|
|
587
|
+
| `running` | For a cells body with `running`: the running totals of the crossing, one per running total, `null` for none. |
|
|
588
|
+
|
|
589
|
+
## Feedback
|
|
590
|
+
|
|
591
|
+
Loading issues…
|
|
592
|
+
|
|
593
|
+
## Example files
|
|
594
|
+
|
|
595
|
+
`LoadPivot.tsx`
|
|
596
|
+
|
|
597
|
+
```tsx
|
|
598
|
+
import { reactive } from '@motifx/core';
|
|
599
|
+
import { data } from '@motifx-community/data';
|
|
600
|
+
import { Button } from '@motifx-community/ui';
|
|
601
|
+
import { makeSales, type Sale } from './sales';
|
|
602
|
+
|
|
603
|
+
interface Cell {
|
|
604
|
+
id: string;
|
|
605
|
+
text: string;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
interface Line {
|
|
609
|
+
id: string;
|
|
610
|
+
depth: number;
|
|
611
|
+
label: string;
|
|
612
|
+
count: number;
|
|
613
|
+
expandable: boolean;
|
|
614
|
+
open: boolean;
|
|
615
|
+
path: readonly unknown[];
|
|
616
|
+
cells: Cell[];
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/** `loadPivot` over sales in memory: the row members down, the years across, `values(row, column)` in the cells. */
|
|
620
|
+
export default function LoadPivot() {
|
|
621
|
+
const s = reactive({ years: [] as { id: string; text: string }[], lines: [] as Line[] });
|
|
622
|
+
const sales = new data.ArrayAdapter<Sale, number>({ data: makeSales(), key: 'id', fields: { date: 'date' } });
|
|
623
|
+
let expanded: unknown[][] = [];
|
|
624
|
+
|
|
625
|
+
const sum = (values: readonly unknown[] | undefined) =>
|
|
626
|
+
values && typeof values[0] === 'number' ? values[0].toLocaleString('en-US', { maximumFractionDigits: 0 }) : '—';
|
|
627
|
+
|
|
628
|
+
const load = async () => {
|
|
629
|
+
const p = await sales.loadPivot({
|
|
630
|
+
rows: [{ field: 'region' }, { field: 'city' }],
|
|
631
|
+
columns: [{ field: 'date', interval: 'year' }],
|
|
632
|
+
measures: [{ type: 'sum', field: 'amount' }],
|
|
633
|
+
expandedRows: expanded,
|
|
634
|
+
});
|
|
635
|
+
const years = p.columns.children ?? [];
|
|
636
|
+
s.years = years.map((y) => ({ id: String(y.key), text: String(y.key) }));
|
|
637
|
+
const lines: Line[] = [];
|
|
638
|
+
const walk = (member: data.PivotMember, depth: number) => {
|
|
639
|
+
const id = JSON.stringify(member.path);
|
|
640
|
+
lines.push({
|
|
641
|
+
id,
|
|
642
|
+
depth,
|
|
643
|
+
label: member.path.length ? String(member.key) : 'All regions',
|
|
644
|
+
count: member.count,
|
|
645
|
+
expandable: member.path.length === 1,
|
|
646
|
+
open: !!member.children,
|
|
647
|
+
path: member.path,
|
|
648
|
+
cells: [...years, p.columns].map((column) => ({ id: `${id}:${String(column.key)}`, text: sum(p.values(member, column)) })),
|
|
649
|
+
});
|
|
650
|
+
for (const child of member.children ?? []) walk(child, depth + 1);
|
|
651
|
+
};
|
|
652
|
+
walk(p.rows, 0);
|
|
653
|
+
s.lines = lines;
|
|
654
|
+
};
|
|
655
|
+
|
|
656
|
+
const toggle = (line: Line) => {
|
|
657
|
+
expanded = line.open ? expanded.filter((path) => JSON.stringify(path) !== line.id) : [...expanded, [...line.path]];
|
|
658
|
+
void load();
|
|
659
|
+
};
|
|
660
|
+
void load();
|
|
661
|
+
|
|
662
|
+
return (
|
|
663
|
+
<div class="table-scroll" tabindex="0">
|
|
664
|
+
<table class="token-table">
|
|
665
|
+
<thead>
|
|
666
|
+
<tr>
|
|
667
|
+
<th>Member</th>
|
|
668
|
+
{s.years.map((y) => (
|
|
669
|
+
<th key={y.id}>{y.text}</th>
|
|
670
|
+
))}
|
|
671
|
+
<th>Total</th>
|
|
672
|
+
</tr>
|
|
673
|
+
</thead>
|
|
674
|
+
<tbody>
|
|
675
|
+
{s.lines.map((line) => (
|
|
676
|
+
<tr key={line.id}>
|
|
677
|
+
<th scope="row">
|
|
678
|
+
<div style={`display: flex; align-items: center; gap: 0.5rem; padding-inline-start: ${line.depth * 1.25}rem`}>
|
|
679
|
+
{line.expandable && <Button text={line.open ? 'Close' : 'Open'} size="sm" variant="ghost" onclick={() => toggle(line)} />}
|
|
680
|
+
<span>{`${line.label} · ${line.count} sales`}</span>
|
|
681
|
+
</div>
|
|
682
|
+
</th>
|
|
683
|
+
{line.cells.map((cell) => (
|
|
684
|
+
<td key={cell.id}>{cell.text}</td>
|
|
685
|
+
))}
|
|
686
|
+
</tr>
|
|
687
|
+
))}
|
|
688
|
+
</tbody>
|
|
689
|
+
</table>
|
|
690
|
+
</div>
|
|
691
|
+
);
|
|
692
|
+
}
|
|
693
|
+
```
|
|
694
|
+
|
|
695
|
+
`sales.ts`
|
|
696
|
+
|
|
697
|
+
```tsx
|
|
698
|
+
/** A sale of the first example: a region, a city, a day and an amount. */
|
|
699
|
+
export interface Sale {
|
|
700
|
+
id: number;
|
|
701
|
+
region: string;
|
|
702
|
+
city: string;
|
|
703
|
+
date: string;
|
|
704
|
+
amount: number;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
const PLACES = [
|
|
708
|
+
{ region: 'Marmara', cities: ['İstanbul', 'Bursa', 'Kocaeli'] },
|
|
709
|
+
{ region: 'Aegean', cities: ['İzmir', 'Denizli', 'Muğla'] },
|
|
710
|
+
{ region: 'Mediterranean', cities: ['Antalya', 'Mersin', 'Adana'] },
|
|
711
|
+
] as const;
|
|
712
|
+
|
|
713
|
+
/** `n` sales over three regions and the years 2024 to 2026, the same ones every time. */
|
|
714
|
+
export function makeSales(n = 405): Sale[] {
|
|
715
|
+
return Array.from({ length: n }, (_, i) => {
|
|
716
|
+
const place = PLACES[i % PLACES.length];
|
|
717
|
+
const year = 2024 + (Math.floor(i / 9) % 3);
|
|
718
|
+
const month = String(((i * 5) % 12) + 1).padStart(2, '0');
|
|
719
|
+
const day = String(((i * 11) % 28) + 1).padStart(2, '0');
|
|
720
|
+
return {
|
|
721
|
+
id: i + 1,
|
|
722
|
+
region: place.region,
|
|
723
|
+
city: place.cities[Math.floor(i / 3) % place.cities.length],
|
|
724
|
+
date: `${year}-${month}-${day}`,
|
|
725
|
+
amount: ((i * 7919) % 90_000) / 100,
|
|
726
|
+
};
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
```
|
|
730
|
+
|
|
731
|
+
`Endpoint.tsx`
|
|
732
|
+
|
|
733
|
+
```tsx
|
|
734
|
+
import { reactive } from '@motifx/core';
|
|
735
|
+
import { data } from '@motifx-community/data';
|
|
736
|
+
import { PivotGrid, type PivotGridField } from '@motifx-community/ui';
|
|
737
|
+
import { answerPivot, salesRows, type SaleRow } from './sales-endpoint';
|
|
738
|
+
|
|
739
|
+
const fields: PivotGridField<SaleRow>[] = [
|
|
740
|
+
{ field: 'region', title: 'Region', area: 'row', expanded: true },
|
|
741
|
+
{ field: 'city', title: 'City', area: 'row' },
|
|
742
|
+
{ field: 'date', title: 'Year', type: 'date', area: 'column', groupInterval: 'year' },
|
|
743
|
+
{ field: 'amount', title: 'Amount', type: 'number', area: 'data', summaryType: 'sum', format: { maximumFractionDigits: 0 } },
|
|
744
|
+
];
|
|
745
|
+
|
|
746
|
+
// One line per request, from its body.
|
|
747
|
+
function describe(body: Record<string, unknown>): string {
|
|
748
|
+
switch (body.kind) {
|
|
749
|
+
case 'members': {
|
|
750
|
+
const slices = body.slices as { parent: unknown[]; skip?: number; take?: number }[];
|
|
751
|
+
return `members: ${slices.length} slice(s), parents on level ${(slices[0]?.parent ?? []).length}${body.descendants ? `, descendants ${body.descendants}` : ''}`;
|
|
752
|
+
}
|
|
753
|
+
case 'counts':
|
|
754
|
+
return `counts: ${(body.paths as unknown[]).length} path(s)`;
|
|
755
|
+
case 'cells':
|
|
756
|
+
return `cells: ${(body.rowPaths as unknown[]).length} row × ${(body.columnPaths as unknown[]).length} column paths`;
|
|
757
|
+
case 'blocks':
|
|
758
|
+
return `blocks: ${(body.parents as unknown[]).length} parent(s), depth ${body.depth}`;
|
|
759
|
+
default:
|
|
760
|
+
return 'a whole pivot';
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/** A REST pivot endpoint served in the page: `fetch` answers each body with the memory engine, and the log names its kind. */
|
|
765
|
+
export default function Endpoint() {
|
|
766
|
+
const s = reactive({ asked: 0, requests: [] as { id: number; text: string }[] });
|
|
767
|
+
|
|
768
|
+
const fetch = async (_url: string, init: RequestInit): Promise<Response> => {
|
|
769
|
+
if (!init.body) return new Response(JSON.stringify(salesRows), { status: 200, headers: { 'content-type': 'application/json' } });
|
|
770
|
+
const body = JSON.parse(String(init.body)) as Record<string, unknown>;
|
|
771
|
+
s.requests.unshift({ id: ++s.asked, text: describe(body) });
|
|
772
|
+
if (s.requests.length > 6) s.requests.pop();
|
|
773
|
+
return new Response(JSON.stringify(answerPivot(body)), { status: 200, headers: { 'content-type': 'application/json' } });
|
|
774
|
+
};
|
|
775
|
+
|
|
776
|
+
const adapter = new data.RestAdapter<SaleRow, number>({
|
|
777
|
+
url: '/sales',
|
|
778
|
+
key: 'id',
|
|
779
|
+
fields: { date: 'date' },
|
|
780
|
+
pivot: { url: '/sales/pivot', blocks: true },
|
|
781
|
+
fetch,
|
|
782
|
+
});
|
|
783
|
+
const source = new data.DataSource<SaleRow, number>({ adapter });
|
|
784
|
+
|
|
785
|
+
return (
|
|
786
|
+
<>
|
|
787
|
+
<PivotGrid style="block-size: 24rem" dataSource={source} fields={fields} label="Sales from a pivot endpoint served in the page" />
|
|
788
|
+
<p class="dl-pivot-asked" aria-live="polite">
|
|
789
|
+
{() => `The endpoint answered ${s.asked} request(s), the newest first:`}
|
|
790
|
+
</p>
|
|
791
|
+
<ol class="dl-pivot-log" aria-label="Pivot requests">
|
|
792
|
+
{s.requests.map((r) => (
|
|
793
|
+
<li key={r.id}>{r.text}</li>
|
|
794
|
+
))}
|
|
795
|
+
</ol>
|
|
796
|
+
</>
|
|
797
|
+
);
|
|
798
|
+
}
|
|
799
|
+
```
|
|
800
|
+
|
|
801
|
+
`sales-endpoint.ts`
|
|
802
|
+
|
|
803
|
+
```tsx
|
|
804
|
+
import { data } from '@motifx-community/data';
|
|
805
|
+
|
|
806
|
+
/** A sale the endpoint holds. */
|
|
807
|
+
export interface SaleRow {
|
|
808
|
+
id: number;
|
|
809
|
+
region: string;
|
|
810
|
+
city: string;
|
|
811
|
+
date: string;
|
|
812
|
+
amount: number;
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
const REGIONS = ['Marmara', 'Aegean', 'Mediterranean', 'Central Anatolia', 'Black Sea', 'Eastern Anatolia'];
|
|
816
|
+
const CITIES = Array.from({ length: 180 }, (_, c) => `City ${String(c + 1).padStart(3, '0')}`);
|
|
817
|
+
const DAYS = Array.from({ length: 730 }, (_, d) => {
|
|
818
|
+
const day = new Date(2024, 0, 1 + d);
|
|
819
|
+
return `${day.getFullYear()}-${String(day.getMonth() + 1).padStart(2, '0')}-${String(day.getDate()).padStart(2, '0')}`;
|
|
820
|
+
});
|
|
821
|
+
|
|
822
|
+
/** The rows the endpoint holds: 6,000 sales of 180 cities in 6 regions over two years. */
|
|
823
|
+
export const salesRows: SaleRow[] = Array.from({ length: 6000 }, (_, i) => {
|
|
824
|
+
const city = (i * 31) % CITIES.length;
|
|
825
|
+
return { id: i + 1, region: REGIONS[city % REGIONS.length], city: CITIES[city], date: DAYS[(i * 13) % DAYS.length], amount: ((i * 7919) % 90_000) / 100 };
|
|
826
|
+
});
|
|
827
|
+
|
|
828
|
+
// How the engine reads the rows: the locale of texts, and that `date` holds dates (they arrive in JSON as text).
|
|
829
|
+
const ctx: data.EvaluateContext = { locale: 'en-US', fields: { date: 'date' } };
|
|
830
|
+
|
|
831
|
+
/** The endpoint: each body the adapter sends, answered with the memory engine over the rows, as a server would answer it. */
|
|
832
|
+
export function answerPivot(body: Record<string, unknown>): unknown {
|
|
833
|
+
switch (body.kind) {
|
|
834
|
+
case 'members':
|
|
835
|
+
return data.pivotMembers(salesRows, body as unknown as data.PivotMembersOptions<SaleRow>, ctx);
|
|
836
|
+
case 'counts': {
|
|
837
|
+
const o = body as unknown as data.PivotCountsOptions<SaleRow>;
|
|
838
|
+
const counts = data.pivotCounts(salesRows, o, ctx);
|
|
839
|
+
return { counts: o.paths.map((path) => ({ path, count: counts.childCount(path) })) };
|
|
840
|
+
}
|
|
841
|
+
case 'cells':
|
|
842
|
+
return data.pivotCellGroups(salesRows, body as unknown as data.PivotCellsOptions<SaleRow>, ctx);
|
|
843
|
+
case 'blocks':
|
|
844
|
+
return data.pivotBlocks(salesRows, body as unknown as data.PivotBlocksOptions<SaleRow>, ctx);
|
|
845
|
+
default:
|
|
846
|
+
// No `kind`: the body of a whole pivot.
|
|
847
|
+
return data.pivotGroups(salesRows, body as unknown as data.PivotLoadOptions<SaleRow>, ctx);
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
```
|