chart.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import {useEffect, useRef} from 'react';
  2. import {Theme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import {AreaChart} from 'sentry/components/charts/areaChart';
  5. import {BarChart} from 'sentry/components/charts/barChart';
  6. import ChartZoom from 'sentry/components/charts/chartZoom';
  7. import Legend from 'sentry/components/charts/components/legend';
  8. import {LineChart} from 'sentry/components/charts/lineChart';
  9. import ReleaseSeries from 'sentry/components/charts/releaseSeries';
  10. import {RELEASE_LINES_THRESHOLD} from 'sentry/components/charts/utils';
  11. import {t} from 'sentry/locale';
  12. import {DateString, PageFilters} from 'sentry/types';
  13. import {ReactEchartsRef} from 'sentry/types/echarts';
  14. import {
  15. formatMetricsUsingUnitAndOp,
  16. getNameFromMRI,
  17. MetricDisplayType,
  18. } from 'sentry/utils/metrics';
  19. import theme from 'sentry/utils/theme';
  20. import useRouter from 'sentry/utils/useRouter';
  21. import {DDM_CHART_GROUP} from 'sentry/views/ddm/constants';
  22. import {getFormatter} from '../../components/charts/components/tooltip';
  23. import {Series} from './widget';
  24. type ChartProps = {
  25. displayType: MetricDisplayType;
  26. environments: PageFilters['environments'];
  27. projects: PageFilters['projects'];
  28. series: Series[];
  29. end?: string;
  30. onZoom?: (start: DateString, end: DateString) => void;
  31. operation?: string;
  32. period?: string;
  33. start?: string;
  34. utc?: boolean;
  35. };
  36. export function MetricChart({
  37. series,
  38. displayType,
  39. start,
  40. end,
  41. period,
  42. utc,
  43. operation,
  44. projects,
  45. environments,
  46. onZoom,
  47. }: ChartProps) {
  48. const chartRef = useRef<ReactEchartsRef>(null);
  49. const router = useRouter();
  50. // TODO(ddm): Try to do this in a more elegant way
  51. useEffect(() => {
  52. const echartsInstance = chartRef?.current?.getEchartsInstance();
  53. if (echartsInstance && !echartsInstance.group) {
  54. echartsInstance.group = DDM_CHART_GROUP;
  55. }
  56. });
  57. const unit = series[0]?.unit;
  58. const seriesToShow = series.filter(s => !s.hidden);
  59. // TODO(ddm): This assumes that all series have the same bucket size
  60. const bucketSize = seriesToShow[0]?.data[1]?.name - seriesToShow[0]?.data[0]?.name;
  61. const seriesLength = seriesToShow[0]?.data.length;
  62. const formatters = {
  63. valueFormatter: (value: number) =>
  64. formatMetricsUsingUnitAndOp(value, unit, operation),
  65. nameFormatter: mri => getNameFromMRI(mri),
  66. isGroupedByDate: true,
  67. bucketSize,
  68. showTimeInTooltip: true,
  69. };
  70. const displayFogOfWar = operation && ['sum', 'count'].includes(operation);
  71. const chartProps = {
  72. forwardedRef: chartRef,
  73. isGroupedByDate: true,
  74. height: 300,
  75. colors: seriesToShow.map(s => s.color),
  76. grid: {top: 20, bottom: 20, left: 15, right: 25},
  77. tooltip: {
  78. formatter: (params, asyncTicket) => {
  79. const hoveredEchartElement = Array.from(document.querySelectorAll(':hover')).find(
  80. element => {
  81. return element.classList.contains('echarts-for-react');
  82. }
  83. );
  84. if (hoveredEchartElement === chartRef?.current?.ele) {
  85. return getFormatter(formatters)(params, asyncTicket);
  86. }
  87. return '';
  88. },
  89. axisPointer: {
  90. label: {show: true},
  91. },
  92. },
  93. yAxis: {
  94. axisLabel: {
  95. formatter: (value: number) => {
  96. return formatMetricsUsingUnitAndOp(value, unit, operation);
  97. },
  98. },
  99. },
  100. };
  101. return (
  102. <ChartWrapper>
  103. <ChartZoom
  104. router={router}
  105. period={period}
  106. start={start}
  107. end={end}
  108. utc={utc}
  109. onZoom={zoomPeriod => {
  110. onZoom?.(zoomPeriod.start, zoomPeriod.end);
  111. }}
  112. >
  113. {zoomRenderProps => (
  114. <ReleaseSeries
  115. utc={utc}
  116. period={period}
  117. start={zoomRenderProps.start!}
  118. end={zoomRenderProps.end!}
  119. projects={projects}
  120. environments={environments}
  121. preserveQueryParams
  122. >
  123. {({releaseSeries}) => {
  124. const releaseSeriesData = releaseSeries?.[0]?.markLine?.data ?? [];
  125. const selected =
  126. releaseSeriesData?.length >= RELEASE_LINES_THRESHOLD
  127. ? {[t('Releases')]: false}
  128. : {};
  129. const legend = releaseSeriesData?.length
  130. ? Legend({
  131. itemGap: 20,
  132. top: 0,
  133. right: 20,
  134. data: releaseSeries.map(s => s.seriesName),
  135. theme: theme as Theme,
  136. selected,
  137. })
  138. : undefined;
  139. const allProps = {
  140. ...chartProps,
  141. ...zoomRenderProps,
  142. series: [...seriesToShow, ...releaseSeries],
  143. legend,
  144. };
  145. return displayType === MetricDisplayType.LINE ? (
  146. <LineChart {...allProps} />
  147. ) : displayType === MetricDisplayType.AREA ? (
  148. <AreaChart {...allProps} />
  149. ) : (
  150. <BarChart stacked animation={false} {...allProps} />
  151. );
  152. }}
  153. </ReleaseSeries>
  154. )}
  155. </ChartZoom>
  156. {displayFogOfWar && (
  157. <FogOfWar bucketSize={bucketSize} seriesLength={seriesLength} />
  158. )}
  159. </ChartWrapper>
  160. );
  161. }
  162. function FogOfWar({
  163. bucketSize,
  164. seriesLength,
  165. }: {
  166. bucketSize?: number;
  167. seriesLength?: number;
  168. }) {
  169. if (!bucketSize || !seriesLength) {
  170. return null;
  171. }
  172. // For smaller time frames we want to show a wider fog of war
  173. const widthFactor = bucketSize > 30 * 60_000 ? 1 : 2;
  174. const fogOfWarWidth = widthFactor * bucketSize + 30_000;
  175. const seriesWidth = bucketSize * seriesLength;
  176. // If either of these are undefiend, NaN or 0 the result will be invalid
  177. if (!fogOfWarWidth || !seriesWidth) {
  178. return null;
  179. }
  180. const width = (fogOfWarWidth / seriesWidth) * 100;
  181. return <FogOfWarOverlay width={width ?? 0} />;
  182. }
  183. const ChartWrapper = styled('div')`
  184. position: relative;
  185. height: 300px;
  186. `;
  187. const FogOfWarOverlay = styled('div')<{width?: number}>`
  188. height: 244px;
  189. width: ${p => p.width}%;
  190. position: absolute;
  191. right: 21px;
  192. top: 18px;
  193. pointer-events: none;
  194. background: linear-gradient(
  195. 90deg,
  196. ${p => p.theme.background}00 0%,
  197. ${p => p.theme.background}FF 70%,
  198. ${p => p.theme.background}FF 100%
  199. );
  200. `;