123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- import type {RefObject} from 'react';
- import {Fragment, useCallback, useEffect, useMemo, useRef, useState} from 'react';
- import {useTheme} from '@emotion/react';
- import styled from '@emotion/styled';
- import {useResizeObserver} from '@react-aria/utils';
- import color from 'color';
- import type {EChartsOption} from 'echarts';
- import isEqual from 'lodash/isEqual';
- import moment from 'moment';
- import {Button} from 'sentry/components/button';
- import {IconClose, IconZoom} from 'sentry/icons';
- import {space} from 'sentry/styles/space';
- import type {EChartBrushEndHandler, ReactEchartsRef} from 'sentry/types/echarts';
- import {getMetricConversionFunction} from 'sentry/utils/metrics/normalizeMetricValue';
- import type {SelectionRange} from 'sentry/utils/metrics/types';
- import type {ValueRect} from 'sentry/views/ddm/chartUtils';
- import {getValueRect} from 'sentry/views/ddm/chartUtils';
- import {CHART_HEIGHT} from 'sentry/views/ddm/constants';
- import type {FocusAreaProps} from 'sentry/views/ddm/context';
- import type {DateTimeObject} from '../../components/charts/utils';
- interface AbsolutePosition {
- height: string;
- left: string;
- top: string;
- width: string;
- }
- interface UseFocusAreaOptions {
- widgetIndex: number;
- isDisabled?: boolean;
- useFullYAxis?: boolean;
- }
- export interface FocusAreaSelection {
- range: SelectionRange;
- widgetIndex: number;
- }
- export interface UseFocusAreaProps extends FocusAreaProps {
- chartRef: RefObject<ReactEchartsRef>;
- opts: UseFocusAreaOptions;
- chartUnit?: string;
- onZoom?: (range: DateTimeObject) => void;
- sampleUnit?: string;
- }
- type BrushEndResult = Parameters<EChartBrushEndHandler>[0];
- export function useFocusArea({
- chartRef,
- selection: selection,
- opts: {widgetIndex, isDisabled, useFullYAxis},
- sampleUnit = 'none',
- chartUnit = 'none',
- onAdd,
- onDraw,
- onRemove,
- onZoom,
- }: UseFocusAreaProps) {
- const hasFocusArea = useMemo(
- () => selection && selection.widgetIndex === widgetIndex,
- [selection, widgetIndex]
- );
- const isDrawingRef = useRef(false);
- const theme = useTheme();
- const startBrush = useCallback(() => {
- if (hasFocusArea || isDisabled) {
- return;
- }
- onDraw?.();
- chartRef.current?.getEchartsInstance().dispatchAction({
- type: 'takeGlobalCursor',
- key: 'brush',
- brushOption: {
- brushType: 'rect',
- },
- });
- }, [chartRef, hasFocusArea, isDisabled, onDraw]);
- useEffect(() => {
- const chartElement = chartRef.current?.ele;
- const handleMouseDown = () => {
- isDrawingRef.current = true;
- startBrush();
- };
- // Handle mouse up is called after onBrushEnd
- // We can use it for a final reliable cleanup as onBrushEnd is not always called (e.g. when simply clicking the chart)
- const handleMouseUp = () => {
- isDrawingRef.current = false;
- };
- chartElement?.addEventListener('mousedown', handleMouseDown, {capture: true});
- window.addEventListener('mouseup', handleMouseUp);
- return () => {
- chartElement?.removeEventListener('mousedown', handleMouseDown, {capture: true});
- window.removeEventListener('mouseup', handleMouseUp);
- };
- }, [chartRef, startBrush]);
- const onBrushEnd = useCallback(
- (brushEnd: BrushEndResult) => {
- if (isDisabled || !isDrawingRef.current) {
- return;
- }
- const rect = brushEnd.areas[0];
- if (!rect) {
- return;
- }
- const valueConverter = getMetricConversionFunction(chartUnit, sampleUnit);
- const range = getSelectionRange(
- brushEnd,
- !!useFullYAxis,
- getValueRect(chartRef),
- valueConverter
- );
- onAdd?.({
- widgetIndex,
- range,
- });
- // Remove brush from echarts immediately after adding the focus area
- // since brushes get added to all charts in the group by default and then randomly
- // render in the wrong place
- chartRef.current?.getEchartsInstance().dispatchAction({
- type: 'brush',
- brushType: 'clear',
- areas: [],
- });
- isDrawingRef.current = false;
- },
- [isDisabled, sampleUnit, chartUnit, useFullYAxis, chartRef, onAdd, widgetIndex]
- );
- const handleRemove = useCallback(() => {
- onRemove?.();
- }, [onRemove]);
- const handleZoomIn = useCallback(() => {
- handleRemove();
- onZoom?.({
- period: null,
- ...selection?.range,
- });
- }, [selection, handleRemove, onZoom]);
- const brushOptions = useMemo(() => {
- return {
- onBrushEnd,
- toolBox: {
- show: false,
- },
- brush: {
- toolbox: ['rect'],
- xAxisIndex: 0,
- brushStyle: {
- borderWidth: 2,
- borderColor: theme.gray500,
- color: 'transparent',
- },
- inBrush: {
- opacity: 1,
- },
- outOfBrush: {
- opacity: 1,
- },
- z: 10,
- } as EChartsOption['brush'],
- };
- }, [onBrushEnd, theme.gray500]);
- if (hasFocusArea) {
- return {
- overlay: (
- <BrushRectOverlay
- rect={selection!}
- onRemove={handleRemove}
- onZoom={handleZoomIn}
- chartRef={chartRef}
- useFullYAxis={!!useFullYAxis}
- sampleUnit={sampleUnit}
- chartUnit={chartUnit}
- />
- ),
- isDrawingRef,
- options: {},
- };
- }
- return {
- overlay: null,
- isDrawingRef,
- options: brushOptions,
- };
- }
- type BrushRectOverlayProps = {
- chartRef: RefObject<ReactEchartsRef>;
- chartUnit: string;
- onRemove: () => void;
- onZoom: () => void;
- rect: FocusAreaSelection | null;
- sampleUnit: string;
- useFullYAxis: boolean;
- };
- function BrushRectOverlay({
- rect,
- onZoom,
- onRemove,
- useFullYAxis,
- chartRef,
- sampleUnit,
- chartUnit,
- }: BrushRectOverlayProps) {
- const [position, setPosition] = useState<AbsolutePosition | null>(null);
- const wrapperRef = useRef<HTMLDivElement>(null);
- useResizeObserver({
- ref: wrapperRef,
- onResize: () => {
- const chartInstance = chartRef.current?.getEchartsInstance();
- chartInstance?.resize();
- updatePosition();
- },
- });
- const updatePosition = useCallback(() => {
- const chartInstance = chartRef.current?.getEchartsInstance();
- if (!rect || !chartInstance) {
- return;
- }
- const finder = {xAxisId: 'xAxis', yAxisId: 'yAxis'};
- const valueConverter = getMetricConversionFunction(sampleUnit, chartUnit);
- const max = valueConverter(rect.range.max ?? null);
- const min = valueConverter(rect.range.min ?? null);
- const topLeft = chartInstance.convertToPixel(finder, [
- getTimestamp(rect.range.start),
- max,
- ] as number[]);
- const bottomRight = chartInstance.convertToPixel(finder, [
- getTimestamp(rect.range.end),
- min,
- ] as number[]);
- if (!topLeft || !bottomRight) {
- return;
- }
- const widthPx = bottomRight[0] - topLeft[0];
- const heightPx = bottomRight[1] - topLeft[1];
- const resultTop = useFullYAxis ? '0px' : `${topLeft[1].toPrecision(5)}px`;
- const resultHeight = useFullYAxis
- ? `${CHART_HEIGHT}px`
- : `${heightPx.toPrecision(5)}px`;
- // Ensure the focus area rect is always within the chart bounds
- const left = Math.max(topLeft[0], 0);
- const width = Math.min(widthPx, chartInstance.getWidth() - left);
- const newPosition = {
- left: `${left.toPrecision(5)}px`,
- top: resultTop,
- width: `${width.toPrecision(5)}px`,
- height: resultHeight,
- };
- if (!isEqual(newPosition, position)) {
- setPosition(newPosition);
- }
- }, [chartRef, rect, sampleUnit, chartUnit, useFullYAxis, position]);
- useEffect(() => {
- updatePosition();
- }, [rect, updatePosition]);
- if (!position) {
- return null;
- }
- const {left, top, width, height} = position;
- return (
- <Fragment>
- <FocusAreaWrapper ref={wrapperRef}>
- <FocusAreaRect top={top} left={left} width={width} height={height} />
- </FocusAreaWrapper>
- <FocusAreaRectActions top={top} rectHeight={height} left={left}>
- <Button
- size="xs"
- onClick={onZoom}
- icon={<IconZoom isZoomIn />}
- aria-label="zoom"
- />
- <Button size="xs" onClick={onRemove} icon={<IconClose />} aria-label="remove" />
- </FocusAreaRectActions>
- </Fragment>
- );
- }
- const getDate = date =>
- date ? moment.utc(date).format(moment.HTML5_FMT.DATETIME_LOCAL_SECONDS) : null;
- const getTimestamp = date => (date ? moment.utc(date).valueOf() : null);
- const getSelectionRange = (
- params: BrushEndResult,
- useFullYAxis: boolean,
- boundingRect: ValueRect,
- valueConverter: (value: number) => number
- ): SelectionRange => {
- const rect = params.areas[0];
- const startTimestamp = Math.min(...rect.coordRange[0]);
- const endTimestamp = Math.max(...rect.coordRange[0]);
- const startDate = getDate(Math.max(startTimestamp, boundingRect.xMin));
- const endDate = getDate(Math.min(endTimestamp, boundingRect.xMax));
- const min = useFullYAxis ? NaN : valueConverter(Math.min(...rect.coordRange[1]));
- const max = useFullYAxis ? NaN : valueConverter(Math.max(...rect.coordRange[1]));
- return {
- start: startDate,
- end: endDate,
- min,
- max,
- };
- };
- const FocusAreaRectActions = styled('div')<{
- left: string;
- rectHeight: string;
- top: string;
- }>`
- position: absolute;
- top: calc(${p => p.top} + ${p => p.rectHeight});
- left: ${p => p.left};
- display: flex;
- gap: ${space(0.5)};
- padding: ${space(0.5)};
- z-index: 2;
- pointer-events: auto;
- `;
- const FocusAreaWrapper = styled('div')`
- position: absolute;
- top: 0px;
- left: 0;
- height: 100%;
- width: 100%;
- overflow: hidden;
- `;
- const FocusAreaRect = styled('div')<{
- height: string;
- left: string;
- top: string;
- width: string;
- }>`
- position: absolute;
- top: ${p => p.top};
- left: ${p => p.left};
- width: ${p => p.width};
- height: ${p => p.height};
- padding: ${space(1)};
- pointer-events: none;
- z-index: 1;
- border: 2px solid ${p => p.theme.gray500};
- border-radius: ${p => p.theme.borderRadius};
- /* requires overflow: hidden on FocusAreaWrapper */
- box-shadow: 0px 0px 0px 9999px ${p => color(p.theme.surface400).alpha(0.75).toString()};
- `;
|