chart.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import {forwardRef, useCallback, useEffect, useMemo, useRef} from 'react';
  2. import styled from '@emotion/styled';
  3. import {useHover} from '@react-aria/interactions';
  4. import * as echarts from 'echarts/core';
  5. import {CanvasRenderer} from 'echarts/renderers';
  6. import {updateDateTime} from 'sentry/actionCreators/pageFilters';
  7. import {AreaChart} from 'sentry/components/charts/areaChart';
  8. import {BarChart} from 'sentry/components/charts/barChart';
  9. import {LineChart} from 'sentry/components/charts/lineChart';
  10. import {DateTimeObject} from 'sentry/components/charts/utils';
  11. import {ReactEchartsRef} from 'sentry/types/echarts';
  12. import mergeRefs from 'sentry/utils/mergeRefs';
  13. import {
  14. formatMetricsUsingUnitAndOp,
  15. MetricDisplayType,
  16. updateQuery,
  17. } from 'sentry/utils/metrics';
  18. import useRouter from 'sentry/utils/useRouter';
  19. import {useFocusAreaBrush} from 'sentry/views/ddm/chartBrush';
  20. import {DDM_CHART_GROUP} from 'sentry/views/ddm/constants';
  21. import {useDDMContext} from 'sentry/views/ddm/context';
  22. import {getFormatter} from '../../components/charts/components/tooltip';
  23. import {Series} from './widget';
  24. type ChartProps = {
  25. displayType: MetricDisplayType;
  26. series: Series[];
  27. widgetIndex: number;
  28. operation?: string;
  29. };
  30. // We need to enable canvas renderer for echarts before we use it here.
  31. // Once we use it in more places, this should probably move to a more global place
  32. // But for now we keep it here to not invluence the bundle size of the main chunks.
  33. echarts.use(CanvasRenderer);
  34. export const MetricChart = forwardRef<ReactEchartsRef, ChartProps>(
  35. ({series, displayType, operation, widgetIndex}, forwardedRef) => {
  36. const router = useRouter();
  37. const chartRef = useRef<ReactEchartsRef>(null);
  38. const {hoverProps, isHovered} = useHover({
  39. isDisabled: false,
  40. });
  41. const {focusArea, addFocusArea, removeFocusArea} = useDDMContext();
  42. const handleAddFocusArea = useCallback(
  43. newFocusArea => {
  44. addFocusArea(newFocusArea);
  45. updateQuery(router, {focusArea: JSON.stringify(newFocusArea)});
  46. },
  47. [addFocusArea, router]
  48. );
  49. const handleRemoveFocusArea = useCallback(() => {
  50. removeFocusArea();
  51. updateQuery(router, {focusArea: null});
  52. }, [removeFocusArea, router]);
  53. const handleZoom = useCallback(
  54. (range: DateTimeObject) => {
  55. updateDateTime(range, router, {save: true});
  56. },
  57. [router]
  58. );
  59. const focusAreaBrush = useFocusAreaBrush(
  60. chartRef,
  61. focusArea,
  62. handleAddFocusArea,
  63. handleRemoveFocusArea,
  64. handleZoom,
  65. {
  66. widgetIndex,
  67. isDisabled: !isHovered,
  68. }
  69. );
  70. useEffect(() => {
  71. if (focusArea) {
  72. return;
  73. }
  74. const urlFocusArea = router.location.query.focusArea;
  75. if (urlFocusArea) {
  76. addFocusArea(JSON.parse(urlFocusArea));
  77. }
  78. }, [router, addFocusArea, focusArea]);
  79. // TODO(ddm): Try to do this in a more elegant way
  80. useEffect(() => {
  81. const echartsInstance = chartRef?.current?.getEchartsInstance();
  82. if (echartsInstance && !echartsInstance.group) {
  83. echartsInstance.group = DDM_CHART_GROUP;
  84. }
  85. });
  86. const unit = series[0]?.unit;
  87. const seriesToShow = useMemo(
  88. () =>
  89. series
  90. .filter(s => !s.hidden)
  91. .map(s => ({...s, silent: displayType === MetricDisplayType.BAR})),
  92. [series, displayType]
  93. );
  94. // TODO(ddm): This assumes that all series have the same bucket size
  95. const bucketSize = seriesToShow[0]?.data[1]?.name - seriesToShow[0]?.data[0]?.name;
  96. const isSubMinuteBucket = bucketSize < 60_000;
  97. const seriesLength = seriesToShow[0]?.data.length;
  98. const displayFogOfWar = operation && ['sum', 'count'].includes(operation);
  99. const chartProps = useMemo(() => {
  100. const formatters = {
  101. valueFormatter: (value: number) =>
  102. formatMetricsUsingUnitAndOp(value, unit, operation),
  103. isGroupedByDate: true,
  104. bucketSize,
  105. showTimeInTooltip: true,
  106. addSecondsToTimeFormat: isSubMinuteBucket,
  107. limit: 10,
  108. };
  109. return {
  110. ...focusAreaBrush.options,
  111. forwardedRef: mergeRefs([forwardedRef, chartRef]),
  112. series: seriesToShow,
  113. renderer: seriesToShow.length > 20 ? ('canvas' as const) : ('svg' as const),
  114. isGroupedByDate: true,
  115. height: 300,
  116. colors: seriesToShow.map(s => s.color),
  117. grid: {top: 20, bottom: 20, left: 15, right: 25},
  118. tooltip: {
  119. formatter: (params, asyncTicket) => {
  120. const hoveredEchartElement = Array.from(
  121. document.querySelectorAll(':hover')
  122. ).find(element => {
  123. return element.classList.contains('echarts-for-react');
  124. });
  125. if (hoveredEchartElement === chartRef?.current?.ele) {
  126. return getFormatter(formatters)(params, asyncTicket);
  127. }
  128. return '';
  129. },
  130. },
  131. yAxis: {
  132. axisLabel: {
  133. formatter: (value: number) => {
  134. return formatMetricsUsingUnitAndOp(value, unit, operation);
  135. },
  136. },
  137. },
  138. xAxis: {
  139. axisPointer: {
  140. snap: true,
  141. },
  142. },
  143. };
  144. }, [
  145. bucketSize,
  146. focusAreaBrush.options,
  147. forwardedRef,
  148. isSubMinuteBucket,
  149. operation,
  150. seriesToShow,
  151. unit,
  152. ]);
  153. return (
  154. <ChartWrapper {...hoverProps} onMouseDownCapture={focusAreaBrush.startBrush}>
  155. {focusAreaBrush.overlay}
  156. {displayType === MetricDisplayType.LINE ? (
  157. <LineChart {...chartProps} />
  158. ) : displayType === MetricDisplayType.AREA ? (
  159. <AreaChart {...chartProps} />
  160. ) : (
  161. <BarChart stacked animation={false} {...chartProps} />
  162. )}
  163. {displayFogOfWar && (
  164. <FogOfWar bucketSize={bucketSize} seriesLength={seriesLength} />
  165. )}
  166. </ChartWrapper>
  167. );
  168. }
  169. );
  170. function FogOfWar({
  171. bucketSize,
  172. seriesLength,
  173. }: {
  174. bucketSize?: number;
  175. seriesLength?: number;
  176. }) {
  177. if (!bucketSize || !seriesLength) {
  178. return null;
  179. }
  180. const widthFactor = getWidthFactor(bucketSize);
  181. const fogOfWarWidth = widthFactor * bucketSize + 30_000;
  182. const seriesWidth = bucketSize * seriesLength;
  183. // If either of these are undefiend, NaN or 0 the result will be invalid
  184. if (!fogOfWarWidth || !seriesWidth) {
  185. return null;
  186. }
  187. const width = (fogOfWarWidth / seriesWidth) * 100;
  188. return <FogOfWarOverlay width={width ?? 0} />;
  189. }
  190. function getWidthFactor(bucketSize: number) {
  191. // In general, fog of war should cover the last bucket
  192. if (bucketSize > 30 * 60_000) {
  193. return 1;
  194. }
  195. // for 10s timeframe we want to show a fog of war that spans last 10 buckets
  196. // because on average, we are missing last 90 seconds of data
  197. if (bucketSize <= 10_000) {
  198. return 10;
  199. }
  200. // For smaller time frames we want to show a wider fog of war
  201. return 2;
  202. }
  203. const ChartWrapper = styled('div')`
  204. position: relative;
  205. height: 300px;
  206. `;
  207. const FogOfWarOverlay = styled('div')<{width?: number}>`
  208. height: 244px;
  209. width: ${p => p.width}%;
  210. position: absolute;
  211. right: 21px;
  212. top: 18px;
  213. pointer-events: none;
  214. background: linear-gradient(
  215. 90deg,
  216. ${p => p.theme.background}00 0%,
  217. ${p => p.theme.background}FF 70%,
  218. ${p => p.theme.background}FF 100%
  219. );
  220. `;