1

I am working on a small project where I feed a data directory with some csv and expect my webview (html/css/js) page to be refreshed with new charts. Generating chart view is fairly easy when I specify the file I want to parse, but I struggle on looping on files in my data directory.
I run "python -m http.server" as my localhost server so don't have access to node.js/fs

| data    
|   | file1.csv   
|   | file2.csv   
|   | filen.csv  
| js    
| css   
| index.html 

My Javascript code

function parseData(createGraph, filePath) {
    Papa.parse("filePath", {
        download: true,
        complete: function(results) {
            console.log(results.data);
            // createGraph(results.data);
            genericGraph(results.data);
        }
    });
}


function genericGraph(data) {
    var xaxis = [];
    var yaxis = [];
    for (var i = 1; i < data[0].length; i++) {
        yaxis.push([data[0][i]])
    }
    for (var i = 1; i < data.length - 1; i++) {
        for (var j = 0; j < yaxis.length; j++) {
            yaxis[j].push(data[i][j + 1])
        }
        xaxis.push(data[i][0])
    }
    console.log(xaxis)
    console.log(yaxis)

    var chart = c3.generate({
        bindto: '#chart',
        data: {
            columns: yaxis
        },
        axis: {
            x: {
                type: 'category',
                categories: xaxis,
                tick: {
                    multiline: false,
                    rotate: 60,
                    culling: {
                        max: 10
                    }
                }
            }
        },
        zoom: {
            enabled: true
        },
        legend: {
            position: 'right'
        }
    });
}

function loopThroughFiles()
{
    var files = []  // How to get the list of files under "../data"?
    files.foreach(file=>
        parseData(genericGraph, file);
        )
}


The Fool
  • 16,715
  • 5
  • 52
  • 86
amestin
  • 31
  • 1
  • 3
  • [Get list of filenames in folder with Javascript](https://stackoverflow.com/q/31274329/14032355) – ikhvjs Jun 23 '21 at 08:57
  • 1
    @ikhvjs, maybe another endpoint that tells you which files are there would be most easy. – The Fool Jun 23 '21 at 08:59
  • all the solution uses fs but I do not have node js server @ikhvjs – amestin Jun 23 '21 at 11:54
  • @amestin, You cannot access the filesystem in client-side javascript. However, does this solution help you? https://stackoverflow.com/a/61705781/14032355 – ikhvjs Jun 23 '21 at 12:18

0 Answers0