@perses-dev/components 0.3.0 → 0.4.1
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/dist/{ECharts.d.ts → EChart.d.ts} +3 -4
- package/dist/EChart.d.ts.map +1 -0
- package/dist/EChart.js +1 -0
- package/dist/GaugeChart.js +1 -1
- package/dist/LineChart.d.ts +1 -1
- package/dist/LineChart.d.ts.map +1 -1
- package/dist/LineChart.js +1 -1
- package/dist/StatChart.d.ts.map +1 -1
- package/dist/StatChart.js +1 -1
- package/dist/cjs/EChart.js +154 -0
- package/dist/cjs/ErrorAlert.js +25 -0
- package/dist/cjs/ErrorBoundary.js +18 -0
- package/dist/cjs/GaugeChart.js +178 -0
- package/dist/cjs/LineChart.js +252 -0
- package/dist/cjs/StatChart.js +207 -0
- package/dist/cjs/index.js +37 -0
- package/dist/cjs/model/graph-model.js +16 -0
- package/dist/cjs/model/units.js +211 -0
- package/dist/cjs/tooltip/SeriesInfo.js +77 -0
- package/dist/cjs/tooltip/SeriesMarker.js +30 -0
- package/dist/cjs/tooltip/Tooltip.js +102 -0
- package/dist/cjs/tooltip/TooltipContent.js +50 -0
- package/dist/cjs/tooltip/focused-series.js +110 -0
- package/dist/cjs/tooltip/tooltip-model.js +82 -0
- package/dist/cjs/utils/combine-sx.js +30 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/model/graph-model.d.ts +1 -0
- package/dist/model/graph-model.d.ts.map +1 -1
- package/dist/model/units.d.ts +4 -0
- package/dist/model/units.d.ts.map +1 -1
- package/dist/model/units.js +1 -1
- package/dist/tooltip/SeriesInfo.d.ts.map +1 -1
- package/dist/tooltip/SeriesInfo.js +1 -1
- package/dist/tooltip/Tooltip.d.ts.map +1 -1
- package/dist/tooltip/Tooltip.js +1 -1
- package/dist/tooltip/TooltipContent.js +1 -1
- package/dist/tooltip/focused-series.d.ts.map +1 -1
- package/dist/tooltip/focused-series.js +1 -1
- package/package.json +5 -2
- package/dist/ECharts.d.ts.map +0 -1
- package/dist/ECharts.js +0 -1
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright 2021 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.SIMPLE_UNITS = exports.removeUnitNamespace = exports.SANCTIONED_UNITS = exports.isSanctionedSimpleUnitIdentifier = exports.abbreviateLargeNumber = exports.formatValue = void 0;
|
|
16
|
+
const date_fns_1 = require("date-fns");
|
|
17
|
+
function formatValue(value, unitOptions) {
|
|
18
|
+
if (unitOptions === undefined) {
|
|
19
|
+
return value.toString();
|
|
20
|
+
}
|
|
21
|
+
if (isTimeUnit(unitOptions)) {
|
|
22
|
+
return formatTime(value, unitOptions);
|
|
23
|
+
}
|
|
24
|
+
if (isPercentUnit(unitOptions)) {
|
|
25
|
+
return formatPercent(value, unitOptions);
|
|
26
|
+
}
|
|
27
|
+
if (isDecimalUnit(unitOptions)) {
|
|
28
|
+
return formatDecimal(value, unitOptions);
|
|
29
|
+
}
|
|
30
|
+
const exhaustive = unitOptions;
|
|
31
|
+
throw new Error(`Unknown unit options ${exhaustive}`);
|
|
32
|
+
}
|
|
33
|
+
exports.formatValue = formatValue;
|
|
34
|
+
const timeUnitKinds = ['Milliseconds', 'Seconds', 'Minutes', 'Hours', 'Days', 'Weeks', 'Months', 'Years'];
|
|
35
|
+
const timeUnitKindsSet = new Set(timeUnitKinds);
|
|
36
|
+
function isTimeUnit(unitOptions) {
|
|
37
|
+
return timeUnitKindsSet.has(unitOptions.kind);
|
|
38
|
+
}
|
|
39
|
+
function formatTime(value, unitOptions) {
|
|
40
|
+
// Create a Duration from the value based on what time unit it is
|
|
41
|
+
const duration = {};
|
|
42
|
+
switch (unitOptions.kind) {
|
|
43
|
+
case 'Milliseconds':
|
|
44
|
+
duration.seconds = value / 1000;
|
|
45
|
+
break;
|
|
46
|
+
case 'Seconds':
|
|
47
|
+
duration.seconds = value;
|
|
48
|
+
break;
|
|
49
|
+
case 'Minutes':
|
|
50
|
+
duration.minutes = value;
|
|
51
|
+
break;
|
|
52
|
+
case 'Hours':
|
|
53
|
+
duration.hours = value;
|
|
54
|
+
break;
|
|
55
|
+
case 'Days':
|
|
56
|
+
duration.days = value;
|
|
57
|
+
break;
|
|
58
|
+
case 'Weeks':
|
|
59
|
+
duration.weeks = value;
|
|
60
|
+
break;
|
|
61
|
+
case 'Months':
|
|
62
|
+
duration.months = value;
|
|
63
|
+
break;
|
|
64
|
+
case 'Years':
|
|
65
|
+
duration.years = value;
|
|
66
|
+
break;
|
|
67
|
+
default:
|
|
68
|
+
const exhaustive = unitOptions.kind;
|
|
69
|
+
throw new Error(`Unknown time unit type ${exhaustive}`);
|
|
70
|
+
}
|
|
71
|
+
// Find the largest whole time unit we can display the value in and use it
|
|
72
|
+
const ms = (0, date_fns_1.milliseconds)(duration);
|
|
73
|
+
const seconds = ms / 1000;
|
|
74
|
+
if (seconds < 1) {
|
|
75
|
+
return `${ms.toFixed()} milliseconds`;
|
|
76
|
+
}
|
|
77
|
+
const minutes = seconds / 60;
|
|
78
|
+
if (minutes < 1) {
|
|
79
|
+
return `${seconds.toFixed()} seconds`;
|
|
80
|
+
}
|
|
81
|
+
const hours = minutes / 60;
|
|
82
|
+
if (hours < 1) {
|
|
83
|
+
return `${minutes.toFixed()} minutes`;
|
|
84
|
+
}
|
|
85
|
+
const days = hours / 24;
|
|
86
|
+
if (days < 1) {
|
|
87
|
+
return `${hours.toFixed()} hours`;
|
|
88
|
+
}
|
|
89
|
+
const weeks = days / 7;
|
|
90
|
+
if (weeks < 1) {
|
|
91
|
+
return `${days.toFixed()} days`;
|
|
92
|
+
}
|
|
93
|
+
const years = weeks / 52;
|
|
94
|
+
if (years < 1) {
|
|
95
|
+
return `${weeks.toFixed()} weeks`;
|
|
96
|
+
}
|
|
97
|
+
return `${years.toFixed()} years`;
|
|
98
|
+
}
|
|
99
|
+
const percentUnitKinds = ['Percent', 'PercentDecimal'];
|
|
100
|
+
const percentUnitKindsSet = new Set(percentUnitKinds);
|
|
101
|
+
function isPercentUnit(unitOptions) {
|
|
102
|
+
return percentUnitKindsSet.has(unitOptions.kind);
|
|
103
|
+
}
|
|
104
|
+
function formatPercent(value, unitOptions) {
|
|
105
|
+
if (unitOptions.kind === 'PercentDecimal') {
|
|
106
|
+
value = value * 100;
|
|
107
|
+
}
|
|
108
|
+
return value.toFixed(unitOptions.decimal_places) + '%';
|
|
109
|
+
}
|
|
110
|
+
const decimalUnitKinds = ['Decimal'];
|
|
111
|
+
const decimalUnitKindsSet = new Set(decimalUnitKinds);
|
|
112
|
+
function isDecimalUnit(unitOptions) {
|
|
113
|
+
return decimalUnitKindsSet.has(unitOptions.kind);
|
|
114
|
+
}
|
|
115
|
+
function formatDecimal(value, unitOptions) {
|
|
116
|
+
var _a, _b;
|
|
117
|
+
const maximumFractionDigits = (_a = unitOptions.decimal_places) !== null && _a !== void 0 ? _a : 2;
|
|
118
|
+
if (unitOptions.suffix !== undefined) {
|
|
119
|
+
if (isSanctionedSimpleUnitIdentifier(unitOptions.suffix)) {
|
|
120
|
+
const formatParams = {
|
|
121
|
+
style: 'unit',
|
|
122
|
+
minimumFractionDigits: 0,
|
|
123
|
+
maximumFractionDigits: maximumFractionDigits,
|
|
124
|
+
useGrouping: true,
|
|
125
|
+
unit: unitOptions.suffix,
|
|
126
|
+
unitDisplay: (_b = unitOptions.unitDisplay) !== null && _b !== void 0 ? _b : 'narrow',
|
|
127
|
+
};
|
|
128
|
+
const unitFormatter = new Intl.NumberFormat('en-US', formatParams);
|
|
129
|
+
return unitFormatter.format(value);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const formatParams = {
|
|
133
|
+
style: 'decimal',
|
|
134
|
+
minimumFractionDigits: 0,
|
|
135
|
+
maximumFractionDigits: maximumFractionDigits,
|
|
136
|
+
useGrouping: true,
|
|
137
|
+
};
|
|
138
|
+
const decimalFormatter = new Intl.NumberFormat('en-US', formatParams);
|
|
139
|
+
return decimalFormatter.format(value);
|
|
140
|
+
}
|
|
141
|
+
// Take a large number and abbreviate appropriate suffix
|
|
142
|
+
// ex) 10000 -> 10k, 1000000 -> 1M
|
|
143
|
+
function abbreviateLargeNumber(num) {
|
|
144
|
+
return num >= 1e12
|
|
145
|
+
? num / 1e12 + 'T'
|
|
146
|
+
: num >= 1e9
|
|
147
|
+
? num / 1e9 + 'B'
|
|
148
|
+
: num >= 1e6
|
|
149
|
+
? num / 1e6 + 'M'
|
|
150
|
+
: num >= 1e3
|
|
151
|
+
? num / 1e3 + 'k'
|
|
152
|
+
: num;
|
|
153
|
+
}
|
|
154
|
+
exports.abbreviateLargeNumber = abbreviateLargeNumber;
|
|
155
|
+
// Util to check unit name against ECMA standard: https://tc39.es/ecma402/#sec-issanctionedsimpleunitidentifier
|
|
156
|
+
function isSanctionedSimpleUnitIdentifier(unitIdentifier) {
|
|
157
|
+
return exports.SIMPLE_UNITS.indexOf(unitIdentifier) > -1;
|
|
158
|
+
}
|
|
159
|
+
exports.isSanctionedSimpleUnitIdentifier = isSanctionedSimpleUnitIdentifier;
|
|
160
|
+
// https://tc39.es/ecma402/#table-sanctioned-simple-unit-identifiers
|
|
161
|
+
exports.SANCTIONED_UNITS = [
|
|
162
|
+
'angle-degree',
|
|
163
|
+
'area-acre',
|
|
164
|
+
'area-hectare',
|
|
165
|
+
'concentr-percent',
|
|
166
|
+
'digital-bit',
|
|
167
|
+
'digital-byte',
|
|
168
|
+
'digital-gigabit',
|
|
169
|
+
'digital-gigabyte',
|
|
170
|
+
'digital-kilobit',
|
|
171
|
+
'digital-kilobyte',
|
|
172
|
+
'digital-megabit',
|
|
173
|
+
'digital-megabyte',
|
|
174
|
+
'digital-petabyte',
|
|
175
|
+
'digital-terabit',
|
|
176
|
+
'digital-terabyte',
|
|
177
|
+
'duration-day',
|
|
178
|
+
'duration-hour',
|
|
179
|
+
'duration-millisecond',
|
|
180
|
+
'duration-minute',
|
|
181
|
+
'duration-month',
|
|
182
|
+
'duration-second',
|
|
183
|
+
'duration-week',
|
|
184
|
+
'duration-year',
|
|
185
|
+
'length-centimeter',
|
|
186
|
+
'length-foot',
|
|
187
|
+
'length-inch',
|
|
188
|
+
'length-kilometer',
|
|
189
|
+
'length-meter',
|
|
190
|
+
'length-mile-scandinavian',
|
|
191
|
+
'length-mile',
|
|
192
|
+
'length-millimeter',
|
|
193
|
+
'length-yard',
|
|
194
|
+
'mass-gram',
|
|
195
|
+
'mass-kilogram',
|
|
196
|
+
'mass-ounce',
|
|
197
|
+
'mass-pound',
|
|
198
|
+
'mass-stone',
|
|
199
|
+
'temperature-celsius',
|
|
200
|
+
'temperature-fahrenheit',
|
|
201
|
+
'volume-fluid-ounce',
|
|
202
|
+
'volume-gallon',
|
|
203
|
+
'volume-liter',
|
|
204
|
+
'volume-milliliter',
|
|
205
|
+
];
|
|
206
|
+
// removes the namespace prefix, ex: duration-hour -> hour
|
|
207
|
+
function removeUnitNamespace(unit) {
|
|
208
|
+
return unit.slice(unit.indexOf('-') + 1);
|
|
209
|
+
}
|
|
210
|
+
exports.removeUnitNamespace = removeUnitNamespace;
|
|
211
|
+
exports.SIMPLE_UNITS = exports.SANCTIONED_UNITS.map(removeUnitNamespace);
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SeriesInfo = void 0;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
// Copyright 2021 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 material_1 = require("@mui/material");
|
|
18
|
+
const SeriesMarker_1 = require("./SeriesMarker");
|
|
19
|
+
const tooltip_model_1 = require("./tooltip-model");
|
|
20
|
+
function SeriesInfo(props) {
|
|
21
|
+
const { seriesName, y, markerColor, totalSeries, wrapLabels } = props;
|
|
22
|
+
// TODO (sjcobb): regex to remove __name__, improve series labels
|
|
23
|
+
const formattedSeriesLabels = seriesName.replace(/[{}"]/g, '');
|
|
24
|
+
if (totalSeries === 1) {
|
|
25
|
+
const jsonFormattedSeries = seriesName[0] === '{' ? true : false;
|
|
26
|
+
return ((0, jsx_runtime_1.jsxs)(material_1.Stack, { spacing: 0.5, children: [(0, jsx_runtime_1.jsxs)(material_1.Box, { sx: (theme) => ({
|
|
27
|
+
height: '16px',
|
|
28
|
+
display: 'flex',
|
|
29
|
+
flexDirection: 'row',
|
|
30
|
+
alignItems: 'center',
|
|
31
|
+
justifyContent: 'left',
|
|
32
|
+
color: theme.palette.common.white,
|
|
33
|
+
fontSize: '11px',
|
|
34
|
+
}), children: [(0, jsx_runtime_1.jsx)(SeriesMarker_1.SeriesMarker, { markerColor: markerColor }), (0, jsx_runtime_1.jsxs)(material_1.Box, { component: "span", children: ["value:", (0, jsx_runtime_1.jsx)(material_1.Box, { component: "span", sx: (theme) => ({
|
|
35
|
+
color: theme.palette.common.white,
|
|
36
|
+
fontWeight: 700,
|
|
37
|
+
paddingLeft: '2px',
|
|
38
|
+
}), children: y })] })] }), (0, jsx_runtime_1.jsx)(material_1.Divider, { sx: (theme) => ({
|
|
39
|
+
borderColor: theme.palette.grey['500'],
|
|
40
|
+
}) }), (0, jsx_runtime_1.jsx)(material_1.Box, { sx: (theme) => ({
|
|
41
|
+
color: theme.palette.common.white,
|
|
42
|
+
}), children: formattedSeriesLabels.split(',').map((name) => {
|
|
43
|
+
if (name) {
|
|
44
|
+
const [key, value] = jsonFormattedSeries ? name.split(':') : name.split('=');
|
|
45
|
+
const formattedKey = value !== undefined ? `${key}: ` : key;
|
|
46
|
+
return ((0, jsx_runtime_1.jsxs)(material_1.Box, { sx: { display: 'flex', gap: '4px' }, children: [(0, jsx_runtime_1.jsx)(material_1.Typography, { sx: { fontSize: '11px' }, children: formattedKey }), (0, jsx_runtime_1.jsx)(material_1.Typography, { sx: (theme) => ({
|
|
47
|
+
color: theme.palette.common.white,
|
|
48
|
+
fontWeight: 700,
|
|
49
|
+
fontSize: '11px',
|
|
50
|
+
}), children: value })] }, name));
|
|
51
|
+
}
|
|
52
|
+
}) })] }));
|
|
53
|
+
}
|
|
54
|
+
const inlineSeriesLabels = formattedSeriesLabels.replace(/[,]/g, ', ').replace(/[:=]/g, ': ');
|
|
55
|
+
return ((0, jsx_runtime_1.jsxs)(material_1.Box, { sx: {
|
|
56
|
+
display: 'table-row',
|
|
57
|
+
paddingTop: 0.5,
|
|
58
|
+
}, children: [(0, jsx_runtime_1.jsxs)(material_1.Box, { sx: {
|
|
59
|
+
display: 'table-cell',
|
|
60
|
+
maxWidth: '520px',
|
|
61
|
+
}, children: [(0, jsx_runtime_1.jsx)(SeriesMarker_1.SeriesMarker, { markerColor: markerColor }), (0, jsx_runtime_1.jsx)(material_1.Box, { component: "span", sx: (theme) => ({
|
|
62
|
+
color: theme.palette.common.white,
|
|
63
|
+
display: 'inline-block',
|
|
64
|
+
maxWidth: tooltip_model_1.TOOLTIP_LABELS_MAX_WIDTH,
|
|
65
|
+
overflow: 'hidden',
|
|
66
|
+
textOverflow: 'ellipsis',
|
|
67
|
+
whiteSpace: wrapLabels ? 'normal' : 'nowrap',
|
|
68
|
+
width: 'calc(100% - 20px)',
|
|
69
|
+
}), children: inlineSeriesLabels })] }), (0, jsx_runtime_1.jsx)(material_1.Box, { sx: {
|
|
70
|
+
display: 'table-cell',
|
|
71
|
+
fontWeight: '700',
|
|
72
|
+
paddingLeft: 1.5,
|
|
73
|
+
textAlign: 'right',
|
|
74
|
+
verticalAlign: 'top',
|
|
75
|
+
}, children: y })] }));
|
|
76
|
+
}
|
|
77
|
+
exports.SeriesInfo = SeriesInfo;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SeriesMarker = void 0;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
// Copyright 2021 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 material_1 = require("@mui/material");
|
|
18
|
+
function SeriesMarker(props) {
|
|
19
|
+
const { markerColor } = props;
|
|
20
|
+
return ((0, jsx_runtime_1.jsx)(material_1.Box, { sx: {
|
|
21
|
+
display: 'inline-block',
|
|
22
|
+
width: '12px',
|
|
23
|
+
borderRadius: '2px',
|
|
24
|
+
height: '12px',
|
|
25
|
+
marginTop: 0.25,
|
|
26
|
+
marginRight: 1,
|
|
27
|
+
verticalAlign: 'top',
|
|
28
|
+
}, style: { backgroundColor: markerColor } }));
|
|
29
|
+
}
|
|
30
|
+
exports.SeriesMarker = SeriesMarker;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Tooltip = 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 focused_series_1 = require("./focused-series");
|
|
20
|
+
const tooltip_model_1 = require("./tooltip-model");
|
|
21
|
+
const TooltipContent_1 = require("./TooltipContent");
|
|
22
|
+
function Tooltip(props) {
|
|
23
|
+
var _a, _b;
|
|
24
|
+
const { chartRef, chartData } = props;
|
|
25
|
+
const [pinnedPos, setPinnedPos] = (0, react_1.useState)(null);
|
|
26
|
+
const mousePos = (0, tooltip_model_1.useMousePosition)();
|
|
27
|
+
if (mousePos === null)
|
|
28
|
+
return null;
|
|
29
|
+
const chart = chartRef.current;
|
|
30
|
+
const focusedSeries = (0, focused_series_1.getFocusedSeriesData)(mousePos, chartData, pinnedPos, chart);
|
|
31
|
+
const chartWidth = (_a = chart === null || chart === void 0 ? void 0 : chart.getWidth()) !== null && _a !== void 0 ? _a : 750;
|
|
32
|
+
const chartHeight = (_b = chart === null || chart === void 0 ? void 0 : chart.getHeight()) !== null && _b !== void 0 ? _b : 230;
|
|
33
|
+
const cursorTransform = assembleTransform(mousePos, focusedSeries.length, chartWidth, chartHeight, pinnedPos);
|
|
34
|
+
function handleMouseEnter() {
|
|
35
|
+
if (mousePos !== null) {
|
|
36
|
+
setPinnedPos(mousePos);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function handleMouseLeave() {
|
|
40
|
+
if (pinnedPos !== null) {
|
|
41
|
+
setPinnedPos(null);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (focusedSeries.length === 0) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
return ((0, jsx_runtime_1.jsx)(material_1.Portal, { children: (0, jsx_runtime_1.jsx)(material_1.Box, { sx: (theme) => ({
|
|
48
|
+
maxWidth: tooltip_model_1.TOOLTIP_MAX_WIDTH,
|
|
49
|
+
maxHeight: tooltip_model_1.TOOLTIP_MAX_HEIGHT,
|
|
50
|
+
position: 'absolute',
|
|
51
|
+
top: 0,
|
|
52
|
+
left: 0,
|
|
53
|
+
backgroundColor: '#2E313E',
|
|
54
|
+
borderRadius: '6px',
|
|
55
|
+
color: '#fff',
|
|
56
|
+
fontSize: '11px',
|
|
57
|
+
visibility: 'visible',
|
|
58
|
+
opacity: 1,
|
|
59
|
+
transition: 'all 0.1s ease-out',
|
|
60
|
+
zIndex: theme.zIndex.tooltip,
|
|
61
|
+
overflow: 'hidden',
|
|
62
|
+
'&:hover': {
|
|
63
|
+
overflowY: 'auto',
|
|
64
|
+
},
|
|
65
|
+
}), style: {
|
|
66
|
+
transform: cursorTransform,
|
|
67
|
+
}, onMouseEnter: () => handleMouseEnter(), onMouseLeave: () => handleMouseLeave(), children: (0, jsx_runtime_1.jsx)(TooltipContent_1.TooltipContent, { focusedSeries: focusedSeries, wrapLabels: props.wrapLabels }) }) }));
|
|
68
|
+
}
|
|
69
|
+
exports.Tooltip = Tooltip;
|
|
70
|
+
function assembleTransform(mousePos, seriesNum, chartWidth, chartHeight, pinnedPos) {
|
|
71
|
+
if (mousePos === null) {
|
|
72
|
+
return 'translate3d(0, 0)';
|
|
73
|
+
}
|
|
74
|
+
if (pinnedPos !== null) {
|
|
75
|
+
mousePos = pinnedPos;
|
|
76
|
+
}
|
|
77
|
+
const cursorPaddingX = 32;
|
|
78
|
+
const cursorPaddingY = 16;
|
|
79
|
+
const x = mousePos.viewport.x;
|
|
80
|
+
let y = mousePos.viewport.y + cursorPaddingY;
|
|
81
|
+
const isCloseToBottom = mousePos.viewport.y > window.innerHeight * 0.8;
|
|
82
|
+
const yPosAdjustThreshold = chartHeight * 0.75;
|
|
83
|
+
// adjust so tooltip does not get cut off at bottom of chart, reduce multiplier to move up
|
|
84
|
+
if (isCloseToBottom === true) {
|
|
85
|
+
if (seriesNum > 6) {
|
|
86
|
+
y = mousePos.viewport.y * 0.65;
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
y = mousePos.viewport.y * 0.75;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
else if (mousePos.plotCanvas.y > yPosAdjustThreshold) {
|
|
93
|
+
y = mousePos.viewport.y * 0.85;
|
|
94
|
+
}
|
|
95
|
+
// use tooltip width to determine when to repos from right to left (width is narrower when only 1 focused series since labels wrap)
|
|
96
|
+
const tooltipWidth = seriesNum > 1 ? tooltip_model_1.TOOLTIP_MAX_WIDTH : tooltip_model_1.TOOLTIP_MAX_WIDTH / 2;
|
|
97
|
+
const xPosAdjustThreshold = chartWidth - tooltipWidth * 0.9;
|
|
98
|
+
// reposition so tooltip is never too close to right side of chart or left side of browser window
|
|
99
|
+
return mousePos.plotCanvas.x > xPosAdjustThreshold && x > tooltip_model_1.TOOLTIP_MAX_WIDTH
|
|
100
|
+
? `translate3d(${x - cursorPaddingX}px, ${y}px, 0) translateX(-100%)`
|
|
101
|
+
: `translate3d(${x + cursorPaddingX}px, ${y}px, 0)`;
|
|
102
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TooltipContent = void 0;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
// Copyright 2021 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 SeriesInfo_1 = require("./SeriesInfo");
|
|
20
|
+
function TooltipContent(props) {
|
|
21
|
+
const { focusedSeries, wrapLabels } = props;
|
|
22
|
+
const seriesTime = focusedSeries && focusedSeries[0] && focusedSeries[0].date ? focusedSeries[0].date : null;
|
|
23
|
+
const formatTimeSeriesHeader = (timeString) => {
|
|
24
|
+
const [month, year, time] = timeString.split(',');
|
|
25
|
+
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)(material_1.Typography, { variant: "caption", sx: (theme) => ({
|
|
26
|
+
color: theme.palette.common.white,
|
|
27
|
+
}), children: [month, ", ", year, " \u2013"] }), (0, jsx_runtime_1.jsx)(material_1.Typography, { variant: "caption", children: (0, jsx_runtime_1.jsx)("strong", { children: time }) })] }));
|
|
28
|
+
};
|
|
29
|
+
const sortedFocusedSeries = (0, react_1.useMemo)(() => {
|
|
30
|
+
if (focusedSeries === null)
|
|
31
|
+
return null;
|
|
32
|
+
return focusedSeries.sort((a, b) => (a.y > b.y ? -1 : 1));
|
|
33
|
+
}, [focusedSeries]);
|
|
34
|
+
if (sortedFocusedSeries !== null && seriesTime !== null) {
|
|
35
|
+
return ((0, jsx_runtime_1.jsxs)(material_1.Stack, { py: 1, px: 1.5, spacing: 0.5, children: [(0, jsx_runtime_1.jsx)(material_1.Typography, { variant: "caption", children: formatTimeSeriesHeader(seriesTime) }), (0, jsx_runtime_1.jsx)(material_1.Divider, { sx: (theme) => ({
|
|
36
|
+
borderColor: theme.palette.grey['500'],
|
|
37
|
+
}) }), (0, jsx_runtime_1.jsx)(material_1.Box, { sx: {
|
|
38
|
+
display: 'table',
|
|
39
|
+
}, children: sortedFocusedSeries.map(({ datumIdx, seriesIdx, seriesName, y, markerColor }) => {
|
|
40
|
+
if (datumIdx === null || seriesIdx === null)
|
|
41
|
+
return null;
|
|
42
|
+
const key = seriesIdx.toString() + datumIdx.toString();
|
|
43
|
+
return ((0, jsx_runtime_1.jsx)(SeriesInfo_1.SeriesInfo, { seriesName: seriesName, y: y, markerColor: markerColor, totalSeries: sortedFocusedSeries.length, wrapLabels: wrapLabels }, key));
|
|
44
|
+
}) })] }));
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, {});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.TooltipContent = TooltipContent;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright 2021 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.getFocusedSeriesData = exports.getNearbySeries = void 0;
|
|
16
|
+
const tooltip_model_1 = require("./tooltip-model");
|
|
17
|
+
/**
|
|
18
|
+
* Returns formatted series data for the points that are close to the user's cursor
|
|
19
|
+
* Adjust yBuffer to increase or decrease number of series shown
|
|
20
|
+
*/
|
|
21
|
+
function getNearbySeries(data, pointInGrid, yBuffer) {
|
|
22
|
+
var _a, _b, _c, _d, _e;
|
|
23
|
+
const currentFocusedData = [];
|
|
24
|
+
const focusedX = (_a = pointInGrid[0]) !== null && _a !== void 0 ? _a : null;
|
|
25
|
+
const focusedY = (_b = pointInGrid[1]) !== null && _b !== void 0 ? _b : null;
|
|
26
|
+
if (focusedX === null || focusedY === null) {
|
|
27
|
+
return currentFocusedData;
|
|
28
|
+
}
|
|
29
|
+
if (Array.isArray(data.xAxis) && Array.isArray(data.timeSeries)) {
|
|
30
|
+
for (let seriesIdx = 0; seriesIdx < data.timeSeries.length; seriesIdx++) {
|
|
31
|
+
const currentSeries = data.timeSeries[seriesIdx];
|
|
32
|
+
if (currentFocusedData.length > tooltip_model_1.TOOLTIP_MAX_ITEMS)
|
|
33
|
+
break;
|
|
34
|
+
if (currentSeries !== undefined) {
|
|
35
|
+
const currentSeriesName = currentSeries.name ? currentSeries.name.toString() : '';
|
|
36
|
+
const markerColor = (_c = currentSeries.color) !== null && _c !== void 0 ? _c : '#000';
|
|
37
|
+
if (Array.isArray(currentSeries.data)) {
|
|
38
|
+
for (let datumIdx = 0; datumIdx < currentSeries.data.length; datumIdx++) {
|
|
39
|
+
const xValue = (_d = data.xAxis[datumIdx]) !== null && _d !== void 0 ? _d : 0;
|
|
40
|
+
const yValue = (_e = currentSeries.data[datumIdx]) !== null && _e !== void 0 ? _e : 0;
|
|
41
|
+
if (focusedX === datumIdx) {
|
|
42
|
+
if (yValue !== '-' && focusedY <= yValue + yBuffer && focusedY >= yValue - yBuffer) {
|
|
43
|
+
// determine whether to convert timestamp to ms, see: https://stackoverflow.com/a/23982005/17575201
|
|
44
|
+
const xValueMilliSeconds = xValue > 99999999999 ? xValue : xValue * 1000;
|
|
45
|
+
const formattedDate = tooltip_model_1.TOOLTIP_DATE_FORMAT.format(xValueMilliSeconds);
|
|
46
|
+
currentFocusedData.push({
|
|
47
|
+
seriesIdx: seriesIdx,
|
|
48
|
+
datumIdx: datumIdx,
|
|
49
|
+
seriesName: currentSeriesName,
|
|
50
|
+
date: formattedDate,
|
|
51
|
+
x: xValue,
|
|
52
|
+
y: yValue,
|
|
53
|
+
markerColor: markerColor.toString(),
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return currentFocusedData;
|
|
63
|
+
}
|
|
64
|
+
exports.getNearbySeries = getNearbySeries;
|
|
65
|
+
/**
|
|
66
|
+
* Uses mouse position to determine whether user is hovering over a chart canvas
|
|
67
|
+
* If yes, convert from pixel values to logical cartesian coordinates and return all focused series
|
|
68
|
+
*/
|
|
69
|
+
function getFocusedSeriesData(mousePos, chartData, pinnedPos, chart) {
|
|
70
|
+
var _a, _b;
|
|
71
|
+
if (chart === undefined || mousePos === null)
|
|
72
|
+
return [];
|
|
73
|
+
// prevents multiple tooltips showing from adjacent charts
|
|
74
|
+
let cursorTargetMatchesChart = false;
|
|
75
|
+
if (mousePos.target !== null) {
|
|
76
|
+
const currentParent = mousePos.target.parentElement;
|
|
77
|
+
if (currentParent !== null) {
|
|
78
|
+
const currentGrandparent = currentParent.parentElement;
|
|
79
|
+
if (currentGrandparent !== null) {
|
|
80
|
+
const chartDom = chart.getDom();
|
|
81
|
+
if (chartDom === currentGrandparent) {
|
|
82
|
+
cursorTargetMatchesChart = true;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// allows moving cursor inside tooltip
|
|
88
|
+
if (pinnedPos !== null) {
|
|
89
|
+
mousePos = pinnedPos;
|
|
90
|
+
cursorTargetMatchesChart = true;
|
|
91
|
+
}
|
|
92
|
+
if (cursorTargetMatchesChart === false)
|
|
93
|
+
return [];
|
|
94
|
+
if (chart['_model'] === undefined)
|
|
95
|
+
return [];
|
|
96
|
+
const chartModel = chart['_model'];
|
|
97
|
+
const yAxisInterval = chartModel.getComponent('yAxis').axis.scale._interval;
|
|
98
|
+
const seriesNum = chartData.timeSeries.length;
|
|
99
|
+
// tooltip trigger area gets smaller with more series
|
|
100
|
+
const yBuffer = seriesNum > tooltip_model_1.TOOLTIP_MAX_ITEMS ? yAxisInterval * 0.5 : yAxisInterval * 2;
|
|
101
|
+
const pointInPixel = [(_a = mousePos.plotCanvas.x) !== null && _a !== void 0 ? _a : 0, (_b = mousePos.plotCanvas.y) !== null && _b !== void 0 ? _b : 0];
|
|
102
|
+
if (chart.containPixel('grid', pointInPixel)) {
|
|
103
|
+
const pointInGrid = chart.convertFromPixel('grid', pointInPixel);
|
|
104
|
+
if (pointInGrid[0] !== undefined && pointInGrid[1] !== undefined) {
|
|
105
|
+
return getNearbySeries(chartData, pointInGrid, yBuffer);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return [];
|
|
109
|
+
}
|
|
110
|
+
exports.getFocusedSeriesData = getFocusedSeriesData;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright 2021 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.useMousePosition = exports.emptyTooltipData = exports.defaultCursorData = exports.TOOLTIP_DATE_FORMAT = exports.TOOLTIP_MAX_ITEMS = exports.TOOLTIP_LABELS_MAX_WIDTH = exports.TOOLTIP_MAX_HEIGHT = exports.TOOLTIP_MAX_WIDTH = void 0;
|
|
16
|
+
const react_1 = require("react");
|
|
17
|
+
exports.TOOLTIP_MAX_WIDTH = 650;
|
|
18
|
+
exports.TOOLTIP_MAX_HEIGHT = 230;
|
|
19
|
+
exports.TOOLTIP_LABELS_MAX_WIDTH = exports.TOOLTIP_MAX_WIDTH - 150;
|
|
20
|
+
exports.TOOLTIP_MAX_ITEMS = 50;
|
|
21
|
+
exports.TOOLTIP_DATE_FORMAT = new Intl.DateTimeFormat(undefined, {
|
|
22
|
+
year: 'numeric',
|
|
23
|
+
month: 'short',
|
|
24
|
+
day: 'numeric',
|
|
25
|
+
hour: 'numeric',
|
|
26
|
+
minute: 'numeric',
|
|
27
|
+
second: 'numeric',
|
|
28
|
+
hour12: true,
|
|
29
|
+
});
|
|
30
|
+
exports.defaultCursorData = {
|
|
31
|
+
coords: {
|
|
32
|
+
viewport: {
|
|
33
|
+
x: 0,
|
|
34
|
+
y: 0,
|
|
35
|
+
},
|
|
36
|
+
plotCanvas: {
|
|
37
|
+
x: 0,
|
|
38
|
+
y: 0,
|
|
39
|
+
},
|
|
40
|
+
zrender: {
|
|
41
|
+
x: 0,
|
|
42
|
+
y: 0,
|
|
43
|
+
},
|
|
44
|
+
target: null,
|
|
45
|
+
},
|
|
46
|
+
chartWidth: 0,
|
|
47
|
+
};
|
|
48
|
+
exports.emptyTooltipData = {
|
|
49
|
+
cursor: exports.defaultCursorData,
|
|
50
|
+
focusedSeries: null,
|
|
51
|
+
};
|
|
52
|
+
const useMousePosition = () => {
|
|
53
|
+
const [coords, setCoords] = (0, react_1.useState)(null);
|
|
54
|
+
(0, react_1.useEffect)(() => {
|
|
55
|
+
const setFromEvent = (e) => {
|
|
56
|
+
return setCoords({
|
|
57
|
+
viewport: {
|
|
58
|
+
x: e.clientX,
|
|
59
|
+
y: e.clientY,
|
|
60
|
+
},
|
|
61
|
+
plotCanvas: {
|
|
62
|
+
x: e.offsetX,
|
|
63
|
+
y: e.offsetY,
|
|
64
|
+
},
|
|
65
|
+
zrender: {
|
|
66
|
+
// echarts canvas coordinates added automatically by zrender
|
|
67
|
+
// zrX and zrY are similar to offsetX and offsetY but they return undefined when not hovering over a chart canvas
|
|
68
|
+
x: e.zrX,
|
|
69
|
+
y: e.zrY,
|
|
70
|
+
},
|
|
71
|
+
// necessary to check whether cursor target matches correct chart canvas (since each chart has its own mousemove listener)
|
|
72
|
+
target: e.target,
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
window.addEventListener('mousemove', setFromEvent);
|
|
76
|
+
return () => {
|
|
77
|
+
window.removeEventListener('mousemove', setFromEvent);
|
|
78
|
+
};
|
|
79
|
+
}, []);
|
|
80
|
+
return coords;
|
|
81
|
+
};
|
|
82
|
+
exports.useMousePosition = useMousePosition;
|