Close

    trial

    Auto-Fetch Excel and Display Chart

    Chart from Excel (Auto-Fetched)

    const excelUrl = ‘https://eyindia-my.sharepoint.com/:u:/r/personal/divishi_saxena_in_ey_com/Documents/Desktop/Category.html?csf=1&web=1&e=e5N1VD’; // Replace with your actual Excel file path

    // Fetch the Excel file from the server
    fetch(excelUrl)
    .then(response => response.arrayBuffer())
    .then(data => {
    const workbook = XLSX.read(data, { type: ‘array’ });

    // Assuming data is in the first sheet
    const sheetName = workbook.SheetNames[0];
    const worksheet = workbook.Sheets[sheetName];

    // Convert to JSON
    const jsonData = XLSX.utils.sheet_to_json(worksheet);

    // Extract labels and values
    const labels = jsonData.map(row => row[‘Category’]);
    const values = jsonData.map(row => row[‘Value’]);

    renderChart(labels, values);
    })
    .catch(error => console.error(‘Error loading Excel file:’, error));

    // Render chart using Chart.js
    function renderChart(labels, values) {
    const ctx = document.getElementById(‘myChart’).getContext(‘2d’);
    new Chart(ctx, {
    type: ‘bar’, // Change to ‘line’ or ‘pie’ as needed
    data: {
    labels: labels,
    datasets: [{
    label: ‘Values from Excel’,
    data: values,
    backgroundColor: ‘rgba(75, 192, 192, 0.6)’,
    borderColor: ‘rgba(75, 192, 192, 1)’,
    borderWidth: 1
    }]
    },
    options: {
    responsive: true,
    scales: {
    y: {
    beginAtZero: true
    }
    }
    }
    });
    }