📊 Real-time Dashboards = Data Visualization
Modern dashboards process thousands of data points. JavaScript array methods efficiently transform, filter, and aggregate data for real-time visualization.
📝 Dashboard Data Processing
// Real-time Sales Data
const salesData = [
{ timestamp: '2024-01-15T10:00:00Z', product: 'Laptop', amount: 1200, region: 'North America' },
{ timestamp: '2024-01-15T10:15:00Z', product: 'Mouse', amount: 25, region: 'Europe' },
{ timestamp: '2024-01-15T10:30:00Z', product: 'Laptop', amount: 1100, region: 'Asia' },
{ timestamp: '2024-01-15T10:45:00Z', product: 'Keyboard', amount: 50, region: 'North America' }
];
// Filter by Date Range
const getSalesInRange = (data, startDate, endDate) => {
const start = new Date(startDate).getTime();
const end = new Date(endDate).getTime();
return data.filter(sale => {
const timestamp = new Date(sale.timestamp).getTime();
return timestamp >= start && timestamp <= end;
});
};
// Group by Region
const aggregateByRegion = (data) => {
return data.reduce((acc, sale) => {
const region = sale.region;
if (!acc[region]) {
acc[region] = {
totalRevenue: 0,
orderCount: 0,
products: new Set()
};
}
acc[region].totalRevenue += sale.amount;
acc[region].orderCount++;
acc[region].products.add(sale.product);
return acc;
}, {});
};
// Top Products
const getTopProducts = (data, limit = 5) => {
const productStats = data.reduce((acc, sale) => {
const product = sale.product;
if (!acc[product]) {
acc[product] = { revenue: 0, orders: 0 };
}
acc[product].revenue += sale.amount;
acc[product].orders++;
return acc;
}, {});
return Object.entries(productStats)
.map(([product, stats]) => ({ product, ...stats }))
.sort((a, b) => b.revenue - a.revenue)
.slice(0, limit);
};
// Time Series
const getTimeSeriesData = (data, interval = 'hour') => {
const grouped = data.reduce((acc, sale) => {
const date = new Date(sale.timestamp);
let key;
switch(interval) {
case 'hour':
key = date.setMinutes(0, 0, 0);
break;
case 'day':
key = date.setHours(0, 0, 0, 0);
break;
}
if (!acc[key]) {
acc[key] = { timestamp: key, revenue: 0, orders: 0 };
}
acc[key].revenue += sale.amount;
acc[key].orders++;
return acc;
}, {});
return Object.values(grouped).sort((a, b) => a.timestamp - b.timestamp);
};
🎯 Dashboard Performance
// KPI Calculation
const calculateKPIs = (data) => {
const totalRevenue = data.reduce((sum, sale) => sum + sale.amount, 0);
const totalOrders = data.length;
const avgOrderValue = totalRevenue / totalOrders;
const uniqueProducts = new Set(data.map(sale => sale.product));
const productCount = uniqueProducts.size;
return {
totalRevenue: Math.round(totalRevenue * 100) / 100,
totalOrders,
avgOrderValue: Math.round(avgOrderValue * 100) / 100,
productCount
};
};
// Real-time Dashboard Class
class Dashboard {
constructor(data) {
this.data = data;
this.updateInterval = 5000;
this.listeners = [];
}
subscribe(listener) {
this.listeners.push(listener);
}
updateData(newData) {
this.data = [...this.data, ...newData];
this.notifyListeners();
}
notifyListeners() {
const snapshot = {
kpis: calculateKPIs(this.data),
topProducts: getTopProducts(this.data),
timeSeries: getTimeSeriesData(this.data),
regionalData: aggregateByRegion(this.data)
};
this.listeners.forEach(listener => listener(snapshot));
}
start() {
setInterval(() => {
const newData = this.generateSampleData();
this.updateData(newData);
}, this.updateInterval);
}
}
💡 Dashboard Development Tips
- Use Web Workers for heavy data processing
- Implement debouncing for rapid UI updates
- Use pagination for large datasets
- Cache processed results
- Virtualize large lists
“Real-time dashboards are the backbone of business intelligence. With array methods, you can process thousands of data points in milliseconds.”
