Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
JavaScript

JavaScript: Build Real-time Data Dashboards with Array Methods

- 26.07.26 - ErcanOPAK

📊 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.”

— Data Engineer

Related posts:

Why AJAX Requests Randomly Fail Under Load

Why AJAX Requests Randomly Fail Under Load

New line in JavaScript alert box

New line in JavaScript alert box

JavaScript: Fix a Debounced Function Still Firing Once per Call Instead of Once per Burst Because th...

JavaScript: Fix a Debounced Function Still Firing Once per Call Instead of Once per Burst Because th...

Views: 21

Post navigation

HTML: Build Accessible Forms with Semantic Elements
AJAX: GraphQL vs REST – Choosing the Right API Architecture

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (854)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (609)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (547)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (854)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com