-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c37eab
commit ba281ac
Showing
3 changed files
with
189 additions
and
12 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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React from 'react'; | ||
import { PieChart, Pie, Label } from 'recharts'; | ||
import { | ||
ChartConfig, | ||
ChartContainer, | ||
ChartTooltip, | ||
ChartTooltipContent, | ||
} from '../ui/chart'; | ||
import { sumBy } from 'lodash-es'; | ||
import { useTranslation } from '@i18next-toolkit/react'; | ||
|
||
export const SimplePieChart: React.FC<{ | ||
className?: string; | ||
data: { label: string; count: number }[]; | ||
chartConfig: ChartConfig; | ||
}> = React.memo((props) => { | ||
const { className, data, chartConfig } = props; | ||
const { t } = useTranslation(); | ||
|
||
const totalCount = React.useMemo(() => { | ||
return sumBy(data, 'count'); | ||
}, []); | ||
|
||
return ( | ||
<ChartContainer className={className} config={chartConfig}> | ||
<PieChart> | ||
<ChartTooltip | ||
cursor={false} | ||
content={<ChartTooltipContent hideLabel />} | ||
/> | ||
<Pie | ||
data={data} | ||
dataKey="count" | ||
nameKey="label" | ||
innerRadius={'50%'} | ||
strokeWidth={5} | ||
> | ||
<Label | ||
content={({ viewBox }) => { | ||
if (viewBox && 'cx' in viewBox && 'cy' in viewBox) { | ||
return ( | ||
<text | ||
x={viewBox.cx} | ||
y={viewBox.cy} | ||
textAnchor="middle" | ||
dominantBaseline="middle" | ||
> | ||
<tspan | ||
x={viewBox.cx} | ||
y={viewBox.cy} | ||
className="fill-foreground text-3xl font-bold" | ||
> | ||
{totalCount.toLocaleString()} | ||
</tspan> | ||
<tspan | ||
x={viewBox.cx} | ||
y={(viewBox.cy || 0) + 24} | ||
className="fill-muted-foreground" | ||
> | ||
{t('Count')} | ||
</tspan> | ||
</text> | ||
); | ||
} | ||
}} | ||
/> | ||
</Pie> | ||
</PieChart> | ||
</ChartContainer> | ||
); | ||
}); | ||
SimplePieChart.displayName = 'SimplePieChart'; |
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,76 @@ | ||
import React, { useMemo, useState } from 'react'; | ||
import { SimplePieChart } from '../chart/SimplePieChart'; | ||
import { trpc } from '@/api/trpc'; | ||
import { useCurrentWorkspaceId } from '@/store/user'; | ||
import { reverse, sortBy, sumBy, take } from 'lodash-es'; | ||
import { ChartConfig } from '../ui/chart'; | ||
import { Checkbox } from '../ui/checkbox'; | ||
import { useTranslation } from '@i18next-toolkit/react'; | ||
|
||
interface SurveyCategoryChartProps { | ||
surveyId: string; | ||
} | ||
export const SurveyCategoryChart: React.FC<SurveyCategoryChartProps> = | ||
React.memo((props) => { | ||
const workspaceId = useCurrentWorkspaceId(); | ||
const { t } = useTranslation(); | ||
const [hideUncategorized, setHideUncategorized] = useState(false); | ||
|
||
const { data: allCategory = [] } = trpc.survey.aiCategoryList.useQuery({ | ||
workspaceId, | ||
surveyId: props.surveyId, | ||
}); | ||
|
||
const { data, chartConfig } = useMemo(() => { | ||
const ordered = reverse( | ||
sortBy( | ||
hideUncategorized | ||
? allCategory.filter((item) => item.name) | ||
: allCategory, | ||
(d) => d.count | ||
) | ||
); | ||
const category = take(ordered, 4); | ||
|
||
if (ordered.length >= 5) { | ||
category[4] = { | ||
name: 'Other', | ||
count: sumBy(ordered, 'count') - sumBy(category, 'count'), | ||
}; | ||
} | ||
|
||
const chartConfig: ChartConfig = {}; | ||
category.forEach((c, i) => { | ||
const name = c.name ? c.name : 'Uncategorized'; | ||
chartConfig[`category-${i}`] = { | ||
label: name, | ||
color: `hsl(var(--chart-${i + 1}))`, | ||
}; | ||
}); | ||
|
||
return { | ||
data: category.map((c, i) => ({ | ||
label: c.name ? c.name : 'Uncategorized', | ||
count: c.count, | ||
fill: `var(--color-category-${i})`, | ||
})), | ||
chartConfig, | ||
}; | ||
}, [allCategory, hideUncategorized]); | ||
|
||
return ( | ||
<div> | ||
<SimplePieChart data={data} chartConfig={chartConfig} /> | ||
<div className="flex items-center gap-1"> | ||
<Checkbox | ||
checked={hideUncategorized} | ||
onCheckedChange={(checked) => | ||
setHideUncategorized(checked === true) | ||
} | ||
/> | ||
<span>{t('Hide Uncategorized')}</span> | ||
</div> | ||
</div> | ||
); | ||
}); | ||
SurveyCategoryChart.displayName = 'SurveyCategoryChart'; |
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