chart.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. },
  90. yAxis: {
  91. axisLabel: {
  92. formatter: (value: number) => {
  93. return formatMetricsUsingUnitAndOp(value, unit, operation);
  94. },
  95. },
  96. },
  97. };
  98. return (
  99. <ChartWrapper>
  100. <ChartZoom
  101. router={router}
  102. period={period}
  103. start={start}
  104. end={end}
  105. utc={utc}
  106. onZoom={zoomPeriod => {
  107. onZoom?.(zoomPeriod.start, zoomPeriod.end);
  108. }}
  109. >
  110. {zoomRenderProps => (
  111. <ReleaseSeries
  112. utc={utc}
  113. period={period}
  114. start={zoomRenderProps.start!}
  115. end={zoomRenderProps.end!}
  116. projects={projects}
  117. environments={environments}
  118. preserveQueryParams
  119. >
  120. {({releaseSeries}) => {
  121. const releaseSeriesData = releaseSeries?.[0]?.markLine?.data ?? [];
  122. const selected =
  123. releaseSeriesData?.length >= RELEASE_LINES_THRESHOLD
  124. ? {[t('Releases')]: false}
  125. : {};
  126. const legend = releaseSeriesData?.length
  127. ? Legend({
  128. itemGap: 20,
  129. top: 0,
  130. right: 20,
  131. data: releaseSeries.map(s => s.seriesName),
  132. theme: theme as Theme,
  133. selected,
  134. })
  135. : undefined;
  136. const allProps = {
  137. ...chartProps,
  138. ...zoomRenderProps,
  139. series: [...seriesToShow, ...releaseSeries],
  140. legend,
  141. };
  142. return displayType === MetricDisplayType.LINE ? (
  143. <LineChart {...allProps} />
  144. ) : displayType === MetricDisplayType.AREA ? (
  145. <AreaChart {...allProps} />
  146. ) : (
  147. <BarChart stacked animation={false} {...allProps} />
  148. );
  149. }}
  150. </ReleaseSeries>
  151. )}
  152. </ChartZoom>
  153. {displayFogOfWar && (
  154. <FogOfWar bucketSize={bucketSize} seriesLength={seriesLength} />
  155. )}
  156. </ChartWrapper>
  157. );
  158. }
  159. function FogOfWar({
  160. bucketSize,
  161. seriesLength,
  162. }: {
  163. bucketSize?: number;
  164. seriesLength?: number;
  165. }) {
  166. if (!bucketSize || !seriesLength) {
  167. return null;
  168. }
  169. // For smaller time frames we want to show a wider fog of war
  170. const widthFactor = bucketSize > 30 * 60_000 ? 1 : 2;
  171. const fogOfWarWidth = widthFactor * bucketSize + 30_000;
  172. const seriesWidth = bucketSize * seriesLength;
  173. // If either of these are undefiend, NaN or 0 the result will be invalid
  174. if (!fogOfWarWidth || !seriesWidth) {
  175. return null;
  176. }
  177. const width = (fogOfWarWidth / seriesWidth) * 100;
  178. return <FogOfWarOverlay width={width ?? 0} />;
  179. }
  180. const ChartWrapper = styled('div')`
  181. position: relative;
  182. height: 300px;
  183. `;
  184. const FogOfWarOverlay = styled('div')<{width?: number}>`
  185. height: 244px;
  186. width: ${p => p.width}%;
  187. position: absolute;
  188. right: 21px;
  189. top: 18px;
  190. pointer-events: none;
  191. background: linear-gradient(
  192. 90deg,
  193. ${p => p.theme.background}00 0%,
  194. ${p => p.theme.background}FF 70%,
  195. ${p => p.theme.background}FF 100%
  196. );
  197. `;