chart.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import {useTheme} from '@emotion/react';
  2. import max from 'lodash/max';
  3. import min from 'lodash/min';
  4. import type {AreaChartProps} from 'sentry/components/charts/areaChart';
  5. import {AreaChart} from 'sentry/components/charts/areaChart';
  6. import ChartZoom from 'sentry/components/charts/chartZoom';
  7. import {LineChart} from 'sentry/components/charts/lineChart';
  8. import type {DateString} from 'sentry/types/core';
  9. import type {Series} from 'sentry/types/echarts';
  10. import {
  11. axisLabelFormatter,
  12. getDurationUnit,
  13. tooltipFormatter,
  14. } from 'sentry/utils/discover/charts';
  15. import {aggregateOutputType} from 'sentry/utils/discover/fields';
  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. export 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 theme = useTheme();
  72. if (!data || data.length <= 0) {
  73. return null;
  74. }
  75. const colors = chartColors ?? theme.charts.getColorPalette(4);
  76. const durationOnly = data.every(
  77. value => aggregateOutputType(value.seriesName) === 'duration'
  78. );
  79. const dataMax = durationOnly ? computeAxisMax(data) : undefined;
  80. const xAxes = disableMultiAxis
  81. ? undefined
  82. : [
  83. {
  84. gridIndex: 0,
  85. type: 'time' as const,
  86. },
  87. {
  88. gridIndex: 1,
  89. type: 'time' as const,
  90. },
  91. ];
  92. const durationUnit = getDurationUnit(data);
  93. const yAxes = disableMultiAxis
  94. ? [
  95. {
  96. minInterval: durationUnit,
  97. splitNumber: definedAxisTicks,
  98. axisLabel: {
  99. color: theme.chartLabel,
  100. formatter(value: number) {
  101. return axisLabelFormatter(
  102. value,
  103. aggregateOutputType(data[0].seriesName),
  104. undefined,
  105. durationUnit
  106. );
  107. },
  108. },
  109. },
  110. ]
  111. : [
  112. {
  113. gridIndex: 0,
  114. scale: true,
  115. minInterval: durationUnit,
  116. max: dataMax,
  117. axisLabel: {
  118. color: theme.chartLabel,
  119. formatter(value: number) {
  120. return axisLabelFormatter(
  121. value,
  122. aggregateOutputType(data[0].seriesName),
  123. undefined,
  124. durationUnit
  125. );
  126. },
  127. },
  128. },
  129. {
  130. gridIndex: 1,
  131. scale: true,
  132. max: dataMax,
  133. minInterval: durationUnit,
  134. axisLabel: {
  135. color: theme.chartLabel,
  136. formatter(value: number) {
  137. return axisLabelFormatter(
  138. value,
  139. aggregateOutputType(data[1].seriesName),
  140. undefined,
  141. durationUnit
  142. );
  143. },
  144. },
  145. },
  146. ];
  147. const axisPointer = disableMultiAxis
  148. ? undefined
  149. : {
  150. // Link the two series x-axis together.
  151. link: [{xAxisIndex: [0, 1]}],
  152. };
  153. const areaChartProps = {
  154. seriesOptions: {
  155. showSymbol: false,
  156. },
  157. grid: disableMultiAxis
  158. ? grid
  159. : [
  160. {
  161. top: '8px',
  162. left: '24px',
  163. right: '52%',
  164. bottom: '16px',
  165. },
  166. {
  167. top: '8px',
  168. left: '52%',
  169. right: '24px',
  170. bottom: '16px',
  171. },
  172. ],
  173. axisPointer,
  174. xAxes,
  175. yAxes,
  176. utc,
  177. isGroupedByDate: true,
  178. showTimeInTooltip: true,
  179. colors: [colors[0], colors[1]] as string[],
  180. tooltip: {
  181. valueFormatter: (value, seriesName) => {
  182. return tooltipFormatter(
  183. value,
  184. aggregateOutputType(data?.length ? data[0].seriesName : seriesName)
  185. );
  186. },
  187. nameFormatter(value: string) {
  188. return value === 'epm()' ? 'tpm()' : value;
  189. },
  190. },
  191. };
  192. if (loading) {
  193. if (isLineChart) {
  194. return <LineChart height={height} series={[]} {...areaChartProps} />;
  195. }
  196. return <AreaChart height={height} series={[]} {...areaChartProps} />;
  197. }
  198. const series = data.map((values, i: number) => ({
  199. ...values,
  200. yAxisIndex: i,
  201. xAxisIndex: i,
  202. }));
  203. const xAxis = disableXAxis
  204. ? {
  205. show: false,
  206. axisLabel: {show: true, margin: 0},
  207. axisLine: {show: false},
  208. }
  209. : undefined;
  210. return (
  211. <ChartZoom
  212. period={statsPeriod}
  213. start={start}
  214. end={end}
  215. utc={utc}
  216. xAxisIndex={disableMultiAxis ? undefined : [0, 1]}
  217. >
  218. {zoomRenderProps => {
  219. if (isLineChart) {
  220. return (
  221. <LineChart
  222. height={height}
  223. {...zoomRenderProps}
  224. series={series}
  225. previousPeriod={previousData}
  226. xAxis={xAxis}
  227. yAxis={areaChartProps.yAxes[0]}
  228. tooltip={areaChartProps.tooltip}
  229. />
  230. );
  231. }
  232. return (
  233. <AreaChart
  234. height={height}
  235. {...zoomRenderProps}
  236. series={series}
  237. previousPeriod={previousData}
  238. xAxis={xAxis}
  239. {...areaChartProps}
  240. />
  241. );
  242. }}
  243. </ChartZoom>
  244. );
  245. }
  246. export default Chart;