chart.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import {useTheme} from '@emotion/react';
  2. import max from 'lodash/max';
  3. import min from 'lodash/min';
  4. import {AreaChart, AreaChartProps} from 'sentry/components/charts/areaChart';
  5. import ChartZoom from 'sentry/components/charts/chartZoom';
  6. import {LineChart} from 'sentry/components/charts/lineChart';
  7. import {DateString} from 'sentry/types';
  8. import {Series} from 'sentry/types/echarts';
  9. import {
  10. axisLabelFormatter,
  11. getDurationUnit,
  12. tooltipFormatter,
  13. } from 'sentry/utils/discover/charts';
  14. import {aggregateOutputType} from 'sentry/utils/discover/fields';
  15. import useRouter from 'sentry/utils/useRouter';
  16. type Props = {
  17. data: Series[];
  18. end: DateString;
  19. loading: boolean;
  20. start: DateString;
  21. statsPeriod: string | null | undefined;
  22. utc: boolean;
  23. chartColors?: string[];
  24. definedAxisTicks?: number;
  25. disableMultiAxis?: boolean;
  26. disableXAxis?: boolean;
  27. grid?: AreaChartProps['grid'];
  28. height?: number;
  29. hideYAxisSplitLine?: boolean;
  30. isLineChart?: boolean;
  31. log?: boolean;
  32. previousData?: Series[];
  33. stacked?: boolean;
  34. };
  35. function computeMax(data: Series[]) {
  36. const valuesDict = data.map(value => value.data.map(point => point.value));
  37. return max(valuesDict.map(max)) as number;
  38. }
  39. // adapted from https://stackoverflow.com/questions/11397239/rounding-up-for-a-graph-maximum
  40. function computeAxisMax(data: Series[]) {
  41. // assumes min is 0
  42. let maxValue = 0;
  43. if (data.length > 2) {
  44. for (let i = 0; i < data.length; i++) {
  45. maxValue += max(data[i].data.map(point => point.value)) as number;
  46. }
  47. } else {
  48. maxValue = computeMax(data);
  49. }
  50. if (maxValue <= 1) {
  51. return 1;
  52. }
  53. const power = Math.log10(maxValue);
  54. const magnitude = min([max([10 ** (power - Math.floor(power)), 0]), 10]) as number;
  55. let scale: number;
  56. if (magnitude <= 2.5) {
  57. scale = 0.2;
  58. } else if (magnitude <= 5) {
  59. scale = 0.5;
  60. } else if (magnitude <= 7.5) {
  61. scale = 1.0;
  62. } else {
  63. scale = 2.0;
  64. }
  65. const step = 10 ** Math.floor(power) * scale;
  66. return Math.round(Math.ceil(maxValue / step) * step);
  67. }
  68. function Chart({
  69. data,
  70. previousData,
  71. statsPeriod,
  72. start,
  73. end,
  74. utc,
  75. loading,
  76. height,
  77. grid,
  78. disableMultiAxis,
  79. disableXAxis,
  80. definedAxisTicks,
  81. chartColors,
  82. isLineChart,
  83. stacked,
  84. log,
  85. hideYAxisSplitLine,
  86. }: Props) {
  87. const router = useRouter();
  88. const theme = useTheme();
  89. if (!data || data.length <= 0) {
  90. return null;
  91. }
  92. const colors = chartColors ?? theme.charts.getColorPalette(4);
  93. const durationOnly = data.every(
  94. value => aggregateOutputType(value.seriesName) === 'duration'
  95. );
  96. const percentOnly = data.every(
  97. value => aggregateOutputType(value.seriesName) === 'percentage'
  98. );
  99. const dataMax = durationOnly
  100. ? computeAxisMax(data)
  101. : percentOnly
  102. ? computeMax(data)
  103. : undefined;
  104. const xAxes = disableMultiAxis
  105. ? undefined
  106. : [
  107. {
  108. gridIndex: 0,
  109. type: 'time' as const,
  110. },
  111. {
  112. gridIndex: 1,
  113. type: 'time' as const,
  114. },
  115. ];
  116. const durationUnit = getDurationUnit(data);
  117. const yAxes = disableMultiAxis
  118. ? [
  119. {
  120. minInterval: durationUnit,
  121. splitNumber: definedAxisTicks,
  122. max: dataMax,
  123. type: log ? 'log' : 'value',
  124. axisLabel: {
  125. color: theme.chartLabel,
  126. formatter(value: number) {
  127. return axisLabelFormatter(
  128. value,
  129. aggregateOutputType(data[0].seriesName),
  130. undefined,
  131. durationUnit
  132. );
  133. },
  134. },
  135. splitLine: hideYAxisSplitLine ? {show: false} : undefined,
  136. },
  137. ]
  138. : [
  139. {
  140. gridIndex: 0,
  141. scale: true,
  142. minInterval: durationUnit,
  143. max: dataMax,
  144. axisLabel: {
  145. color: theme.chartLabel,
  146. formatter(value: number) {
  147. return axisLabelFormatter(
  148. value,
  149. aggregateOutputType(data[0].seriesName),
  150. undefined,
  151. durationUnit
  152. );
  153. },
  154. },
  155. splitLine: hideYAxisSplitLine ? {show: false} : undefined,
  156. },
  157. {
  158. gridIndex: 1,
  159. scale: true,
  160. max: dataMax,
  161. minInterval: durationUnit,
  162. axisLabel: {
  163. color: theme.chartLabel,
  164. formatter(value: number) {
  165. return axisLabelFormatter(
  166. value,
  167. aggregateOutputType(data[1].seriesName),
  168. undefined,
  169. durationUnit
  170. );
  171. },
  172. },
  173. splitLine: hideYAxisSplitLine ? {show: false} : undefined,
  174. },
  175. ];
  176. const axisPointer = disableMultiAxis
  177. ? undefined
  178. : {
  179. // Link the two series x-axis together.
  180. link: [{xAxisIndex: [0, 1]}],
  181. };
  182. const areaChartProps = {
  183. seriesOptions: {
  184. showSymbol: false,
  185. },
  186. grid: disableMultiAxis
  187. ? grid
  188. : [
  189. {
  190. top: '8px',
  191. left: '24px',
  192. right: '52%',
  193. bottom: '16px',
  194. },
  195. {
  196. top: '8px',
  197. left: '52%',
  198. right: '24px',
  199. bottom: '16px',
  200. },
  201. ],
  202. axisPointer,
  203. xAxes,
  204. yAxes,
  205. utc,
  206. legend: {
  207. top: 0,
  208. right: 10,
  209. },
  210. isGroupedByDate: true,
  211. showTimeInTooltip: true,
  212. colors,
  213. tooltip: {
  214. valueFormatter: (value, seriesName) => {
  215. return tooltipFormatter(
  216. value,
  217. aggregateOutputType(data && data.length ? data[0].seriesName : seriesName)
  218. );
  219. },
  220. nameFormatter(value: string) {
  221. return value === 'epm()' ? 'tpm()' : value;
  222. },
  223. },
  224. } as Omit<AreaChartProps, 'series'>;
  225. if (loading) {
  226. if (isLineChart) {
  227. return <LineChart height={height} series={[]} {...areaChartProps} />;
  228. }
  229. return <AreaChart height={height} series={[]} {...areaChartProps} />;
  230. }
  231. const series = data.map((values, _) => ({
  232. ...values,
  233. yAxisIndex: 0,
  234. xAxisIndex: 0,
  235. }));
  236. const xAxis = disableXAxis
  237. ? {
  238. show: false,
  239. axisLabel: {show: true, margin: 0},
  240. axisLine: {show: false},
  241. }
  242. : undefined;
  243. return (
  244. <ChartZoom
  245. router={router}
  246. period={statsPeriod}
  247. start={start}
  248. end={end}
  249. utc={utc}
  250. xAxisIndex={disableMultiAxis ? undefined : [0, 1]}
  251. >
  252. {zoomRenderProps => {
  253. if (isLineChart) {
  254. return (
  255. <LineChart
  256. height={height}
  257. {...zoomRenderProps}
  258. series={series}
  259. previousPeriod={previousData}
  260. xAxis={xAxis}
  261. yAxis={areaChartProps.yAxes ? areaChartProps.yAxes[0] : []}
  262. tooltip={areaChartProps.tooltip}
  263. colors={colors}
  264. grid={grid}
  265. />
  266. );
  267. }
  268. return (
  269. <AreaChart
  270. height={height}
  271. {...zoomRenderProps}
  272. series={series}
  273. previousPeriod={previousData}
  274. xAxis={xAxis}
  275. stacked={stacked}
  276. {...areaChartProps}
  277. />
  278. );
  279. }}
  280. </ChartZoom>
  281. );
  282. }
  283. export default Chart;