Skip to content

Commit 4e3b56c

Browse files
committed
added dates to chart
1 parent ee2e26e commit 4e3b56c

10 files changed

+230
-163
lines changed

.env

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
PORT=3000
2-
NODE_ENV=production
3-
LASTFM_API_KEY=your_api_key
1+
LASTFM_API_KEY=974fb2e0a3add0ac42c2729f6c1e854a

chart.js

+113-57
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,144 @@
1-
async function renderChart() {
2-
try {
3-
const response = await fetch('last_week_tracks.json');
4-
const tracks = await response.json();
5-
6-
const playCounts = {};
7-
const labels = [];
1+
// Single global chart instance
2+
let listeningChart = null;
83

9-
10-
tracks.forEach(track => {
11-
const trackDate = new Date(track.date['#text']).toLocaleDateString();
12-
playCounts[trackDate] = (playCounts[trackDate] || 0) + 1;
13-
});
4+
// Function to create or update the chart
5+
async function initializeChart() {
6+
try {
7+
// Get canvas context
8+
const canvas = document.getElementById('listening-chart');
9+
if (!canvas) {
10+
console.error('Canvas element not found');
11+
return;
12+
}
1413

15-
16-
for (const [date, count] of Object.entries(playCounts)) {
17-
labels.push(date);
14+
// Destroy existing chart if it exists
15+
if (listeningChart instanceof Chart) {
16+
listeningChart.destroy();
1817
}
1918

20-
const data = {
21-
labels: labels,
22-
datasets: [{
23-
label: 'Plays Over the Last Week',
24-
data: Object.values(playCounts),
25-
backgroundColor: 'rgba(142, 141, 190, 0.0)',
26-
borderColor: 'rgba(142, 141, 190, 1)',
27-
borderWidth: 2,
28-
fill: true,
29-
pointRadius: 0,
30-
tension: 0.3
31-
}]
32-
};
19+
// Fetch new data
20+
const response = await fetch('weekly_chart.json');
21+
if (!response.ok) {
22+
throw new Error(`Failed to fetch chart data: ${response.status} ${response.statusText}`);
23+
}
24+
const chartData = await response.json();
25+
console.log('Chart data:', chartData);
3326

34-
const config = {
35-
type: 'line',
36-
data: data,
27+
// Chart configuration
28+
const chartConfig = {
29+
type: 'line',
30+
data: {
31+
labels: chartData.data.map(d => d.day),
32+
datasets: [{
33+
label: 'Tracks Played',
34+
data: chartData.data.map(d => ({
35+
x: d.day,
36+
y: d.count,
37+
date: d.date // Store date for tooltip
38+
})),
39+
backgroundColor: 'rgba(142, 141, 190, 0.0)',
40+
borderColor: 'rgba(142, 141, 190, 1)',
41+
borderWidth: 2,
42+
tension: 0.4,
43+
fill: true,
44+
pointRadius: 0
45+
}]
46+
},
3747
options: {
3848
responsive: true,
39-
maintainAspectRatio: false,
49+
maintainAspectRatio: false,
4050
scales: {
4151
y: {
4252
beginAtZero: true,
4353
grid: {
44-
display: false
54+
display: false
55+
},
56+
ticks: {
57+
stepSize: 20,
58+
font: {
59+
size: 10
60+
},
61+
color: '#666'
4562
}
4663
},
4764
x: {
4865
grid: {
49-
display: false
66+
display: false
5067
},
51-
reverse: true
68+
ticks: {
69+
font: {
70+
size: 10
71+
},
72+
color: '#666'
73+
}
5274
}
5375
},
5476
plugins: {
5577
legend: {
56-
display: false
78+
display: false
5779
},
5880
tooltip: {
59-
enabled: true
81+
enabled: true,
82+
mode: 'index',
83+
intersect: false,
84+
callbacks: {
85+
label: function(context) {
86+
const point = context.raw;
87+
const date = new Date(point.date).toLocaleDateString('en-US', {
88+
month: 'short',
89+
day: 'numeric'
90+
});
91+
return `${point.y} tracks on ${date}`;
92+
}
93+
}
6094
}
6195
}
6296
}
6397
};
6498

65-
const playsChart = new Chart(
66-
document.getElementById('listening-chart'),
67-
config
68-
);
69-
70-
// Add hover effects
71-
const musicBox = document.getElementById('box-lastfm');
72-
73-
musicBox.addEventListener('mouseenter', () => {
74-
playsChart.data.datasets[0].backgroundColor = 'rgba(142, 141, 190, 0.2)';
75-
playsChart.update('none'); // Use 'none' for smoother transition
76-
});
77-
78-
musicBox.addEventListener('mouseleave', () => {
79-
playsChart.data.datasets[0].backgroundColor = 'rgba(142, 141, 190, 0.0)';
80-
playsChart.update('none'); // Use 'none' for smoother transition
81-
});
82-
99+
// Create new chart
100+
listeningChart = new Chart(canvas, chartConfig);
101+
console.log('Chart initialized:', listeningChart);
102+
103+
// Setup hover effects
104+
setupHoverEffects();
105+
83106
} catch (error) {
84-
console.error('Error rendering chart:', error);
107+
console.error('Failed to initialize chart:', error);
85108
}
86109
}
87110

88-
renderChart();
111+
// Function to handle hover effects
112+
function setupHoverEffects() {
113+
const musicBox = document.getElementById('box-lastfm');
114+
if (!musicBox) {
115+
console.error('Music box element not found');
116+
return;
117+
}
118+
119+
// Add mouseenter event
120+
musicBox.addEventListener('mouseenter', () => {
121+
if (listeningChart) {
122+
listeningChart.data.datasets[0].backgroundColor = 'rgba(142, 141, 190, 0.2)';
123+
listeningChart.update('none');
124+
}
125+
});
126+
127+
// Add mouseleave event
128+
musicBox.addEventListener('mouseleave', () => {
129+
if (listeningChart) {
130+
listeningChart.data.datasets[0].backgroundColor = 'rgba(142, 141, 190, 0.0)';
131+
listeningChart.update('none');
132+
}
133+
});
134+
}
135+
136+
// Initialize chart when DOM is ready
137+
if (document.readyState === 'loading') {
138+
document.addEventListener('DOMContentLoaded', initializeChart);
139+
} else {
140+
initializeChart();
141+
}
142+
143+
// Export for external use
144+
window.updateChart = initializeChart;

deploy.sh

-10
This file was deleted.

ecosystem.config.js

-14
This file was deleted.

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ <h3>My Portfolio</h3>
8080
</div>
8181
</div>
8282

83-
<div class="project" data-preview="./assets/portfolio/test.png">
83+
<div class="project" data-preview="./assets/default-track.jpg">
8484
<h3>Notes Website</h3>
8585
<p>A simple notes project featuring real-time Markdown implementation.
8686
<div class="project-links">

monthly_stats.json

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
22
"topTrack": {
3-
"name": "One More Dance",
4-
"artist": "d4vd",
5-
"plays": 137,
6-
"image": "https://lastfm.freetls.fastly.net/i/u/174s/1b1d95c32cf8508b25eb44072be69ad7.jpg"
3+
"name": "Stripper Song",
4+
"artist": "Paris Texas",
5+
"plays": 42,
6+
"image": "https://lastfm.freetls.fastly.net/i/u/174s/f55aa71008286b17519ea2ceafc3eb00.jpg"
77
},
88
"topArtist": {
99
"name": "d4vd",
10-
"plays": 308,
11-
"image": "https://lastfm.freetls.fastly.net/i/u/174s/04abd50f999854510c83b9a8c88f92a5.png"
10+
"plays": 193,
11+
"image": "https://lastfm.freetls.fastly.net/i/u/174s/1b1d95c32cf8508b25eb44072be69ad7.jpg"
1212
},
1313
"topAlbum": {
14-
"name": "One More Dance",
15-
"artist": "d4vd",
16-
"plays": 137,
17-
"image": "https://lastfm.freetls.fastly.net/i/u/174s/1b1d95c32cf8508b25eb44072be69ad7.jpg"
14+
"name": "11100011",
15+
"artist": "Asian Glow",
16+
"plays": 80,
17+
"image": "https://lastfm.freetls.fastly.net/i/u/174s/69eb1d0e0dba7268eb80e8ddbf35571a.png"
1818
}
1919
}

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
"main": "server.js",
66
"scripts": {
77
"start": "node server.js",
8-
"dev": "nodemon server.js",
9-
"deploy": "./deploy.sh"
8+
"dev": "nodemon server.js"
109
},
1110
"engines": {
1211
"node": "18.x"

server.js

+3-24
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,8 @@ require('dotenv').config();
66
const app = express();
77
const PORT = process.env.PORT || 3000;
88

9-
// Basic security middleware
10-
app.use((req, res, next) => {
11-
res.setHeader('X-Content-Type-Options', 'nosniff');
12-
res.setHeader('X-Frame-Options', 'DENY');
13-
res.setHeader('X-XSS-Protection', '1; mode=block');
14-
next();
15-
});
16-
17-
// Serve static files with caching
18-
app.use(express.static(path.join(__dirname), {
19-
maxAge: '1d',
20-
setHeaders: (res, path) => {
21-
if (path.endsWith('.html')) {
22-
res.setHeader('Cache-Control', 'no-cache');
23-
}
24-
}
25-
}));
9+
// Serve static files from the root directory
10+
app.use(express.static(path.join(__dirname)));
2611

2712
// Serve static data files
2813
app.use('/data', express.static(path.join(__dirname, 'data')));
@@ -329,13 +314,7 @@ app.get('/api/debug/config', (req, res) => {
329314
});
330315
});
331316

332-
// Error handling
333-
app.use((err, req, res, next) => {
334-
console.error(err.stack);
335-
res.status(500).send('Something broke!');
336-
});
337-
338317
// Start the server
339-
app.listen(PORT, '0.0.0.0', () => {
318+
app.listen(PORT, () => {
340319
console.log(`Server is running on port ${PORT}`);
341320
});

0 commit comments

Comments
 (0)