chart.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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, Project} from 'sentry/types';
  10. import {browserHistory} from 'sentry/utils/browserHistory';
  11. import {getUtcToLocalDateObject} from 'sentry/utils/dates';
  12. import {
  13. axisLabelFormatter,
  14. getDurationUnit,
  15. tooltipFormatter,
  16. } from 'sentry/utils/discover/charts';
  17. import {aggregateOutputType} from 'sentry/utils/discover/fields';
  18. import getDynamicText from 'sentry/utils/getDynamicText';
  19. import {decodeList} from 'sentry/utils/queryString';
  20. import {useLocation} from 'sentry/utils/useLocation';
  21. import useRouter from 'sentry/utils/useRouter';
  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. transformEventStatsSmoothed,
  37. trendToColor,
  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 router = useRouter();
  97. const theme = useTheme();
  98. const handleLegendSelectChanged = legendChange => {
  99. const {selected} = legendChange;
  100. const unselected = Object.keys(selected).filter(key => !selected[key]);
  101. const query = {
  102. ...location.query,
  103. };
  104. const queryKey = getUnselectedSeries(trendChangeType);
  105. query[queryKey] = unselected;
  106. const to = {
  107. ...location,
  108. query,
  109. };
  110. browserHistory.push(to);
  111. };
  112. const derivedTrendChangeType = organization.features.includes('performance-new-trends')
  113. ? transaction?.change
  114. : trendChangeType;
  115. const lineColor =
  116. trendToColor[neutralColor ? 'neutral' : derivedTrendChangeType || trendChangeType];
  117. const events =
  118. statsData && transaction?.project && transaction?.transaction
  119. ? statsData[[transaction.project, transaction.transaction].join(',')]
  120. : undefined;
  121. const data = events?.data ?? [];
  122. const trendFunction = getCurrentTrendFunction(location, trendFunctionField);
  123. const trendParameter = getCurrentTrendParameter(location, projects, project);
  124. const chartLabel = generateTrendFunctionAsString(
  125. trendFunction.field,
  126. trendParameter.column
  127. );
  128. const results = transformEventStats(data, chartLabel);
  129. const {smoothedResults, minValue, maxValue} = transformEventStatsSmoothed(
  130. results,
  131. chartLabel
  132. );
  133. const start = propsStart ? getUtcToLocalDateObject(propsStart) : null;
  134. const end = propsEnd ? getUtcToLocalDateObject(propsEnd) : null;
  135. const {utc} = normalizeDateTimeParams(location.query);
  136. const seriesSelection = decodeList(
  137. location.query[getUnselectedSeries(trendChangeType)]
  138. ).reduce((selection, metric) => {
  139. selection[metric] = false;
  140. return selection;
  141. }, {});
  142. const legend: LegendComponentOption = disableLegend
  143. ? {show: false}
  144. : {
  145. ...getLegend(chartLabel),
  146. selected: seriesSelection,
  147. };
  148. const loading = isLoading;
  149. const reloading = isLoading;
  150. const yMax = Math.max(
  151. maxValue,
  152. transaction?.aggregate_range_2 || 0,
  153. transaction?.aggregate_range_1 || 0
  154. );
  155. const yMin = Math.min(
  156. minValue,
  157. transaction?.aggregate_range_1 || Number.MAX_SAFE_INTEGER,
  158. transaction?.aggregate_range_2 || Number.MAX_SAFE_INTEGER
  159. );
  160. const smoothedSeries = smoothedResults
  161. ? smoothedResults.map(values => {
  162. return {
  163. ...values,
  164. color: lineColor.default,
  165. lineStyle: {
  166. opacity: 1,
  167. },
  168. };
  169. })
  170. : [];
  171. const needsLabel = true;
  172. const intervalSeries = getIntervalLine(
  173. theme,
  174. smoothedResults || [],
  175. 0.5,
  176. needsLabel,
  177. transaction,
  178. applyRegressionFormatToInterval
  179. );
  180. const yDiff = yMax - yMin;
  181. const yMargin = yDiff * 0.1;
  182. const series = [...smoothedSeries, ...intervalSeries];
  183. const durationUnit = getDurationUnit(series);
  184. const chartOptions: Omit<LineChartProps, 'series'> = {
  185. tooltip: {
  186. valueFormatter: (value, seriesName) => {
  187. return tooltipFormatter(value, aggregateOutputType(seriesName));
  188. },
  189. },
  190. yAxis: {
  191. min: Math.max(0, yMin - yMargin),
  192. max: yMax + yMargin,
  193. minInterval: durationUnit,
  194. axisLabel: {
  195. color: theme.chartLabel,
  196. formatter: (value: number) =>
  197. axisLabelFormatter(value, 'duration', undefined, durationUnit),
  198. },
  199. },
  200. };
  201. return (
  202. <ChartZoom
  203. router={router}
  204. period={statsPeriod}
  205. start={start}
  206. end={end}
  207. utc={utc === 'true'}
  208. >
  209. {zoomRenderProps => {
  210. return (
  211. <TransitionChart loading={loading} reloading={reloading}>
  212. <TransparentLoadingMask visible={reloading} />
  213. {getDynamicText({
  214. value: (
  215. <LineChart
  216. height={height}
  217. {...zoomRenderProps}
  218. {...chartOptions}
  219. additionalSeries={additionalSeries}
  220. onLegendSelectChanged={handleLegendSelectChanged}
  221. series={series}
  222. seriesOptions={{
  223. showSymbol: false,
  224. }}
  225. legend={legend}
  226. toolBox={{
  227. show: false,
  228. }}
  229. grid={
  230. grid ?? {
  231. left: '10px',
  232. right: '10px',
  233. top: '40px',
  234. bottom: '0px',
  235. }
  236. }
  237. xAxis={disableXAxis ? {show: false} : undefined}
  238. />
  239. ),
  240. fixed: 'Duration Chart',
  241. })}
  242. </TransitionChart>
  243. );
  244. }}
  245. </ChartZoom>
  246. );
  247. }
  248. export default Chart;