chart.tsx 6.2 KB

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