aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/app/_elements/dataset-load/dataset-load.component.ts
blob: d97e7cbe8a0d93a896aa106c8bd1da79b09b6ea5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { Component, OnInit, ViewChild } from '@angular/core';

@Component({
  selector: 'app-dataset-load',
  templateUrl: './dataset-load.component.html',
  styleUrls: ['./dataset-load.component.css']
})
export class DatasetLoadComponent {

   //array varibales to store csv data
   lines : any[] = []; //for headings
   linesR : any[] = []; // for rows

/*
    const csv = require('csv-parser')
    const fs = require('fs')
    const res : string[] = [];

    fs.createReadStream('https://raw.githubusercontent.com/sharmaroshan/Churn-Modelling-Dataset/master/Churn_Modelling.csv')
      .pipe(csv())
      .on('data', (data : string) => res.push(data))
      .on('end', () => {
        console.log(res);

*/

  changeListener(files: FileList) {

    console.log(files);

    if(files && files.length > 0) {
    
      let file: File | null = files.item(0);
      if (file == null)
        return;

      if (file) { 
          console.log(file.name);
          console.log(file.size);
          console.log(file.type);
          //File reader method
          let reader: FileReader = new FileReader();
          reader.readAsText(file);
          reader.onload = (e) => {
            let csv: any = reader.result;
            let allTextLines = [];
            allTextLines = csv.split(/\r|\n|\r/);
          
          //Table Headings
            let headers = allTextLines[0].split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/);
            let data = headers;
            let tarr = [];
            for (let j = 0; j < headers.length; j++) {
              tarr.push(data[j]);
            }
            //Pusd headings to array variable
            this.lines.push(tarr);
            //console.log(this.lines);
            
          
            // Table Rows
            let tarrR : string[] = [];
            
            let arrl = allTextLines.length;
            let rows = [];
            for(let i = 1; i < arrl; i++){
            rows.push(allTextLines[i].split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/));
          
            }
            
            for (let j = 0; j < arrl; j++) {
                tarrR.push(rows[j]);
            }
          //Push rows to array variable
            this.linesR.push(tarrR);
            console.log(this.linesR);
        }
      }
    }
  }

}