Skip to content

Commit 5a5579a

Browse files
committed
feat: add stack chart type
1 parent ca0601d commit 5a5579a

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

src/client/components/chart/TimeEventChart.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
} from '../ui/chart';
2121
import { useStrokeDasharray } from '@/hooks/useStrokeDasharray';
2222

23+
export type TimeEventChartType = 'area' | 'stack';
24+
2325
const defaultChartConfig: ChartConfig = {
2426
pv: {
2527
label: 'PV',
@@ -35,11 +37,13 @@ export const TimeEventChart: React.FC<{
3537
unit: DateUnit;
3638
chartConfig?: ChartConfig;
3739
drawGradientArea?: boolean;
40+
chartType?: TimeEventChartType;
3841
}> = React.memo((props) => {
3942
const {
4043
className,
4144
drawGradientArea = true,
4245
chartConfig = defaultChartConfig,
46+
chartType = 'area',
4347
} = props;
4448
const { colors } = useTheme();
4549
const [calcStrokeDasharray, strokes] = useStrokeDasharray({});
@@ -53,6 +57,8 @@ export const TimeEventChart: React.FC<{
5357
Object.keys(chartConfig)
5458
);
5559

60+
const stacked = chartType === 'stack';
61+
5662
return (
5763
<ChartContainer className={className} config={chartConfig}>
5864
<AreaChart
@@ -123,6 +129,7 @@ export const TimeEventChart: React.FC<{
123129
hide={!selectedItem.includes(key)}
124130
type="monotone"
125131
dataKey={key}
132+
stackId={stacked ? '1' : undefined}
126133
stroke={
127134
chartConfig[key].color ??
128135
(colors.chart as any)[key] ??

src/client/components/insights/ChartRender.tsx

+11-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import { useTranslation } from '@i18next-toolkit/react';
2020
import { DelayRender } from '../DelayRender';
2121
import { SearchLoadingView } from '../loading/Searching';
2222
import { get, groupBy, merge, omit, values } from 'lodash-es';
23+
import { ChartTypeSelection } from './ChartTypeSelection';
24+
import { useWatch } from '@/hooks/useWatch';
2325

2426
interface ChartRenderProps {
2527
insightId: string;
@@ -135,6 +137,7 @@ export const ChartRender: React.FC<ChartRenderProps> = React.memo((props) => {
135137
unit={dateUnit}
136138
chartConfig={chartConfig}
137139
drawGradientArea={false}
140+
chartType={chartType}
138141
/>
139142
</div>
140143
</ResizablePanel>
@@ -167,7 +170,14 @@ export const ChartRender: React.FC<ChartRenderProps> = React.memo((props) => {
167170
}}
168171
/>
169172

170-
<DateUnitSelection value={dateUnit} onChange={setDateUnit} />
173+
<div className="flex gap-2">
174+
<DateUnitSelection
175+
allowMinute={allowMinute}
176+
value={dateUnit}
177+
onChange={setDateUnit}
178+
/>
179+
<ChartTypeSelection value={chartType} onChange={setChartType} />
180+
</div>
171181
</div>
172182

173183
{mainEl}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from 'react';
2+
import {
3+
Select,
4+
SelectContent,
5+
SelectItem,
6+
SelectTrigger,
7+
SelectValue,
8+
} from '../ui/select';
9+
import { TimeEventChartType } from '../chart/TimeEventChart';
10+
import { useTranslation } from '@i18next-toolkit/react';
11+
import { LuChartArea, LuChartLine } from 'react-icons/lu';
12+
13+
interface ChartTypeSelectionProps {
14+
value: TimeEventChartType;
15+
onChange: (val: TimeEventChartType) => void;
16+
}
17+
export const ChartTypeSelection: React.FC<ChartTypeSelectionProps> = React.memo(
18+
(props) => {
19+
const { value, onChange } = props;
20+
const { t } = useTranslation();
21+
22+
return (
23+
<Select value={value} onValueChange={onChange}>
24+
<SelectTrigger className="w-[60px]">
25+
<LuChartArea />
26+
</SelectTrigger>
27+
<SelectContent align="end">
28+
<SelectItem value="area" className="flex gap-1">
29+
{t('Area')}
30+
</SelectItem>
31+
<SelectItem value="stack" className="flex gap-1">
32+
{t('Stack')}
33+
</SelectItem>
34+
</SelectContent>
35+
</Select>
36+
);
37+
}
38+
);
39+
ChartTypeSelection.displayName = 'ChartTypeSelection';

0 commit comments

Comments
 (0)