81 lines
2.3 KiB
JavaScript
81 lines
2.3 KiB
JavaScript
// Dashboard JavaScript functionality
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
// Initialize user trend chart if canvas exists
|
|
const chartCanvas = document.getElementById('userTrendChart');
|
|
if (chartCanvas) {
|
|
initUserTrendChart();
|
|
}
|
|
|
|
// Auto refresh dashboard data every 5 minutes
|
|
setInterval(function() {
|
|
refreshDashboardStats();
|
|
}, 300000); // 5 minutes
|
|
});
|
|
|
|
function initUserTrendChart() {
|
|
const ctx = document.getElementById('userTrendChart').getContext('2d');
|
|
|
|
// Get data from template variables (these will be rendered by Jinja2)
|
|
const labels = window.userTrendLabels || [];
|
|
const data = window.userTrendData || [];
|
|
|
|
const userTrendChart = new Chart(ctx, {
|
|
type: 'line',
|
|
data: {
|
|
labels: labels,
|
|
datasets: [{
|
|
label: '注册用户数',
|
|
data: data,
|
|
borderColor: 'rgb(102, 126, 234)',
|
|
backgroundColor: 'rgba(102, 126, 234, 0.1)',
|
|
tension: 0.4,
|
|
fill: true
|
|
}]
|
|
},
|
|
options: {
|
|
responsive: true,
|
|
maintainAspectRatio: false,
|
|
plugins: {
|
|
legend: {
|
|
display: false
|
|
}
|
|
},
|
|
scales: {
|
|
y: {
|
|
beginAtZero: true,
|
|
ticks: {
|
|
stepSize: 1
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
function refreshDashboardStats() {
|
|
// This function could be used to refresh dashboard statistics via AJAX
|
|
// For now, it's a placeholder for future implementation
|
|
console.log('Refreshing dashboard stats...');
|
|
}
|
|
|
|
// Utility function to format numbers
|
|
function formatNumber(num) {
|
|
if (num >= 1000000) {
|
|
return (num / 1000000).toFixed(1) + 'M';
|
|
} else if (num >= 1000) {
|
|
return (num / 1000).toFixed(1) + 'K';
|
|
}
|
|
return num.toString();
|
|
}
|
|
|
|
// Function to update stats cards (for future AJAX updates)
|
|
function updateStatsCard(cardSelector, value) {
|
|
const card = document.querySelector(cardSelector);
|
|
if (card) {
|
|
const valueElement = card.querySelector('h3');
|
|
if (valueElement) {
|
|
valueElement.textContent = formatNumber(value);
|
|
}
|
|
}
|
|
}
|