@malloydata/render 0.0.135-dev240325191847 → 0.0.135-dev240326020414

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 (86) hide show
  1. package/dist/component/render-webcomponent.d.ts +6 -0
  2. package/dist/component/render.d.ts +7 -17
  3. package/dist/component/result-context.d.ts +2 -3
  4. package/dist/component/table/table-context.d.ts +9 -0
  5. package/dist/component/{table-layout.d.ts → table/table-layout.d.ts} +2 -2
  6. package/dist/component/table/table.d.ts +9 -0
  7. package/dist/html/html_view.d.ts +1 -1
  8. package/dist/module/index.mjs +145001 -0
  9. package/dist/module/index.umd.js +1643 -0
  10. package/dist/module/style.css +1 -0
  11. package/dist/stories/bars.stories.d.ts +1 -1
  12. package/dist/stories/tables.stories.d.ts +1 -1
  13. package/dist/stories/themes.stories.d.ts +1 -1
  14. package/dist/webcomponent/malloy-render.mjs +90572 -0
  15. package/dist/webcomponent/malloy-render.umd.js +1491 -0
  16. package/dist/webcomponent/style.css +1 -0
  17. package/package.json +21 -7
  18. package/vite.config.ts +25 -0
  19. package/vite.config.webcomponent.ts +25 -0
  20. package/dist/bundle/bundled_renderer.js +0 -139374
  21. package/dist/bundle/bundled_renderer.min.js +0 -2374
  22. package/dist/bundle/main.js +0 -29
  23. package/dist/bundle/renderer.js +0 -71
  24. package/dist/component/bar-chart.js +0 -130
  25. package/dist/component/chart-settings.js +0 -152
  26. package/dist/component/render-numeric-field.js +0 -104
  27. package/dist/component/render-result-metadata.js +0 -91
  28. package/dist/component/render.js +0 -155
  29. package/dist/component/result-context.js +0 -28
  30. package/dist/component/table-layout.js +0 -83
  31. package/dist/component/table.d.ts +0 -35
  32. package/dist/component/table.js +0 -375
  33. package/dist/component/util.js +0 -53
  34. package/dist/component/vega-chart.js +0 -105
  35. package/dist/component/vega-lite-base-spec.js +0 -61
  36. package/dist/component/vega-types.js +0 -34
  37. package/dist/data_styles.js +0 -50
  38. package/dist/drill.js +0 -124
  39. package/dist/html/area_sparkline.js +0 -118
  40. package/dist/html/bar_chart.js +0 -70
  41. package/dist/html/bar_sparkline.js +0 -119
  42. package/dist/html/boolean.js +0 -53
  43. package/dist/html/bytes.js +0 -47
  44. package/dist/html/cartesian_chart.js +0 -110
  45. package/dist/html/chart.js +0 -120
  46. package/dist/html/column_sparkline.js +0 -120
  47. package/dist/html/container.js +0 -44
  48. package/dist/html/currency.js +0 -75
  49. package/dist/html/dashboard.js +0 -242
  50. package/dist/html/data_volume.js +0 -94
  51. package/dist/html/date.js +0 -66
  52. package/dist/html/duration.js +0 -108
  53. package/dist/html/html_view.js +0 -187
  54. package/dist/html/image.js +0 -77
  55. package/dist/html/index.js +0 -28
  56. package/dist/html/json.js +0 -52
  57. package/dist/html/line_chart.js +0 -72
  58. package/dist/html/link.js +0 -57
  59. package/dist/html/list.js +0 -73
  60. package/dist/html/list_detail.js +0 -33
  61. package/dist/html/number.js +0 -73
  62. package/dist/html/percent.js +0 -50
  63. package/dist/html/point_map.js +0 -182
  64. package/dist/html/scatter_chart.js +0 -72
  65. package/dist/html/segment_map.js +0 -151
  66. package/dist/html/shape_map.js +0 -194
  67. package/dist/html/sparkline.js +0 -120
  68. package/dist/html/state_codes.js +0 -137
  69. package/dist/html/table.js +0 -475
  70. package/dist/html/text.js +0 -65
  71. package/dist/html/unsupported.js +0 -72
  72. package/dist/html/utils.js +0 -277
  73. package/dist/html/vega_spec.js +0 -544
  74. package/dist/index.js +0 -47
  75. package/dist/main_renderer_factory.js +0 -101
  76. package/dist/renderer.js +0 -34
  77. package/dist/renderer_factory.js +0 -57
  78. package/dist/renderer_types.js +0 -25
  79. package/dist/stories/bars.stories.js +0 -52
  80. package/dist/stories/basic.stories.js +0 -34
  81. package/dist/stories/image.stories.js +0 -34
  82. package/dist/stories/render-malloy-legacy.js +0 -27
  83. package/dist/stories/tables.stories.js +0 -65
  84. package/dist/stories/themes.stories.js +0 -47
  85. package/dist/stories/util.js +0 -25
  86. package/dist/tags_utils.js +0 -30
