-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from pyyding/kp-scatter-chart
Scatter chart component
- Loading branch information
Showing
36 changed files
with
589 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
export { Area, type AreaProps } from "recharts"; | ||
import { Area, type AreaProps as RechartsAreaProps } from "recharts"; | ||
|
||
type AreaProps = Omit<RechartsAreaProps, "ref">; | ||
|
||
export { Area, type AreaProps }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { type ChartCellProps } from "."; | ||
|
||
export const scatterChartCellProps: ChartCellProps = { | ||
width: "20px", | ||
height: "20px", | ||
style: { opacity: 1 }, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Cell as ChartCell, type CellProps as ChartCellProps } from "recharts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { cx } from "../common/utils"; | ||
|
||
type ContentProps = React.HTMLProps<HTMLDivElement>; | ||
|
||
export const ChartTooltipContent: React.FC<ContentProps> = ({ | ||
children, | ||
className, | ||
...rest | ||
}) => { | ||
return ( | ||
<div | ||
className={cx("bg-gray-900 px-3 py-2 rounded-md", className)} | ||
{...rest} | ||
> | ||
{children} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,42 @@ | ||
import { TooltipProps } from "recharts"; | ||
import { TooltipProps as RechartsTooltipProps } from "recharts"; | ||
import { ChartTooltipValue } from "./Value"; | ||
import { ChartTooltipTitle } from "./Title"; | ||
import { ChartTooltipFooter } from "./Footer"; | ||
import { Payload as RechartsTooltipPayload } from "recharts/types/component/DefaultTooltipContent"; | ||
import { ChartTooltipContent } from "./Content.tsx"; | ||
|
||
type TypeFromArray<T> = T extends Array<infer K> ? K : never; | ||
export type TooltipFullPayload = RechartsTooltipPayload<any, any>; | ||
type TooltipProps = RechartsTooltipProps<any, any>; | ||
type Payload = TooltipFullPayload["payload"]; | ||
|
||
interface DefaultTooltipProps extends TooltipProps<any, any> { | ||
interface DefaultTooltipProps extends TooltipProps { | ||
label: string; | ||
valueFormatter: (payload: TypeFromArray<any[]>) => string; | ||
footerFormatter?: (payload: TypeFromArray<any[]>) => string; | ||
valueFormatter: (payload: Payload) => string; | ||
footerFormatter?: (payload: Payload) => string; | ||
active?: boolean; | ||
} | ||
|
||
export const DefaultTooltip: React.FC<DefaultTooltipProps> = ({ label, valueFormatter, footerFormatter, active, payload }) => { | ||
export const DefaultTooltip: React.FC<DefaultTooltipProps> = ({ | ||
label, | ||
valueFormatter, | ||
footerFormatter, | ||
active, | ||
payload, | ||
}) => { | ||
const firstPayload = payload?.[0]; | ||
if (!active || !firstPayload) return null; | ||
|
||
return ( | ||
<div className="bg-gray-900 px-3 py-2 rounded-md"> | ||
<ChartTooltipContent> | ||
<ChartTooltipTitle>{label}</ChartTooltipTitle> | ||
{payload | ||
?.map((p, index) => ( | ||
<div key={index}> | ||
<ChartTooltipValue value={valueFormatter(p.payload)}/> | ||
{footerFormatter && <ChartTooltipFooter subtitle={footerFormatter(p.payload)} />} | ||
</div> | ||
))} | ||
</div> | ||
{payload?.map((p, index) => ( | ||
<div key={index}> | ||
<ChartTooltipValue>{valueFormatter(p.payload)}</ChartTooltipValue> | ||
{footerFormatter && ( | ||
<ChartTooltipFooter subtitle={footerFormatter(p.payload)} /> | ||
)} | ||
</div> | ||
))} | ||
</ChartTooltipContent> | ||
); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
export const ChartTooltipTitle: React.FC<{ children: React.ReactNode }> = ({ children }) => ( | ||
<p className="text-xs text-gray-400">{children}</p> | ||
import { cx } from "../common/utils.ts"; | ||
|
||
type ChartTooltipTitleProps = React.HTMLProps<HTMLParagraphElement>; | ||
|
||
export const ChartTooltipTitle: React.FC<ChartTooltipTitleProps> = ({ | ||
children, | ||
className, | ||
...rest | ||
}) => ( | ||
<p className={cx(className, "text-xs text-gray-400")} {...rest}> | ||
{children} | ||
</p> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
export const ChartTooltipValue: React.FC<{ value: string }> = ({ value }) => ( | ||
<p className="text-sm text-white">{value}</p> | ||
); | ||
import { cx } from "../common/utils.ts"; | ||
|
||
type ChartTooltipValueProps = React.HTMLProps<HTMLParagraphElement>; | ||
|
||
export const ChartTooltipValue: React.FC<ChartTooltipValueProps> = ({ | ||
children, | ||
className, | ||
...rest | ||
}) => ( | ||
<p className={cx(className, "text-sm text-white gap-1 flex")} {...rest}> | ||
{children} | ||
</p> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TooltipProps } from "recharts"; | ||
|
||
const cursor = { fill: "#d1d5db", opacity: "0.15" }; | ||
|
||
export const defaultTooltipProps: TooltipProps<any, any> = { | ||
cursor, | ||
position: { y: 0 }, | ||
isAnimationActive: false, | ||
wrapperStyle: { outline: "none" }, | ||
}; | ||
|
||
export const scatterChartTooltipProps: TooltipProps<any, any> = { | ||
cursor, | ||
isAnimationActive: false, | ||
wrapperStyle: { outline: "none" }, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,19 @@ | ||
|
||
import { Tooltip as RechartsTooltip } from "recharts"; | ||
import { | ||
Tooltip as ChartTooltip, | ||
TooltipProps as ChartTooltipProps, | ||
} from "recharts"; | ||
import { ChartTooltipValue } from "./Value"; | ||
import { ChartTooltipTitle } from "./Title"; | ||
import { ChartTooltipFooter } from "./Footer"; | ||
import { DefaultTooltip } from "./DefaultTooltip"; | ||
import { ChartTooltipContent } from "./Content"; | ||
|
||
export interface TooltipProps { | ||
label?: string; | ||
active?: boolean; | ||
payload?: any[]; | ||
valueFormatter: (payload: any) => string; | ||
footerFormatter?: (payload: any) => string; | ||
} | ||
|
||
class ChartTooltip extends RechartsTooltip<any, any> { | ||
static defaultProps = { | ||
...RechartsTooltip.defaultProps, | ||
cursor: { fill: "#d1d5db", opacity: "0.15" } as any, | ||
position: { y: 0 }, | ||
isAnimationActive: false, | ||
wrapperStyle: { outline: "none" }, | ||
}; | ||
} | ||
|
||
|
||
export { ChartTooltip, ChartTooltipTitle, ChartTooltipValue, ChartTooltipFooter, DefaultTooltip }; | ||
export { | ||
ChartTooltip, | ||
ChartTooltipTitle, | ||
ChartTooltipValue, | ||
ChartTooltipFooter, | ||
DefaultTooltip, | ||
ChartTooltipContent, | ||
type ChartTooltipProps, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export const ScatterShapeCircle = (props: unknown): React.ReactElement => { | ||
const svgProps = props as React.SVGProps<SVGCircleElement>; | ||
return ( | ||
<circle | ||
r="10" | ||
cx={svgProps.cx} | ||
cy={svgProps.cy} | ||
fill={svgProps.fill} | ||
height={svgProps.height} | ||
name={svgProps.name} | ||
style={svgProps.style} | ||
width={svgProps.width} | ||
x={svgProps.x} | ||
y={svgProps.y} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { type ScatterProps } from "."; | ||
import { ScatterShapeCircle } from "./ScatterShapeCircle.tsx"; | ||
|
||
export const defaultScatterProps: ScatterProps = { | ||
shape: ScatterShapeCircle, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Scatter, ScatterProps as RechartsScatterProps } from "recharts"; | ||
|
||
type ScatterProps = Omit<RechartsScatterProps, "ref">; | ||
|
||
export { Scatter, type ScatterProps }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { | ||
ScatterChart as RechartsScatterChart, | ||
ResponsiveContainer as RechartsResponsiveContainer, | ||
} from "recharts"; | ||
|
||
import { cx } from "../common/utils"; | ||
import React from "react"; | ||
|
||
type ScatterChartProps = React.ComponentProps<typeof RechartsScatterChart>; | ||
|
||
export type { ScatterChartProps }; | ||
|
||
export const ScatterChart: React.FC<ScatterChartProps> = ({ | ||
className, | ||
children, | ||
...rest | ||
}) => ( | ||
<div | ||
data-testid="scatter-chart-wrapper" | ||
className={cx(className, "w-full h-80")} | ||
> | ||
<RechartsResponsiveContainer className="w-full h-full"> | ||
<RechartsScatterChart {...rest}>{children}</RechartsScatterChart> | ||
</RechartsResponsiveContainer> | ||
</div> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { TickText } from "../TickText"; | ||
import { Payload } from "recharts/types/component/DefaultTooltipContent"; | ||
|
||
type ScatterChartTickProps = { | ||
x: number; | ||
y: number; | ||
payload: Payload<number, string>; | ||
unit?: string; | ||
dx?: number; | ||
dy?: number; | ||
} & React.SVGProps<SVGTextElement>; | ||
|
||
export const ScatterChartTick: React.FC<ScatterChartTickProps> = ({ | ||
x, | ||
y, | ||
dx = 0, | ||
dy = 0, | ||
unit, | ||
...rest | ||
}: ScatterChartTickProps) => ( | ||
<TickText | ||
x={x + dx} | ||
y={y + dy} | ||
fill={rest.fill} | ||
height={rest.height} | ||
name={rest.name} | ||
orientation={rest.orientation} | ||
stroke={rest.stroke} | ||
textAnchor={rest.textAnchor} | ||
width={rest.width} | ||
> | ||
{rest.payload.value} | ||
{unit} | ||
</TickText> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React from "react"; | ||
import { cx } from "../common/utils.ts"; | ||
|
||
export type TickTextProps = React.SVGProps<SVGTextElement>; | ||
|
||
export const TickText: React.FC<TickTextProps> = ({ | ||
children, | ||
className, | ||
...rest | ||
}) => ( | ||
<text {...rest} className={cx("text-xs text-gray-800", className)}> | ||
{children} | ||
</text> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,18 @@ | ||
import { XAxisProps } from "recharts"; | ||
|
||
export const PERCENTAGE_TICKS = [0, 25, 50, 75, 100]; | ||
|
||
export const defaultXAxisProps: XAxisProps = { | ||
interval: "preserveStartEnd", | ||
className: "text-xs fill-gray-600", | ||
minTickGap: 5, | ||
fill: "", | ||
stroke: "", | ||
}; | ||
|
||
export const scatterXAxisProps: XAxisProps = { | ||
axisLine: { className: "stroke-gray-300" }, | ||
tickLine: false, | ||
type: "number", | ||
ticks: PERCENTAGE_TICKS, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import { XAxisProps } from "recharts"; | ||
import { XAxis, XAxisProps as RechartsXAxisProps } from "recharts"; | ||
|
||
export { XAxis } from "recharts"; | ||
|
||
export type { XAxisProps }; | ||
type XAxisProps = Omit<RechartsXAxisProps, "ref">; | ||
|
||
export { XAxis, type XAxisProps }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import { YAxisProps } from "recharts"; | ||
import { YAxis, type YAxisProps as RechartsYAxisProps } from "recharts"; | ||
|
||
export { YAxis } from "recharts"; | ||
|
||
export type { YAxisProps }; | ||
type YAxisProps = Omit<RechartsYAxisProps, "ref">; | ||
|
||
export { YAxis, type YAxisProps }; |
Oops, something went wrong.