chart.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. import {Component} from 'react';
  2. import {InjectedRouter} from 'react-router';
  3. import {withTheme} from '@emotion/react';
  4. import styled from '@emotion/styled';
  5. import {DataZoomComponentOption, LegendComponentOption} from 'echarts';
  6. import {Location} from 'history';
  7. import isEqual from 'lodash/isEqual';
  8. import omit from 'lodash/omit';
  9. import {AreaChart} from 'sentry/components/charts/areaChart';
  10. import {BarChart} from 'sentry/components/charts/barChart';
  11. import ChartZoom from 'sentry/components/charts/chartZoom';
  12. import ErrorPanel from 'sentry/components/charts/errorPanel';
  13. import {LineChart} from 'sentry/components/charts/lineChart';
  14. import SimpleTableChart from 'sentry/components/charts/simpleTableChart';
  15. import TransitionChart from 'sentry/components/charts/transitionChart';
  16. import TransparentLoadingMask from 'sentry/components/charts/transparentLoadingMask';
  17. import {getSeriesSelection, processTableResults} from 'sentry/components/charts/utils';
  18. import {WorldMapChart} from 'sentry/components/charts/worldMapChart';
  19. import LoadingIndicator from 'sentry/components/loadingIndicator';
  20. import Placeholder, {PlaceholderProps} from 'sentry/components/placeholder';
  21. import Tooltip from 'sentry/components/tooltip';
  22. import {IconWarning} from 'sentry/icons';
  23. import space from 'sentry/styles/space';
  24. import {Organization, PageFilters} from 'sentry/types';
  25. import {EChartDataZoomHandler, EChartEventHandler} from 'sentry/types/echarts';
  26. import {axisLabelFormatter, tooltipFormatter} from 'sentry/utils/discover/charts';
  27. import {getFieldFormatter} from 'sentry/utils/discover/fieldRenderers';
  28. import {
  29. getAggregateArg,
  30. getEquation,
  31. getMeasurementSlug,
  32. isEquation,
  33. maybeEquationAlias,
  34. stripDerivedMetricsPrefix,
  35. stripEquationPrefix,
  36. } from 'sentry/utils/discover/fields';
  37. import getDynamicText from 'sentry/utils/getDynamicText';
  38. import {Theme} from 'sentry/utils/theme';
  39. import {eventViewFromWidget} from 'sentry/views/dashboardsV2/utils';
  40. import {getDatasetConfig} from '../datasetConfig/base';
  41. import {DisplayType, Widget, WidgetType} from '../types';
  42. import {GenericWidgetQueriesChildrenProps} from './genericWidgetQueries';
  43. const OTHER = 'Other';
  44. export const SLIDER_HEIGHT = 60;
  45. export type AugmentedEChartDataZoomHandler = (
  46. params: Parameters<EChartDataZoomHandler>[0] & {
  47. seriesEnd: string | number;
  48. seriesStart: string | number;
  49. },
  50. instance: Parameters<EChartDataZoomHandler>[1]
  51. ) => void;
  52. type TableResultProps = Pick<
  53. GenericWidgetQueriesChildrenProps,
  54. 'errorMessage' | 'loading' | 'tableResults'
  55. >;
  56. type WidgetCardChartProps = Pick<
  57. GenericWidgetQueriesChildrenProps,
  58. 'timeseriesResults' | 'tableResults' | 'errorMessage' | 'loading'
  59. > & {
  60. location: Location;
  61. organization: Organization;
  62. router: InjectedRouter;
  63. selection: PageFilters;
  64. theme: Theme;
  65. widget: Widget;
  66. chartZoomOptions?: DataZoomComponentOption;
  67. expandNumbers?: boolean;
  68. isMobile?: boolean;
  69. legendOptions?: LegendComponentOption;
  70. noPadding?: boolean;
  71. onLegendSelectChanged?: EChartEventHandler<{
  72. name: string;
  73. selected: Record<string, boolean>;
  74. type: 'legendselectchanged';
  75. }>;
  76. onZoom?: AugmentedEChartDataZoomHandler;
  77. showSlider?: boolean;
  78. windowWidth?: number;
  79. };
  80. type State = {
  81. // For tracking height of the container wrapping BigNumber widgets
  82. // so we can dynamically scale font-size
  83. containerHeight: number;
  84. };
  85. class WidgetCardChart extends Component<WidgetCardChartProps, State> {
  86. state = {containerHeight: 0};
  87. shouldComponentUpdate(nextProps: WidgetCardChartProps, nextState: State): boolean {
  88. if (
  89. this.props.widget.displayType === DisplayType.BIG_NUMBER &&
  90. nextProps.widget.displayType === DisplayType.BIG_NUMBER &&
  91. (this.props.windowWidth !== nextProps.windowWidth ||
  92. !isEqual(this.props.widget?.layout, nextProps.widget?.layout))
  93. ) {
  94. return true;
  95. }
  96. // Widget title changes should not update the WidgetCardChart component tree
  97. const currentProps = {
  98. ...omit(this.props, ['windowWidth']),
  99. widget: {
  100. ...this.props.widget,
  101. title: '',
  102. },
  103. };
  104. nextProps = {
  105. ...omit(nextProps, ['windowWidth']),
  106. widget: {
  107. ...nextProps.widget,
  108. title: '',
  109. },
  110. };
  111. return !isEqual(currentProps, nextProps) || !isEqual(this.state, nextState);
  112. }
  113. tableResultComponent({
  114. loading,
  115. errorMessage,
  116. tableResults,
  117. }: TableResultProps): React.ReactNode {
  118. const {location, widget, organization, selection} = this.props;
  119. if (errorMessage) {
  120. return (
  121. <StyledErrorPanel>
  122. <IconWarning color="gray500" size="lg" />
  123. </StyledErrorPanel>
  124. );
  125. }
  126. if (typeof tableResults === 'undefined') {
  127. // Align height to other charts.
  128. return <LoadingPlaceholder />;
  129. }
  130. const datasetConfig = getDatasetConfig(widget.widgetType);
  131. return tableResults.map((result, i) => {
  132. const fields = widget.queries[i]?.fields?.map(stripDerivedMetricsPrefix) ?? [];
  133. const fieldAliases = widget.queries[i]?.fieldAliases ?? [];
  134. const eventView = eventViewFromWidget(
  135. widget.title,
  136. widget.queries[0],
  137. selection,
  138. widget.displayType
  139. );
  140. return (
  141. <StyledSimpleTableChart
  142. key={`table:${result.title}`}
  143. eventView={eventView}
  144. fieldAliases={fieldAliases}
  145. location={location}
  146. fields={fields}
  147. title={tableResults.length > 1 ? result.title : ''}
  148. loading={loading}
  149. loader={<LoadingPlaceholder />}
  150. metadata={result.meta}
  151. data={result.data}
  152. organization={organization}
  153. stickyHeaders
  154. getCustomFieldRenderer={datasetConfig.getCustomFieldRenderer}
  155. />
  156. );
  157. });
  158. }
  159. bigNumberComponent({
  160. loading,
  161. errorMessage,
  162. tableResults,
  163. }: TableResultProps): React.ReactNode {
  164. if (errorMessage) {
  165. return (
  166. <StyledErrorPanel>
  167. <IconWarning color="gray500" size="lg" />
  168. </StyledErrorPanel>
  169. );
  170. }
  171. if (typeof tableResults === 'undefined' || loading) {
  172. return <BigNumber>{'\u2014'}</BigNumber>;
  173. }
  174. const {containerHeight} = this.state;
  175. const {organization, widget, isMobile, expandNumbers} = this.props;
  176. const isAlias =
  177. !organization.features.includes('discover-frontend-use-events-endpoint') &&
  178. widget.widgetType !== WidgetType.RELEASE;
  179. return tableResults.map(result => {
  180. const tableMeta = {...result.meta};
  181. const fields = Object.keys(tableMeta);
  182. const field = fields[0];
  183. // Change tableMeta for the field from integer to string since we will be rendering with toLocaleString
  184. const shouldExpandInteger = !!expandNumbers && tableMeta[field] === 'integer';
  185. if (shouldExpandInteger) {
  186. tableMeta[field] = 'string';
  187. }
  188. if (!field || !result.data?.length) {
  189. return <BigNumber key={`big_number:${result.title}`}>{'\u2014'}</BigNumber>;
  190. }
  191. const dataRow = result.data[0];
  192. const fieldRenderer = getFieldFormatter(field, tableMeta, isAlias);
  193. const rendered = fieldRenderer(
  194. shouldExpandInteger ? {[field]: dataRow[field].toLocaleString()} : dataRow
  195. );
  196. const isModalWidget = !!!(widget.id || widget.tempId);
  197. if (
  198. !!!organization.features.includes('dashboard-grid-layout') ||
  199. isModalWidget ||
  200. isMobile
  201. ) {
  202. return <BigNumber key={`big_number:${result.title}`}>{rendered}</BigNumber>;
  203. }
  204. // The font size is the container height, minus the top and bottom padding
  205. const fontSize = !!!expandNumbers
  206. ? containerHeight - parseInt(space(1), 10) - parseInt(space(3), 10)
  207. : `max(min(8vw, 90px), ${space(4)})`;
  208. return (
  209. <BigNumber
  210. key={`big_number:${result.title}`}
  211. style={{
  212. fontSize,
  213. ...(!!expandNumbers
  214. ? {padding: `${space(1)} ${space(3)} 0 ${space(3)}`}
  215. : {}),
  216. }}
  217. >
  218. <Tooltip title={rendered} showOnlyOnOverflow>
  219. {rendered}
  220. </Tooltip>
  221. </BigNumber>
  222. );
  223. });
  224. }
  225. chartComponent(chartProps): React.ReactNode {
  226. const {organization, widget} = this.props;
  227. const stacked =
  228. organization.features.includes('new-widget-builder-experience-design') &&
  229. widget.queries[0].columns.length > 0;
  230. switch (widget.displayType) {
  231. case 'bar':
  232. return <BarChart {...chartProps} stacked={stacked} />;
  233. case 'area':
  234. case 'top_n':
  235. return <AreaChart stacked {...chartProps} />;
  236. case 'world_map':
  237. return <WorldMapChart {...chartProps} />;
  238. case 'line':
  239. default:
  240. return <LineChart {...chartProps} />;
  241. }
  242. }
  243. render() {
  244. const {
  245. theme,
  246. tableResults,
  247. timeseriesResults,
  248. errorMessage,
  249. loading,
  250. widget,
  251. organization,
  252. onZoom,
  253. legendOptions,
  254. expandNumbers,
  255. showSlider,
  256. noPadding,
  257. chartZoomOptions,
  258. } = this.props;
  259. if (widget.displayType === 'table') {
  260. return getDynamicText({
  261. value: (
  262. <TransitionChart loading={loading} reloading={loading}>
  263. <LoadingScreen loading={loading} />
  264. {this.tableResultComponent({tableResults, loading, errorMessage})}
  265. </TransitionChart>
  266. ),
  267. fixed: <Placeholder height="200px" testId="skeleton-ui" />,
  268. });
  269. }
  270. if (widget.displayType === 'big_number') {
  271. return (
  272. <TransitionChart loading={loading} reloading={loading}>
  273. <LoadingScreen loading={loading} />
  274. <BigNumberResizeWrapper
  275. ref={el => {
  276. if (el !== null && !!!expandNumbers) {
  277. const {height} = el.getBoundingClientRect();
  278. if (height !== this.state.containerHeight) {
  279. this.setState({containerHeight: height});
  280. }
  281. }
  282. }}
  283. >
  284. {this.bigNumberComponent({tableResults, loading, errorMessage})}
  285. </BigNumberResizeWrapper>
  286. </TransitionChart>
  287. );
  288. }
  289. if (errorMessage) {
  290. return (
  291. <StyledErrorPanel>
  292. <IconWarning color="gray500" size="lg" />
  293. </StyledErrorPanel>
  294. );
  295. }
  296. const {location, router, selection, onLegendSelectChanged} = this.props;
  297. const {start, end, period, utc} = selection.datetime;
  298. // Only allow height resizing for widgets that are on a dashboard
  299. const autoHeightResize = Boolean(
  300. organization.features.includes('dashboard-grid-layout') &&
  301. (widget.id || widget.tempId)
  302. );
  303. if (widget.displayType === 'world_map') {
  304. const {data, title} = processTableResults(tableResults);
  305. const series = [
  306. {
  307. seriesName: title,
  308. data,
  309. },
  310. ];
  311. return (
  312. <TransitionChart loading={loading} reloading={loading}>
  313. <LoadingScreen loading={loading} />
  314. <ChartWrapper autoHeightResize={autoHeightResize}>
  315. {getDynamicText({
  316. value: this.chartComponent({
  317. series,
  318. autoHeightResize,
  319. }),
  320. fixed: <Placeholder height="200px" testId="skeleton-ui" />,
  321. })}
  322. </ChartWrapper>
  323. </TransitionChart>
  324. );
  325. }
  326. const legend = {
  327. left: 0,
  328. top: 0,
  329. selected: getSeriesSelection(location),
  330. formatter: (seriesName: string) => {
  331. const arg = getAggregateArg(seriesName);
  332. if (arg !== null) {
  333. const slug = getMeasurementSlug(arg);
  334. if (slug !== null) {
  335. seriesName = slug.toUpperCase();
  336. }
  337. }
  338. if (maybeEquationAlias(seriesName)) {
  339. seriesName = stripEquationPrefix(seriesName);
  340. }
  341. return seriesName;
  342. },
  343. ...legendOptions,
  344. };
  345. const axisField = widget.queries[0]?.aggregates?.[0] ?? 'count()';
  346. const axisLabel = isEquation(axisField) ? getEquation(axisField) : axisField;
  347. const chartOptions = {
  348. autoHeightResize,
  349. grid: {
  350. left: 0,
  351. right: 4,
  352. top: '40px',
  353. bottom: showSlider ? SLIDER_HEIGHT : 0,
  354. },
  355. seriesOptions: {
  356. showSymbol: false,
  357. },
  358. tooltip: {
  359. trigger: 'axis',
  360. valueFormatter: tooltipFormatter,
  361. },
  362. yAxis: {
  363. axisLabel: {
  364. color: theme.chartLabel,
  365. formatter: (value: number) => axisLabelFormatter(value, axisLabel),
  366. },
  367. },
  368. };
  369. return (
  370. <ChartZoom
  371. router={router}
  372. period={period}
  373. start={start}
  374. end={end}
  375. utc={utc}
  376. showSlider={showSlider}
  377. chartZoomOptions={chartZoomOptions}
  378. >
  379. {zoomRenderProps => {
  380. if (errorMessage) {
  381. return (
  382. <StyledErrorPanel>
  383. <IconWarning color="gray500" size="lg" />
  384. </StyledErrorPanel>
  385. );
  386. }
  387. const otherRegex = new RegExp(`(?:.* : ${OTHER}$)|^${OTHER}$`);
  388. const shouldColorOther = timeseriesResults?.some(
  389. ({seriesName}) => seriesName && seriesName.match(otherRegex)
  390. );
  391. const colors = timeseriesResults
  392. ? theme.charts.getColorPalette(
  393. timeseriesResults.length - (shouldColorOther ? 3 : 2)
  394. )
  395. : [];
  396. // TODO(wmak): Need to change this when updating dashboards to support variable topEvents
  397. if (shouldColorOther) {
  398. colors[colors.length] = theme.chartOther;
  399. }
  400. // Create a list of series based on the order of the fields,
  401. const series = timeseriesResults
  402. ? timeseriesResults.map((values, i: number) => {
  403. let seriesName = '';
  404. if (values.seriesName !== undefined) {
  405. seriesName = isEquation(values.seriesName)
  406. ? getEquation(values.seriesName)
  407. : values.seriesName;
  408. }
  409. return {
  410. ...values,
  411. seriesName,
  412. color: colors[i],
  413. };
  414. })
  415. : [];
  416. const seriesStart = series[0]?.data[0]?.name;
  417. const seriesEnd = series[0]?.data[series[0].data.length - 1]?.name;
  418. return (
  419. <TransitionChart loading={loading} reloading={loading}>
  420. <LoadingScreen loading={loading} />
  421. <ChartWrapper autoHeightResize={autoHeightResize} noPadding={noPadding}>
  422. {getDynamicText({
  423. value: this.chartComponent({
  424. ...zoomRenderProps,
  425. ...chartOptions,
  426. // Override default datazoom behaviour for updating Global Selection Header
  427. ...(onZoom
  428. ? {
  429. onDataZoom: (evt, chartProps) =>
  430. // Need to pass seriesStart and seriesEnd to onZoom since slider zooms
  431. // callback with percentage instead of datetime values. Passing seriesStart
  432. // and seriesEnd allows calculating datetime values with percentage.
  433. onZoom({...evt, seriesStart, seriesEnd}, chartProps),
  434. }
  435. : {}),
  436. legend,
  437. series,
  438. onLegendSelectChanged,
  439. }),
  440. fixed: <Placeholder height="200px" testId="skeleton-ui" />,
  441. })}
  442. </ChartWrapper>
  443. </TransitionChart>
  444. );
  445. }}
  446. </ChartZoom>
  447. );
  448. }
  449. }
  450. export default withTheme(WidgetCardChart);
  451. const StyledTransparentLoadingMask = styled(props => (
  452. <TransparentLoadingMask {...props} maskBackgroundColor="transparent" />
  453. ))`
  454. display: flex;
  455. justify-content: center;
  456. align-items: center;
  457. `;
  458. const LoadingScreen = ({loading}: {loading: boolean}) => {
  459. if (!loading) {
  460. return null;
  461. }
  462. return (
  463. <StyledTransparentLoadingMask visible={loading}>
  464. <LoadingIndicator mini />
  465. </StyledTransparentLoadingMask>
  466. );
  467. };
  468. const LoadingPlaceholder = styled(({className}: PlaceholderProps) => (
  469. <Placeholder height="200px" className={className} />
  470. ))`
  471. background-color: ${p => p.theme.surface200};
  472. `;
  473. const BigNumberResizeWrapper = styled('div')`
  474. height: 100%;
  475. width: 100%;
  476. overflow: hidden;
  477. `;
  478. const BigNumber = styled('div')`
  479. line-height: 1;
  480. display: inline-flex;
  481. flex: 1;
  482. width: 100%;
  483. min-height: 0;
  484. font-size: 32px;
  485. color: ${p => p.theme.headingColor};
  486. padding: ${space(1)} ${space(3)} ${space(3)} ${space(3)};
  487. * {
  488. text-align: left !important;
  489. }
  490. `;
  491. const ChartWrapper = styled('div')<{autoHeightResize: boolean; noPadding?: boolean}>`
  492. ${p => p.autoHeightResize && 'height: 100%;'}
  493. padding: ${p => (!!p.noPadding ? `0` : `0 ${space(3)} ${space(3)}`)};
  494. `;
  495. const StyledSimpleTableChart = styled(SimpleTableChart)`
  496. margin-top: ${space(1.5)};
  497. border-bottom-left-radius: ${p => p.theme.borderRadius};
  498. border-bottom-right-radius: ${p => p.theme.borderRadius};
  499. font-size: ${p => p.theme.fontSizeMedium};
  500. box-shadow: none;
  501. `;
  502. const StyledErrorPanel = styled(ErrorPanel)`
  503. padding: ${space(2)};
  504. `;