forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeeklyComparisonChart.tsx
More file actions
141 lines (129 loc) 路 4.22 KB
/
Copy pathWeeklyComparisonChart.tsx
File metadata and controls
141 lines (129 loc) 路 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'use client';
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
PointElement,
LineElement,
Tooltip,
Legend,
} from 'chart.js';
import type { TooltipItem } from 'chart.js';
import { Line } from 'react-chartjs-2';
import { useMemo, useState } from 'react';
ChartJS.register(CategoryScale, LinearScale, PointElement, LineElement, Tooltip, Legend);
const days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
const thisWeek = [142, 158, 175, 161, 189, 110, 95];
const lastWeek = [130, 144, 160, 155, 172, 102, 88];
function pctDiff(a: number, b: number) {
if (b === 0) return 0;
return ((a - b) / b) * 100;
}
export default function WeeklyComparisonChart() {
const [hideLast, setHideLast] = useState(false);
const totalThis = thisWeek.reduce((s, v) => s + v, 0);
const totalLast = lastWeek.reduce((s, v) => s + v, 0);
const diff = pctDiff(totalThis, totalLast);
const data = useMemo(() => ({
labels: days,
datasets: [
{
label: 'This Week',
data: thisWeek,
borderColor: 'rgba(99,102,241,1)',
borderWidth: 2.5,
borderDash: [],
pointRadius: 4,
pointBackgroundColor: 'rgba(99,102,241,1)',
tension: 0.35,
fill: false,
},
{
label: 'Last Week',
data: lastWeek,
borderColor: 'rgba(156,163,175,0.9)',
borderWidth: 2,
borderDash: [6, 4],
pointRadius: 3,
pointBackgroundColor: 'rgba(156,163,175,0.9)',
tension: 0.35,
fill: false,
hidden: hideLast,
},
],
}), [hideLast]);
const options = {
responsive: true,
interaction: { mode: 'index' as const, intersect: false },
scales: {
x: {
grid: { display: false },
ticks: { color: '#9ca3af', font: { size: 12 } },
border: { color: '#e5e7eb' },
},
y: {
beginAtZero: true,
grid: { color: 'rgba(0,0,0,0.05)' },
ticks: { color: '#9ca3af', font: { size: 12 } },
border: { display: false },
},
},
plugins: {
legend: { display: false },
tooltip: {
backgroundColor: 'rgba(17,24,39,0.9)',
titleColor: '#f9fafb',
bodyColor: '#d1d5db',
padding: 12,
cornerRadius: 8,
callbacks: {
label: (item: TooltipItem<'line'>) => {
const cur = item.raw as number;
const prev = lastWeek[item.dataIndex];
const pct = pctDiff(cur, prev);
const sign = pct >= 0 ? '+' : '';
return ` ${item.dataset.label}: ${cur.toLocaleString()} gists (${sign}${pct.toFixed(1)}%)`;
},
},
},
},
};
const diffColor = diff >= 0 ? '#16a34a' : '#dc2626';
const diffSign = diff >= 0 ? '+' : '';
return (
<div style={{ position: 'relative', width: '100%' }}>
{/* Header row: legend + weekly % diff */}
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 12 }}>
{/* Manual legend with toggle */}
<div style={{ display: 'flex', gap: 16, fontSize: 12 }}>
<span style={{ display: 'flex', alignItems: 'center', gap: 5 }}>
<svg width="20" height="10"><line x1="0" y1="5" x2="20" y2="5" stroke="rgba(99,102,241,1)" strokeWidth="2.5" /></svg>
This Week
</span>
<button
onClick={() => setHideLast(h => !h)}
style={{
display: 'flex', alignItems: 'center', gap: 5, background: 'none',
border: 'none', cursor: 'pointer', fontSize: 12, padding: 0,
opacity: hideLast ? 0.4 : 1,
}}
>
<svg width="20" height="10">
<line x1="0" y1="5" x2="20" y2="5" stroke="rgba(156,163,175,0.9)" strokeWidth="2" strokeDasharray="6 4" />
</svg>
Last Week
</button>
</div>
{/* Weekly % difference badge */}
<span style={{
fontSize: 13, fontWeight: 700, color: diffColor,
background: diff >= 0 ? 'rgba(22,163,74,0.1)' : 'rgba(220,38,38,0.1)',
padding: '3px 10px', borderRadius: 20,
}}>
{diffSign}{diff.toFixed(1)}% vs last week
</span>
</div>
<Line data={data} options={options} />
</div>
);
}