baseChart.tsx 21 KB

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