123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- import {Component} from 'react';
- import type {Location} from 'history';
- import DataZoomInside from 'sentry/components/charts/components/dataZoomInside';
- import ToolBox from 'sentry/components/charts/components/toolBox';
- import type {EChartChartReadyHandler, EChartDataZoomHandler} from 'sentry/types/echarts';
- import {browserHistory} from 'sentry/utils/browserHistory';
- type RenderProps = {
- dataZoom: ReturnType<typeof DataZoomInside>;
- onChartReady: EChartChartReadyHandler;
- onDataZoom: EChartDataZoomHandler;
- toolBox: ReturnType<typeof ToolBox>;
- };
- export type BarChartBucket = {
- end: number;
- start: number;
- };
- type Props = {
-
- buckets: BarChartBucket[];
-
- children: (props: RenderProps) => React.ReactNode;
- location: Location;
-
- paramEnd: string;
-
- paramStart: string;
-
- xAxisIndex: number[];
-
- minZoomWidth?: number;
- onChartReady?: EChartChartReadyHandler;
- onDataZoom?: EChartDataZoomHandler;
-
- onDataZoomCancelled?: () => void;
-
- onHistoryPush?: (start: number, end: number) => void;
- };
- class BarChartZoom extends Component<Props> {
- zooming: (() => void) | null = null;
- /**
- * Enable zoom immediately instead of having to toggle to zoom
- */
- handleChartReady = chart => {
- this.props.onChartReady?.(chart);
- };
-
- handleChartFinished = (_props, chart) => {
- if (typeof this.zooming === 'function') {
- this.zooming();
- this.zooming = null;
- }
-
- const zoom = chart._componentsViews?.find(c => c._features?.dataZoom);
- if (zoom && !zoom._features.dataZoom._isZoomActive) {
-
- chart.dispatchAction({
- type: 'takeGlobalCursor',
- key: 'dataZoomSelect',
- dataZoomSelectActive: true,
- });
- }
- };
- handleDataZoom = (evt, chart) => {
- const model = chart.getModel();
- const {startValue, endValue} = model._payload.batch[0];
-
-
-
- if (startValue !== null && endValue !== null) {
- const {buckets, location, paramStart, paramEnd, minZoomWidth, onHistoryPush} =
- this.props;
- const {start} = buckets[startValue];
- const {end} = buckets[endValue];
- if (minZoomWidth === undefined || end - start > minZoomWidth) {
- const target = {
- pathname: location.pathname,
- query: {
- ...location.query,
- [paramStart]: start,
- [paramEnd]: end,
- },
- };
- if (onHistoryPush) {
- onHistoryPush(start, end);
- } else {
- browserHistory.push(target);
- }
- } else {
-
- chart.dispatchAction({type: 'restore'});
- this.props.onDataZoomCancelled?.();
- }
- } else {
-
- chart.dispatchAction({type: 'restore'});
- this.props.onDataZoomCancelled?.();
- }
- this.props.onDataZoom?.(evt, chart);
- };
- render() {
- const {children, xAxisIndex} = this.props;
- const renderProps = {
- onChartReady: this.handleChartReady,
- onFinished: this.handleChartFinished,
- dataZoom: DataZoomInside({xAxisIndex}),
-
-
- toolBox: ToolBox(
- {},
- {
- dataZoom: {
- title: {
- zoom: '',
- back: '',
- },
- iconStyle: {
- borderWidth: 0,
- color: 'transparent',
- opacity: 0,
- },
- },
- }
- ),
- onDataZoom: this.handleDataZoom,
- };
- return children(renderProps);
- }
- }
- export default BarChartZoom;
|