@perses-dev/components 0.5.0 → 0.6.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.
- package/dist/LineChart/LineChart.d.ts.map +1 -1
- package/dist/LineChart/LineChart.js +1 -1
- package/dist/Tooltip/Tooltip.d.ts +3 -4
- package/dist/Tooltip/Tooltip.d.ts.map +1 -1
- package/dist/Tooltip/Tooltip.js +1 -1
- package/dist/Tooltip/focused-series.js +1 -1
- package/dist/Tooltip/utils.d.ts +6 -0
- package/dist/Tooltip/utils.d.ts.map +1 -0
- package/dist/Tooltip/utils.js +1 -0
- package/dist/cjs/LineChart/LineChart.js +13 -4
- package/dist/cjs/Tooltip/Tooltip.js +36 -50
- package/dist/cjs/Tooltip/focused-series.js +1 -1
- package/dist/cjs/Tooltip/utils.js +53 -0
- package/package.json +4 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LineChart.d.ts","sourceRoot":"","sources":["../../src/LineChart/LineChart.tsx"],"names":[],"mappings":";AAgBA,OAAO,KAAK,EAEV,mBAAmB,EAEnB,qBAAqB,EACrB,wBAAwB,EACzB,MAAM,SAAS,CAAC;AAiBjB,OAAO,EAAiC,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAClF,OAAO,EAAe,WAAW,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"LineChart.d.ts","sourceRoot":"","sources":["../../src/LineChart/LineChart.tsx"],"names":[],"mappings":";AAgBA,OAAO,KAAK,EAEV,mBAAmB,EAEnB,qBAAqB,EACrB,wBAAwB,EACzB,MAAM,SAAS,CAAC;AAiBjB,OAAO,EAAiC,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAClF,OAAO,EAAe,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAG1D,OAAO,EAAgE,aAAa,EAAE,MAAM,SAAS,CAAC;AAiBtG,UAAU,cAAc;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,iBAAiB,CAAC;IACxB,IAAI,CAAC,EAAE,WAAW,CAAC;IACnB,IAAI,CAAC,EAAE,mBAAmB,CAAC;IAC3B,MAAM,CAAC,EAAE,qBAAqB,CAAC;IAC/B,SAAS,CAAC,EAAE,wBAAwB,EAAE,CAAC;IACvC,UAAU,CAAC,EAAE,CAAC,CAAC,EAAE,aAAa,KAAK,IAAI,CAAC;CACzC;AAED,wBAAgB,SAAS,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,EAAE,cAAc,eAsJpG"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as _jsx,jsxs as _jsxs}from"react/jsx-runtime";import{useMemo,useRef,useState}from"react";import{useDeepMemo}from"@perses-dev/core";import{Box}from"@mui/material";import{use}from"echarts/core";import{LineChart as EChartsLineChart}from"echarts/charts";import{GridComponent,DataZoomComponent,MarkAreaComponent,MarkLineComponent,MarkPointComponent,TitleComponent,ToolboxComponent,TooltipComponent,LegendComponent,VisualMapComponent}from"echarts/components";import{CanvasRenderer}from"echarts/renderers";import{EChart}from"../EChart";import{PROGRESSIVE_MODE_SERIES_LIMIT}from"../model/graph";import{formatValue}from"../model/units";import{useChartsTheme}from"../context/ChartsThemeProvider";import{
|
|
1
|
+
import{jsx as _jsx,jsxs as _jsxs}from"react/jsx-runtime";import{useMemo,useRef,useState}from"react";import{useDeepMemo}from"@perses-dev/core";import{Box}from"@mui/material";import{use}from"echarts/core";import{LineChart as EChartsLineChart}from"echarts/charts";import{GridComponent,DataZoomComponent,MarkAreaComponent,MarkLineComponent,MarkPointComponent,TitleComponent,ToolboxComponent,TooltipComponent,LegendComponent,VisualMapComponent}from"echarts/components";import{CanvasRenderer}from"echarts/renderers";import{EChart}from"../EChart";import{PROGRESSIVE_MODE_SERIES_LIMIT}from"../model/graph";import{formatValue}from"../model/units";import{useChartsTheme}from"../context/ChartsThemeProvider";import{Tooltip}from"../Tooltip/Tooltip";import{enableDataZoom,restoreChart,getDateRange,getFormattedDate}from"./utils";use([EChartsLineChart,GridComponent,DataZoomComponent,MarkAreaComponent,MarkLineComponent,MarkPointComponent,TitleComponent,ToolboxComponent,TooltipComponent,LegendComponent,VisualMapComponent,CanvasRenderer]);export function LineChart({height:e,data:o,unit:t,grid:r,legend:n,visualMap:a,onDataZoom:i}){const s=useChartsTheme(),m=useRef(),[p,l]=useState(!0),[u,h]=useState(!1),x=useMemo((()=>({datazoom:e=>{var t,r;if(void 0===i&&setTimeout((()=>{h(!1)}),10),void 0===i||void 0===e.batch[0])return;const n=null!==(t=e.batch[0].startValue)&&void 0!==t?t:0,a=null!==(r=e.batch[0].endValue)&&void 0!==r?r:o.xAxis.length-1,s=o.xAxis[n],m=o.xAxis[a];void 0!==s&&void 0!==m&&i({start:s,end:m,startIndex:n,endIndex:a})}})),[o,i,h]);void 0!==m.current&&enableDataZoom(m.current);const d=useDeepMemo((()=>{var e;if(void 0===o.timeSeries)return{};if(null===o.timeSeries||0===o.timeSeries.length)return s.noDataOption;const i=o.timeSeries.length<PROGRESSIVE_MODE_SERIES_LIMIT,m=null!==(e=o.rangeMs)&&void 0!==e?e:getDateRange(o.xAxis);return{series:o.timeSeries,xAxis:{type:"category",data:o.xAxis,max:o.xAxisMax,axisLabel:{formatter:e=>getFormattedDate(e,m)}},yAxis:{type:"value",boundaryGap:[0,"10%"],axisLabel:{formatter:e=>formatValue(e,t)}},animation:!1,tooltip:{show:i,trigger:"axis",showContent:!1,axisPointer:{type:"none"}},toolbox:{feature:{dataZoom:{icon:null,yAxisIndex:"none"}}},grid:r,legend:n,visualMap:a}}),[o,r,n,a]);return _jsxs(Box,{sx:{height:e},onClick:()=>h((e=>!e)),onDoubleClick:()=>{h(!1),void 0!==m.current&&restoreChart(m.current)},onMouseDown:e=>{e.target instanceof HTMLCanvasElement&&l(!1)},onMouseUp:()=>{l(!0)},onMouseLeave:()=>{l(!1),h(!1)},onMouseEnter:()=>{l(!0)},children:[!0===p&&_jsx(Tooltip,{chartRef:m,chartData:o,wrapLabels:!0,pinTooltip:u}),_jsx(EChart,{sx:{width:"100%",height:"100%"},option:d,theme:s.themeName,onEvents:x,_instance:m})]})}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { ECharts as EChartsInstance } from 'echarts/core';
|
|
3
3
|
import { EChartsDataFormat } from '../model/graph';
|
|
4
|
-
import { TooltipData } from './tooltip-model';
|
|
5
4
|
interface TooltipProps {
|
|
6
5
|
chartRef: React.MutableRefObject<EChartsInstance | undefined>;
|
|
7
|
-
tooltipData: TooltipData;
|
|
8
6
|
chartData: EChartsDataFormat;
|
|
7
|
+
pinTooltip: boolean;
|
|
9
8
|
wrapLabels?: boolean;
|
|
10
9
|
}
|
|
11
|
-
|
|
12
|
-
export {};
|
|
10
|
+
declare const Tooltip: React.NamedExoticComponent<TooltipProps>;
|
|
11
|
+
export { Tooltip };
|
|
13
12
|
//# sourceMappingURL=Tooltip.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Tooltip.d.ts","sourceRoot":"","sources":["../../src/Tooltip/Tooltip.tsx"],"names":[],"mappings":"AAaA,OAAO,KAAmB,MAAM,OAAO,CAAC;AAExC,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,cAAc,CAAC;AAC1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"Tooltip.d.ts","sourceRoot":"","sources":["../../src/Tooltip/Tooltip.tsx"],"names":[],"mappings":"AAaA,OAAO,KAAmB,MAAM,OAAO,CAAC;AAExC,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,cAAc,CAAC;AAC1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAMnD,UAAU,YAAY;IACpB,QAAQ,EAAE,KAAK,CAAC,gBAAgB,CAAC,eAAe,GAAG,SAAS,CAAC,CAAC;IAC9D,SAAS,EAAE,iBAAiB,CAAC;IAC7B,UAAU,EAAE,OAAO,CAAC;IACpB,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,QAAA,MAAM,OAAO,0CAqDX,CAAC;AAEH,OAAO,EAAE,OAAO,EAAE,CAAC"}
|
package/dist/Tooltip/Tooltip.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as _jsx}from"react/jsx-runtime";import{useState}from"react";import{Box,Portal}from"@mui/material";import{getFocusedSeriesData}from"./focused-series";import{TOOLTIP_MAX_HEIGHT,TOOLTIP_MAX_WIDTH,useMousePosition}from"./tooltip-model";import{TooltipContent}from"./TooltipContent";
|
|
1
|
+
import{jsx as _jsx}from"react/jsx-runtime";import React,{useState}from"react";import{Box,Portal}from"@mui/material";import{getFocusedSeriesData}from"./focused-series";import{TOOLTIP_MAX_HEIGHT,TOOLTIP_MAX_WIDTH,useMousePosition}from"./tooltip-model";import{TooltipContent}from"./TooltipContent";import{assembleTransform}from"./utils";const Tooltip=React.memo((function({chartRef:o,chartData:t,wrapLabels:e,pinTooltip:r}){var l,i;const[s,n]=useState(null),a=useMousePosition();if(null===a||null===a.target)return null;if(null===s&&"CANVAS"!==a.target.tagName)return null;const u=o.current,m=getFocusedSeriesData(a,t,s,u),p=null!==(l=null==u?void 0:u.getWidth())&&void 0!==l?l:750,f=null!==(i=null==u?void 0:u.getHeight())&&void 0!==i?i:230,c=assembleTransform(a,m.length,p,f,s);return 0===m.length?null:(!0===r&&null===s&&n(a),_jsx(Portal,{children:_jsx(Box,{sx:o=>({maxWidth:TOOLTIP_MAX_WIDTH,maxHeight:TOOLTIP_MAX_HEIGHT,position:"absolute",top:0,left:0,backgroundColor:"#2E313E",borderRadius:"6px",color:"#fff",fontSize:"11px",visibility:"visible",opacity:1,transition:"all 0.1s ease-out",zIndex:o.zIndex.tooltip,overflow:"hidden","&:hover":{overflowY:"auto"}}),style:{transform:c},children:_jsx(TooltipContent,{focusedSeries:m,wrapLabels:e})})}))}));export{Tooltip};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{TOOLTIP_DATE_FORMAT,TOOLTIP_MAX_ITEMS}from"./tooltip-model";export function getNearbySeries(e,t,r){var i,n,l,o,a;const s=[],u=null!==(i=t[0])&&void 0!==i?i:null,d=null!==(n=t[1])&&void 0!==n?n:null;if(null===u||null===d)return s;if(Array.isArray(e.xAxis)&&Array.isArray(e.timeSeries))for(let t=0;t<e.timeSeries.length;t++){const i=e.timeSeries[t];if(s.length
|
|
1
|
+
import{TOOLTIP_DATE_FORMAT,TOOLTIP_MAX_ITEMS}from"./tooltip-model";export function getNearbySeries(e,t,r){var i,n,l,o,a;const s=[],u=null!==(i=t[0])&&void 0!==i?i:null,d=null!==(n=t[1])&&void 0!==n?n:null;if(null===u||null===d)return s;if(Array.isArray(e.xAxis)&&Array.isArray(e.timeSeries))for(let t=0;t<e.timeSeries.length;t++){const i=e.timeSeries[t];if(s.length>=TOOLTIP_MAX_ITEMS)break;if(void 0!==i){const n=i.name?i.name.toString():"",m=null!==(l=i.color)&&void 0!==l?l:"#000";if(Array.isArray(i.data))for(let l=0;l<i.data.length;l++){const f=null!==(o=e.xAxis[l])&&void 0!==o?o:0,v=null!==(a=i.data[l])&&void 0!==a?a:0;if(u===l&&"-"!==v&&d<=v+r&&d>=v-r){const e=f>99999999999?f:1e3*f,r=TOOLTIP_DATE_FORMAT.format(e);s.push({seriesIdx:t,datumIdx:l,seriesName:n,date:r,x:f,y:v,markerColor:m.toString()})}}}}return s}export function getFocusedSeriesData(e,t,r,i){var n,l;if(void 0===i||null===e)return[];let o=!1;if(null!==e.target){const t=e.target.parentElement;if(null!==t){const e=t.parentElement;null!==e&&i.getDom()===e&&(o=!0)}}if(null!==r&&(e=r,o=!0),!1===o)return[];if(void 0===i._model)return[];const a=i._model.getComponent("yAxis").axis.scale._interval,s=t.timeSeries.length>TOOLTIP_MAX_ITEMS?.5*a:2*a,u=[null!==(n=e.plotCanvas.x)&&void 0!==n?n:0,null!==(l=e.plotCanvas.y)&&void 0!==l?l:0];if(i.containPixel("grid",u)){const e=i.convertFromPixel("grid",u);if(void 0!==e[0]&&void 0!==e[1])return getNearbySeries(t,e,s)}return[]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { CursorCoordinates, CursorData } from './tooltip-model';
|
|
2
|
+
/**
|
|
3
|
+
* Determine position of tooltip depending on chart dimensions and the number of focused series
|
|
4
|
+
*/
|
|
5
|
+
export declare function assembleTransform(mousePos: CursorData['coords'], seriesNum: number, chartWidth: number, chartHeight: number, pinnedPos: CursorCoordinates | null): string;
|
|
6
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/Tooltip/utils.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAqB,MAAM,iBAAiB,CAAC;AAEnF;;GAEG;AACH,wBAAgB,iBAAiB,CAC/B,QAAQ,EAAE,UAAU,CAAC,QAAQ,CAAC,EAC9B,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,MAAM,EACnB,SAAS,EAAE,iBAAiB,GAAG,IAAI,UAoCpC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{TOOLTIP_MAX_WIDTH}from"./tooltip-model";export function assembleTransform(t,o,r,e,n){if(null===t)return"translate3d(0, 0)";null!==n&&(t=n);const p=t.viewport.x;let a=t.viewport.y+16;const l=.75*e;!0==t.viewport.y>.8*window.innerHeight?a=o>6?.65*t.viewport.y:.75*t.viewport.y:t.plotCanvas.y>l&&(a=.85*t.viewport.y);const i=r-.9*(o>1?TOOLTIP_MAX_WIDTH:TOOLTIP_MAX_WIDTH/2);return t.plotCanvas.x>i&&p>TOOLTIP_MAX_WIDTH?`translate3d(${p-32}px, ${a}px, 0) translateX(-100%)`:`translate3d(${p+32}px, ${a}px, 0)`}
|
|
@@ -25,7 +25,6 @@ const EChart_1 = require("../EChart");
|
|
|
25
25
|
const graph_1 = require("../model/graph");
|
|
26
26
|
const units_1 = require("../model/units");
|
|
27
27
|
const ChartsThemeProvider_1 = require("../context/ChartsThemeProvider");
|
|
28
|
-
const tooltip_model_1 = require("../Tooltip/tooltip-model");
|
|
29
28
|
const Tooltip_1 = require("../Tooltip/Tooltip");
|
|
30
29
|
const utils_1 = require("./utils");
|
|
31
30
|
(0, core_2.use)([
|
|
@@ -46,11 +45,17 @@ function LineChart({ height, data, unit, grid, legend, visualMap, onDataZoom })
|
|
|
46
45
|
const chartsTheme = (0, ChartsThemeProvider_1.useChartsTheme)();
|
|
47
46
|
const chartRef = (0, react_1.useRef)();
|
|
48
47
|
const [showTooltip, setShowTooltip] = (0, react_1.useState)(true);
|
|
48
|
+
const [pinTooltip, setPinTooltip] = (0, react_1.useState)(false);
|
|
49
49
|
const handleEvents = (0, react_1.useMemo)(() => {
|
|
50
50
|
return {
|
|
51
|
-
// TODO: use legendselectchanged event to fix tooltip when legend selected
|
|
52
51
|
datazoom: (params) => {
|
|
53
52
|
var _a, _b;
|
|
53
|
+
if (onDataZoom === undefined) {
|
|
54
|
+
setTimeout(() => {
|
|
55
|
+
// workaround so unpin happens after click event
|
|
56
|
+
setPinTooltip(false);
|
|
57
|
+
}, 10);
|
|
58
|
+
}
|
|
54
59
|
if (onDataZoom === undefined || params.batch[0] === undefined)
|
|
55
60
|
return;
|
|
56
61
|
const startIndex = (_a = params.batch[0].startValue) !== null && _a !== void 0 ? _a : 0;
|
|
@@ -67,12 +72,15 @@ function LineChart({ height, data, unit, grid, legend, visualMap, onDataZoom })
|
|
|
67
72
|
onDataZoom(zoomEvent);
|
|
68
73
|
}
|
|
69
74
|
},
|
|
75
|
+
// TODO: use legendselectchanged event to fix tooltip when legend selected
|
|
70
76
|
};
|
|
71
|
-
}, [data, onDataZoom]);
|
|
77
|
+
}, [data, onDataZoom, setPinTooltip]);
|
|
72
78
|
if (chartRef.current !== undefined) {
|
|
73
79
|
(0, utils_1.enableDataZoom)(chartRef.current);
|
|
74
80
|
}
|
|
81
|
+
const handleOnClick = () => setPinTooltip((current) => !current);
|
|
75
82
|
const handleOnDoubleClick = () => {
|
|
83
|
+
setPinTooltip(false);
|
|
76
84
|
if (chartRef.current !== undefined) {
|
|
77
85
|
(0, utils_1.restoreChart)(chartRef.current);
|
|
78
86
|
}
|
|
@@ -91,6 +99,7 @@ function LineChart({ height, data, unit, grid, legend, visualMap, onDataZoom })
|
|
|
91
99
|
};
|
|
92
100
|
const handleOnMouseLeave = () => {
|
|
93
101
|
setShowTooltip(false);
|
|
102
|
+
setPinTooltip(false);
|
|
94
103
|
};
|
|
95
104
|
const option = (0, core_1.useDeepMemo)(() => {
|
|
96
105
|
var _a;
|
|
@@ -146,7 +155,7 @@ function LineChart({ height, data, unit, grid, legend, visualMap, onDataZoom })
|
|
|
146
155
|
}, [data, grid, legend, visualMap]);
|
|
147
156
|
return ((0, jsx_runtime_1.jsxs)(material_1.Box, { sx: {
|
|
148
157
|
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,
|
|
158
|
+
}, onClick: handleOnClick, onDoubleClick: handleOnDoubleClick, onMouseDown: handleOnMouseDown, onMouseUp: handleOnMouseUp, onMouseLeave: handleOnMouseLeave, onMouseEnter: handleOnMouseEnter, children: [showTooltip === true && ((0, jsx_runtime_1.jsx)(Tooltip_1.Tooltip, { chartRef: chartRef, chartData: data, wrapLabels: true, pinTooltip: pinTooltip })), (0, jsx_runtime_1.jsx)(EChart_1.EChart, { sx: {
|
|
150
159
|
width: '100%',
|
|
151
160
|
height: '100%',
|
|
152
161
|
}, option: option, theme: chartsTheme.themeName, onEvents: handleEvents, _instance: chartRef })] }));
|
|
@@ -1,4 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
26
|
exports.Tooltip = void 0;
|
|
4
27
|
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
@@ -14,36 +37,32 @@ const jsx_runtime_1 = require("react/jsx-runtime");
|
|
|
14
37
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
38
|
// See the License for the specific language governing permissions and
|
|
16
39
|
// limitations under the License.
|
|
17
|
-
const react_1 = require("react");
|
|
40
|
+
const react_1 = __importStar(require("react"));
|
|
18
41
|
const material_1 = require("@mui/material");
|
|
19
42
|
const focused_series_1 = require("./focused-series");
|
|
20
43
|
const tooltip_model_1 = require("./tooltip-model");
|
|
21
44
|
const TooltipContent_1 = require("./TooltipContent");
|
|
22
|
-
|
|
45
|
+
const utils_1 = require("./utils");
|
|
46
|
+
const Tooltip = react_1.default.memo(function Tooltip({ chartRef, chartData, wrapLabels, pinTooltip }) {
|
|
23
47
|
var _a, _b;
|
|
24
|
-
const { chartRef, chartData } = props;
|
|
25
48
|
const [pinnedPos, setPinnedPos] = (0, react_1.useState)(null);
|
|
26
49
|
const mousePos = (0, tooltip_model_1.useMousePosition)();
|
|
27
|
-
if (mousePos === null)
|
|
50
|
+
if (mousePos === null || mousePos.target === null)
|
|
51
|
+
return null;
|
|
52
|
+
// ensure user is hovering over a chart before checking for nearby series
|
|
53
|
+
if (pinnedPos === null && mousePos.target.tagName !== 'CANVAS')
|
|
28
54
|
return null;
|
|
29
55
|
const chart = chartRef.current;
|
|
30
56
|
const focusedSeries = (0, focused_series_1.getFocusedSeriesData)(mousePos, chartData, pinnedPos, chart);
|
|
31
57
|
const chartWidth = (_a = chart === null || chart === void 0 ? void 0 : chart.getWidth()) !== null && _a !== void 0 ? _a : 750;
|
|
32
58
|
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
|
-
}
|
|
59
|
+
const cursorTransform = (0, utils_1.assembleTransform)(mousePos, focusedSeries.length, chartWidth, chartHeight, pinnedPos);
|
|
44
60
|
if (focusedSeries.length === 0) {
|
|
45
61
|
return null;
|
|
46
62
|
}
|
|
63
|
+
if (pinTooltip === true && pinnedPos === null) {
|
|
64
|
+
setPinnedPos(mousePos);
|
|
65
|
+
}
|
|
47
66
|
return ((0, jsx_runtime_1.jsx)(material_1.Portal, { children: (0, jsx_runtime_1.jsx)(material_1.Box, { sx: (theme) => ({
|
|
48
67
|
maxWidth: tooltip_model_1.TOOLTIP_MAX_WIDTH,
|
|
49
68
|
maxHeight: tooltip_model_1.TOOLTIP_MAX_HEIGHT,
|
|
@@ -64,39 +83,6 @@ function Tooltip(props) {
|
|
|
64
83
|
},
|
|
65
84
|
}), style: {
|
|
66
85
|
transform: cursorTransform,
|
|
67
|
-
},
|
|
68
|
-
}
|
|
86
|
+
}, children: (0, jsx_runtime_1.jsx)(TooltipContent_1.TooltipContent, { focusedSeries: focusedSeries, wrapLabels: wrapLabels }) }) }));
|
|
87
|
+
});
|
|
69
88
|
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
|
-
}
|
|
@@ -29,7 +29,7 @@ function getNearbySeries(data, pointInGrid, yBuffer) {
|
|
|
29
29
|
if (Array.isArray(data.xAxis) && Array.isArray(data.timeSeries)) {
|
|
30
30
|
for (let seriesIdx = 0; seriesIdx < data.timeSeries.length; seriesIdx++) {
|
|
31
31
|
const currentSeries = data.timeSeries[seriesIdx];
|
|
32
|
-
if (currentFocusedData.length
|
|
32
|
+
if (currentFocusedData.length >= tooltip_model_1.TOOLTIP_MAX_ITEMS)
|
|
33
33
|
break;
|
|
34
34
|
if (currentSeries !== undefined) {
|
|
35
35
|
const currentSeriesName = currentSeries.name ? currentSeries.name.toString() : '';
|
|
@@ -0,0 +1,53 @@
|
|
|
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.assembleTransform = void 0;
|
|
16
|
+
const tooltip_model_1 = require("./tooltip-model");
|
|
17
|
+
/**
|
|
18
|
+
* Determine position of tooltip depending on chart dimensions and the number of focused series
|
|
19
|
+
*/
|
|
20
|
+
function assembleTransform(mousePos, seriesNum, chartWidth, chartHeight, pinnedPos) {
|
|
21
|
+
if (mousePos === null) {
|
|
22
|
+
return 'translate3d(0, 0)';
|
|
23
|
+
}
|
|
24
|
+
if (pinnedPos !== null) {
|
|
25
|
+
mousePos = pinnedPos;
|
|
26
|
+
}
|
|
27
|
+
const cursorPaddingX = 32;
|
|
28
|
+
const cursorPaddingY = 16;
|
|
29
|
+
const x = mousePos.viewport.x;
|
|
30
|
+
let y = mousePos.viewport.y + cursorPaddingY;
|
|
31
|
+
const isCloseToBottom = mousePos.viewport.y > window.innerHeight * 0.8;
|
|
32
|
+
const yPosAdjustThreshold = chartHeight * 0.75;
|
|
33
|
+
// adjust so tooltip does not get cut off at bottom of chart, reduce multiplier to move up
|
|
34
|
+
if (isCloseToBottom === true) {
|
|
35
|
+
if (seriesNum > 6) {
|
|
36
|
+
y = mousePos.viewport.y * 0.65;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
y = mousePos.viewport.y * 0.75;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else if (mousePos.plotCanvas.y > yPosAdjustThreshold) {
|
|
43
|
+
y = mousePos.viewport.y * 0.85;
|
|
44
|
+
}
|
|
45
|
+
// use tooltip width to determine when to repos from right to left (width is narrower when only 1 focused series since labels wrap)
|
|
46
|
+
const tooltipWidth = seriesNum > 1 ? tooltip_model_1.TOOLTIP_MAX_WIDTH : tooltip_model_1.TOOLTIP_MAX_WIDTH / 2;
|
|
47
|
+
const xPosAdjustThreshold = chartWidth - tooltipWidth * 0.9;
|
|
48
|
+
// reposition so tooltip is never too close to right side of chart or left side of browser window
|
|
49
|
+
return mousePos.plotCanvas.x > xPosAdjustThreshold && x > tooltip_model_1.TOOLTIP_MAX_WIDTH
|
|
50
|
+
? `translate3d(${x - cursorPaddingX}px, ${y}px, 0) translateX(-100%)`
|
|
51
|
+
: `translate3d(${x + cursorPaddingX}px, ${y}px, 0)`;
|
|
52
|
+
}
|
|
53
|
+
exports.assembleTransform = assembleTransform;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@perses-dev/components",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Common UI components used across Perses features",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://github.com/perses/perses/blob/main/README.md",
|
|
@@ -24,14 +24,15 @@
|
|
|
24
24
|
"lint:fix": "eslint --fix src --ext .ts,.tsx"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@perses-dev/core": "^0.
|
|
27
|
+
"@perses-dev/core": "^0.6.0",
|
|
28
28
|
"echarts": "^5.3.3",
|
|
29
29
|
"lodash-es": "^4.17.21",
|
|
30
30
|
"react-error-boundary": "^3.1.4"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
33
|
"@mui/material": "^5.5.1",
|
|
34
|
-
"react": "^17.0.2"
|
|
34
|
+
"react": "^17.0.2",
|
|
35
|
+
"react-dom": "^17.0.2"
|
|
35
36
|
},
|
|
36
37
|
"files": [
|
|
37
38
|
"dist"
|