chart.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import {useTheme} from '@emotion/react';
  2. import type {LegendComponentOption, LineSeriesOption} from 'echarts';
  3. import ChartZoom from 'sentry/components/charts/chartZoom';
  4. import type {LineChartProps} from 'sentry/components/charts/lineChart';
  5. import {LineChart} from 'sentry/components/charts/lineChart';
  6. import TransitionChart from 'sentry/components/charts/transitionChart';
  7. import TransparentLoadingMask from 'sentry/components/charts/transparentLoadingMask';
  8. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  9. import type {OrganizationSummary} from 'sentry/types/organization';
  10. import type {Project} from 'sentry/types/project';
  11. import {browserHistory} from 'sentry/utils/browserHistory';
  12. import {getUtcToLocalDateObject} from 'sentry/utils/dates';
  13. import {
  14. axisLabelFormatter,
  15. getDurationUnit,
  16. tooltipFormatter,
  17. } from 'sentry/utils/discover/charts';
  18. import {aggregateOutputType} from 'sentry/utils/discover/fields';
  19. import getDynamicText from 'sentry/utils/getDynamicText';
  20. import {decodeList} from 'sentry/utils/queryString';
  21. import {useLocation} from 'sentry/utils/useLocation';
  22. import generateTrendFunctionAsString from 'sentry/views/performance/trends/utils/generateTrendFunctionAsString';
  23. import transformEventStats from 'sentry/views/performance/trends/utils/transformEventStats';
  24. import {getIntervalLine} from 'sentry/views/performance/utils/getIntervalLine';
  25. import type {ViewProps} from '../types';
  26. import type {
  27. NormalizedTrendsTransaction,
  28. TrendChangeType,
  29. TrendFunctionField,
  30. TrendsStats,
  31. } from './types';
  32. import {
  33. getCurrentTrendFunction,
  34. getCurrentTrendParameter,
  35. getUnselectedSeries,
  36. makeTrendToColorMapping,
  37. transformEventStatsSmoothed,
  38. } from './utils';
  39. type Props = ViewProps & {
  40. isLoading: boolean;
  41. organization: OrganizationSummary;
  42. projects: Project[];
  43. statsData: TrendsStats;
  44. trendChangeType: TrendChangeType;
  45. additionalSeries?: LineSeriesOption[];
  46. applyRegressionFormatToInterval?: boolean;
  47. disableLegend?: boolean;
  48. disableXAxis?: boolean;
  49. grid?: LineChartProps['grid'];
  50. height?: number;
  51. neutralColor?: boolean;
  52. transaction?: NormalizedTrendsTransaction;
  53. trendFunctionField?: TrendFunctionField;
  54. };
  55. function getLegend(trendFunction: string): LegendComponentOption {
  56. return {
  57. right: 10,
  58. top: 0,
  59. itemGap: 12,
  60. align: 'left',
  61. data: [
  62. {
  63. name: 'Baseline',
  64. icon: 'path://M180 1000 l0 -40 200 0 200 0 0 40 0 40 -200 0 -200 0 0 -40z, M810 1000 l0 -40 200 0 200 0 0 40 0 40 -200 0 -200 0 0 -40zm, M1440 1000 l0 -40 200 0 200 0 0 40 0 40 -200 0 -200 0 0 -40z',
  65. },
  66. {
  67. name: 'Releases',
  68. },
  69. {
  70. name: trendFunction,
  71. },
  72. ],
  73. };
  74. }
  75. export function Chart({
  76. trendChangeType,
  77. statsPeriod,
  78. transaction,
  79. statsData,
  80. isLoading,
  81. start: propsStart,
  82. end: propsEnd,
  83. trendFunctionField,
  84. disableXAxis,
  85. disableLegend,
  86. neutralColor,
  87. grid,
  88. height,
  89. projects,
  90. project,
  91. organization,
  92. additionalSeries,
  93. applyRegressionFormatToInterval = false,
  94. }: Props) {
  95. const location = useLocation();
  96. const theme = useTheme();
  97. const handleLegendSelectChanged = (legendChange: any) => {
  98. const {selected} = legendChange;
  99. const unselected = Object.keys(selected).filter(key => !selected[key]);
  100. const query = {
  101. ...location.query,
  102. };
  103. const queryKey = getUnselectedSeries(trendChangeType);
  104. query[queryKey] = unselected;
  105. const to = {
  106. ...location,
  107. query,
  108. };
  109. browserHistory.push(to);
  110. };
  111. const derivedTrendChangeType = organization.features.includes('performance-new-trends')
  112. ? transaction?.change
  113. : trendChangeType;
  114. const trendToColor = makeTrendToColorMapping(theme);
  115. const lineColor =
  116. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  117. trendToColor[neutralColor ? 'neutral' : derivedTrendChangeType || trendChangeType];
  118. const events =
  119. statsData && transaction?.project && transaction?.transaction
  120. ? statsData[[transaction.project, transaction.transaction].join(',')]
  121. : undefined;
  122. const data = events?.data ?? [];
  123. const trendFunction = getCurrentTrendFunction(location, trendFunctionField);
  124. const trendParameter = getCurrentTrendParameter(location, projects, project);
  125. const chartLabel = generateTrendFunctionAsString(
  126. trendFunction.field,
  127. trendParameter.column
  128. );
  129. const results = transformEventStats(data, chartLabel);
  130. const {smoothedResults, minValue, maxValue} = transformEventStatsSmoothed(
  131. results,
  132. chartLabel
  133. );
  134. const start = propsStart ? getUtcToLocalDateObject(propsStart) : null;
  135. const end = propsEnd ? getUtcToLocalDateObject(propsEnd) : null;
  136. const {utc} = normalizeDateTimeParams(location.query);
  137. const seriesSelection = decodeList(
  138. location.query[getUnselectedSeries(trendChangeType)]
  139. ).reduce((selection, metric) => {
  140. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  141. selection[metric] = false;
  142. return selection;
  143. }, {});
  144. const legend: LegendComponentOption = disableLegend
  145. ? {show: false}
  146. : {
  147. ...getLegend(chartLabel),
  148. selected: seriesSelection,
  149. };
  150. const loading = isLoading;
  151. const reloading = isLoading;
  152. const yMax = Math.max(
  153. maxValue,
  154. transaction?.aggregate_range_2 || 0,
  155. transaction?.aggregate_range_1 || 0
  156. );
  157. const yMin = Math.min(
  158. minValue,
  159. transaction?.aggregate_range_1 || Number.MAX_SAFE_INTEGER,
  160. transaction?.aggregate_range_2 || Number.MAX_SAFE_INTEGER
  161. );
  162. const smoothedSeries = smoothedResults
  163. ? smoothedResults.map(values => {
  164. return {
  165. ...values,
  166. color: lineColor.default,
  167. lineStyle: {
  168. opacity: 1,
  169. },
  170. };
  171. })
  172. : [];
  173. const needsLabel = true;
  174. const intervalSeries = getIntervalLine(
  175. theme,
  176. smoothedResults || [],
  177. 0.5,
  178. needsLabel,
  179. transaction,
  180. applyRegressionFormatToInterval
  181. );
  182. const yDiff = yMax - yMin;
  183. const yMargin = yDiff * 0.1;
  184. const series = [...smoothedSeries, ...intervalSeries];
  185. const durationUnit = getDurationUnit(series);
  186. const chartOptions: Omit<LineChartProps, 'series'> = {
  187. tooltip: {
  188. valueFormatter: (value, seriesName) => {
  189. return tooltipFormatter(value, aggregateOutputType(seriesName));
  190. },
  191. },
  192. yAxis: {
  193. min: Math.max(0, yMin - yMargin),
  194. max: yMax + yMargin,
  195. minInterval: durationUnit,
  196. axisLabel: {
  197. color: theme.chartLabel,
  198. formatter: (value: number) =>
  199. axisLabelFormatter(value, 'duration', undefined, durationUnit),
  200. },
  201. },
  202. };
  203. return (
  204. <ChartZoom period={statsPeriod} start={start} end={end} utc={utc === 'true'}>
  205. {zoomRenderProps => {
  206. return (
  207. <TransitionChart loading={loading} reloading={reloading}>
  208. <TransparentLoadingMask visible={reloading} />
  209. {getDynamicText({
  210. value: (
  211. <LineChart
  212. height={height}
  213. {...zoomRenderProps}
  214. {...chartOptions}
  215. additionalSeries={additionalSeries}
  216. onLegendSelectChanged={handleLegendSelectChanged}
  217. series={series}
  218. seriesOptions={{
  219. showSymbol: false,
  220. }}
  221. legend={legend}
  222. toolBox={{
  223. show: false,
  224. }}
  225. grid={
  226. grid ?? {
  227. left: '10px',
  228. right: '10px',
  229. top: '40px',
  230. bottom: '0px',
  231. }
  232. }
  233. xAxis={disableXAxis ? {show: false} : undefined}
  234. />
  235. ),
  236. fixed: 'Duration Chart',
  237. })}
  238. </TransitionChart>
  239. );
  240. }}
  241. </ChartZoom>
  242. );
  243. }
  244. export default Chart;