Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

110
Views
Reading numbers from array in javascript and plotting these using plotly

I'm trying to make an interface that lets a user upload CSV files and plots these using plotly, only using javascript and obviously the plotly library. I'm close, but my suspicion is that there's an issue with the asynchronous reading of the csv files. As you can probably see, I'm relatively new to javascript, so any feedback is welcome. I cannot however use any other libraries or packages plotly.

The problem is that the resulting figure only shows the initialized values (1).

EDIT: The heatmap function works on test data, or if I modify specific elements of the data_y object, just not when I update the information from the file.

  1. There's a button that allows uploading of the csv files. On event this code triggers:
<script>
let picker  = document.getElementById('picker');

picker.addEventListener('change', event => {0
    file_list  = event.target.files;
    var fig_y  = [];

    for (let i = 0 ; i< file_list.length ; i++){
    if(file_list[i].name == (".DS_Store")){continue}
    else {
    var ready = read_data(file_list[i]); 
    fig_y.push(ready);
    }
    }   
    console.log(fig_y);
    plot_heatmap(fig_y);
}
);
</script>
  1. The data is read using this code.
<script>
function read_data(input){
    var xs = 1212;               // length of the data
    file_contents = [];
    var data_y    = Array(xs).fill(1);
    let file      = input;
    
    let reader    = new FileReader();
    reader.readAsText(file);
    reader.onload   = function(){
    file_contents = reader.result.split('\n');

// open the data file. First two lines contain a description of the data.
    for (let j  = 2 ; j<file_contents.length-1 ; j++) {
// the relevant data is the third number in the column
    var nr = file_contents[j].split(",").map(Number)[2];  
    data_y[j-2] =   nr;
    }
    }
    return data_y;
}
</script>
  1. the code that makes the plotly heatmap.
<script>
function plot_heatmap(data_z){

    var data = [
      {
           z: data_z,
        type: 'heatmap'
      }
];
Plotly.newPlot('raw_data', data);
    };
</script>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

OK, so I figured out the answer. It comes from the asynchronous reading of the text files. Putting the plot_heatmap function in the following Timeout function solved the issue (well, maybe it's more a workaround).

setTimeout(() => { plot_heatmap(fig_y); }, 100);

Actually, by changing the length of the timeout, I could catch JS in its act and see half the heatmap filled in with the real values and the other half still with the initialized value!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!