Skip to content

Commit 0ae6569

Browse files
committed
feat: Implement date formatting for candlestick chart, adjust chart layout
1 parent ed80f29 commit 0ae6569

File tree

1 file changed

+22
-54
lines changed

1 file changed

+22
-54
lines changed

frontend/src/components/valuecell/charts/candlestick-chart.tsx

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { CanvasRenderer } from "echarts/renderers";
1515
import type { EChartsOption } from "echarts/types/dist/shared";
1616
import { useEffect, useMemo, useRef } from "react";
1717
import { useChartResize } from "@/hooks/use-chart-resize";
18+
import { TIME_FORMATS, TimeUtils } from "@/lib/time";
1819
import { cn } from "@/lib/utils";
1920
import { useStockColors } from "@/store/settings-store";
2021

@@ -46,6 +47,7 @@ interface CandlestickChartProps {
4647
className?: string;
4748
loading?: boolean;
4849
showVolume?: boolean;
50+
dateFormat?: string;
4951
}
5052

5153
function CandlestickChart({
@@ -55,6 +57,7 @@ function CandlestickChart({
5557
className,
5658
loading,
5759
showVolume = true,
60+
dateFormat = TIME_FORMATS.DATETIME_SHORT,
5861
}: CandlestickChartProps) {
5962
const chartRef = useRef<HTMLDivElement>(null);
6063
const chartInstance = useRef<ECharts | null>(null);
@@ -63,7 +66,9 @@ function CandlestickChart({
6366
const option: EChartsOption = useMemo(() => {
6467
if (!data || data.length === 0) return {};
6568

66-
const dates = data.map((item) => item.time);
69+
const dates = data.map((item) =>
70+
TimeUtils.formatUTC(item.time, dateFormat),
71+
);
6772
const ohlcData = data.map((item) => [
6873
item.open,
6974
item.close,
@@ -84,6 +89,7 @@ function CandlestickChart({
8489
name: "K-Line",
8590
type: "candlestick",
8691
data: ohlcData,
92+
clip: true,
8793
itemStyle: {
8894
color: stockColors.positive, // up candle fill
8995
color0: stockColors.negative, // down candle fill
@@ -110,36 +116,21 @@ function CandlestickChart({
110116
});
111117
}
112118

113-
const grids: Array<{
114-
left: string;
115-
right: string;
116-
height: string;
117-
top?: string;
118-
}> = [
119+
const grids: EChartsOption["grid"] = [
119120
{
120-
left: "10%",
121-
right: "8%",
121+
left: "4%",
122+
right: "4%",
123+
top: "3%",
122124
height: mainGridHeight,
125+
containLabel: true,
123126
},
124127
];
125128

126-
const xAxes: Array<{
127-
type: "category";
128-
data: string[];
129-
boundaryGap: boolean;
130-
axisLine: { onZero: boolean };
131-
splitLine: { show: boolean };
132-
min: string;
133-
max: string;
134-
axisPointer?: { z: number };
135-
gridIndex?: number;
136-
axisTick?: { show: boolean };
137-
axisLabel?: { show: boolean };
138-
}> = [
129+
const xAxes: EChartsOption["xAxis"] = [
139130
{
140-
type: "category",
131+
type: "category" as const,
141132
data: dates,
142-
boundaryGap: false,
133+
boundaryGap: true,
143134
axisLine: { onZero: false },
144135
splitLine: { show: false },
145136
min: "dataMin",
@@ -148,16 +139,7 @@ function CandlestickChart({
148139
},
149140
];
150141

151-
const yAxes: Array<{
152-
scale: boolean;
153-
splitArea?: { show: boolean };
154-
gridIndex?: number;
155-
splitNumber?: number;
156-
axisLabel?: { show: boolean };
157-
axisLine?: { show: boolean };
158-
axisTick?: { show: boolean };
159-
splitLine?: { show: boolean };
160-
}> = [
142+
const yAxes: EChartsOption["yAxis"] = [
161143
{
162144
scale: true,
163145
splitArea: { show: true },
@@ -166,17 +148,18 @@ function CandlestickChart({
166148

167149
if (showVolume) {
168150
grids.push({
169-
left: "10%",
170-
right: "8%",
151+
left: "8%",
152+
right: "6%",
171153
top: volumeGridTop,
172154
height: "16%",
155+
containLabel: true,
173156
});
174157

175158
xAxes.push({
176-
type: "category",
159+
type: "category" as const,
177160
gridIndex: 1,
178161
data: dates,
179-
boundaryGap: false,
162+
boundaryGap: true,
180163
axisLine: { onZero: false },
181164
axisTick: { show: false },
182165
splitLine: { show: false },
@@ -205,17 +188,6 @@ function CandlestickChart({
205188
borderColor: "#ccc",
206189
padding: 10,
207190
textStyle: { color: "#000" },
208-
position: (
209-
pos: number[],
210-
_params: unknown,
211-
_el: unknown,
212-
_rect: unknown,
213-
size: { viewSize: number[] },
214-
) => {
215-
const obj: Record<string, number> = { top: 10 };
216-
obj[["left", "right"][+(pos[0] < size.viewSize[0] / 2)]] = 30;
217-
return obj;
218-
},
219191
},
220192
axisPointer: {
221193
link: [{ xAxisIndex: "all" }],
@@ -228,21 +200,17 @@ function CandlestickChart({
228200
{
229201
type: "inside",
230202
xAxisIndex: showVolume ? [0, 1] : [0],
231-
start: 50,
232-
end: 100,
233203
},
234204
{
235205
show: true,
236206
xAxisIndex: showVolume ? [0, 1] : [0],
237207
type: "slider",
238208
top: "85%",
239-
start: 50,
240-
end: 100,
241209
},
242210
],
243211
series,
244212
};
245-
}, [data, stockColors, showVolume]);
213+
}, [data, stockColors, showVolume, dateFormat]);
246214

247215
useChartResize(chartInstance);
248216

0 commit comments

Comments
 (0)