baseChart.tsx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. import 'echarts/lib/component/grid';
  2. import 'echarts/lib/component/graphic';
  3. import 'echarts/lib/component/toolbox';
  4. import 'echarts/lib/component/brush';
  5. import 'zrender/lib/svg/svg';
  6. import {forwardRef, useMemo} from 'react';
  7. import type {Theme} from '@emotion/react';
  8. import {css, Global, useTheme} from '@emotion/react';
  9. import styled from '@emotion/styled';
  10. import type {
  11. AxisPointerComponentOption,
  12. ECharts,
  13. EChartsOption,
  14. GridComponentOption,
  15. LegendComponentOption,
  16. LineSeriesOption,
  17. SeriesOption,
  18. TooltipComponentFormatterCallback,
  19. TooltipComponentFormatterCallbackParams,
  20. TooltipComponentOption,
  21. VisualMapComponentOption,
  22. XAXisComponentOption,
  23. YAXisComponentOption,
  24. } from 'echarts';
  25. import {AriaComponent} from 'echarts/components';
  26. import * as echarts from 'echarts/core';
  27. import ReactEchartsCore from 'echarts-for-react/lib/core';
  28. import MarkLine from 'sentry/components/charts/components/markLine';
  29. import {space} from 'sentry/styles/space';
  30. import type {
  31. EChartBrushEndHandler,
  32. EChartBrushSelectedHandler,
  33. EChartBrushStartHandler,
  34. EChartChartReadyHandler,
  35. EChartClickHandler,
  36. EChartDataZoomHandler,
  37. EChartEventHandler,
  38. EChartFinishedHandler,
  39. EChartHighlightHandler,
  40. EChartMouseOutHandler,
  41. EChartMouseOverHandler,
  42. EChartRenderedHandler,
  43. EChartRestoreHandler,
  44. ReactEchartsRef,
  45. Series,
  46. } from 'sentry/types/echarts';
  47. import {defined} from 'sentry/utils';
  48. import Grid from './components/grid';
  49. import Legend from './components/legend';
  50. import type {TooltipSubLabel} from './components/tooltip';
  51. import {CHART_TOOLTIP_VIEWPORT_OFFSET, computeChartTooltip} from './components/tooltip';
  52. import XAxis from './components/xAxis';
  53. import YAxis from './components/yAxis';
  54. import LineSeries from './series/lineSeries';
  55. import {
  56. computeEchartsAriaLabels,
  57. getDiffInMinutes,
  58. getDimensionValue,
  59. lightenHexToRgb,
  60. } from './utils';
  61. // TODO(ts): What is the series type? EChartOption.Series's data cannot have
  62. // `onClick` since it's typically an array.
  63. //
  64. // Handle series item clicks (e.g. Releases mark line or a single series
  65. // item) This is different than when you hover over an "axis" line on a chart
  66. // (e.g. if there are 2 series for an axis and you're not directly hovered
  67. // over an item)
  68. //
  69. // Calls "onClick" inside of series data
  70. const handleClick = (clickSeries: any, instance: ECharts) => {
  71. if (clickSeries.data) {
  72. clickSeries.data.onClick?.(clickSeries, instance);
  73. }
  74. };
  75. echarts.use(AriaComponent);
  76. type ReactEchartProps = React.ComponentProps<typeof ReactEchartsCore>;
  77. type ReactEChartOpts = NonNullable<ReactEchartProps['opts']>;
  78. /**
  79. * Used for some properties that can be truncated
  80. */
  81. type Truncateable = {
  82. /**
  83. * Truncate the label / value some number of characters.
  84. * If true is passed, it will use truncate based on a default length.
  85. */
  86. truncate?: number | boolean;
  87. };
  88. interface TooltipOption
  89. extends Omit<TooltipComponentOption, 'valueFormatter'>,
  90. Truncateable {
  91. filter?: (value: number, seriesParam: TooltipComponentOption['formatter']) => boolean;
  92. formatAxisLabel?: (
  93. value: number,
  94. isTimestamp: boolean,
  95. utc: boolean,
  96. showTimeInTooltip: boolean,
  97. addSecondsToTimeFormat: boolean,
  98. bucketSize: number | undefined,
  99. seriesParamsOrParam: TooltipComponentFormatterCallbackParams
  100. ) => string;
  101. markerFormatter?: (marker: string, label?: string) => string;
  102. nameFormatter?: (
  103. name: string,
  104. seriesParams?: TooltipComponentFormatterCallback<any>
  105. ) => string;
  106. /**
  107. * If true does not display sublabels with a value of 0.
  108. */
  109. skipZeroValuedSubLabels?: boolean;
  110. /**
  111. * Array containing data that is used to display indented sublabels.
  112. */
  113. subLabels?: TooltipSubLabel[];
  114. valueFormatter?: (
  115. value: number,
  116. label?: string,
  117. seriesParams?: TooltipComponentFormatterCallback<any>
  118. ) => string;
  119. }
  120. export interface BaseChartProps {
  121. /**
  122. * Additional Chart Series
  123. * This is to pass series to BaseChart bypassing the wrappers like LineChart, AreaChart etc.
  124. */
  125. additionalSeries?: LineSeriesOption[];
  126. /**
  127. * If true, ignores height value and auto-scales chart to fit container height.
  128. */
  129. autoHeightResize?: boolean;
  130. /**
  131. * Axis pointer options
  132. */
  133. axisPointer?: AxisPointerComponentOption;
  134. /**
  135. * ECharts Brush options
  136. */
  137. brush?: EChartsOption['brush'];
  138. /**
  139. * Bucket size to display time range in chart tooltip
  140. */
  141. bucketSize?: number;
  142. /**
  143. * Array of color codes to use in charts. May also take a function which is
  144. * provided with the current theme
  145. */
  146. colors?: string[] | ((theme: Theme) => string[]);
  147. 'data-test-id'?: string;
  148. /**
  149. * DataZoom (allows for zooming of chart)
  150. */
  151. dataZoom?: EChartsOption['dataZoom'];
  152. devicePixelRatio?: ReactEChartOpts['devicePixelRatio'];
  153. /**
  154. * theme name
  155. * example theme: https://github.com/apache/incubator-echarts/blob/master/theme/dark.js
  156. */
  157. echartsTheme?: ReactEchartProps['theme'];
  158. /**
  159. * optional, used to determine how xAxis is formatted if `isGroupedByDate == true`
  160. */
  161. end?: Date;
  162. /**
  163. * Forwarded Ref
  164. */
  165. forwardedRef?: React.Ref<ReactEchartsCore>;
  166. /**
  167. * Graphic options
  168. */
  169. graphic?: EChartsOption['graphic'];
  170. /**
  171. * ECharts Grid options. multiple grids allow multiple sub-graphs.
  172. */
  173. grid?: GridComponentOption | GridComponentOption[];
  174. /**
  175. * Chart height
  176. */
  177. height?: ReactEChartOpts['height'];
  178. /**
  179. * If data is grouped by date; then apply default date formatting to x-axis
  180. * and tooltips.
  181. */
  182. isGroupedByDate?: boolean;
  183. /**
  184. * states whether not to update chart immediately
  185. */
  186. lazyUpdate?: boolean;
  187. /**
  188. * Chart legend
  189. */
  190. legend?: LegendComponentOption & Truncateable;
  191. /**
  192. * optional, threshold in minutes used to add seconds to the xAxis datetime format if `isGroupedByDate == true`
  193. */
  194. minutesThresholdToDisplaySeconds?: number;
  195. /**
  196. * states whether or not to merge with previous `option`
  197. */
  198. notMerge?: boolean;
  199. onBrushEnd?: EChartBrushEndHandler;
  200. onBrushSelected?: EChartBrushSelectedHandler;
  201. onBrushStart?: EChartBrushStartHandler;
  202. onChartReady?: EChartChartReadyHandler;
  203. onClick?: EChartClickHandler;
  204. onDataZoom?: EChartDataZoomHandler;
  205. onFinished?: EChartFinishedHandler;
  206. onHighlight?: EChartHighlightHandler;
  207. onLegendSelectChanged?: EChartEventHandler<{
  208. name: string;
  209. selected: Record<string, boolean>;
  210. type: 'legendselectchanged';
  211. }>;
  212. onMouseOut?: EChartMouseOutHandler;
  213. onMouseOver?: EChartMouseOverHandler;
  214. onRendered?: EChartRenderedHandler;
  215. /**
  216. * One example of when this is called is restoring chart from zoom levels
  217. */
  218. onRestore?: EChartRestoreHandler;
  219. options?: EChartsOption;
  220. /**
  221. * optional, used to determine how xAxis is formatted if `isGroupedByDate == true`
  222. */
  223. period?: string | null;
  224. /**
  225. * Custom chart props that are implemented by us (and not a feature of eCharts)
  226. *
  227. * Display previous period as a LineSeries
  228. */
  229. previousPeriod?: Series[];
  230. /**
  231. * Use `canvas` when dealing with large datasets
  232. * See: https://ecomfe.github.io/echarts-doc/public/en/tutorial.html#Render%20by%20Canvas%20or%20SVG
  233. */
  234. renderer?: ReactEChartOpts['renderer'];
  235. /**
  236. * Chart Series
  237. * This is different than the interface to higher level charts, these need to
  238. * be an array of ECharts "Series" components.
  239. */
  240. series?: SeriesOption[];
  241. /**
  242. * Format timestamp with date AND time
  243. */
  244. showTimeInTooltip?: boolean;
  245. /**
  246. * optional, used to determine how xAxis is formatted if `isGroupedByDate == true`
  247. */
  248. start?: Date;
  249. /**
  250. * Inline styles
  251. */
  252. style?: React.CSSProperties;
  253. /**
  254. * Toolbox options
  255. */
  256. toolBox?: EChartsOption['toolbox'];
  257. /**
  258. * Tooltip options
  259. */
  260. tooltip?: TooltipOption;
  261. /**
  262. * If true and there's only one datapoint in series.data, we show a bar chart to increase the visibility.
  263. * Especially useful with line / area charts, because you can't draw line with single data point and one alone point is hard to spot.
  264. */
  265. transformSinglePointToBar?: boolean;
  266. /**
  267. * If true and there's only one datapoint in series.data, we show a horizontal line to increase the visibility
  268. * Similarly to single point bar in area charts a flat line for line charts makes it easy to spot the single data point.
  269. */
  270. transformSinglePointToLine?: boolean;
  271. /**
  272. * Use short date formatting for xAxis
  273. */
  274. useShortDate?: boolean;
  275. /**
  276. * Formats dates as UTC?
  277. */
  278. utc?: boolean;
  279. /**
  280. * ECharts Visual Map Options.
  281. */
  282. visualMap?: VisualMapComponentOption | VisualMapComponentOption[];
  283. /**
  284. * Chart width
  285. */
  286. width?: ReactEChartOpts['width'];
  287. /**
  288. * Pass `true` to have 2 x-axes with default properties. Can pass an array
  289. * of multiple objects to customize xAxis properties
  290. */
  291. xAxes?: true | BaseChartProps['xAxis'][];
  292. /**
  293. * Must be explicitly `null` to disable xAxis
  294. *
  295. * Additionally a `truncate` option
  296. */
  297. xAxis?: (XAXisComponentOption & Truncateable) | null;
  298. /**
  299. * Pass `true` to have 2 y-axes with default properties. Can pass an array of
  300. * objects to customize yAxis properties
  301. */
  302. yAxes?: true | BaseChartProps['yAxis'][];
  303. /**
  304. * Must be explicitly `null` to disable yAxis
  305. */
  306. yAxis?: YAXisComponentOption | null;
  307. }
  308. const DEFAULT_CHART_READY = () => {};
  309. const DEFAULT_OPTIONS = {};
  310. const DEFAULT_SERIES = [];
  311. const DEFAULT_ADDITIONAL_SERIES = [];
  312. const DEFAULT_Y_AXIS = {};
  313. const DEFAULT_X_AXIS = {};
  314. function BaseChartUnwrapped({
  315. brush,
  316. colors,
  317. grid,
  318. tooltip,
  319. legend,
  320. dataZoom,
  321. toolBox,
  322. graphic,
  323. axisPointer,
  324. previousPeriod,
  325. echartsTheme,
  326. devicePixelRatio,
  327. minutesThresholdToDisplaySeconds,
  328. showTimeInTooltip,
  329. useShortDate,
  330. start,
  331. end,
  332. period,
  333. utc,
  334. yAxes,
  335. xAxes,
  336. style,
  337. forwardedRef,
  338. onClick,
  339. onLegendSelectChanged,
  340. onHighlight,
  341. onMouseOut,
  342. onMouseOver,
  343. onDataZoom,
  344. onRestore,
  345. onFinished,
  346. onRendered,
  347. onBrushStart,
  348. onBrushEnd,
  349. onBrushSelected,
  350. options = DEFAULT_OPTIONS,
  351. series = DEFAULT_SERIES,
  352. additionalSeries = DEFAULT_ADDITIONAL_SERIES,
  353. yAxis = DEFAULT_Y_AXIS,
  354. xAxis = DEFAULT_X_AXIS,
  355. autoHeightResize = false,
  356. height = 200,
  357. width,
  358. renderer = 'svg',
  359. notMerge = true,
  360. lazyUpdate = false,
  361. isGroupedByDate = false,
  362. transformSinglePointToBar = false,
  363. transformSinglePointToLine = false,
  364. onChartReady = DEFAULT_CHART_READY,
  365. 'data-test-id': dataTestId,
  366. }: BaseChartProps) {
  367. const theme = useTheme();
  368. const resolveColors =
  369. colors !== undefined ? (Array.isArray(colors) ? colors : colors(theme)) : null;
  370. const color =
  371. resolveColors ||
  372. (series.length ? theme.charts.getColorPalette(series.length) : theme.charts.colors);
  373. const resolvedSeries = useMemo(() => {
  374. const previousPeriodColors =
  375. (previousPeriod?.length ?? 0) > 1 ? lightenHexToRgb(color) : undefined;
  376. const hasSinglePoints = (series as LineSeriesOption[] | undefined)?.every(
  377. s => Array.isArray(s.data) && s.data.length <= 1
  378. );
  379. const transformedSeries =
  380. (hasSinglePoints && transformSinglePointToBar
  381. ? (series as LineSeriesOption[] | undefined)?.map(s => ({
  382. ...s,
  383. type: 'bar',
  384. barWidth: 40,
  385. barGap: 0,
  386. itemStyle: {...(s.areaStyle ?? {})},
  387. }))
  388. : hasSinglePoints && transformSinglePointToLine
  389. ? (series as LineSeriesOption[] | undefined)?.map(s => ({
  390. ...s,
  391. type: 'line',
  392. itemStyle: {...(s.lineStyle ?? {})},
  393. markLine:
  394. s?.data?.[0]?.[1] !== undefined
  395. ? MarkLine({
  396. silent: true,
  397. lineStyle: {
  398. type: 'solid',
  399. width: 1.5,
  400. },
  401. data: [{yAxis: s?.data?.[0]?.[1]}],
  402. label: {
  403. show: false,
  404. },
  405. })
  406. : undefined,
  407. }))
  408. : series) ?? [];
  409. const transformedPreviousPeriod =
  410. previousPeriod?.map((previous, seriesIndex) =>
  411. LineSeries({
  412. name: previous.seriesName,
  413. data: previous.data.map(({name, value}) => [name, value]),
  414. lineStyle: {
  415. color: previousPeriodColors
  416. ? previousPeriodColors[seriesIndex]
  417. : theme.gray200,
  418. type: 'dotted',
  419. },
  420. itemStyle: {
  421. color: previousPeriodColors
  422. ? previousPeriodColors[seriesIndex]
  423. : theme.gray200,
  424. },
  425. stack: 'previous',
  426. animation: false,
  427. })
  428. ) ?? [];
  429. return !previousPeriod
  430. ? transformedSeries.concat(additionalSeries)
  431. : transformedSeries.concat(transformedPreviousPeriod, additionalSeries);
  432. }, [
  433. series,
  434. color,
  435. transformSinglePointToBar,
  436. transformSinglePointToLine,
  437. previousPeriod,
  438. additionalSeries,
  439. theme.gray200,
  440. ]);
  441. /**
  442. * If true seconds will be added to the time format in the tooltips and chart xAxis
  443. */
  444. const addSecondsToTimeFormat =
  445. isGroupedByDate && defined(minutesThresholdToDisplaySeconds)
  446. ? getDiffInMinutes({start, end, period}) <= minutesThresholdToDisplaySeconds
  447. : false;
  448. const isTooltipPortalled = tooltip?.appendToBody;
  449. const chartOption = useMemo(() => {
  450. const seriesData =
  451. Array.isArray(series?.[0]?.data) && series[0].data.length > 1
  452. ? series[0].data
  453. : undefined;
  454. const bucketSize = seriesData ? seriesData[1][0] - seriesData[0][0] : undefined;
  455. const tooltipOrNone =
  456. tooltip !== null
  457. ? computeChartTooltip(
  458. {
  459. showTimeInTooltip,
  460. isGroupedByDate,
  461. addSecondsToTimeFormat,
  462. utc,
  463. bucketSize,
  464. ...tooltip,
  465. className: isTooltipPortalled
  466. ? `${tooltip?.className ?? ''} chart-tooltip-portal`
  467. : tooltip?.className,
  468. },
  469. theme
  470. )
  471. : undefined;
  472. const aria = computeEchartsAriaLabels(
  473. {series: resolvedSeries, useUTC: utc},
  474. isGroupedByDate
  475. );
  476. const defaultAxesProps = {theme};
  477. const yAxisOrCustom = !yAxes
  478. ? yAxis !== null
  479. ? YAxis({theme, ...yAxis})
  480. : undefined
  481. : Array.isArray(yAxes)
  482. ? yAxes.map(axis => YAxis({...axis, theme}))
  483. : [YAxis(defaultAxesProps), YAxis(defaultAxesProps)];
  484. const xAxisOrCustom = !xAxes
  485. ? xAxis !== null
  486. ? XAxis({
  487. ...xAxis,
  488. theme,
  489. useShortDate,
  490. start,
  491. end,
  492. period,
  493. isGroupedByDate,
  494. addSecondsToTimeFormat,
  495. utc,
  496. })
  497. : undefined
  498. : Array.isArray(xAxes)
  499. ? xAxes.map(axis =>
  500. XAxis({
  501. ...axis,
  502. theme,
  503. useShortDate,
  504. start,
  505. end,
  506. period,
  507. isGroupedByDate,
  508. addSecondsToTimeFormat,
  509. utc,
  510. })
  511. )
  512. : [XAxis(defaultAxesProps), XAxis(defaultAxesProps)];
  513. return {
  514. ...options,
  515. useUTC: utc,
  516. color,
  517. grid: Array.isArray(grid) ? grid.map(Grid) : Grid(grid),
  518. tooltip: tooltipOrNone,
  519. legend: legend ? Legend({theme, ...legend}) : undefined,
  520. yAxis: yAxisOrCustom,
  521. xAxis: xAxisOrCustom,
  522. series: resolvedSeries,
  523. toolbox: toolBox,
  524. axisPointer,
  525. dataZoom,
  526. graphic,
  527. aria,
  528. brush,
  529. };
  530. }, [
  531. color,
  532. resolvedSeries,
  533. isTooltipPortalled,
  534. theme,
  535. series,
  536. tooltip,
  537. showTimeInTooltip,
  538. addSecondsToTimeFormat,
  539. options,
  540. utc,
  541. grid,
  542. legend,
  543. toolBox,
  544. brush,
  545. axisPointer,
  546. dataZoom,
  547. graphic,
  548. isGroupedByDate,
  549. useShortDate,
  550. start,
  551. end,
  552. period,
  553. xAxis,
  554. xAxes,
  555. yAxes,
  556. yAxis,
  557. ]);
  558. // XXX(epurkhiser): Echarts can become unhappy if one of these event handlers
  559. // causes the chart to re-render and be passed a whole different instance of
  560. // event handlers.
  561. //
  562. // We use React.useMemo to keep the value across renders
  563. //
  564. const eventsMap = useMemo(
  565. () =>
  566. ({
  567. click: (props, instance) => {
  568. handleClick(props, instance);
  569. onClick?.(props, instance);
  570. },
  571. highlight: (props, instance) => onHighlight?.(props, instance),
  572. mouseout: (props, instance) => onMouseOut?.(props, instance),
  573. mouseover: (props, instance) => onMouseOver?.(props, instance),
  574. datazoom: (props, instance) => onDataZoom?.(props, instance),
  575. restore: (props, instance) => onRestore?.(props, instance),
  576. finished: (props, instance) => onFinished?.(props, instance),
  577. rendered: (props, instance) => onRendered?.(props, instance),
  578. legendselectchanged: (props, instance) =>
  579. onLegendSelectChanged?.(props, instance),
  580. brush: (props, instance) => onBrushStart?.(props, instance),
  581. brushend: (props, instance) => onBrushEnd?.(props, instance),
  582. brushselected: (props, instance) => onBrushSelected?.(props, instance),
  583. }) as ReactEchartProps['onEvents'],
  584. [
  585. onClick,
  586. onHighlight,
  587. onLegendSelectChanged,
  588. onMouseOut,
  589. onMouseOver,
  590. onDataZoom,
  591. onRestore,
  592. onFinished,
  593. onRendered,
  594. onBrushStart,
  595. onBrushEnd,
  596. onBrushSelected,
  597. ]
  598. );
  599. const coreOptions = useMemo(() => {
  600. return {
  601. height: autoHeightResize ? undefined : height,
  602. width,
  603. renderer,
  604. devicePixelRatio,
  605. };
  606. }, [autoHeightResize, height, width, renderer, devicePixelRatio]);
  607. const chartStyles = useMemo(() => {
  608. return {
  609. height: autoHeightResize ? '100%' : getDimensionValue(height),
  610. width: getDimensionValue(width),
  611. ...style,
  612. };
  613. }, [style, autoHeightResize, height, width]);
  614. return (
  615. <ChartContainer autoHeightResize={autoHeightResize} data-test-id={dataTestId}>
  616. {isTooltipPortalled && <Global styles={getPortalledTooltipStyles({theme})} />}
  617. <ReactEchartsCore
  618. ref={forwardedRef}
  619. echarts={echarts}
  620. notMerge={notMerge}
  621. lazyUpdate={lazyUpdate}
  622. theme={echartsTheme}
  623. onChartReady={onChartReady}
  624. onEvents={eventsMap}
  625. style={chartStyles}
  626. opts={coreOptions}
  627. option={chartOption}
  628. />
  629. </ChartContainer>
  630. );
  631. }
  632. // Tooltip styles shared for regular and portalled tooltips
  633. const getTooltipStyles = (p: {theme: Theme}) => css`
  634. /* Tooltip styling */
  635. .tooltip-series,
  636. .tooltip-footer {
  637. color: ${p.theme.subText};
  638. font-family: ${p.theme.text.family};
  639. font-variant-numeric: tabular-nums;
  640. padding: ${space(1)} ${space(2)};
  641. border-radius: ${p.theme.borderRadius} ${p.theme.borderRadius} 0 0;
  642. }
  643. .tooltip-series {
  644. border-bottom: none;
  645. max-width: calc(100vw - 2 * ${CHART_TOOLTIP_VIEWPORT_OFFSET}px);
  646. }
  647. .tooltip-series-solo {
  648. border-radius: ${p.theme.borderRadius};
  649. }
  650. .tooltip-label {
  651. margin-right: ${space(1)};
  652. ${p.theme.overflowEllipsis};
  653. }
  654. .tooltip-label strong {
  655. font-weight: ${p.theme.fontWeightNormal};
  656. color: ${p.theme.textColor};
  657. }
  658. .tooltip-label-value {
  659. color: ${p.theme.textColor};
  660. }
  661. .tooltip-label-indent {
  662. margin-left: 18px;
  663. }
  664. .tooltip-series > div {
  665. display: flex;
  666. justify-content: space-between;
  667. align-items: baseline;
  668. }
  669. .tooltip-footer {
  670. border-top: solid 1px ${p.theme.innerBorder};
  671. text-align: center;
  672. position: relative;
  673. width: auto;
  674. border-radius: ${p.theme.borderRadiusBottom};
  675. display: flex;
  676. justify-content: space-between;
  677. gap: ${space(3)};
  678. }
  679. .tooltip-footer-centered {
  680. justify-content: center;
  681. gap: 0;
  682. }
  683. .tooltip-arrow {
  684. top: 100%;
  685. left: 50%;
  686. position: absolute;
  687. pointer-events: none;
  688. border-left: 8px solid transparent;
  689. border-right: 8px solid transparent;
  690. border-top: 8px solid ${p.theme.backgroundElevated};
  691. margin-left: -8px;
  692. &:before {
  693. border-left: 8px solid transparent;
  694. border-right: 8px solid transparent;
  695. border-top: 8px solid ${p.theme.translucentBorder};
  696. content: '';
  697. display: block;
  698. position: absolute;
  699. top: -7px;
  700. left: -8px;
  701. z-index: -1;
  702. }
  703. }
  704. /* Tooltip description styling */
  705. .tooltip-description {
  706. color: ${p.theme.white};
  707. border-radius: ${p.theme.borderRadius};
  708. background: #000;
  709. opacity: 0.9;
  710. padding: 5px 10px;
  711. position: relative;
  712. font-weight: ${p.theme.fontWeightBold};
  713. font-size: ${p.theme.fontSizeSmall};
  714. line-height: 1.4;
  715. font-family: ${p.theme.text.family};
  716. max-width: 230px;
  717. min-width: 230px;
  718. white-space: normal;
  719. text-align: center;
  720. :after {
  721. content: '';
  722. position: absolute;
  723. top: 100%;
  724. left: 50%;
  725. width: 0;
  726. height: 0;
  727. border-left: 5px solid transparent;
  728. border-right: 5px solid transparent;
  729. border-top: 5px solid #000;
  730. transform: translateX(-50%);
  731. }
  732. }
  733. `;
  734. // Contains styling for chart elements as we can't easily style those
  735. // elements directly
  736. const ChartContainer = styled('div')<{autoHeightResize: boolean}>`
  737. ${p => p.autoHeightResize && 'height: 100%;'}
  738. .echarts-for-react div:first-of-type {
  739. width: 100% !important;
  740. }
  741. .echarts-for-react text {
  742. font-variant-numeric: tabular-nums !important;
  743. }
  744. ${p => getTooltipStyles(p)}
  745. `;
  746. const getPortalledTooltipStyles = (p: {theme: Theme}) => css`
  747. .chart-tooltip-portal {
  748. ${getTooltipStyles(p)};
  749. }
  750. `;
  751. const BaseChart = forwardRef<ReactEchartsRef, BaseChartProps>((props, ref) => (
  752. <BaseChartUnwrapped forwardedRef={ref} {...props} />
  753. ));
  754. BaseChart.displayName = 'forwardRef(BaseChart)';
  755. export default BaseChart;