Skip to content

Commit ff007ab

Browse files
committed
style: ปรับรูปแบบการแสดงผล
1 parent 6467e72 commit ff007ab

File tree

1 file changed

+64
-22
lines changed

1 file changed

+64
-22
lines changed

client/my-app/src/components/features/analytics/Chart/EventCount.tsx

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@ type EventCountChartProps = {
1414

1515
type ApexSeries = NonNullable<ApexOptions["series"]>;
1616

17-
const AXIS_LABEL_STYLE = {
18-
colors: "#6b7280",
19-
fontSize: "12px",
20-
};
21-
22-
// ===== CONFIG =====
23-
const MAX_VALUE = 300; // เปลี่ยน max แค่ตรงนี้ ที่เหลือจะตามให้เอง
24-
2517
// ===== DATA =====
2618
const EVENTS = [
2719
"Event 1",
@@ -37,12 +29,47 @@ const EVENTS = [
3729
"Event 11",
3830
"Event 12",
3931
"Event 13",
32+
"Event 14",
33+
"Event 15",
34+
"Event 16",
35+
];
36+
37+
const EVENT_COUNTS = [
38+
80, 35, 70, 65, 20, 55, 75, 95, 60, 80, 30, 15, 45, 45, 90, 50,
4039
];
4140

42-
const EVENT_COUNTS = [80, 35, 70, 65, 20, 55, 75, 95, 60, 80, 30, 15, 267];
41+
// ===== Scroll / height settings =====
42+
const MAX_VISIBLE_EVENTS = 10; // จำนวนแถวที่โชว์ก่อนจะเริ่ม scroll
43+
const ROW_HEIGHT = 38; // px ต่อแถว
44+
const NEED_SCROLL = EVENTS.length > MAX_VISIBLE_EVENTS;
45+
46+
const computedMaxHeight = NEED_SCROLL
47+
? MAX_VISIBLE_EVENTS * ROW_HEIGHT
48+
: undefined;
49+
50+
const fullChartHeight = Math.max(EVENTS.length * ROW_HEIGHT, 120);
4351

44-
// ===== OPTIONS =====
45-
const chartOptions: ApexOptions = {
52+
const AXIS_LABEL_STYLE = {
53+
colors: "#6b7280",
54+
fontSize: "12px",
55+
};
56+
57+
// ===== MAX_VALUE (อิงจาก data) =====
58+
// หา max จาก data แล้วเผื่อ 10% เพื่อไม่ให้แท่งแตะขอบแกนพอดี
59+
const maxData = Math.max(...EVENT_COUNTS, 0);
60+
61+
// ถ้า maxData = 0 จะกันหาร 0/undefined
62+
const PADDING_RATIO = 1.1; // เผื่อ 10%
63+
const roundTo = 10; // ปัดเป็นเลขกลมตามหน่วยนี้ (10 => 270 -> 280)
64+
const padded = Math.ceil(maxData * PADDING_RATIO || 1);
65+
const MAX_VALUE = Math.max(
66+
padded % roundTo === 0 ? padded : Math.ceil(padded / roundTo) * roundTo,
67+
roundTo
68+
);
69+
const TOTAL_EVENTS = EVENT_COUNTS.reduce((sum, v) => sum + v, 0);
70+
71+
// ===== CHART OPTIONS =====
72+
const chartOptions = (maxValue: number): ApexOptions => ({
4673
chart: {
4774
type: "bar",
4875
stacked: true,
@@ -54,8 +81,8 @@ const chartOptions: ApexOptions = {
5481
plotOptions: {
5582
bar: {
5683
horizontal: true,
57-
barHeight: "55%",
58-
borderRadius: 0,
84+
barHeight: "60%",
85+
borderRadius: 4,
5986
},
6087
},
6188

@@ -74,7 +101,7 @@ const chartOptions: ApexOptions = {
74101
position: "top",
75102
categories: EVENTS,
76103
min: 0,
77-
max: MAX_VALUE, // ใช้ MAX_VALUE ตรงนี้
104+
max: maxValue, // ใช้ MAX_VALUE ที่คำนวณแล้ว
78105
tickAmount: 10,
79106
labels: {
80107
style: AXIS_LABEL_STYLE,
@@ -99,9 +126,9 @@ const chartOptions: ApexOptions = {
99126
legend: {
100127
show: false,
101128
},
102-
};
129+
});
103130

104-
// series 1 = ม่วง (ค่าจริง) , series 2 = เทา (ส่วนที่เหลือถึง MAX_VALUE)
131+
// ===== SERIES (ค่าจริง + remaining) =====
105132
const chartSeries: ApexSeries = [
106133
{
107134
name: "Event Count",
@@ -110,36 +137,51 @@ const chartSeries: ApexSeries = [
110137
},
111138
{
112139
name: "Remaining",
113-
data: EVENT_COUNTS.map((v) => MAX_VALUE - v), // เปลี่ยนจาก 100 → MAX_VALUE
140+
data: EVENT_COUNTS.map((v) => Math.max(0, MAX_VALUE - v)),
114141
color: "#f3f4f6",
115142
},
116143
];
117144

118145
export default function EventCountChart({ height = 380 }: EventCountChartProps) {
146+
const chartHeight = NEED_SCROLL ? fullChartHeight : height;
147+
119148
return (
120149
<div className="w-full rounded-3xl bg-white px-6 py-5 shadow-sm border border-[#e5e7f5]">
121150
<h2 className="text-lg font-bold text-[var(--color-primary,#111827)]">
122151
Event Count
123152
</h2>
124153

125-
<div className="w-full md:overflow-visible overflow-x-auto overflow-y-hidden">
154+
{/* Scroll Container */}
155+
<div
156+
className="w-full md:overflow-visible overflow-x-auto"
157+
style={{
158+
maxHeight: computedMaxHeight ? `${computedMaxHeight}px` : "auto",
159+
overflowY: NEED_SCROLL ? "auto" : "visible",
160+
}}
161+
>
126162
<div className="min-w-[700px] md:min-w-0">
127163
<ReactApexChart
128-
options={chartOptions}
164+
options={chartOptions(MAX_VALUE)}
129165
series={chartSeries}
130166
type="bar"
131-
height={height}
167+
height={chartHeight}
132168
/>
133169
</div>
134170
</div>
135171

136-
{/* Legend อธิบายความหมายสี — ล็อกกลางทุกหน้าจอ */}
172+
{/* Legend */}
137173
<div className="mt-4 flex flex-wrap items-center justify-center gap-3 text-xs text-slate-600">
138174
<div className="flex items-center gap-2">
139175
<span className="h-2.5 w-2.5 rounded-full bg-[#a0a1ff]" />
140176
<span className="whitespace-nowrap">Event Count</span>
141177
</div>
178+
<div className="flex items-center gap-2">
179+
{/* <span className="h-2.5 w-2.5 rounded-full bg-[#f3f4f6]" /> */}
180+
<span className="whitespace-nowrap">
181+
Total: {TOTAL_EVENTS.toLocaleString()}
182+
</span>
183+
</div>
142184
</div>
143185
</div>
144186
);
145-
}
187+
}

0 commit comments

Comments
 (0)