Browse Source

ref(charts): Convert charts from class to func (#32566)

Priscila Oliveira 2 years ago
parent
commit
360ad91a94

+ 1 - 1
docs-ui/stories/components/areaChart.stories.js

@@ -1,6 +1,6 @@
 import styled from '@emotion/styled';
 import styled from '@emotion/styled';
 
 
-import AreaChart from 'sentry/components/charts/areaChart';
+import {AreaChart} from 'sentry/components/charts/areaChart';
 import space from 'sentry/styles/space';
 import space from 'sentry/styles/space';
 
 
 export default {
 export default {

+ 1 - 1
docs-ui/stories/components/barChart.stories.js

@@ -1,4 +1,4 @@
-import BarChart from 'sentry/components/charts/barChart';
+import {BarChart} from 'sentry/components/charts/barChart';
 
 
 export default {
 export default {
   title: 'Components/Data Visualization/Charts/Bar Chart',
   title: 'Components/Data Visualization/Charts/Bar Chart',

+ 1 - 1
docs-ui/stories/components/chart-utilities.stories.js

@@ -1,7 +1,7 @@
 import {action} from '@storybook/addon-actions';
 import {action} from '@storybook/addon-actions';
 
 
 import ChartZoom from 'sentry/components/charts/chartZoom';
 import ChartZoom from 'sentry/components/charts/chartZoom';
-import LineChart from 'sentry/components/charts/lineChart';
+import {LineChart} from 'sentry/components/charts/lineChart';
 
 
 export default {
 export default {
   title: 'Components/Data Visualization/Charts/Utilities/Chart Zoom',
   title: 'Components/Data Visualization/Charts/Utilities/Chart Zoom',

+ 2 - 2
docs-ui/stories/components/charts.stories.js

@@ -1,5 +1,5 @@
-import BarChart from 'sentry/components/charts/barChart';
-import LineChart from 'sentry/components/charts/lineChart';
+import {BarChart} from 'sentry/components/charts/barChart';
+import {LineChart} from 'sentry/components/charts/lineChart';
 
 
 export default {
 export default {
   title: 'Components/Data Visualization/Charts/Playground',
   title: 'Components/Data Visualization/Charts/Playground',

+ 1 - 1
docs-ui/stories/components/lineChart.stories.js

@@ -1,4 +1,4 @@
-import LineChart from 'sentry/components/charts/lineChart';
+import {LineChart} from 'sentry/components/charts/lineChart';
 
 
 export default {
 export default {
   title: 'Components/Data Visualization/Charts/Line Chart',
   title: 'Components/Data Visualization/Charts/Line Chart',

+ 30 - 35
static/app/components/charts/areaChart.tsx

@@ -10,42 +10,37 @@ type ChartProps = Omit<React.ComponentProps<typeof BaseChart>, 'css'>;
 
 
 export type AreaChartSeries = Series & Omit<LineSeriesOption, 'data' | 'name'>;
 export type AreaChartSeries = Series & Omit<LineSeriesOption, 'data' | 'name'>;
 
 
-type Props = Omit<ChartProps, 'series'> & {
+export interface AreaChartProps extends Omit<ChartProps, 'series'> {
   series: AreaChartSeries[];
   series: AreaChartSeries[];
   stacked?: boolean;
   stacked?: boolean;
-};
-
-class AreaChart extends React.Component<Props> {
-  render() {
-    const {series, stacked, colors, ...props} = this.props;
-
-    return (
-      <BaseChart
-        {...props}
-        colors={colors}
-        series={series.map(({seriesName, data, ...otherSeriesProps}, i) =>
-          AreaSeries({
-            stack: stacked ? 'area' : undefined,
-            name: seriesName,
-            data: data.map(({name, value}) => [name, value]),
-            lineStyle: {
-              color: colors?.[i],
-              opacity: 1,
-              width: 0.4,
-            },
-            areaStyle: {
-              color: colors?.[i],
-              opacity: 1.0,
-            },
-            animation: false,
-            animationThreshold: 1,
-            animationDuration: 0,
-            ...otherSeriesProps,
-          })
-        )}
-      />
-    );
-  }
 }
 }
 
 
-export default AreaChart;
+export function AreaChart({series, stacked, colors, ...props}: AreaChartProps) {
+  return (
+    <BaseChart
+      {...props}
+      data-test-id="area-chart"
+      colors={colors}
+      series={series.map(({seriesName, data, ...otherSeriesProps}, i) =>
+        AreaSeries({
+          stack: stacked ? 'area' : undefined,
+          name: seriesName,
+          data: data.map(({name, value}) => [name, value]),
+          lineStyle: {
+            color: colors?.[i],
+            opacity: 1,
+            width: 0.4,
+          },
+          areaStyle: {
+            color: colors?.[i],
+            opacity: 1.0,
+          },
+          animation: false,
+          animationThreshold: 1,
+          animationDuration: 0,
+          ...otherSeriesProps,
+        })
+      )}
+    />
+  );
+}

+ 23 - 29
static/app/components/charts/barChart.tsx

@@ -10,37 +10,31 @@ type ChartProps = Omit<React.ComponentProps<typeof BaseChart>, 'css'>;
 
 
 export type BarChartSeries = Series & Omit<BarSeriesOption, 'data' | 'name'>;
 export type BarChartSeries = Series & Omit<BarSeriesOption, 'data' | 'name'>;
 
 
-type Props = Omit<ChartProps, 'series'> & {
+export interface BarChartProps extends Omit<ChartProps, 'series'> {
   series: BarChartSeries[];
   series: BarChartSeries[];
   animation?: boolean;
   animation?: boolean;
   stacked?: boolean;
   stacked?: boolean;
-};
-
-class BarChart extends React.Component<Props> {
-  render() {
-    const {series, stacked, xAxis, animation, ...props} = this.props;
-
-    return (
-      <BaseChart
-        {...props}
-        xAxis={xAxis !== null ? {...(xAxis || {}), boundaryGap: true} : null}
-        series={series.map(({seriesName, data, ...options}) =>
-          BarSeries({
-            name: seriesName,
-            stack: stacked ? 'stack1' : undefined,
-            data: data.map(({value, name, itemStyle}) => {
-              if (itemStyle === undefined) {
-                return [name, value];
-              }
-              return {value: [name, value], itemStyle};
-            }),
-            animation,
-            ...options,
-          })
-        )}
-      />
-    );
-  }
 }
 }
 
 
-export default BarChart;
+export function BarChart({series, stacked, xAxis, animation, ...props}: BarChartProps) {
+  return (
+    <BaseChart
+      {...props}
+      xAxis={xAxis !== null ? {...(xAxis || {}), boundaryGap: true} : null}
+      series={series.map(({seriesName, data, ...options}) =>
+        BarSeries({
+          name: seriesName,
+          stack: stacked ? 'stack1' : undefined,
+          data: data.map(({value, name, itemStyle}) => {
+            if (itemStyle === undefined) {
+              return [name, value];
+            }
+            return {value: [name, value], itemStyle};
+          }),
+          animation,
+          ...options,
+        })
+      )}
+    />
+  );
+}

+ 4 - 1
static/app/components/charts/baseChart.tsx

@@ -129,6 +129,7 @@ type Props = {
    * provided with the current theme
    * provided with the current theme
    */
    */
   colors?: string[] | ((theme: Theme) => string[]);
   colors?: string[] | ((theme: Theme) => string[]);
+  'data-test-id'?: string;
   /**
   /**
    * DataZoom (allows for zooming of chart)
    * DataZoom (allows for zooming of chart)
    */
    */
@@ -275,6 +276,7 @@ type Props = {
    * Additionally a `truncate` option
    * Additionally a `truncate` option
    */
    */
   xAxis?: (XAXisComponentOption & Truncateable) | null;
   xAxis?: (XAXisComponentOption & Truncateable) | null;
+
   /**
   /**
    * Pass `true` to have 2 y-axes with default properties. Can pass an array of
    * Pass `true` to have 2 y-axes with default properties. Can pass an array of
    * objects to customize yAxis properties
    * objects to customize yAxis properties
@@ -338,6 +340,7 @@ function BaseChartUnwrapped({
   transformSinglePointToBar = false,
   transformSinglePointToBar = false,
   transformSinglePointToLine = false,
   transformSinglePointToLine = false,
   onChartReady = () => {},
   onChartReady = () => {},
+  'data-test-id': dataTestId,
 }: Props) {
 }: Props) {
   const theme = useTheme();
   const theme = useTheme();
 
 
@@ -520,7 +523,7 @@ function BaseChartUnwrapped({
   );
   );
 
 
   return (
   return (
-    <ChartContainer autoHeightResize={autoHeightResize}>
+    <ChartContainer autoHeightResize={autoHeightResize} data-test-id={dataTestId}>
       <ReactEchartsCore
       <ReactEchartsCore
         ref={forwardedRef}
         ref={forwardedRef}
         echarts={echarts}
         echarts={echarts}

+ 24 - 23
static/app/components/charts/eventsChart.tsx

@@ -11,11 +11,11 @@ import {Query} from 'history';
 import isEqual from 'lodash/isEqual';
 import isEqual from 'lodash/isEqual';
 
 
 import {Client} from 'sentry/api';
 import {Client} from 'sentry/api';
-import AreaChart from 'sentry/components/charts/areaChart';
-import BarChart from 'sentry/components/charts/barChart';
+import {AreaChart, AreaChartProps} from 'sentry/components/charts/areaChart';
+import {BarChart, BarChartProps} from 'sentry/components/charts/barChart';
 import ChartZoom, {ZoomRenderProps} from 'sentry/components/charts/chartZoom';
 import ChartZoom, {ZoomRenderProps} from 'sentry/components/charts/chartZoom';
 import ErrorPanel from 'sentry/components/charts/errorPanel';
 import ErrorPanel from 'sentry/components/charts/errorPanel';
-import LineChart from 'sentry/components/charts/lineChart';
+import {LineChart, LineChartProps} from 'sentry/components/charts/lineChart';
 import ReleaseSeries from 'sentry/components/charts/releaseSeries';
 import ReleaseSeries from 'sentry/components/charts/releaseSeries';
 import TransitionChart from 'sentry/components/charts/transitionChart';
 import TransitionChart from 'sentry/components/charts/transitionChart';
 import TransparentLoadingMask from 'sentry/components/charts/transparentLoadingMask';
 import TransparentLoadingMask from 'sentry/components/charts/transparentLoadingMask';
@@ -24,7 +24,7 @@ import {
   processTableResults,
   processTableResults,
   RELEASE_LINES_THRESHOLD,
   RELEASE_LINES_THRESHOLD,
 } from 'sentry/components/charts/utils';
 } from 'sentry/components/charts/utils';
-import WorldMapChart from 'sentry/components/charts/worldMapChart';
+import {WorldMapChart, WorldMapChartProps} from 'sentry/components/charts/worldMapChart';
 import {IconWarning} from 'sentry/icons';
 import {IconWarning} from 'sentry/icons';
 import {t} from 'sentry/locale';
 import {t} from 'sentry/locale';
 import {DateString, OrganizationSummary} from 'sentry/types';
 import {DateString, OrganizationSummary} from 'sentry/types';
@@ -44,10 +44,10 @@ import EventsGeoRequest from './eventsGeoRequest';
 import EventsRequest from './eventsRequest';
 import EventsRequest from './eventsRequest';
 
 
 type ChartComponent =
 type ChartComponent =
-  | React.ComponentType<BarChart['props']>
-  | React.ComponentType<AreaChart['props']>
-  | React.ComponentType<React.ComponentProps<typeof LineChart>>
-  | React.ComponentType<React.ComponentProps<typeof WorldMapChart>>;
+  | React.ComponentType<BarChartProps>
+  | React.ComponentType<AreaChartProps>
+  | React.ComponentType<LineChartProps>
+  | React.ComponentType<WorldMapChartProps>;
 
 
 type ChartProps = {
 type ChartProps = {
   currentSeriesNames: string[];
   currentSeriesNames: string[];
@@ -129,6 +129,7 @@ class Chart extends React.Component<ChartProps, State> {
 
 
   getChartComponent(): ChartComponent {
   getChartComponent(): ChartComponent {
     const {showDaily, timeseriesData, yAxis, chartComponent} = this.props;
     const {showDaily, timeseriesData, yAxis, chartComponent} = this.props;
+
     if (defined(chartComponent)) {
     if (defined(chartComponent)) {
       return chartComponent;
       return chartComponent;
     }
     }
@@ -136,6 +137,7 @@ class Chart extends React.Component<ChartProps, State> {
     if (showDaily) {
     if (showDaily) {
       return BarChart;
       return BarChart;
     }
     }
+
     if (timeseriesData.length > 1) {
     if (timeseriesData.length > 1) {
       switch (aggregateMultiPlotType(yAxis)) {
       switch (aggregateMultiPlotType(yAxis)) {
         case 'line':
         case 'line':
@@ -146,6 +148,7 @@ class Chart extends React.Component<ChartProps, State> {
           throw new Error(`Unknown multi plot type for ${yAxis}`);
           throw new Error(`Unknown multi plot type for ${yAxis}`);
       }
       }
     }
     }
+
     return AreaChart;
     return AreaChart;
   }
   }
 
 
@@ -168,18 +171,6 @@ class Chart extends React.Component<ChartProps, State> {
     );
     );
   };
   };
 
 
-  renderWorldMap() {
-    const {tableData, fromDiscover} = this.props;
-    const {data, title} = processTableResults(tableData);
-    const tableSeries = [
-      {
-        seriesName: title,
-        data,
-      },
-    ];
-    return <WorldMapChart series={tableSeries} fromDiscover={fromDiscover} />;
-  }
-
   render() {
   render() {
     const {
     const {
       theme,
       theme,
@@ -201,18 +192,28 @@ class Chart extends React.Component<ChartProps, State> {
       height,
       height,
       timeframe,
       timeframe,
       topEvents,
       topEvents,
+      tableData,
+      fromDiscover,
       ...props
       ...props
     } = this.props;
     } = this.props;
     const {seriesSelection} = this.state;
     const {seriesSelection} = this.state;
 
 
     let Component = this.getChartComponent();
     let Component = this.getChartComponent();
 
 
-    if (typeof Component === typeof WorldMapChart) {
-      return this.renderWorldMap();
+    if (Component === WorldMapChart) {
+      const {data, title} = processTableResults(tableData);
+      const tableSeries = [
+        {
+          seriesName: title,
+          data,
+        },
+      ];
+      return <WorldMapChart series={tableSeries} fromDiscover={fromDiscover} />;
     }
     }
+
     Component = Component as Exclude<
     Component = Component as Exclude<
       ChartComponent,
       ChartComponent,
-      React.ComponentType<React.ComponentProps<typeof WorldMapChart>>
+      React.ComponentType<WorldMapChartProps>
     >;
     >;
 
 
     const data = [
     const data = [

+ 3 - 5
static/app/components/charts/lineChart.tsx

@@ -13,12 +13,12 @@ export type LineChartSeries = Series &
     dataArray?: LineSeriesOption['data'];
     dataArray?: LineSeriesOption['data'];
   };
   };
 
 
-type Props = Omit<ChartProps, 'series'> & {
+export interface LineChartProps extends Omit<ChartProps, 'series'> {
   series: LineChartSeries[];
   series: LineChartSeries[];
   seriesOptions?: LineSeriesOption;
   seriesOptions?: LineSeriesOption;
-};
+}
 
 
-function LineChart({series, seriesOptions, ...props}: Props) {
+export function LineChart({series, seriesOptions, ...props}: LineChartProps) {
   return (
   return (
     <BaseChart
     <BaseChart
       {...props}
       {...props}
@@ -36,5 +36,3 @@ function LineChart({series, seriesOptions, ...props}: Props) {
     />
     />
   );
   );
 }
 }
-
-export default LineChart;

Some files were not shown because too many files changed in this diff