@gramlot/gramlot 0.3.32 → 0.3.33

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.
Files changed (60) hide show
  1. package/dist/components/charts/__init__.py +1 -1
  2. package/dist/components/charts/assets/collection.js +36 -21
  3. package/dist/components/charts/assets/index.js +1 -1
  4. package/dist/components/charts/grammar.json +15482 -232
  5. package/dist/gramlot.js +22 -10
  6. package/dist/gramlot.min.js +2 -2
  7. package/dist/standard-collections.js +1 -1
  8. package/package.json +1 -1
  9. package/src/components/chart-boxplot-options.js +22 -0
  10. package/src/components/chart-candlestick-options.js +27 -0
  11. package/src/components/chart-controls.js +28 -6
  12. package/src/components/chart-domains.js +1 -1
  13. package/src/components/chart-engines/echarts.js +18 -0
  14. package/src/components/chart-fields.js +11 -1
  15. package/src/components/chart-funnel-options.js +19 -0
  16. package/src/components/chart-gauge-options.js +98 -0
  17. package/src/components/chart-gauge.js +105 -0
  18. package/src/components/chart-heatmap-options.js +29 -0
  19. package/src/components/chart-notice.js +126 -0
  20. package/src/components/chart-pane.js +7 -1
  21. package/src/components/chart-parallel-options.js +24 -0
  22. package/src/components/chart-radar-options.js +29 -0
  23. package/src/components/chart-renderer.js +2 -8
  24. package/src/components/chart-sankey-options.js +43 -0
  25. package/src/components/chart-sunburst-options.js +12 -0
  26. package/src/components/chart-syntax.js +82 -0
  27. package/src/components/chart-time.js +2 -1
  28. package/src/components/chart-treemap-options.js +16 -0
  29. package/src/components/chart-type-data.js +62 -0
  30. package/src/components/chart-types/area.js +6 -1
  31. package/src/components/chart-types/bar.js +6 -1
  32. package/src/components/chart-types/bar3D.js +7 -1
  33. package/src/components/chart-types/boxplot.js +11 -0
  34. package/src/components/chart-types/candlestick.js +10 -0
  35. package/src/components/chart-types/donut.js +6 -1
  36. package/src/components/chart-types/funnel.js +7 -0
  37. package/src/components/chart-types/heatmap.js +8 -0
  38. package/src/components/chart-types/horizontalBar.js +6 -1
  39. package/src/components/chart-types/index.js +21 -1
  40. package/src/components/chart-types/line.js +6 -1
  41. package/src/components/chart-types/line3D.js +7 -1
  42. package/src/components/chart-types/lines3D.js +7 -1
  43. package/src/components/chart-types/map.js +7 -1
  44. package/src/components/chart-types/map3D.js +6 -1
  45. package/src/components/chart-types/mapBar3D.js +6 -1
  46. package/src/components/chart-types/parallel.js +7 -0
  47. package/src/components/chart-types/pie.js +6 -1
  48. package/src/components/chart-types/polygons3D.js +7 -1
  49. package/src/components/chart-types/radar.js +7 -0
  50. package/src/components/chart-types/sankey.js +8 -0
  51. package/src/components/chart-types/scatter.js +6 -1
  52. package/src/components/chart-types/scatter3D.js +7 -1
  53. package/src/components/chart-types/stackedArea.js +6 -1
  54. package/src/components/chart-types/stackedBar.js +6 -1
  55. package/src/components/chart-types/sunburst.js +7 -0
  56. package/src/components/chart-types/surface.js +7 -1
  57. package/src/components/chart-types/treemap.js +7 -0
  58. package/src/components/charts.js +60 -13
  59. package/src/components/registry.js +9 -5
  60. package/src/page-store-connection.js +15 -5
