.dashboard-container {
background: #f8f9ff;
border-radius: 20px;
padding: 25px;
max-width: 1400px;
margin: 0 auto;
}
.dashboard-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 30px;
flex-wrap: wrap;
gap: 20px;
}
.dashboard-header h2 {
margin: 0;
font-size: 2rem;
font-weight: 700;
color: #333;
}
.dashboard-controls {
display: flex;
gap: 15px;
}
.control-btn {
padding: 10px 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
border-radius: 8px;
font-size: 14px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}
.control-btn:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}
.dashboard-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
gap: 25px;
}
.widget {
background: white;
border-radius: 15px;
box-shadow: 0 5px 20px rgba(0, 0, 0, 0.08);
transition: all 0.3s ease;
display: flex;
flex-direction: column;
min-height: 300px;
}
.widget:hover {
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
transform: translateY(-3px);
}
.widget-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px 20px 15px;
border-bottom: 1px solid #f0f0f0;
}
.widget-header h3 {
margin: 0;
font-size: 1.2rem;
font-weight: 600;
color: #333;
}
.widget-controls {
display: flex;
gap: 8px;
}
.widget-btn {
width: 28px;
height: 28px;
border: none;
border-radius: 6px;
background: #f1f5f9;
color: #666;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
transition: all 0.2s ease;
}
.widget-btn:hover {
background: #e2e8f0;
color: #333;
}
.close-btn:hover {
background: #fee2e2;
color: #ef4444;
}
.widget-content {
padding: 20px;
flex: 1;
display: flex;
flex-direction: column;
}
.chart-container {
flex: 1;
min-height: 150px;
margin-bottom: 20px;
}
.widget-stats {
display: flex;
justify-content: space-between;
gap: 20px;
}
.stat-item {
text-align: center;
flex: 1;
}
.stat-value {
display: block;
font-size: 1.5rem;
font-weight: 700;
color: #333;
margin-bottom: 5px;
}
.stat-label {
font-size: 0.9rem;
color: #666;
}
.activity-list {
flex: 1;
}
.activity-item {
display: flex;
align-items: center;
gap: 15px;
padding: 15px 0;
border-bottom: 1px solid #f0f0f0;
}
.activity-item:last-child {
border-bottom: none;
}
.activity-icon {
width: 40px;
height: 40px;
background: #f1f5f9;
border-radius: 10px;
display: flex;
align-items: center;
justify-content: center;
font-size: 18px;
}
.activity-details {
flex: 1;
}
.activity-title {
font-weight: 500;
color: #333;
margin-bottom: 3px;
}
.activity-time {
font-size: 0.85rem;
color: #999;
}
.metrics-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
flex: 1;
}
.metric-card {
background: #f8fafc;
border-radius: 12px;
padding: 20px;
text-align: center;
transition: all 0.3s ease;
}
.metric-card:hover {
background: #f1f5f9;
transform: translateY(-2px);
}
.metric-title {
font-size: 0.9rem;
color: #666;
margin-bottom: 10px;
}
.metric-value {
font-size: 1.5rem;
font-weight: 700;
color: #333;
margin-bottom: 5px;
}
.metric-trend {
font-size: 0.85rem;
font-weight: 600;
}
.metric-trend.positive {
color: #10b981;
}
.metric-trend.negative {
color: #ef4444;
}
/* Drag and drop styles */
.widget.dragging {
opacity: 0.7;
transform: scale(0.98);
z-index: 100;
}
.widget-placeholder {
background: #e2e8f0;
border: 2px dashed #94a3b8;
border-radius: 15px;
}
/* Responsive design */
@media (max-width: 768px) {
.dashboard-container {
padding: 15px;
}
.dashboard-header {
flex-direction: column;
align-items: stretch;
}
.dashboard-controls {
justify-content: center;
}
.dashboard-grid {
grid-template-columns: 1fr;
}
.widget-stats {
flex-direction: column;
gap: 15px;
}
.metrics-grid {
grid-template-columns: 1fr;
}
}
/* Loading animation */
.widget.loading::after {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(255, 255, 255, 0.7);
display: flex;
align-items: center;
justify-content: center;
border-radius: 15px;
z-index: 10;
}
.widget.loading::after::before {
content: '';
width: 30px;
height: 30px;
border: 3px solid #f3f3f3;
border-top: 3px solid #667eea;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
document.addEventListener('DOMContentLoaded', function() {
// DOM elements
const dashboardGrid = document.getElementById('dashboardGrid');
const addWidgetBtn = document.getElementById('addWidgetBtn');
const resetLayoutBtn = document.getElementById('resetLayoutBtn');
const widgets = document.querySelectorAll('.widget');
// Initialize dashboard
function initDashboard() {
initCharts();
setupEventListeners();
setupDragAndDrop();
}
// Initialize charts
function initCharts() {
// Traffic chart
const trafficCtx = document.getElementById('trafficChart').getContext('2d');
new Chart(trafficCtx, {
type: 'line',
data: {
labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
datasets: [{
label: 'Visitors',
data: [12000, 19000, 15000, 18000, 22000, 19000, 24000],
borderColor: '#667eea',
backgroundColor: 'rgba(102, 126, 234, 0.1)',
borderWidth: 3,
pointBackgroundColor: '#fff',
pointBorderWidth: 2,
pointRadius: 4,
tension: 0.4,
fill: true
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
display: false
}
},
scales: {
y: {
beginAtZero: true,
grid: {
color: 'rgba(0, 0, 0, 0.05)'
}
},
x: {
grid: {
display: false
}
}
}
}
});
// Revenue chart
const revenueCtx = document.getElementById('revenueChart').getContext('2d');
new Chart(revenueCtx, {
type: 'bar',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
datasets: [{
label: 'Revenue',
data: [35000, 42000, 38000, 45000, 41000, 48000],
backgroundColor: [
'rgba(102, 126, 234, 0.7)',
'rgba(118, 75, 162, 0.7)',
'rgba(52, 152, 219, 0.7)',
'rgba(46, 204, 113, 0.7)',
'rgba(241, 196, 15, 0.7)',
'rgba(230, 126, 34, 0.7)'
],
borderWidth: 0,
borderRadius: 6
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
display: false
}
},
scales: {
y: {
beginAtZero: true,
grid: {
color: 'rgba(0, 0, 0, 0.05)'
}
},
x: {
grid: {
display: false
}
}
}
}
});
}
// Set up event listeners
function setupEventListeners() {
// Widget controls
widgets.forEach(widget => {
const expandBtn = widget.querySelector('.expand-btn');
const closeBtn = widget.querySelector('.close-btn');
expandBtn.addEventListener('click', () => {
widget.classList.toggle('expanded');
const isExpanded = widget.classList.contains('expanded');
expandBtn.textContent = isExpanded ? 'β' : 'β';
});
closeBtn.addEventListener('click', () => {
widget.style.animation = 'fadeOut 0.3s ease';
setTimeout(() => {
widget.remove();
}, 300);
});
});
// Dashboard controls
addWidgetBtn.addEventListener('click', addWidget);
resetLayoutBtn.addEventListener('click', resetLayout);
}
// Add new widget
function addWidget() {
const widgetCount = document.querySelectorAll('.widget').length;
const widgetId = `widget-${widgetCount + 1}`;
const newWidget = document.createElement('div');
newWidget.className = 'widget';
newWidget.setAttribute('data-id', widgetId);
newWidget.innerHTML = `
<div class="widget-header">
<h3>New Widget ${widgetCount + 1}</h3>
<div class="widget-controls">
<button class="widget-btn expand-btn">β</button>
<button class="widget-btn close-btn">Γ</button>
</div>
</div>
<div class="widget-content">
<div class="widget-placeholder-content">
<p>This is a new widget. You can customize it as needed.</p>
</div>
</div>
`;
dashboardGrid.appendChild(newWidget);
// Add event listeners to new widget
const expandBtn = newWidget.querySelector('.expand-btn');
const closeBtn = newWidget.querySelector('.close-btn');
expandBtn.addEventListener('click', () => {
newWidget.classList.toggle('expanded');
const isExpanded = newWidget.classList.contains('expanded');
expandBtn.textContent = isExpanded ? 'β' : 'β';
});
closeBtn.addEventListener('click', () => {
newWidget.style.animation = 'fadeOut 0.3s ease';
setTimeout(() => {
newWidget.remove();
}, 300);
});
// Setup drag and drop for new widget
setupDragAndDrop();
}
// Reset layout
function resetLayout() {
// Add confirmation dialog
if (confirm('Are you sure you want to reset the dashboard layout?')) {
// Reload the page to reset to default layout
location.reload();
}
}
// Setup drag and drop functionality
function setupDragAndDrop() {
let draggedWidget = null;
widgets.forEach(widget => {
// Make widgets draggable
widget.setAttribute('draggable', true);
widget.addEventListener('dragstart', (e) => {
draggedWidget = widget;
widget.classList.add('dragging');
e.dataTransfer.effectAllowed = 'move';
});
widget.addEventListener('dragend', () => {
widget.classList.remove('dragging');
document.querySelectorAll('.widget-placeholder').forEach(placeholder => {
placeholder.remove();
});
});
});
// Add dragover and drop events to the grid
dashboardGrid.addEventListener('dragover', (e) => {
e.preventDefault();
e.dataTransfer.dropEffect = 'move';
const afterElement = getDragAfterElement(dashboardGrid, e.clientY);
const placeholder = document.querySelector('.widget-placeholder');
if (!placeholder) {
const placeholderElement = document.createElement('div');
placeholderElement.className = 'widget widget-placeholder';
if (afterElement == null) {
dashboardGrid.appendChild(placeholderElement);
} else {
dashboardGrid.insertBefore(placeholderElement, afterElement);
}
} else if (afterElement == null) {
dashboardGrid.appendChild(placeholder);
} else {
dashboardGrid.insertBefore(placeholder, afterElement);
}
});
dashboardGrid.addEventListener('dragenter', (e) => {
e.preventDefault();
});
dashboardGrid.addEventListener('drop', (e) => {
e.preventDefault();
const placeholder = document.querySelector('.widget-placeholder');
if (placeholder && draggedWidget) {
dashboardGrid.insertBefore(draggedWidget, placeholder);
placeholder.remove();
}
draggedWidget = null;
});
}
// Helper function to determine where to place the dragged element
function getDragAfterElement(container, y) {
const draggableElements = [...container.querySelectorAll('.widget:not(.dragging):not(.widget-placeholder)')];
return draggableElements.reduce((closest, child) => {
const box = child.getBoundingClientRect();
const offset = y - box.top - box.height / 2;
if (offset < 0 && offset > closest.offset) {
return { offset: offset, element: child };
} else {
return closest;
}
}, { offset: Number.NEGATIVE_INFINITY }).element;
}
// Initialize the dashboard
initDashboard();
});