@@ -1,29 +0,0 @@
1
- "use strict";
2
- /*
3
- * Copyright 2024 Google LLC
4
- *
5
- * Permission is hereby granted, free of charge, to any person obtaining
6
- * a copy of this software and associated documentation files
7
- * (the "Software"), to deal in the Software without restriction,
8
- * including without limitation the rights to use, copy, modify, merge,
9
- * publish, distribute, sublicense, and/or sell copies of the Software,
10
- * and to permit persons to whom the Software is furnished to do so,
11
- * subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be
14
- * included in all copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
- */
24
- Object.defineProperty(exports, "__esModule", { value: true });
25
- const renderer_1 = require("./renderer");
26
- window.renderMalloyResults = async (resultStr, totalRows, preparedResultStr) => {
27
- return await (0, renderer_1.renderMalloyResults)(resultStr, totalRows, preparedResultStr);
28
- };
29
- //# sourceMappingURL=main.js.map
@@ -1,71 +0,0 @@
1
- "use strict";
2
- /*
3
- * Copyright 2024 Google LLC
4
- *
5
- * Permission is hereby granted, free of charge, to any person obtaining
6
- * a copy of this software and associated documentation files
7
- * (the "Software"), to deal in the Software without restriction,
8
- * including without limitation the rights to use, copy, modify, merge,
9
- * publish, distribute, sublicense, and/or sell copies of the Software,
10
- * and to permit persons to whom the Software is furnished to do so,
11
- * subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be
14
- * included in all copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
- */
24
- Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.renderMalloyResults = void 0;
26
- const malloy_1 = require("@malloydata/malloy");
27
- const html_view_1 = require("../html/html_view");
28
- async function renderMalloyResults(queryResult, totalRows, preparedResult) {
29
- try {
30
- let preparedResultObj;
31
- if (preparedResult._modelDef !== undefined &&
32
- preparedResult._rawQuery !== undefined) {
33
- // prepared result is safely typecasted.
34
- preparedResultObj = preparedResult;
35
- }
36
- else if (preparedResult.inner !== undefined &&
37
- preparedResult.modelDef !== undefined) {
38
- // prepared result is raw json.
39
- preparedResultObj = malloy_1.PreparedResult.fromJson({
40
- query: preparedResult.inner,
41
- modelDef: preparedResult.modelDef,
42
- });
43
- }
44
- else {
45
- throw new Error('preparedResult has missing properties.');
46
- }
47
- const malloyRes = {
48
- queryResult: {
49
- ...preparedResultObj._rawQuery,
50
- result: queryResult,
51
- totalRows: totalRows,
52
- },
53
- modelDef: preparedResultObj._modelDef,
54
- };
55
- const result = malloy_1.Result.fromJSON(malloyRes);
56
- const htmlView = new html_view_1.HTMLView(document).render(result, {
57
- dataStyles: {},
58
- isDrillingEnabled: false,
59
- });
60
- return await htmlView;
61
- }
62
- catch (error) {
63
- // eslint-disable-next-line no-console
64
- console.error('Error rendering results:', error);
65
- const span = document.createElement('span');
66
- span.textContent = 'Unable to render malloy results.';
67
- return span;
68
- }
69
- }
70
- exports.renderMalloyResults = renderMalloyResults;
71
- //# sourceMappingURL=renderer.js.map
@@ -1,130 +0,0 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.BarChart = void 0;
10
- /*
11
- * Copyright 2023 Google LLC
12
- *
13
- * Permission is hereby granted, free of charge, to any person obtaining
14
- * a copy of this software and associated documentation files
15
- * (the "Software"), to deal in the Software without restriction,
16
- * including without limitation the rights to use, copy, modify, merge,
17
- * publish, distribute, sublicense, and/or sell copies of the Software,
18
- * and to permit persons to whom the Software is furnished to do so,
19
- * subject to the following conditions:
20
- *
21
- * The above copyright notice and this permission notice shall be
22
- * included in all copies or substantial portions of the Software.
23
- *
24
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
- */
32
- const lit_1 = require("lit");
33
- const decorators_js_1 = require("lit/decorators.js");
34
- require("./vega-chart");
35
- const context_1 = require("@lit/context");
36
- const result_context_1 = require("./result-context");
37
- const chart_settings_1 = require("./chart-settings");
38
- const vega_lite_base_spec_1 = require("./vega-lite-base-spec");
39
- const util_1 = require("./util");
40
- let BarChart = class BarChart extends lit_1.LitElement {
41
- render() {
42
- const field = this.data.field;
43
- const keys = field.allFields;
44
- const { tag } = field.tagParse();
45
- const isSpark = tag.text('size') === 'spark';
46
- const chartSettings = (0, chart_settings_1.getChartSettings)(field, this.metadata);
47
- const records = [];
48
- for (const rec of this.data) {
49
- const record = {};
50
- const xValue = rec.cell(chartSettings.xField.name).value;
51
- const yValue = rec.cell(chartSettings.yField.name).value;
52
- if ((0, util_1.valueIsString)(chartSettings.xField, xValue)) {
53
- record.barDim = xValue;
54
- }
55
- if ((0, util_1.valueIsNumber)(chartSettings.yField, yValue)) {
56
- record.sizeMeasure = yValue;
57
- }
58
- records.push(record);
59
- }
60
- const spec = (0, vega_lite_base_spec_1.baseSpec)();
61
- spec.data = {
62
- values: records,
63
- };
64
- spec.layer = [
65
- {
66
- mark: {
67
- type: 'bar',
68
- width: { 'band': 0.8 },
69
- cornerRadiusEnd: 0,
70
- },
71
- encoding: {
72
- x: {
73
- field: 'barDim',
74
- type: 'nominal',
75
- sort: null, // Uses the sort order of underlying data
76
- axis: {
77
- labelAngle: chartSettings.xAxis.labelAngle,
78
- maxExtent: chartSettings.xAxis.height,
79
- labelLimit: chartSettings.xAxis.labelSize,
80
- title: keys[0].name,
81
- },
82
- scale: {},
83
- },
84
- y: {
85
- field: 'sizeMeasure',
86
- type: 'quantitative',
87
- scale: {
88
- domain: chartSettings.yScale.domain,
89
- },
90
- axis: {
91
- maxExtent: chartSettings.yAxis.width,
92
- labelLimit: chartSettings.yAxis.width + 10,
93
- tickCount: chartSettings.yAxis.tickCount,
94
- title: keys[1].name,
95
- },
96
- },
97
- // TODO: fill color from theme
98
- fill: { value: '#53B2C8' },
99
- },
100
- },
101
- ];
102
- if (isSpark) {
103
- spec.padding = 0;
104
- spec.layer[0].encoding.y.axis = null;
105
- }
106
- else {
107
- spec.padding = chartSettings.padding;
108
- }
109
- return (0, lit_1.html) `<div style="">
110
- <malloy-vega-chart
111
- .spec="${spec}"
112
- type="vega-lite"
113
- width="${chartSettings.plotWidth}"
114
- height="${chartSettings.plotHeight}"
115
- ></malloy-vega-chart>
116
- </div>`;
117
- }
118
- };
119
- exports.BarChart = BarChart;
120
- __decorate([
121
- (0, decorators_js_1.property)({ attribute: false })
122
- ], BarChart.prototype, "data", void 0);
123
- __decorate([
124
- (0, context_1.consume)({ context: result_context_1.resultContext }),
125
- (0, decorators_js_1.property)({ attribute: false })
126
- ], BarChart.prototype, "metadata", void 0);
127
- exports.BarChart = BarChart = __decorate([
128
- (0, decorators_js_1.customElement)('malloy-bar-chart')
129
- ], BarChart);
130
- //# sourceMappingURL=bar-chart.js.map
@@ -1,152 +0,0 @@
1
- "use strict";
2
- /*
3
- * Copyright 2023 Google LLC
4
- *
5
- * Permission is hereby granted, free of charge, to any person obtaining
6
- * a copy of this software and associated documentation files
7
- * (the "Software"), to deal in the Software without restriction,
8
- * including without limitation the rights to use, copy, modify, merge,
9
- * publish, distribute, sublicense, and/or sell copies of the Software,
10
- * and to permit persons to whom the Software is furnished to do so,
11
- * subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be
14
- * included in all copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
- */
24
- Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.getChartSettings = void 0;
26
- const vega_1 = require("vega");
27
- const util_1 = require("./util");
28
- // Later should depend on chart type?
29
- const CHART_SIZES = {
30
- 'spark': [180, 1], // row height
31
- 'xs': [170, 2], // 2x row height
32
- 'sm': [216, 3], // 3x row height
33
- 'md': [256, 4], // 4x row height
34
- 'lg': [472, 7], // 7x row height
35
- 'xl': [508, 10], // 10x row height
36
- '2xl': [730, 14], // 14x row height
37
- };
38
- // TODO: read from theme CSS
39
- const ROW_HEIGHT = 28;
40
- function getChartSettings(field, metadata) {
41
- // TODO: improve logic for field extraction
42
- const xField = field.allFields.at(0);
43
- const yField = field.allFields.at(1);
44
- const { tag } = field.tagParse();
45
- let chartWidth = 0, chartHeight = 0;
46
- const customWidth = tag.numeric('size', 'width');
47
- const customHeight = tag.numeric('size', 'height');
48
- let presetSize = tag.text('size');
49
- if (customWidth && customHeight) {
50
- chartWidth = customWidth;
51
- chartHeight = customHeight;
52
- }
53
- else {
54
- presetSize = presetSize || 'md';
55
- [chartWidth, chartHeight] = CHART_SIZES[presetSize];
56
- chartHeight = chartHeight * ROW_HEIGHT;
57
- }
58
- let xAxisHeight = 0;
59
- let yAxisWidth = 0;
60
- let labelAngle = -90;
61
- let labelSize = 0;
62
- let xTitleSize = 0;
63
- const hasXAxis = presetSize !== 'spark';
64
- const hasYAxis = presetSize !== 'spark';
65
- const exploreMetadata = metadata.fields[(0, util_1.getFieldKey)(field)];
66
- let topPadding = presetSize !== 'spark' ? ROW_HEIGHT - 1 : 0; // Subtract 1 to account for top border
67
- let yTickCount;
68
- const yKey = (0, util_1.getFieldKey)(yField);
69
- const maxVal = metadata.fields[yKey].max;
70
- const yScale = (0, vega_1.scale)('linear')()
71
- .domain([0, maxVal])
72
- .nice()
73
- .range([chartHeight, 0]);
74
- const yDomain = yScale.domain();
75
- if (hasYAxis) {
76
- const maxAxisVal = yScale.domain().at(1);
77
- const l = (0, vega_1.locale)();
78
- const formatted = l.format(',')(maxAxisVal);
79
- const yTitleSize = 31; // Estimate for now, can be dynamic later
80
- const yLabelOffset = 5;
81
- yAxisWidth =
82
- (0, util_1.getTextWidth)(formatted, 'Inter, sans-serif 12px') +
83
- yLabelOffset +
84
- yTitleSize;
85
- // Check whether we need to adjust axis values manually
86
- const noOfTicks = Math.ceil(chartHeight / 40);
87
- const ticks = yScale.ticks(noOfTicks);
88
- if (ticks.at(-1) < maxAxisVal) {
89
- const offRatio = (maxAxisVal - ticks.at(-1)) / maxAxisVal;
90
- // adjust chart height
91
- const newChartHeight = chartHeight / (1 - offRatio);
92
- // adjust chart padding
93
- topPadding = topPadding - (newChartHeight - chartHeight);
94
- chartHeight = newChartHeight;
95
- // Hardcode # of ticks, or the resize could make room for more ticks and then screw things up
96
- yTickCount = noOfTicks;
97
- }
98
- }
99
- if (hasXAxis) {
100
- // TODO: add type checking here for axis. for now assume number, string
101
- const xKey = (0, util_1.getFieldKey)(xField);
102
- const maxString = metadata.fields[xKey].maxString;
103
- const maxStringSize = (0, util_1.getTextWidth)(maxString, 'Inter, sans-serif 12px');
104
- const X_AXIS_THRESHOLD = 0.3;
105
- const minBottomPadding = 15;
106
- xTitleSize = 22 + minBottomPadding;
107
- xAxisHeight = Math.min(maxStringSize, X_AXIS_THRESHOLD * chartHeight);
108
- labelSize = xAxisHeight;
109
- const xSpacePerLabel = (chartWidth - yAxisWidth) / exploreMetadata.maxRecordCt;
110
- if (xSpacePerLabel > xAxisHeight) {
111
- labelAngle = 0;
112
- labelSize = xSpacePerLabel;
113
- }
114
- }
115
- // Additional xTitle padding to snap to row height grid
116
- const totalSize = chartHeight + xAxisHeight + xTitleSize;
117
- const roundedUpRowHeight = Math.ceil(totalSize / ROW_HEIGHT) * ROW_HEIGHT;
118
- xTitleSize += roundedUpRowHeight - totalSize;
119
- return {
120
- plotWidth: chartWidth,
121
- plotHeight: chartHeight,
122
- xAxis: {
123
- labelAngle,
124
- labelSize,
125
- height: xAxisHeight,
126
- titleSize: xTitleSize,
127
- },
128
- yAxis: {
129
- width: yAxisWidth,
130
- tickCount: yTickCount,
131
- },
132
- yScale: {
133
- domain: yDomain,
134
- },
135
- padding: {
136
- top: topPadding,
137
- left: yAxisWidth,
138
- bottom: xAxisHeight + xTitleSize,
139
- right: 0,
140
- },
141
- xField,
142
- yField,
143
- get totalWidth() {
144
- return this.plotWidth + this.padding.left + this.padding.right;
145
- },
146
- get totalHeight() {
147
- return this.plotHeight + this.padding.top + this.padding.bottom;
148
- },
149
- };
150
- }
151
- exports.getChartSettings = getChartSettings;
152
- //# sourceMappingURL=chart-settings.js.map
@@ -1,104 +0,0 @@
1
- "use strict";
2
- /*
3
- * Copyright 2023 Google LLC
4
- *
5
- * Permission is hereby granted, free of charge, to any person obtaining
6
- * a copy of this software and associated documentation files
7
- * (the "Software"), to deal in the Software without restriction,
8
- * including without limitation the rights to use, copy, modify, merge,
9
- * publish, distribute, sublicense, and/or sell copies of the Software,
10
- * and to permit persons to whom the Software is furnished to do so,
11
- * subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be
14
- * included in all copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
- */
24
- Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.renderNumericField = void 0;
26
- const data_styles_1 = require("../data_styles");
27
- const ssf_1 = require("ssf");
28
- // Map of unit to how many units of the make up the following time unit.
29
- const multiplierMap = new Map([
30
- [data_styles_1.DurationUnit.Nanoseconds, 1000],
31
- [data_styles_1.DurationUnit.Microseconds, 1000],
32
- [data_styles_1.DurationUnit.Milliseconds, 1000],
33
- [data_styles_1.DurationUnit.Seconds, 60],
34
- [data_styles_1.DurationUnit.Minutes, 60],
35
- [data_styles_1.DurationUnit.Hours, 24],
36
- [data_styles_1.DurationUnit.Days, Number.MAX_VALUE],
37
- ]);
38
- function formatTimeUnit(value, unit) {
39
- let unitString = unit.toString();
40
- if (value === 1) {
41
- unitString = unitString.substring(0, unitString.length - 1);
42
- }
43
- return `${value} ${unitString}`;
44
- }
45
- function renderNumericField(f, value) {
46
- let displayValue = value;
47
- const { tag } = f.tagParse();
48
- if (tag.has('currency')) {
49
- let unitText = '$';
50
- switch (tag.text('currency')) {
51
- case data_styles_1.Currency.Euros:
52
- unitText = '€';
53
- break;
54
- case data_styles_1.Currency.Pounds:
55
- unitText = '£';
56
- break;
57
- case data_styles_1.Currency.Dollars:
58
- // Do nothing.
59
- break;
60
- }
61
- displayValue = (0, ssf_1.format)(`${unitText}#,##0.00`, value);
62
- }
63
- else if (tag.has('percent'))
64
- displayValue = (0, ssf_1.format)('#,##0.00%', value);
65
- else if (tag.has('duration')) {
66
- const duration_unit = tag.text('duration');
67
- const targetUnit = duration_unit !== null && duration_unit !== void 0 ? duration_unit : data_styles_1.DurationUnit.Seconds;
68
- let currentDuration = value;
69
- let currentUnitValue = 0;
70
- let durationParts = [];
71
- let foundUnit = false;
72
- for (const [unit, multiplier] of multiplierMap) {
73
- if (unit === targetUnit) {
74
- foundUnit = true;
75
- }
76
- if (!foundUnit) {
77
- continue;
78
- }
79
- currentUnitValue = currentDuration % multiplier;
80
- currentDuration = Math.floor((currentDuration /= multiplier));
81
- if (currentUnitValue > 0) {
82
- durationParts = [
83
- formatTimeUnit(currentUnitValue, unit),
84
- ...durationParts,
85
- ];
86
- }
87
- if (currentDuration === 0) {
88
- break;
89
- }
90
- }
91
- if (durationParts.length > 0) {
92
- displayValue = durationParts.slice(0, 2).join(' ');
93
- }
94
- else
95
- displayValue = formatTimeUnit(0, targetUnit);
96
- }
97
- else if (tag.has('number'))
98
- displayValue = (0, ssf_1.format)(tag.text('number'), value);
99
- else
100
- displayValue = value.toLocaleString();
101
- return displayValue;
102
- }
103
- exports.renderNumericField = renderNumericField;
104
- //# sourceMappingURL=render-numeric-field.js.map
@@ -1,91 +0,0 @@
1
- "use strict";
2
- /*
3
- * Copyright 2023 Google LLC
4
- *
5
- * Permission is hereby granted, free of charge, to any person obtaining
6
- * a copy of this software and associated documentation files
7
- * (the "Software"), to deal in the Software without restriction,
8
- * including without limitation the rights to use, copy, modify, merge,
9
- * publish, distribute, sublicense, and/or sell copies of the Software,
10
- * and to permit persons to whom the Software is furnished to do so,
11
- * subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be
14
- * included in all copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
- */
24
- Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.getResultMetadata = void 0;
26
- const util_1 = require("./util");
27
- function getResultMetadata(result) {
28
- const fieldKeyMap = new WeakMap();
29
- const getCachedFieldKey = (f) => {
30
- if (fieldKeyMap.has(f))
31
- return fieldKeyMap.get(f);
32
- const fieldKey = (0, util_1.getFieldKey)(f);
33
- fieldKeyMap.set(f, fieldKey);
34
- return fieldKey;
35
- };
36
- const metadata = {
37
- fields: {},
38
- };
39
- function initFieldMeta(e) {
40
- for (const f of e.allFields) {
41
- const fieldKey = getCachedFieldKey(f);
42
- metadata.fields[fieldKey] = {
43
- field: f,
44
- min: null,
45
- max: null,
46
- minString: null,
47
- maxString: null,
48
- values: new Set(),
49
- maxRecordCt: null,
50
- };
51
- if (f.isExploreField()) {
52
- initFieldMeta(f);
53
- }
54
- }
55
- }
56
- const populateFieldMeta = (data, metadata, cb) => {
57
- var _a, _b, _c;
58
- for (const row of data) {
59
- cb === null || cb === void 0 ? void 0 : cb(row);
60
- for (const f of data.field.allFields) {
61
- const value = f.isAtomicField() ? row.cell(f).value : undefined;
62
- const fieldKey = (0, util_1.getFieldKey)(f);
63
- const fieldMeta = metadata.fields[fieldKey];
64
- if ((0, util_1.valueIsNumber)(f, value)) {
65
- const n = value;
66
- fieldMeta.min = Math.min((_a = fieldMeta.min) !== null && _a !== void 0 ? _a : n, n);
67
- fieldMeta.max = Math.max((_b = fieldMeta.max) !== null && _b !== void 0 ? _b : n, n);
68
- }
69
- else if ((0, util_1.valueIsString)(f, value)) {
70
- const s = value;
71
- fieldMeta.values.add(s);
72
- if (!fieldMeta.minString || fieldMeta.minString.length > s.length)
73
- fieldMeta.minString = s;
74
- if (!fieldMeta.maxString || fieldMeta.maxString.length < s.length)
75
- fieldMeta.maxString = s;
76
- }
77
- else if (f.isExploreField()) {
78
- const data = row.cell(f);
79
- let recordCt = 0;
80
- populateFieldMeta(data, metadata, () => recordCt++);
81
- fieldMeta.maxRecordCt = Math.max((_c = fieldMeta.maxRecordCt) !== null && _c !== void 0 ? _c : recordCt, recordCt);
82
- }
83
- }
84
- }
85
- };
86
- initFieldMeta(result.data.field);
87
- populateFieldMeta(result.data, metadata);
88
- return metadata;
89
- }
90
- exports.getResultMetadata = getResultMetadata;
91
- //# sourceMappingURL=render-result-metadata.js.map