@@ -0,0 +1,7 @@
1
+ /** Engine-independent treemap configuration: nested Bag, one measure carried by the leaves. */
2
+ export default {id:'treemap',label:'Treemap',controls:[],threeD:false,domains:false,
3
+ doc:'Shows a hierarchy as nested rectangles whose area is the value.',
4
+ syntax:[
5
+ {name:'captionField',parameter:'captionField',count:1,required:true,dtype:null,caption:'Label',doc:'The attribute that names each node of the nested Bag.'},
6
+ {name:'measure',parameter:'valueFields',count:1,required:true,dtype:'numeric',caption:'Measure',doc:'Carried by the leaves; a parent is the sum of its children. Values must not be negative.'}
7
+ ]};
@@ -10,26 +10,27 @@ import {ChartDataView} from './chart-data-view.js';
10
10
  import {ChartGeoJSON} from './chart-geojson.js';
11
11
  import {ChartFields} from './chart-fields.js';
12
12
  import {ChartRenderer} from './chart-renderer.js';
13
+ import {Gauge} from './chart-gauge.js';
14
+ import {ChartNotice,CHART_NOTICE_STYLE,chartErrorSections,chartTypeSections} from './chart-notice.js';
15
+ import {ChartSyntaxError} from './chart-syntax.js';
16
+ import {chartTypes} from './chart-types/index.js';
13
17
 
