MCPcopy
hub / github.com/chakra-ui/chakra-ui / useChart

Function useChart

packages/charts/src/use-chart.ts:34–173  ·  view source on GitHub ↗
(props: UseChartProps<T>)

Source from the content-addressed store, hash-verified

32 | ((props: { min: number; max: number }) => [number, number])
33
34export function useChart<T = any>(props: UseChartProps<T>) {
35 const { data, series = [], sort } = props
36
37 const id = React.useId()
38
39 const [highlightedSeries, setHighlightedSeries] = React.useState<
40 string | null
41 >(null)
42 const isHighlightedSeries = (name: string | undefined) =>
43 highlightedSeries === name
44
45 const env = useLocaleContext()
46 const sys = useChakraContext()
47
48 const color = (key: ChartColor | undefined) => sys.token(`colors.${key}`, key)
49 const size = (key: ChartSize | undefined) => sys.token(`sizes.${key}`, key)
50 const spacing = (key: ChartSpacing | undefined) =>
51 sys.token(`spacing.${key}`, key)
52
53 const key = <K extends ItemDataKey<T>>(prop: K | undefined): K =>
54 prop ?? ("value" as K)
55
56 const formatNumber = React.useCallback(
57 (options?: Intl.NumberFormatOptions) => {
58 const formatter = new Intl.NumberFormat(env.locale, options)
59 return (value: number) => formatter.format(value)
60 },
61 [env.locale],
62 )
63
64 const formatDate = React.useCallback(
65 (options?: Intl.DateTimeFormatOptions) => {
66 return (value: string) =>
67 new Date(value).toLocaleDateString(env.locale, options)
68 },
69 [env.locale],
70 )
71
72 const getSeries = (item: unknown) => {
73 if (!isObject(item)) return
74 const result = series.find((s) => {
75 return (
76 s.name === item.name ||
77 s.name === getProp(item.payload, "name") ||
78 s.name === item.dataKey ||
79 s.name === getProp(item.payload, "dataKey")
80 )
81 }) || { color: undefined }
82
83 result.color ||= getProp(item.payload, "color")
84 result.label ||=
85 result.name?.toLocaleString() || getProp(item.payload, "name")
86
87 return result
88 }
89
90 const getTotal = (key: keyof T) => {
91 return data.reduce((acc, d) => acc + Number(d[key]), 0)

Callers 15

BarChartWithBarLabelFunction · 0.90
SparklineBasicFunction · 0.90
BarChartHistogramFunction · 0.90
ScatterChartBasicFunction · 0.90
LineChartWithNullsFunction · 0.90
BarChartRangeFunction · 0.90
BarChartMultipleFunction · 0.90
RadarChartWithTooltipFunction · 0.90
PieChartWithLabelOutsideFunction · 0.90
DonutChartWithOtherLabelFunction · 0.90
BarChartRoundedFunction · 0.90

Calls 1

sortMethod · 0.80

Tested by

no test coverage detected