Skip to content

Commit

Permalink
✨ feat: Update to recharts 3.0.0-alpha.0
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 committed Jan 6, 2025
1 parent 3e5965e commit cd64691
Show file tree
Hide file tree
Showing 19 changed files with 113 additions and 30 deletions.
10 changes: 1 addition & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
},
"dependencies": {
"date-fns": "^3.6.0",
"recharts": "^2.15.0"
"recharts": "^3.0.0-alpha.0"
},
"devDependencies": {
"@commitlint/cli": "^19.6.1",
Expand Down Expand Up @@ -121,13 +121,5 @@
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
},
"pnpm": {
"overrides": {
"react-is": "^19.0.0"
}
},
"overrides": {
"react-is": "^19.0.0"
}
}
1 change: 1 addition & 0 deletions src/AreaChart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ const AreaChart = forwardRef<HTMLDivElement, AreaChartProps>((props, ref) => {
<ResponsiveContainer>
{data?.length ? (
<ReChartsAreaChart
className={styles.chart}
data={data}
margin={{
bottom: xAxisLabel ? 30 : undefined,
Expand Down
5 changes: 5 additions & 0 deletions src/AreaChart/styles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { createStyles } from 'antd-style';

export const useStyles = createStyles(({ css, token }) => ({
chart: css`
svg {
outline: none;
}
`,
emphasis: css`
overflow: hidden;
text-overflow: ellipsis;
Expand Down
1 change: 1 addition & 0 deletions src/BarChart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ const BarChart = forwardRef<HTMLDivElement, BarChartProps>((props, ref) => {
{data?.length ? (
<ReChartsBarChart
barCategoryGap={barCategoryGap}
className={styles.chart}
data={data}
layout={layout === 'vertical' ? 'vertical' : 'horizontal'}
margin={{
Expand Down
5 changes: 5 additions & 0 deletions src/BarChart/styles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { createStyles } from 'antd-style';

export const useStyles = createStyles(({ css, token }) => ({
chart: css`
svg {
outline: none;
}
`,
emphasis: css`
overflow: hidden;
text-overflow: ellipsis;
Expand Down
36 changes: 25 additions & 11 deletions src/DonutChart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

import { Skeleton } from 'antd';
import { css, useThemeMode } from 'antd-style';
import { CSSProperties, ComponentType, MouseEvent, forwardRef, useEffect, useState } from 'react';
import {
CSSProperties,
ComponentType,
MouseEvent,
forwardRef,
useEffect,
useMemo,
useState,
} from 'react';
import { Flexbox } from 'react-layout-kit';
import { Pie, PieChart as ReChartsDonutChart, ResponsiveContainer, Tooltip } from 'recharts';

Expand All @@ -16,6 +24,7 @@ import { defaultValueFormatter, isOnSeverSide } from '@/utils';

import { DonutChartTooltip } from './DonutChartTooltip';
import { parseLabelInput } from './inputParser';
import { renderActiveShape } from './renderActiveShape';
import { renderInactiveShape } from './renderInactiveShape';
import { useStyles } from './styles';

Expand Down Expand Up @@ -89,8 +98,6 @@ const DonutChart = forwardRef<HTMLDivElement, DonutChartProps>((props, ref) => {
}
}, [activeIndex]);

if (loading || !data) return <Skeleton.Button active block style={{ height, width }} />;

const onShapeClick = (data: any, index: number, event: MouseEvent) => {
event.stopPropagation();

Expand Down Expand Up @@ -120,6 +127,15 @@ const DonutChart = forwardRef<HTMLDivElement, DonutChartProps>((props, ref) => {
};
});

const paredData = useMemo(() => parseData(data, colors), [data, colors]);

const paredDonutData = useMemo(
() => parseData([{ [category]: 1 }], [isDarkMode ? 'rgba(0,0,0,.33)' : 'rgba(0,0,0,.1)']),
[category, isDarkMode],
);

if (loading || !data) return <Skeleton.Button active block style={{ height, width }} />;

return (
<Flexbox
className={className}
Expand All @@ -132,6 +148,7 @@ const DonutChart = forwardRef<HTMLDivElement, DonutChartProps>((props, ref) => {
<ResponsiveContainer>
{data?.length ? (
<ReChartsDonutChart
className={styles.chart}
margin={{ bottom: 0, left: 0, right: 0, top: 0 }}
onClick={
hasOnValueChange && activeIndex
Expand All @@ -157,11 +174,11 @@ const DonutChart = forwardRef<HTMLDivElement, DonutChartProps>((props, ref) => {
</text>
) : null}
<Pie
activeIndex={activeIndex}
activeShape={renderActiveShape}
animationDuration={animationDuration}
cx="50%"
cy="50%"
data={parseData(data, colors)}
data={paredData}
dataKey={category}
endAngle={-270}
inactiveShape={renderInactiveShape}
Expand All @@ -181,19 +198,16 @@ const DonutChart = forwardRef<HTMLDivElement, DonutChartProps>((props, ref) => {
/>
{isDonut && (
<Pie
activeIndex={activeIndex}
activeShape={renderActiveShape}
animationDuration={animationDuration}
cx="50%"
cy="50%"
data={parseData(
[{ [category]: 1 }],
[isDarkMode ? 'rgba(0,0,0,.33)' : 'rgba(0,0,0,.1)'],
)}
data={paredDonutData}
dataKey={category}
endAngle={-270}
inactiveShape={renderInactiveShape}
innerRadius={isDonut ? '75%' : '0%'}
isAnimationActive={false}
isAnimationActive={showAnimation}
nameKey={index}
outerRadius="80%"
startAngle={90}
Expand Down
22 changes: 22 additions & 0 deletions src/DonutChart/renderActiveShape.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Sector } from 'recharts';
import { PieSectorDataItem } from 'recharts/types/polar/Pie';

export const renderActiveShape = (props: PieSectorDataItem) => {
const { cx, cy, innerRadius, outerRadius, startAngle, endAngle, className } = props;

return (
<g>
<Sector
className={className}
cx={cx}
cy={cy}
endAngle={endAngle}
fill=""
innerRadius={innerRadius}
outerRadius={outerRadius}
startAngle={startAngle}
style={{ outline: 'none' }}
/>
</g>
);
};
3 changes: 2 additions & 1 deletion src/DonutChart/renderInactiveShape.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Sector } from 'recharts';
import { PieSectorDataItem } from 'recharts/types/polar/Pie';

export const renderInactiveShape = (props: any) => {
export const renderInactiveShape = (props: PieSectorDataItem) => {
const { cx, cy, innerRadius, outerRadius, startAngle, endAngle, className } = props;

return (
Expand Down
5 changes: 5 additions & 0 deletions src/DonutChart/styles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { createStyles } from 'antd-style';

export const useStyles = createStyles(({ css, token }) => ({
chart: css`
svg {
outline: none;
}
`,
emphasis: css`
overflow: hidden;
text-overflow: ellipsis;
Expand Down
1 change: 1 addition & 0 deletions src/FunnelChart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ const FunnelChart = forwardRef<HTMLDivElement, FunnelChartProps>((props, ref) =>
{data?.length ? (
<>
<svg
className={styles.chart}
onMouseLeave={() => setTooltip({ x: 0, y: 0 })}
onMouseMove={(e) => {
const fakeTouch = {
Expand Down
3 changes: 3 additions & 0 deletions src/FunnelChart/styles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { createStyles } from 'antd-style';

export const useStyles = createStyles(({ css, token }) => ({
chart: css`
outline: none;
`,
emphasis: css`
overflow: hidden;
text-overflow: ellipsis;
Expand Down
1 change: 1 addition & 0 deletions src/LineChart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ const LineChart = forwardRef<HTMLDivElement, LineChartProps>((props, ref) => {
<ResponsiveContainer>
{data?.length ? (
<ReChartsLineChart
className={styles.chart}
data={data}
margin={{
bottom: xAxisLabel ? 30 : undefined,
Expand Down
5 changes: 5 additions & 0 deletions src/LineChart/styles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { createStyles } from 'antd-style';

export const useStyles = createStyles(({ css, token }) => ({
chart: css`
svg {
outline: none;
}
`,
emphasis: css`
overflow: hidden;
text-overflow: ellipsis;
Expand Down
3 changes: 2 additions & 1 deletion src/ScatterChart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export interface ScatterChartProps
showXAxis?: boolean;
showYAxis?: boolean;
size?: string;
sizeRange?: number[];
sizeRange?: [number, number];
startEndOnly?: boolean;
tickGap?: number;
valueFormatter?: ScatterChartValueFormatter;
Expand Down Expand Up @@ -214,6 +214,7 @@ const ScatterChart = forwardRef<HTMLDivElement, ScatterChartProps>((props, ref)
<ResponsiveContainer>
{data?.length ? (
<ReChartsScatterChart
className={styles.chart}
margin={{
bottom: xAxisLabel ? 20 : undefined,
left: 20,
Expand Down
5 changes: 5 additions & 0 deletions src/ScatterChart/styles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { createStyles } from 'antd-style';

export const useStyles = createStyles(({ css, token }) => ({
chart: css`
svg {
outline: none;
}
`,
emphasis: css`
overflow: hidden;
text-overflow: ellipsis;
Expand Down
11 changes: 8 additions & 3 deletions src/SparkChart/SparkAreaChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import { Flexbox } from 'react-layout-kit';
import { Area, AreaChart as ReChartsAreaChart, ResponsiveContainer, XAxis, YAxis } from 'recharts';
import { AxisDomain } from 'recharts/types/util/types';

import { useStyles } from '@/AreaChart/styles';
import BaseSparkChartProps from '@/common/BaseSparkChartProps';
import NoData from '@/common/NoData';
import { constructCategoryColors, getYAxisDomain } from '@/common/utils';
import { useThemeColorRange } from '@/hooks/useThemeColorRange';
import { CurveType } from '@/types/charts';

import { useStyles } from './styles';

export interface SparkAreaChartProps extends BaseSparkChartProps {
connectNulls?: boolean;
curveType?: CurveType;
Expand All @@ -23,7 +24,7 @@ export interface SparkAreaChartProps extends BaseSparkChartProps {
}

const SparkAreaChart = forwardRef<HTMLDivElement, SparkAreaChartProps>((props, ref) => {
const { cx, theme } = useStyles();
const { cx, theme, styles } = useStyles();
const themeColorRange = useThemeColorRange();

const {
Expand Down Expand Up @@ -65,7 +66,11 @@ const SparkAreaChart = forwardRef<HTMLDivElement, SparkAreaChartProps>((props, r
>
<ResponsiveContainer>
{data?.length ? (
<ReChartsAreaChart data={data} margin={{ bottom: 1, left: 1, right: 1, top: 1 }}>
<ReChartsAreaChart
className={styles.chart}
data={data}
margin={{ bottom: 1, left: 1, right: 1, top: 1 }}
>
<YAxis domain={yAxisDomain as AxisDomain} hide />
<XAxis dataKey={index} hide />
{categories.map((category) => {
Expand Down
6 changes: 4 additions & 2 deletions src/SparkChart/SparkBarChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ import { Flexbox } from 'react-layout-kit';
import { Bar, BarChart as ReChartsBarChart, ResponsiveContainer, XAxis, YAxis } from 'recharts';
import { AxisDomain } from 'recharts/types/util/types';

import { useStyles } from '@/BarChart/styles';
import BaseSparkChartProps from '@/common/BaseSparkChartProps';
import NoData from '@/common/NoData';
import { constructCategoryColors, getYAxisDomain } from '@/common/utils';
import { useThemeColorRange } from '@/hooks/useThemeColorRange';

import { useStyles } from './styles';

export interface SparkBarChartProps extends BaseSparkChartProps {
loading?: boolean;
relative?: boolean;
stack?: boolean;
}

const SparkBarChart = forwardRef<HTMLDivElement, SparkBarChartProps>((props, ref) => {
const { cx, theme } = useStyles();
const { cx, theme, styles } = useStyles();
const themeColorRange = useThemeColorRange();
const {
data = [],
Expand Down Expand Up @@ -60,6 +61,7 @@ const SparkBarChart = forwardRef<HTMLDivElement, SparkBarChartProps>((props, ref
<ResponsiveContainer>
{data?.length ? (
<ReChartsBarChart
className={styles.chart}
data={data}
margin={{ bottom: 0, left: -1.5, right: -1.5, top: 0 }}
stackOffset={relative ? 'expand' : 'none'}
Expand Down
11 changes: 8 additions & 3 deletions src/SparkChart/SparkLineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@ import { Flexbox } from 'react-layout-kit';
import { Line, LineChart as ReChartsLineChart, ResponsiveContainer, XAxis, YAxis } from 'recharts';
import { AxisDomain } from 'recharts/types/util/types';

import { useStyles } from '@/LineChart/styles';
import BaseSparkChartProps from '@/common/BaseSparkChartProps';
import NoData from '@/common/NoData';
import { constructCategoryColors, getYAxisDomain } from '@/common/utils';
import { useThemeColorRange } from '@/hooks/useThemeColorRange';
import { CurveType } from '@/types/charts';

import { useStyles } from './styles';

export interface SparkLineChartProps extends BaseSparkChartProps {
connectNulls?: boolean;
curveType?: CurveType;
loading?: boolean;
}

const SparkLineChart = forwardRef<HTMLDivElement, SparkLineChartProps>((props, ref) => {
const { cx, theme } = useStyles();
const { cx, theme, styles } = useStyles();
const themeColorRange = useThemeColorRange();
const {
data = [],
Expand Down Expand Up @@ -60,7 +61,11 @@ const SparkLineChart = forwardRef<HTMLDivElement, SparkLineChartProps>((props, r
>
<ResponsiveContainer>
{data?.length ? (
<ReChartsLineChart data={data} margin={{ bottom: 1, left: 1, right: 1, top: 1 }}>
<ReChartsLineChart
className={styles.chart}
data={data}
margin={{ bottom: 1, left: 1, right: 1, top: 1 }}
>
<YAxis domain={yAxisDomain as AxisDomain} hide />
<XAxis dataKey={index} hide />
{categories.map((category) => (
Expand Down
9 changes: 9 additions & 0 deletions src/SparkChart/styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createStyles } from 'antd-style';

export const useStyles = createStyles(({ css }) => ({
chart: css`
svg {
outline: none;
}
`,
}));

0 comments on commit cd64691

Please sign in to comment.