File size: 3,277 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import * as React from 'react';
import { forwardRef } from 'react';
import { ChartOptions } from '../state/optionsSlice';
import { RechartsStoreProvider } from '../state/RechartsStoreProvider';
import { ChartDataContextProvider } from '../context/chartDataContext';
import { ReportMainChartProps } from '../state/ReportMainChartProps';
import { ReportChartProps } from '../state/ReportChartProps';
import { CartesianChartProps, Margin, TooltipEventType } from '../util/types';
import { TooltipPayloadSearcher } from '../state/tooltipSlice';
import { CategoricalChart } from './CategoricalChart';
import { resolveDefaultProps } from '../util/resolveDefaultProps';
import { isPositiveNumber } from '../util/isWellBehavedNumber';

const defaultMargin: Margin = { top: 5, right: 5, bottom: 5, left: 5 };

const defaultProps = {
  accessibilityLayer: true,
  layout: 'horizontal',
  stackOffset: 'none',
  barCategoryGap: '10%',
  barGap: 4,
  margin: defaultMargin,
  reverseStackOrder: false,
  syncMethod: 'index',
} as const satisfies Partial<CartesianChartProps>;

/**
 * These are one-time, immutable options that decide the chart's behavior.
 * Users who wish to call CartesianChart may decide to pass these options explicitly,
 * but usually we would expect that they use one of the convenience components like BarChart, LineChart, etc.
 */
export type CartesianChartOptions = {
  chartName: string;
  defaultTooltipEventType: TooltipEventType;
  validateTooltipEventTypes: ReadonlyArray<TooltipEventType>;
  tooltipPayloadSearcher: TooltipPayloadSearcher;
  categoricalChartProps: CartesianChartProps;
};

export const CartesianChart = forwardRef<SVGSVGElement, CartesianChartOptions>(function CartesianChart(
  props: CartesianChartOptions,
  ref,
) {
  const rootChartProps = resolveDefaultProps(props.categoricalChartProps, defaultProps);

  const { width, height, ...otherCategoricalProps } = rootChartProps;

  if (!isPositiveNumber(width) || !isPositiveNumber(height)) {
    return null;
  }

  const {
    chartName,
    defaultTooltipEventType,
    validateTooltipEventTypes,
    tooltipPayloadSearcher,
    categoricalChartProps,
  } = props;

  const options: ChartOptions = {
    chartName,
    defaultTooltipEventType,
    validateTooltipEventTypes,
    tooltipPayloadSearcher,
    eventEmitter: undefined,
  };

  return (
    <RechartsStoreProvider preloadedState={{ options }} reduxStoreName={categoricalChartProps.id ?? chartName}>
      <ChartDataContextProvider chartData={categoricalChartProps.data} />
      <ReportMainChartProps
        width={width}
        height={height}
        layout={rootChartProps.layout}
        margin={rootChartProps.margin}
      />
      <ReportChartProps
        accessibilityLayer={rootChartProps.accessibilityLayer}
        barCategoryGap={rootChartProps.barCategoryGap}
        maxBarSize={rootChartProps.maxBarSize}
        stackOffset={rootChartProps.stackOffset}
        barGap={rootChartProps.barGap}
        barSize={rootChartProps.barSize}
        syncId={rootChartProps.syncId}
        syncMethod={rootChartProps.syncMethod}
        className={rootChartProps.className}
      />
      <CategoricalChart {...otherCategoricalProps} width={width} height={height} ref={ref} />
    </RechartsStoreProvider>
  );
});