chart.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 useRouter from 'sentry/utils/useRouter';
  23. import generateTrendFunctionAsString from 'sentry/views/performance/trends/utils/generateTrendFunctionAsString';
  24. import transformEventStats from 'sentry/views/performance/trends/utils/transformEventStats';
  25. import {getIntervalLine} from 'sentry/views/performance/utils/getIntervalLine';
  26. import type {ViewProps} from '../types';
  27. import type {
  28. NormalizedTrendsTransaction,
  29. TrendChangeType,
  30. TrendFunctionField,
  31. TrendsStats,
  32. } from './types';
  33. import {
  34. getCurrentTrendFunction,
  35. getCurrentTrendParameter,
  36. getUnselectedSeries,
  37. transformEventStatsSmoothed,
  38. trendToColor,
  39. } from './utils';
  40. type Props = ViewProps & {
  41. isLoading: boolean;
  42. organization: OrganizationSummary;
  43. projects: Project[];
  44. statsData: TrendsStats;
  45. trendChangeType: TrendChangeType;
  46. additionalSeries?: LineSeriesOption[];
  47. applyRegressionFormatToInterval?: boolean;
  48. disableLegend?: boolean;
  49. disableXAxis?: boolean;
  50. grid?: LineChartProps['grid'];
  51. height?: number;
  52. neutralColor?: boolean;
  53. transaction?: NormalizedTrendsTransaction;
  54. trendFunctionField?: TrendFunctionField;
  55. };
  56. function getLegend(trendFunction: string): LegendComponentOption {
  57. return {
  58. right: 10,
  59. top: 0,
  60. itemGap: 12,
  61. align: 'left',
  62. data: [
  63. {
  64. name: 'Baseline',
  65. 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',
  66. },
  67. {
  68. name: 'Releases',
  69. },
  70. {
  71. name: trendFunction,
  72. },
  73. ],
  74. };
  75. }
  76. export function Chart({
  77. trendChangeType,
  78. statsPeriod,
  79. transaction,
  80. statsData,
  81. isLoading,
  82. start: propsStart,
  83. end: propsEnd,
  84. trendFunctionField,
  85. disableXAxis,
  86. disableLegend,
  87. neutralColor,
  88. grid,
  89. height,
  90. projects,
  91. project,
  92. organization,
  93. additionalSeries,
  94. applyRegressionFormatToInterval = false,
  95. }: Props) {
  96. const location = useLocation();
  97. const router = useRouter();
  98. const theme = useTheme();
  99. const handleLegendSelectChanged = legendChange => {
  100. const {selected} = legendChange;
  101. const unselected = Object.keys(selected).filter(key => !selected[key]);
  102. const query = {
  103. ...location.query,
  104. };
  105. const queryKey = getUnselectedSeries(trendChangeType);
  106. query[queryKey] = unselected;
  107. const to = {
  108. ...location,
  109. query,
  110. };
  111. browserHistory.push(to);
  112. };
  113. const derivedTrendChangeType = organization.features.includes('performance-new-trends')
  114. ? transaction?.change
  115. : trendChangeType;
  116. const lineColor =
  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. selection[metric] = false;
  141. return selection;
  142. }, {});
  143. const legend: LegendComponentOption = disableLegend
  144. ? {show: false}
  145. : {
  146. ...getLegend(chartLabel),
  147. selected: seriesSelection,
  148. };
  149. const loading = isLoading;
  150. const reloading = isLoading;
  151. const yMax = Math.max(
  152. maxValue,
  153. transaction?.aggregate_range_2 || 0,
  154. transaction?.aggregate_range_1 || 0
  155. );
  156. const yMin = Math.min(
  157. minValue,
  158. transaction?.aggregate_range_1 || Number.MAX_SAFE_INTEGER,
  159. transaction?.aggregate_range_2 || Number.MAX_SAFE_INTEGER
  160. );
  161. const smoothedSeries = smoothedResults
  162. ? smoothedResults.map(values => {
  163. return {
  164. ...values,
  165. color: lineColor.default,
  166. lineStyle: {
  167. opacity: 1,
  168. },
  169. };
  170. })
  171. : [];
  172. const needsLabel = true;
  173. const intervalSeries = getIntervalLine(
  174. theme,
  175. smoothedResults || [],
  176. 0.5,
  177. needsLabel,
  178. transaction,
  179. applyRegressionFormatToInterval
  180. );
  181. const yDiff = yMax - yMin;
  182. const yMargin = yDiff * 0.1;
  183. const series = [...smoothedSeries, ...intervalSeries];
  184. const durationUnit = getDurationUnit(series);
  185. const chartOptions: Omit<LineChartProps, 'series'> = {
  186. tooltip: {
  187. valueFormatter: (value, seriesName) => {
  188. return tooltipFormatter(value, aggregateOutputType(seriesName));
  189. },
  190. },
  191. yAxis: {
  192. min: Math.max(0, yMin - yMargin),
  193. max: yMax + yMargin,
  194. minInterval: durationUnit,
  195. axisLabel: {
  196. color: theme.chartLabel,
  197. formatter: (value: number) =>
  198. axisLabelFormatter(value, 'duration', undefined, durationUnit),
  199. },
  200. },
  201. };
  202. return (
  203. <ChartZoom
  204. router={router}
  205. period={statsPeriod}
  206. start={start}
  207. end={end}
  208. utc={utc === 'true'}
  209. >
  210. {zoomRenderProps => {
  211. return (
  212. <TransitionChart loading={loading} reloading={reloading}>
  213. <TransparentLoadingMask visible={reloading} />
  214. {getDynamicText({
  215. value: (
  216. <LineChart
  217. height={height}
  218. {...zoomRenderProps}
  219. {...chartOptions}
  220. additionalSeries={additionalSeries}
  221. onLegendSelectChanged={handleLegendSelectChanged}
  222. series={series}
  223. seriesOptions={{
  224. showSymbol: false,
  225. }}
  226. legend={legend}
  227. toolBox={{
  228. show: false,
  229. }}
  230. grid={
  231. grid ?? {
  232. left: '10px',
  233. right: '10px',
  234. top: '40px',
  235. bottom: '0px',
  236. }
  237. }
  238. xAxis={disableXAxis ? {show: false} : undefined}
  239. />
  240. ),
  241. fixed: 'Duration Chart',
  242. })}
  243. </TransitionChart>
  244. );
  245. }}
  246. </ChartZoom>
  247. );
  248. }
  249. export default Chart;