@perses-dev/components 0.4.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/README.md +18 -0
  2. package/dist/EChart.d.ts +4 -1
  3. package/dist/EChart.d.ts.map +1 -1
  4. package/dist/EChart.js +1 -1
  5. package/dist/{GaugeChart.d.ts → GaugeChart/GaugeChart.d.ts} +2 -1
  6. package/dist/GaugeChart/GaugeChart.d.ts.map +1 -0
  7. package/dist/GaugeChart/GaugeChart.js +1 -0
  8. package/dist/GaugeChart/index.d.ts +2 -0
  9. package/dist/GaugeChart/index.d.ts.map +1 -0
  10. package/dist/GaugeChart/index.js +1 -0
  11. package/dist/InfoTooltip/InfoTooltip.d.ts +17 -0
  12. package/dist/InfoTooltip/InfoTooltip.d.ts.map +1 -0
  13. package/dist/InfoTooltip/InfoTooltip.js +1 -0
  14. package/dist/InfoTooltip/InfoTooltip.test.d.ts +2 -0
  15. package/dist/InfoTooltip/InfoTooltip.test.d.ts.map +1 -0
  16. package/dist/InfoTooltip/InfoTooltip.test.js +1 -0
  17. package/dist/InfoTooltip/index.d.ts +2 -0
  18. package/dist/InfoTooltip/index.d.ts.map +1 -0
  19. package/dist/InfoTooltip/index.js +1 -0
  20. package/dist/LineChart/LineChart.d.ts +17 -0
  21. package/dist/LineChart/LineChart.d.ts.map +1 -0
  22. package/dist/LineChart/LineChart.js +1 -0
  23. package/dist/LineChart/index.d.ts +3 -0
  24. package/dist/LineChart/index.d.ts.map +1 -0
  25. package/dist/LineChart/index.js +1 -0
  26. package/dist/LineChart/utils.d.ts +12 -0
  27. package/dist/LineChart/utils.d.ts.map +1 -0
  28. package/dist/LineChart/utils.js +1 -0
  29. package/dist/{StatChart.d.ts → StatChart/StatChart.d.ts} +4 -9
  30. package/dist/StatChart/StatChart.d.ts.map +1 -0
  31. package/dist/StatChart/StatChart.js +1 -0
  32. package/dist/StatChart/StatChart.test.d.ts +2 -0
  33. package/dist/StatChart/StatChart.test.d.ts.map +1 -0
  34. package/dist/StatChart/StatChart.test.js +1 -0
  35. package/dist/StatChart/index.d.ts +2 -0
  36. package/dist/StatChart/index.d.ts.map +1 -0
  37. package/dist/StatChart/index.js +1 -0
  38. package/dist/{tooltip → Tooltip}/SeriesInfo.d.ts +0 -0
  39. package/dist/{tooltip → Tooltip}/SeriesInfo.d.ts.map +1 -1
  40. package/dist/{tooltip → Tooltip}/SeriesInfo.js +0 -0
  41. package/dist/{tooltip → Tooltip}/SeriesMarker.d.ts +0 -0
  42. package/dist/{tooltip → Tooltip}/SeriesMarker.d.ts.map +1 -1
  43. package/dist/{tooltip → Tooltip}/SeriesMarker.js +0 -0
  44. package/dist/{tooltip → Tooltip}/Tooltip.d.ts +1 -1
  45. package/dist/{tooltip → Tooltip}/Tooltip.d.ts.map +1 -1
  46. package/dist/{tooltip → Tooltip}/Tooltip.js +0 -0
  47. package/dist/{tooltip → Tooltip}/TooltipContent.d.ts +0 -0
  48. package/dist/{tooltip → Tooltip}/TooltipContent.d.ts.map +1 -1
  49. package/dist/{tooltip → Tooltip}/TooltipContent.js +0 -0
  50. package/dist/{tooltip → Tooltip}/focused-series.d.ts +1 -1
  51. package/dist/{tooltip → Tooltip}/focused-series.d.ts.map +1 -1
  52. package/dist/{tooltip → Tooltip}/focused-series.js +0 -0
  53. package/dist/Tooltip/focused-series.test.d.ts +2 -0
  54. package/dist/Tooltip/focused-series.test.d.ts.map +1 -0
  55. package/dist/Tooltip/focused-series.test.js +1 -0
  56. package/dist/Tooltip/index.d.ts +3 -0
  57. package/dist/Tooltip/index.d.ts.map +1 -0
  58. package/dist/Tooltip/index.js +1 -0
  59. package/dist/{tooltip → Tooltip}/tooltip-model.d.ts +0 -0
  60. package/dist/{tooltip → Tooltip}/tooltip-model.d.ts.map +1 -1
  61. package/dist/{tooltip → Tooltip}/tooltip-model.js +0 -0
  62. package/dist/cjs/EChart.js +3 -3
  63. package/dist/cjs/{GaugeChart.js → GaugeChart/GaugeChart.js} +12 -45
  64. package/dist/cjs/GaugeChart/index.js +29 -0
  65. package/dist/cjs/InfoTooltip/InfoTooltip.js +39 -0
  66. package/dist/cjs/InfoTooltip/InfoTooltip.test.js +33 -0
  67. package/dist/cjs/InfoTooltip/index.js +29 -0
  68. package/dist/cjs/LineChart/LineChart.js +154 -0
  69. package/dist/cjs/LineChart/index.js +29 -0
  70. package/dist/cjs/LineChart/utils.js +72 -0
  71. package/dist/cjs/StatChart/StatChart.js +104 -0
  72. package/dist/cjs/StatChart/StatChart.test.js +45 -0
  73. package/dist/cjs/StatChart/index.js +29 -0
  74. package/dist/cjs/{tooltip → Tooltip}/SeriesInfo.js +0 -0
  75. package/dist/cjs/{tooltip → Tooltip}/SeriesMarker.js +0 -0
  76. package/dist/cjs/{tooltip → Tooltip}/Tooltip.js +0 -0
  77. package/dist/cjs/{tooltip → Tooltip}/TooltipContent.js +0 -0
  78. package/dist/cjs/{tooltip → Tooltip}/focused-series.js +0 -0
  79. package/dist/cjs/Tooltip/focused-series.test.js +57 -0
  80. package/dist/cjs/Tooltip/index.js +30 -0
  81. package/dist/cjs/{tooltip → Tooltip}/tooltip-model.js +0 -0
  82. package/dist/cjs/context/ChartsThemeProvider.js +36 -0
  83. package/dist/cjs/index.js +5 -4
  84. package/dist/cjs/model/{graph-model.js → graph.js} +0 -0
  85. package/dist/cjs/model/index.js +31 -0
  86. package/dist/cjs/model/theme.js +14 -0
  87. package/dist/cjs/model/units.js +58 -85
  88. package/dist/cjs/test/index.js +29 -0
  89. package/dist/cjs/test/render.js +26 -0
  90. package/dist/cjs/test/setup-tests.js +18 -0
  91. package/dist/cjs/utils/index.js +30 -0
  92. package/dist/cjs/utils/theme-gen.js +180 -0
  93. package/dist/cjs/utils/theme-gen.test.js +196 -0
  94. package/dist/context/ChartsThemeProvider.d.ts +11 -0
  95. package/dist/context/ChartsThemeProvider.d.ts.map +1 -0
  96. package/dist/context/ChartsThemeProvider.js +1 -0
  97. package/dist/index.d.ts +5 -4
  98. package/dist/index.d.ts.map +1 -1
  99. package/dist/index.js +1 -1
  100. package/dist/model/{graph-model.d.ts → graph.d.ts} +6 -2
  101. package/dist/model/graph.d.ts.map +1 -0
  102. package/dist/model/{graph-model.js → graph.js} +0 -0
  103. package/dist/model/index.d.ts +4 -0
  104. package/dist/model/index.d.ts.map +1 -0
  105. package/dist/model/index.js +1 -0
  106. package/dist/model/theme.d.ts +16 -0
  107. package/dist/model/theme.d.ts.map +1 -0
  108. package/dist/model/theme.js +1 -0
  109. package/dist/model/units.d.ts +22 -11
  110. package/dist/model/units.d.ts.map +1 -1
  111. package/dist/model/units.js +1 -1
  112. package/dist/test/index.d.ts +2 -0
  113. package/dist/test/index.d.ts.map +1 -0
  114. package/dist/test/index.js +1 -0
  115. package/dist/test/render.d.ts +7 -0
  116. package/dist/test/render.d.ts.map +1 -0
  117. package/dist/test/render.js +1 -0
  118. package/dist/test/setup-tests.d.ts +2 -0
  119. package/dist/test/setup-tests.d.ts.map +1 -0
  120. package/dist/test/setup-tests.js +1 -0
  121. package/dist/utils/index.d.ts +3 -0
  122. package/dist/utils/index.d.ts.map +1 -0
  123. package/dist/utils/index.js +1 -0
  124. package/dist/utils/theme-gen.d.ts +6 -0
  125. package/dist/utils/theme-gen.d.ts.map +1 -0
  126. package/dist/utils/theme-gen.js +1 -0
  127. package/dist/utils/theme-gen.test.d.ts +2 -0
  128. package/dist/utils/theme-gen.test.d.ts.map +1 -0
  129. package/dist/utils/theme-gen.test.js +1 -0
  130. package/package.json +5 -4
  131. package/dist/GaugeChart.d.ts.map +0 -1
  132. package/dist/GaugeChart.js +0 -1
  133. package/dist/LineChart.d.ts +0 -22
  134. package/dist/LineChart.d.ts.map +0 -1
  135. package/dist/LineChart.js +0 -1
  136. package/dist/StatChart.d.ts.map +0 -1
  137. package/dist/StatChart.js +0 -1
  138. package/dist/cjs/LineChart.js +0 -252
  139. package/dist/cjs/StatChart.js +0 -207
  140. package/dist/model/graph-model.d.ts.map +0 -1
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const jsx_runtime_1 = require("react/jsx-runtime");
7
+ // Copyright 2022 The Perses Authors
8
+ // Licensed under the Apache License, Version 2.0 (the "License");
9
+ // you may not use this file except in compliance with the License.
10
+ // You may obtain a copy of the License at
11
+ //
12
+ // http://www.apache.org/licenses/LICENSE-2.0
13
+ //
14
+ // Unless required by applicable law or agreed to in writing, software
15
+ // distributed under the License is distributed on an "AS IS" BASIS,
16
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ // See the License for the specific language governing permissions and
18
+ // limitations under the License.
19
+ const react_1 = require("@testing-library/react");
20
+ const user_event_1 = __importDefault(require("@testing-library/user-event"));
21
+ const dom_1 = require("@testing-library/dom");
22
+ const InfoTooltip_1 = require("./InfoTooltip");
23
+ describe('InfoTooltip', () => {
24
+ const title = 'Tooltip Title';
25
+ const description = 'this is a tooltip';
26
+ it('should render title and description', async () => {
27
+ (0, react_1.render)((0, jsx_runtime_1.jsx)(InfoTooltip_1.InfoTooltip, { title: title, description: description, children: (0, jsx_runtime_1.jsx)("div", { children: "tooltipAnchor" }) }));
28
+ const tooltipAnchor = dom_1.screen.getByText('tooltipAnchor');
29
+ user_event_1.default.hover(tooltipAnchor);
30
+ await dom_1.screen.findByText(title);
31
+ dom_1.screen.findByText(description);
32
+ });
33
+ });
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ // Copyright 2022 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ var desc = Object.getOwnPropertyDescriptor(m, k);
17
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
18
+ desc = { enumerable: true, get: function() { return m[k]; } };
19
+ }
20
+ Object.defineProperty(o, k2, desc);
21
+ }) : (function(o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ }));
25
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ __exportStar(require("./InfoTooltip"), exports);
@@ -0,0 +1,154 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LineChart = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ // Copyright 2022 The Perses Authors
6
+ // Licensed under the Apache License, Version 2.0 (the "License");
7
+ // you may not use this file except in compliance with the License.
8
+ // You may obtain a copy of the License at
9
+ //
10
+ // http://www.apache.org/licenses/LICENSE-2.0
11
+ //
12
+ // Unless required by applicable law or agreed to in writing, software
13
+ // distributed under the License is distributed on an "AS IS" BASIS,
14
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ // See the License for the specific language governing permissions and
16
+ // limitations under the License.
17
+ const react_1 = require("react");
18
+ const core_1 = require("@perses-dev/core");
19
+ const material_1 = require("@mui/material");
20
+ const core_2 = require("echarts/core");
21
+ const charts_1 = require("echarts/charts");
22
+ const components_1 = require("echarts/components");
23
+ const renderers_1 = require("echarts/renderers");
24
+ const EChart_1 = require("../EChart");
25
+ const graph_1 = require("../model/graph");
26
+ const units_1 = require("../model/units");
27
+ const ChartsThemeProvider_1 = require("../context/ChartsThemeProvider");
28
+ const tooltip_model_1 = require("../Tooltip/tooltip-model");
29
+ const Tooltip_1 = require("../Tooltip/Tooltip");
30
+ const utils_1 = require("./utils");
31
+ (0, core_2.use)([
32
+ charts_1.LineChart,
33
+ components_1.GridComponent,
34
+ components_1.DataZoomComponent,
35
+ components_1.MarkAreaComponent,
36
+ components_1.MarkLineComponent,
37
+ components_1.MarkPointComponent,
38
+ components_1.TitleComponent,
39
+ components_1.ToolboxComponent,
40
+ components_1.TooltipComponent,
41
+ components_1.LegendComponent,
42
+ components_1.VisualMapComponent,
43
+ renderers_1.CanvasRenderer,
44
+ ]);
45
+ function LineChart({ height, data, unit, grid, legend, visualMap, onDataZoom }) {
46
+ const chartsTheme = (0, ChartsThemeProvider_1.useChartsTheme)();
47
+ const chartRef = (0, react_1.useRef)();
48
+ const [showTooltip, setShowTooltip] = (0, react_1.useState)(true);
49
+ const handleEvents = (0, react_1.useMemo)(() => {
50
+ return {
51
+ // TODO: use legendselectchanged event to fix tooltip when legend selected
52
+ datazoom: (params) => {
53
+ var _a, _b;
54
+ if (onDataZoom === undefined || params.batch[0] === undefined)
55
+ return;
56
+ const startIndex = (_a = params.batch[0].startValue) !== null && _a !== void 0 ? _a : 0;
57
+ const endIndex = (_b = params.batch[0].endValue) !== null && _b !== void 0 ? _b : data.xAxis.length - 1;
58
+ const xAxisStartValue = data.xAxis[startIndex];
59
+ const xAxisEndValue = data.xAxis[endIndex];
60
+ if (xAxisStartValue !== undefined && xAxisEndValue !== undefined) {
61
+ const zoomEvent = {
62
+ start: xAxisStartValue,
63
+ end: xAxisEndValue,
64
+ startIndex,
65
+ endIndex,
66
+ };
67
+ onDataZoom(zoomEvent);
68
+ }
69
+ },
70
+ };
71
+ }, [data, onDataZoom]);
72
+ if (chartRef.current !== undefined) {
73
+ (0, utils_1.enableDataZoom)(chartRef.current);
74
+ }
75
+ const handleOnDoubleClick = () => {
76
+ if (chartRef.current !== undefined) {
77
+ (0, utils_1.restoreChart)(chartRef.current);
78
+ }
79
+ };
80
+ const handleOnMouseDown = (event) => {
81
+ // hide tooltip when user drags to zoom, but allow clicking inside tooltip to copy labels
82
+ if (event.target instanceof HTMLCanvasElement) {
83
+ setShowTooltip(false);
84
+ }
85
+ };
86
+ const handleOnMouseUp = () => {
87
+ setShowTooltip(true);
88
+ };
89
+ const handleOnMouseEnter = () => {
90
+ setShowTooltip(true);
91
+ };
92
+ const handleOnMouseLeave = () => {
93
+ setShowTooltip(false);
94
+ };
95
+ const option = (0, core_1.useDeepMemo)(() => {
96
+ var _a;
97
+ if (data.timeSeries === undefined)
98
+ return {};
99
+ if (data.timeSeries === null || data.timeSeries.length === 0)
100
+ return chartsTheme.noDataOption;
101
+ const showPointsOnHover = data.timeSeries.length < graph_1.PROGRESSIVE_MODE_SERIES_LIMIT;
102
+ const rangeMs = (_a = data.rangeMs) !== null && _a !== void 0 ? _a : (0, utils_1.getDateRange)(data.xAxis);
103
+ const option = {
104
+ series: data.timeSeries,
105
+ xAxis: {
106
+ type: 'category',
107
+ data: data.xAxis,
108
+ max: data.xAxisMax,
109
+ axisLabel: {
110
+ formatter: (value) => {
111
+ return (0, utils_1.getFormattedDate)(value, rangeMs);
112
+ },
113
+ },
114
+ },
115
+ yAxis: {
116
+ type: 'value',
117
+ boundaryGap: [0, '10%'],
118
+ axisLabel: {
119
+ formatter: (value) => {
120
+ return (0, units_1.formatValue)(value, unit);
121
+ },
122
+ },
123
+ },
124
+ animation: false,
125
+ tooltip: {
126
+ show: showPointsOnHover,
127
+ trigger: 'axis',
128
+ showContent: false,
129
+ axisPointer: {
130
+ type: 'none',
131
+ },
132
+ },
133
+ toolbox: {
134
+ feature: {
135
+ dataZoom: {
136
+ icon: null,
137
+ yAxisIndex: 'none',
138
+ },
139
+ },
140
+ },
141
+ grid,
142
+ legend,
143
+ visualMap,
144
+ };
145
+ return option;
146
+ }, [data, grid, legend, visualMap]);
147
+ return ((0, jsx_runtime_1.jsxs)(material_1.Box, { sx: {
148
+ height,
149
+ }, onDoubleClick: handleOnDoubleClick, onMouseDown: handleOnMouseDown, onMouseUp: handleOnMouseUp, onMouseLeave: handleOnMouseLeave, onMouseEnter: handleOnMouseEnter, children: [showTooltip === true && ((0, jsx_runtime_1.jsx)(Tooltip_1.Tooltip, { chartRef: chartRef, tooltipData: tooltip_model_1.emptyTooltipData, chartData: data, wrapLabels: true })), (0, jsx_runtime_1.jsx)(EChart_1.EChart, { sx: {
150
+ width: '100%',
151
+ height: '100%',
152
+ }, option: option, theme: chartsTheme.themeName, onEvents: handleEvents, _instance: chartRef })] }));
153
+ }
154
+ exports.LineChart = LineChart;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ // Copyright 2022 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ var desc = Object.getOwnPropertyDescriptor(m, k);
17
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
18
+ desc = { enumerable: true, get: function() { return m[k]; } };
19
+ }
20
+ Object.defineProperty(o, k2, desc);
21
+ }) : (function(o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ }));
25
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ __exportStar(require("./LineChart"), exports);
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ // Copyright 2022 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.getFormattedDate = exports.getDateRange = exports.restoreChart = exports.enableDataZoom = void 0;
16
+ // enable dataZoom without requring user to click toolbox icon
17
+ function enableDataZoom(chart) {
18
+ const chartModel = chart['_model'];
19
+ if (chartModel === undefined)
20
+ return;
21
+ if (chartModel.option.toolbox !== undefined && chartModel.option.toolbox.length > 0) {
22
+ // check if hidden data zoom icon is unselected (if selected it would be 'emphasis' instead of 'normal')
23
+ if (chartModel.option.toolbox[0].feature.dataZoom.iconStatus.zoom === 'normal') {
24
+ chart.dispatchAction({
25
+ type: 'takeGlobalCursor',
26
+ key: 'dataZoomSelect',
27
+ dataZoomSelectActive: true,
28
+ });
29
+ }
30
+ }
31
+ }
32
+ exports.enableDataZoom = enableDataZoom;
33
+ // restore chart to original state before zoom or other actions were dispatched
34
+ // TODO: support incremental unzoom instead of restore to original state
35
+ function restoreChart(chart) {
36
+ chart.dispatchAction({
37
+ type: 'restore', // https://echarts.apache.org/en/api.html#events.restore
38
+ });
39
+ }
40
+ exports.restoreChart = restoreChart;
41
+ // fallback when xAxis time range not passed as prop
42
+ function getDateRange(data) {
43
+ const defaultRange = 3600000; // hour in ms
44
+ if (data.length === 0)
45
+ return defaultRange;
46
+ const lastDatum = data[data.length - 1];
47
+ if (data[0] === undefined || lastDatum === undefined)
48
+ return defaultRange;
49
+ return lastDatum - data[0];
50
+ }
51
+ exports.getDateRange = getDateRange;
52
+ // determines time granularity for axis labels, defaults to hh:mm
53
+ function getFormattedDate(value, rangeMs) {
54
+ const dateFormatOptions = {
55
+ hour: 'numeric',
56
+ minute: 'numeric',
57
+ hourCycle: 'h23',
58
+ };
59
+ const thirtyMinMs = 1800000;
60
+ const dayMs = 86400000;
61
+ if (rangeMs <= thirtyMinMs) {
62
+ dateFormatOptions.second = 'numeric';
63
+ }
64
+ else if (rangeMs >= dayMs) {
65
+ dateFormatOptions.month = 'numeric';
66
+ dateFormatOptions.day = 'numeric';
67
+ }
68
+ const DATE_FORMAT = new Intl.DateTimeFormat(undefined, dateFormatOptions);
69
+ // remove comma when month / day present
70
+ return DATE_FORMAT.format(value).replace(/, /g, ' ');
71
+ }
72
+ exports.getFormattedDate = getFormattedDate;
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StatChart = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ // Copyright 2022 The Perses Authors
6
+ // Licensed under the Apache License, Version 2.0 (the "License");
7
+ // you may not use this file except in compliance with the License.
8
+ // You may obtain a copy of the License at
9
+ //
10
+ // http://www.apache.org/licenses/LICENSE-2.0
11
+ //
12
+ // Unless required by applicable law or agreed to in writing, software
13
+ // distributed under the License is distributed on an "AS IS" BASIS,
14
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ // See the License for the specific language governing permissions and
16
+ // limitations under the License.
17
+ const react_1 = require("react");
18
+ const material_1 = require("@mui/material");
19
+ const lodash_es_1 = require("lodash-es");
20
+ const core_1 = require("echarts/core");
21
+ const charts_1 = require("echarts/charts");
22
+ const charts_2 = require("echarts/charts");
23
+ const components_1 = require("echarts/components");
24
+ const renderers_1 = require("echarts/renderers");
25
+ const ChartsThemeProvider_1 = require("../context/ChartsThemeProvider");
26
+ const units_1 = require("../model/units");
27
+ const EChart_1 = require("../EChart");
28
+ (0, core_1.use)([
29
+ charts_1.GaugeChart,
30
+ charts_2.LineChart,
31
+ components_1.GridComponent,
32
+ components_1.DatasetComponent,
33
+ components_1.TitleComponent,
34
+ components_1.TooltipComponent,
35
+ renderers_1.CanvasRenderer,
36
+ ]);
37
+ const PANEL_PADDING = 32;
38
+ const MIN_VALUE_SIZE = 12;
39
+ const MAX_VALUE_SIZE = 36;
40
+ function StatChart(props) {
41
+ const { width, height, data, unit, sparkline } = props;
42
+ const chartsTheme = (0, ChartsThemeProvider_1.useChartsTheme)();
43
+ const formattedValue = data.calculatedValue === undefined ? 'No data' : (0, units_1.formatValue)(data.calculatedValue, unit);
44
+ const option = (0, react_1.useMemo)(() => {
45
+ if (data.seriesData === undefined)
46
+ return chartsTheme.noDataOption;
47
+ const series = data.seriesData;
48
+ const statSeries = [];
49
+ if (sparkline !== undefined) {
50
+ const lineSeries = {
51
+ type: 'line',
52
+ data: [...series.values],
53
+ zlevel: 1,
54
+ symbol: 'none',
55
+ animation: false,
56
+ silent: true,
57
+ };
58
+ const mergedSeries = (0, lodash_es_1.merge)(lineSeries, sparkline);
59
+ statSeries.push(mergedSeries);
60
+ }
61
+ const option = {
62
+ title: {
63
+ show: false,
64
+ },
65
+ grid: {
66
+ show: false,
67
+ top: '35%',
68
+ right: 0,
69
+ bottom: 0,
70
+ left: 0,
71
+ containLabel: false,
72
+ },
73
+ xAxis: {
74
+ type: 'time',
75
+ show: false,
76
+ boundaryGap: false,
77
+ },
78
+ yAxis: {
79
+ type: 'value',
80
+ show: false,
81
+ },
82
+ tooltip: {
83
+ show: false,
84
+ },
85
+ series: statSeries,
86
+ };
87
+ return option;
88
+ }, [data, chartsTheme, sparkline]);
89
+ const isLargePanel = width > 250 && height > 180;
90
+ // adjusts fontSize depending on number of characters, clamp also used in fontSize attribute
91
+ const charactersAdjust = formattedValue.length;
92
+ const valueSize = isLargePanel === true ? MAX_VALUE_SIZE : Math.min(width, height) / charactersAdjust;
93
+ return ((0, jsx_runtime_1.jsxs)(material_1.Box, { children: [(0, jsx_runtime_1.jsx)(material_1.Typography, { variant: "h3", sx: (theme) => ({
94
+ color: theme.palette.text.primary,
95
+ fontSize: `clamp(${MIN_VALUE_SIZE}px, ${valueSize}px, ${MAX_VALUE_SIZE}px)`,
96
+ }), children: formattedValue }), sparkline !== undefined && ((0, jsx_runtime_1.jsx)(EChart_1.EChart, { sx: {
97
+ width: width + PANEL_PADDING,
98
+ height: height,
99
+ position: 'absolute',
100
+ bottom: 0,
101
+ left: 0,
102
+ }, option: option, theme: chartsTheme.themeName, renderer: "svg" }))] }));
103
+ }
104
+ exports.StatChart = StatChart;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const jsx_runtime_1 = require("react/jsx-runtime");
4
+ const react_1 = require("@testing-library/react");
5
+ const ChartsThemeProvider_1 = require("../context/ChartsThemeProvider");
6
+ const StatChart_1 = require("./StatChart");
7
+ describe('StatChart', () => {
8
+ const contentDimensions = {
9
+ width: 200,
10
+ height: 200,
11
+ };
12
+ const unit = {
13
+ kind: 'Decimal',
14
+ decimal_places: 2,
15
+ };
16
+ const mockStatData = {
17
+ calculatedValue: 7.72931659687181,
18
+ name: 'Example Stat Chart',
19
+ seriesData: {
20
+ name: '(((count(count(node_cpu_seconds_total{job="example"}) by (cpu))',
21
+ values: [
22
+ [1654006170000, 7.736401673473903],
23
+ [1654006185000, 7.733891213538757],
24
+ [1654006200000, 7.731101813010433],
25
+ [1654006215000, 7.722454672079215],
26
+ [1654006230000, 7.722733612256738],
27
+ ],
28
+ },
29
+ };
30
+ const exampleChartsTheme = {
31
+ themeName: 'perses',
32
+ echartsTheme: {},
33
+ noDataOption: {},
34
+ sparkline: {
35
+ width: 1,
36
+ color: '#000000',
37
+ },
38
+ };
39
+ describe('Render default options (no sparkline)', () => {
40
+ it('should render', () => {
41
+ (0, react_1.render)((0, jsx_runtime_1.jsx)(ChartsThemeProvider_1.ChartsThemeProvider, { themeName: "perses", chartsTheme: exampleChartsTheme, children: (0, jsx_runtime_1.jsx)(StatChart_1.StatChart, { width: contentDimensions.width, height: contentDimensions.height, data: mockStatData, unit: unit }) }));
42
+ expect(react_1.screen.getByText('7.73')).toBeInTheDocument();
43
+ });
44
+ });
45
+ });
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ // Copyright 2022 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ var desc = Object.getOwnPropertyDescriptor(m, k);
17
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
18
+ desc = { enumerable: true, get: function() { return m[k]; } };
19
+ }
20
+ Object.defineProperty(o, k2, desc);
21
+ }) : (function(o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ }));
25
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ __exportStar(require("./StatChart"), exports);
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ // Copyright 2022 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const focused_series_1 = require("./focused-series");
16
+ describe('getNearbySeries', () => {
17
+ const chartData = {
18
+ timeSeries: [
19
+ {
20
+ type: 'line',
21
+ name: 'env="demo", instance="demo.do.prometheus", job="node", mode="test"',
22
+ color: 'hsla(-1365438424,50%,50%,0.8)',
23
+ data: [
24
+ 0.0002315202231525094, 0.00022873082287300112, 0.00023152022315149463, 0.00023152022315149463,
25
+ 0.00022873082287300112,
26
+ ],
27
+ symbol: 'circle',
28
+ },
29
+ {
30
+ type: 'line',
31
+ name: 'env="demo", instance="demo.do.prometheus", job="node", mode="test alt"',
32
+ color: 'hsla(286664040,50%,50%,0.8)',
33
+ data: [0.05245188284519867, 0.0524463040446356, 0.0524463040446356, 0.05247140864723438, 0.052482566248230646],
34
+ symbol: 'circle',
35
+ },
36
+ ],
37
+ xAxis: [1654007865000, 1654007880000, 1654007895000, 1654007910000, 1654007925000],
38
+ rangeMs: 60000,
39
+ };
40
+ // https://echarts.apache.org/en/api.html#echartsInstance.convertFromPixel
41
+ const pointInGrid = [2, 0.0560655737704918]; // converted from chart.getZr() mousemove coordinates
42
+ const yBuffer = 0.02; // calculated from y axis interval
43
+ const focusedSeriesOutput = [
44
+ {
45
+ date: 'May 31, 2022, 2:38:15 PM',
46
+ datumIdx: 2,
47
+ markerColor: 'hsla(286664040,50%,50%,0.8)',
48
+ seriesName: 'env="demo", instance="demo.do.prometheus", job="node", mode="test alt"',
49
+ seriesIdx: 1,
50
+ x: 1654007895000,
51
+ y: 0.0524463040446356,
52
+ },
53
+ ];
54
+ it('should return focused series data for points nearby the cursor', () => {
55
+ expect((0, focused_series_1.getNearbySeries)(chartData, pointInGrid, yBuffer)).toEqual(focusedSeriesOutput);
56
+ });
57
+ });
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ // Copyright 2022 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ var desc = Object.getOwnPropertyDescriptor(m, k);
17
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
18
+ desc = { enumerable: true, get: function() { return m[k]; } };
19
+ }
20
+ Object.defineProperty(o, k2, desc);
21
+ }) : (function(o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ }));
25
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ __exportStar(require("./Tooltip"), exports);
30
+ __exportStar(require("./SeriesMarker"), exports);
File without changes
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useChartsTheme = exports.ChartsThemeContext = exports.ChartsThemeProvider = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ // Copyright 2022 The Perses Authors
6
+ // Licensed under the Apache License, Version 2.0 (the "License");
7
+ // you may not use this file except in compliance with the License.
8
+ // You may obtain a copy of the License at
9
+ //
10
+ // http://www.apache.org/licenses/LICENSE-2.0
11
+ //
12
+ // Unless required by applicable law or agreed to in writing, software
13
+ // distributed under the License is distributed on an "AS IS" BASIS,
14
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ // See the License for the specific language governing permissions and
16
+ // limitations under the License.
17
+ const react_1 = require("react");
18
+ const echarts_1 = require("echarts");
19
+ function ChartsThemeProvider(props) {
20
+ const { children, themeName, chartsTheme } = props;
21
+ if (chartsTheme !== undefined && chartsTheme.echartsTheme !== undefined) {
22
+ // register ECharts theme to be used in individual charts, see: https://apache.github.io/echarts-handbook/en/concepts/style/#theme
23
+ (0, echarts_1.registerTheme)(themeName, chartsTheme.echartsTheme);
24
+ }
25
+ return (0, jsx_runtime_1.jsx)(exports.ChartsThemeContext.Provider, { value: chartsTheme, children: children });
26
+ }
27
+ exports.ChartsThemeProvider = ChartsThemeProvider;
28
+ exports.ChartsThemeContext = (0, react_1.createContext)(undefined);
29
+ function useChartsTheme() {
30
+ const ctx = (0, react_1.useContext)(exports.ChartsThemeContext);
31
+ if (ctx === undefined) {
32
+ throw new Error('No ChartsThemeContext found. Did you forget a Provider?');
33
+ }
34
+ return ctx;
35
+ }
36
+ exports.useChartsTheme = useChartsTheme;
package/dist/cjs/index.js CHANGED
@@ -29,9 +29,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  __exportStar(require("./EChart"), exports);
30
30
  __exportStar(require("./ErrorAlert"), exports);
31
31
  __exportStar(require("./ErrorBoundary"), exports);
32
- __exportStar(require("./GaugeChart"), exports);
32
+ __exportStar(require("./InfoTooltip"), exports);
33
33
  __exportStar(require("./LineChart"), exports);
34
+ __exportStar(require("./GaugeChart"), exports);
34
35
  __exportStar(require("./StatChart"), exports);
35
- __exportStar(require("./utils/combine-sx"), exports);
36
- __exportStar(require("./model/graph-model"), exports);
37
- __exportStar(require("./model/units"), exports);
36
+ __exportStar(require("./context/ChartsThemeProvider"), exports);
37
+ __exportStar(require("./utils"), exports);
38
+ __exportStar(require("./model"), exports);
File without changes