14
18
  const ChartView = defineComponent({
15
19
  name:'chartView', tag:'gramlot-chart-view', base:Container, sourceContext:true, bagStore:true,
16
20
  parameters:{sampleMin:{dtype:'R',nullable:true,binding:'read'},sampleMax:{dtype:'R',nullable:true,binding:'read'},sampleStep:{dtype:'R',default:1,binding:'read'},sampleStart:{dtype:'R',default:0,binding:'read'},mapLabelMode:{dtype:'T',default:'value',binding:'read'},mapBarScale:{dtype:'T',default:'zero',binding:'read'},selectedTerritory:{dtype:'T',default:'',binding:'twoWay'},configpath:{dtype:'T'},timeEmpty:{dtype:'T',default:'previous',binding:'read'},timeField:{dtype:'T',default:'',binding:'read'},timeValue:{dtype:'X',nullable:true,binding:'read'},timeStep:{dtype:'T',default:'month',binding:'read'},engine:{dtype:'T',default:'echarts',binding:'read'},mapValueField:{dtype:'T',binding:'read'},mapLabelField:{dtype:'T',binding:'read'},geoJSON:{dtype:'X',nullable:true,binding:'read'},geometryField:{dtype:'T',binding:'read'},secondCaptionField:{dtype:'T',binding:'read'},seriesOptions:{dtype:'X',binding:'read'},storepath:{dtype:'T'}, rowMode:{dtype:'T'}, gridId:{dtype:'T'},
17
21
  chartType:{dtype:'T',binding:'read'}, captionField:{dtype:'T',binding:'read'}, valueFields:{dtype:'T',nullable:true,binding:'read'}},
18
22
  implementation:Base => class extends Base {
19
- #sampleView; #timeView; #revision=0; #active=false; #message; #context; #store; #grid; #releaseGrid; #view; #releaseView; #renderer; #resize; #options; #optionsSubscription = Symbol('chart-options');
23
+ #sampleView; #timeView; #revision=0; #active=false; #notice; #context; #store; #grid; #releaseGrid; #view; #releaseView; #renderer; #resize; #options; #optionsSubscription = Symbol('chart-options');
20
24
  constructor() {
21
25
  super();
22
26
  this.control.replaceChildren();
23
27
  const style = this.ownerDocument.createElement('style');
24
- style.textContent = ':host{height:100%;min-height:0}.control-box{height:100%;min-height:0;overflow:hidden}';
28
+ style.textContent = ':host{height:100%;min-height:0}.control-box{height:100%;min-height:0;overflow:hidden}' + CHART_NOTICE_STYLE;
25
29
  this.shadowRoot.append(style);
26
- this.#message = this.ownerDocument.createElement('div');
27
- this.#message.setAttribute('role','alert'); this.#message.hidden = true;
28
- this.#message.style.cssText = 'padding:20px;white-space:normal';
30
+ this.#notice = new ChartNotice(this.ownerDocument);
29
31
  this.control = this.ownerDocument.createElement('div');
30
- this.controlBox.append(this.#message, this.control);
31
- this.controlBox.style.backgroundColor = '#fff';
32
- this.#message.style.color = '#333';
32
+ this.controlBox.append(this.#notice.element, this.control);
33
+ this.controlBox.style.backgroundColor = 'var(--gramlot-surface,#fff)';
33
34
  this.control.style.cssText = 'width:100%;height:100%;min-width:0;background:#fff';
34
35
  this.#resize = new this.ownerDocument.defaultView.ResizeObserver(() => this.#renderer?.resize());
35
36
  }
@@ -122,8 +123,7 @@ const ChartView = defineComponent({
122
123
  }
123
124
  if (!rows.length && !(this.parameters.timeField && this.#view.view.getRows().some(row=>this.#view.view.value(row,this.parameters.timeField)!=null))) {
124
125
  this.control.hidden = true;
125
- this.#message.setAttribute('role','status');
126
- this.#message.textContent = 'No data to display'; this.#message.hidden = false;
126
+ this.#notice.info('No data to display','No data to display',this.#emptySections(typeId));
127
127
  return;
128
128
  }
129
129
  await this.#renderer.prepare(engine,typeId);
@@ -133,10 +133,27 @@ const ChartView = defineComponent({
133
133
  structure.setItem('geoJSON',geoJSON);
134
134
  await this.#renderer.update(structure, this.#options);
135
135
  if(revision!==this.#revision || !this.#renderer)return;
136
- this.#message.setAttribute('role','alert'); this.#message.hidden = true; this.#message.textContent = '';
136
+ this.#notice.hide();
137
137
  this.#renderer.resize();
138
138
  }
139
- catch (error) { if(revision!==this.#revision || !this.#renderer)return; this.control.hidden = true; this.#message.setAttribute('role','alert'); this.#message.textContent = error.message; this.#message.hidden = false; }
139
+ catch (error) {
140
+ if(revision!==this.#revision || !this.#renderer)return;
141
+ this.control.hidden = true;
142
+ const type=chartTypes.find(item=>item.id===typeId) ?? null;
143
+ const title=error instanceof ChartSyntaxError ? `${type.label}: the configuration does not match the chart type` : `${type?.label ?? 'Chart'}: the chart cannot be drawn`;
144
+ this.#notice.error(error.message,title,chartErrorSections(error,type));
145
+ }
146
+ }
147
+ /** Why a chart has no rows to draw, from its own parameters. */
148
+ #emptySections(typeId) {
149
+ const {storepath,rowMode,gridId,timeField}=this.parameters;
150
+ const reasons=[rowMode==='samples' ? 'The chart reads its samples from the bound array, which is empty.'
151
+ : `The chart reads its rows from storepath ${storepath} (rowMode ${rowMode}), which has no rows.`];
152
+ if(gridId)reasons.push(`It shows only the rows that pass the filter of grid ${gridId}: the filter may hide every row.`);
153
+ if(timeField)reasons.push(`Temporal animation is on: no row has a value in ${timeField}, its date field.`);
154
+ reasons.push('Add rows to the store, or clear the filter, to draw the chart.');
155
+ const type=chartTypes.find(item=>item.id===typeId);
156
+ return [{title:'Why the chart is empty',paragraphs:reasons},...(type?chartTypeSections(type):[])];
140
157
  }
141
158
  disconnectedCallback() {
142
159
  this.#active = false;
@@ -149,4 +166,34 @@ const ChartView = defineComponent({
149
166
  }
150
167
  }
151
168
  });
152
- export default defineCollection({name:'charts',assets:{echartsGlLicense:new URL('./licenses/echarts-gl.txt',import.meta.url),clayglLicense:new URL('./licenses/claygl.txt',import.meta.url),plotlyLicense:new URL('./licenses/plotly.txt',import.meta.url),vegaLicense:new URL('./licenses/vega.txt',import.meta.url),uplotLicense:new URL('./licenses/uplot.txt',import.meta.url)},components:[ChartPane,ChartView,ChartFields,ChartGeoJSON,ChartTimeline]});
169
+ /**
170
+ * One element per chart type, generated from its syntax: `chartCandlestick(captionField, open, high, low, close)`.
171
+ * It is chartView with a fixed chartType; a fixed measure role is its own attribute, a list of measures is `measures`.
172
+ * The roles the syntax requires are required attributes, so the Builders reject the element without them.
173
+ */
174
+ function chartTypeElement(type) {
175
+ const name=`chart${type.id[0].toUpperCase()}${type.id.slice(1)}`;
176
+ const tag=`gramlot-chart-${type.id.replace(/([a-z])([A-Z])/g,'$1-$2').toLowerCase()}`;
177
+ const parameters=structuredClone(ChartView.parameters);
178
+ delete parameters.chartType; delete parameters.valueFields;
179
+ const measures=type.syntax.filter(role=>role.parameter==='valueFields');
180
+ for(const role of type.syntax) {
181
+ const spec=role.parameter==='valueFields' ? {dtype:'T',binding:'read'} : parameters[role.parameter];
182
+ delete spec.default; delete spec.nullable;
183
+ if(role.required)spec.required=true;
184
+ parameters[role.parameter==='valueFields'?role.name:role.parameter]=spec;
185
+ }
186
+ return defineComponent({name,tag,base:ChartView.base,sourceContext:true,bagStore:true,parameters,
187
+ doc:`${type.doc} chartView with chartType '${type.id}'.`,
188
+ implementation:Base=>ChartView.implementation(class extends Base {
189
+ projectAttributes(attrs,adapt) {
190
+ const rest=super.projectAttributes(attrs,adapt);
191
+ const values=measures.map(role=>this.parameters[role.name]).filter(Boolean);
192
+ this.parameters={...this.parameters,chartType:type.id,valueFields:values.length?values.join(','):null};
193
+ return rest;
194
+ }
195
+ })});
196
+ }
197
+ const chartTypeElements=chartTypes.map(chartTypeElement);
198
+
199
+ export default defineCollection({name:'charts',assets:{echartsGlLicense:new URL('./licenses/echarts-gl.txt',import.meta.url),clayglLicense:new URL('./licenses/claygl.txt',import.meta.url),plotlyLicense:new URL('./licenses/plotly.txt',import.meta.url),vegaLicense:new URL('./licenses/vega.txt',import.meta.url),uplotLicense:new URL('./licenses/uplot.txt',import.meta.url)},components:[ChartPane,ChartView,ChartFields,ChartGeoJSON,ChartTimeline,Gauge,...chartTypeElements]});
@@ -17,7 +17,7 @@ function annotation(parameter) {
17
17
  }
18
18
 
19
19
  /** Compose effective metadata without invoking a DOM class factory. */
20
- export function defineComponent({name, tag, base, mixins = [], parameters = {}, subTags = '', presentationDefaults = [], dataScope = false, command = false, childParameters = {}, sourceContext = false, bagStore = false, bagStruct = false, dragSource = false, implementation}) {
20
+ export function defineComponent({name, tag, base, doc = null, mixins = [], parameters = {}, subTags = '', presentationDefaults = [], dataScope = false, command = false, childParameters = {}, sourceContext = false, bagStore = false, bagStruct = false, dragSource = false, implementation}) {
21
21
  webcomponentCode({code: tag});
22
22
  if (!/^[a-zA-Z][a-zA-Z0-9]*$/.test(name)) throw new TypeError('Invalid component authoring name');
23
23
  const effective = {...base.parameters};
@@ -28,8 +28,11 @@ export function defineComponent({name, tag, base, mixins = [], parameters = {},
28
28
  }
29
29
  }
30
30
  Object.assign(effective, parameters);
31
- for (const spec of Object.values(effective)) annotation(spec);
32
- return Object.freeze({name, tag, base, subTags, presentationDefaults: Object.freeze([...presentationDefaults]), dataScope, command, bagStore, bagStruct, mixins: Object.freeze([...mixins]),
31
+ for (const spec of Object.values(effective)) {
32
+ annotation(spec);
33
+ if (spec.required !== undefined && (spec.required !== true || Object.hasOwn(spec, 'default'))) throw new TypeError('A required component parameter is true and has no default');
34
+ }
35
+ return Object.freeze({name, tag, base, doc, subTags, presentationDefaults: Object.freeze([...presentationDefaults]), dataScope, command, bagStore, bagStruct, mixins: Object.freeze([...mixins]),
33
36
  labelbox: mixins.some(mixin => mixin.labelbox === true),
34
37
  parameters: freeze(structuredClone(effective)), childParameters: freeze(structuredClone(childParameters)), sourceContext, dragSource, implementation});
35
38
  }
@@ -66,12 +69,13 @@ export class ComponentCollection {
66
69
  return {document_format: {name: 'builder_grammar', version: '1.1'},
67
70
  grammar: {name: this.name, version: null, title: this.name, description: null},
68
71
  abstracts: {}, elements: Object.fromEntries(this.components.map(component => [component.name, {
69
- doc: null, sub_tags: component.subTags, parent_tags: null, inherits_from: null, ns: null,
72
+ doc: component.doc, sub_tags: component.subTags, parent_tags: null, inherits_from: null, ns: null,
70
73
  node_label: null, collection_key: null,
71
74
  attributes: {parameters: [
75
+ // A parameter with `required: true` has no default: the Builders reject the element without it.
72
76
  ...Object.entries(component.parameters).map(([name, spec]) => ({name,
73
77
  kind: 'keyword_only', role: 'attribute', annotation: annotation(spec),
74
- has_default: true, default: spec.default ?? null})),
78
+ ...(spec.required ? {has_default: false} : {has_default: true, default: spec.default ?? null})})),
75
79
  {name: 'attr', kind: 'var_keyword', role: 'attribute',
76
80
  annotation: {kind: 'any'}, has_default: false}],
77
81
  accepts_var_keyword: true, accepts_var_positional: false},
@@ -25,7 +25,7 @@ export class PageStoreConnection {
25
25
  this.frames.registerChannel('server', {
26
26
  changed: peer => this.peerChanged(peer),
27
27
  disconnected: peer => {
28
- if (peer.parent) this.fail(new Error('PageStore parent disconnected'));
28
+ if (peer.parent && this.parent) this.fail(new Error('PageStore parent disconnected'));
29
29
  else this.forget(peer.window);
30
30
  },
31
31
  receive: (peer, message) => {
@@ -33,8 +33,14 @@ export class PageStoreConnection {
33
33
  catch (error) { this.fail(error); }
34
34
  },
35
35
  }, {endpoint: this.endpoint, ready: false});
36
- if (this.parent) return;
37
- address.searchParams.set('pageId', gramlot.pageId);
36
+ this.address = address;
37
+ if (!this.parent) this.open();
38
+ }
39
+
40
+ /** Opens this page's own WebSocket: the page is the root of its family channel. */
41
+ open() {
42
+ const address = this.address;
43
+ address.searchParams.set('pageId', this.gramlot.pageId);
38
44
  this.socket = new this.window.WebSocket(address.href);
39
45
  this.socket.addEventListener('open', () => this.connected());
40
46
  this.socket.addEventListener('message', event => {
@@ -48,9 +54,13 @@ export class PageStoreConnection {
48
54
  }
49
55
 
50
56
  peerChanged(peer) {
51
- if (this.closed || !peer.parent) return;
57
+ if (this.closed || !peer.parent || !this.parent) return;
52
58
  const state = peer.channels.server;
53
- if (peer.origin !== this.origin || state?.endpoint !== this.endpoint) {
59
+ if (!state) {
60
+ // A parent without a PageStore (a plain container page) leaves this page its own socket.
61
+ this.parent = null;
62
+ this.open();
63
+ } else if (peer.origin !== this.origin || state?.endpoint !== this.endpoint) {
54
64
  this.fail(new Error('Parent does not provide the same PageStore endpoint'));
55
65
  } else if (state.ready) this.connected();
56
66
  else if (this.ready) this.fail(new Error('PageStore parent channel closed'));