Web-based Interactive Dashboard React, JavaScript

👤 Sharing: AI
```javascript
import React, { useState, useEffect } from 'react';
import Chart from 'chart.js/auto'; // Import Chart.js

const Dashboard = () => {
  const [data, setData] = useState({
    labels: ['January', 'February', 'March', 'April', 'May', 'June'],
    datasets: [
      {
        label: 'Sales',
        data: [65, 59, 80, 81, 56, 55],
        backgroundColor: 'rgba(255, 99, 132, 0.2)',
        borderColor: 'rgba(255, 99, 132, 1)',
        borderWidth: 1,
      },
      {
        label: 'Expenses',
        data: [28, 48, 40, 19, 86, 27],
        backgroundColor: 'rgba(54, 162, 235, 0.2)',
        borderColor: 'rgba(54, 162, 235, 1)',
        borderWidth: 1,
      },
    ],
  });

  const [chartInstance, setChartInstance] = useState(null); // Store the chart instance

  useEffect(() => {
    // Fetch data or perform any initial setup here
    // For example, you can fetch data from an API and update the 'data' state.

    // Create the chart when the component mounts.
    const ctx = document.getElementById('myChart').getContext('2d');

    if (chartInstance) {
      chartInstance.destroy(); // Destroy the previous instance if exists. Important for updates!
    }

    const newChartInstance = new Chart(ctx, {
      type: 'bar', // Or 'line', 'pie', 'doughnut', etc.
      data: data,
      options: {
        scales: {
          y: {
            beginAtZero: true,
          },
        },
      },
    });

    setChartInstance(newChartInstance);


    // Cleanup function to destroy the chart when the component unmounts or updates
    return () => {
      if (newChartInstance) {
          newChartInstance.destroy();
      }

    };

  }, [data]); // Re-run the effect when `data` changes


  const handleUpdateData = () => {
    // Example of updating the data (you'd typically fetch this from an API)
    const newData = {
      labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul'],
      datasets: [
        {
          label: 'New Sales',
          data: [55, 60, 75, 85, 60, 70, 90],
          backgroundColor: 'rgba(255, 99, 132, 0.2)',
          borderColor: 'rgba(255, 99, 132, 1)',
          borderWidth: 1,
        },
        {
          label: 'New Expenses',
          data: [30, 45, 35, 25, 90, 30, 40],
          backgroundColor: 'rgba(54, 162, 235, 0.2)',
          borderColor: 'rgba(54, 162, 235, 1)',
          borderWidth: 1,
        },
      ],
    };
    setData(newData); // Update the state, triggering a re-render and chart update
  };


  return (
    <div>
      <h1>Interactive Dashboard</h1>

      <canvas id="myChart" width="400" height="200"></canvas>

      <button onClick={handleUpdateData}>Update Data</button>


        {/* Additional Dashboard Components */}
      <div>
        <h2>Key Metrics</h2>
        <p>Total Sales: $1,200,000</p>
        <p>Average Order Value: $120</p>
        <p>Customer Acquisition Cost: $30</p>
      </div>


      <div>
        <h2>Filters</h2>
        <select>
          <option value="all">All Products</option>
          <option value="productA">Product A</option>
          <option value="productB">Product B</option>
        </select>
        <input type="date" />
        <input type="date" />
      </div>


      <div>
        <h2>Real-time Updates</h2>
        <p>New order placed: Order #12345 - $50 (1 minute ago)</p>
      </div>

    </div>
  );
};

export default Dashboard;
```
👁️ Viewed: 9

Comments