areaChart.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import type {LineSeriesOption} from 'echarts';
  2. import type {Series} from 'sentry/types/echarts';
  3. import AreaSeries from './series/areaSeries';
  4. import type {BaseChartProps} from './baseChart';
  5. import BaseChart from './baseChart';
  6. export interface AreaChartSeries
  7. extends Omit<LineSeriesOption, 'data' | 'areaStyle' | 'color' | 'id'>,
  8. Series {}
  9. export interface AreaChartProps extends Omit<BaseChartProps, 'series'> {
  10. series: AreaChartSeries[];
  11. additionalSeries?: LineSeriesOption[];
  12. stacked?: boolean;
  13. }
  14. export function transformToAreaSeries({
  15. series,
  16. stacked,
  17. colors,
  18. }: Pick<AreaChartProps, 'series' | 'stacked' | 'colors'>) {
  19. return series.map(({seriesName, data, ...otherSeriesProps}, i) =>
  20. AreaSeries({
  21. stack: stacked ? 'area' : undefined,
  22. name: seriesName,
  23. data: data?.map(({name, value}) => [name, value]),
  24. lineStyle: {
  25. color: colors?.[i],
  26. opacity: 1,
  27. width: 0.4,
  28. },
  29. areaStyle: {
  30. color: colors?.[i],
  31. opacity: 1.0,
  32. },
  33. // Define the z level so that the series remain stacked in the correct order
  34. // even after operations like hiding / highlighting series
  35. z: i,
  36. animation: false,
  37. animationThreshold: 1,
  38. animationDuration: 0,
  39. ...otherSeriesProps,
  40. })
  41. );
  42. }
  43. export function AreaChart({series, stacked, colors, ...props}: AreaChartProps) {
  44. return (
  45. <BaseChart
  46. {...props}
  47. data-test-id="area-chart"
  48. colors={colors}
  49. series={transformToAreaSeries({series, stacked, colors})}
  50. />
  51. );
  52. }