forked from oftheheadland/prodgannt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
productgannt.html
138 lines (98 loc) · 3.63 KB
/
productgannt.html
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<!DOCTYPE html>
<html lang="en">
<head>
<title>Gantt</title>
<body>
<p>Select local CSV File:</p>
<input id="csv" type="file">
<output id="out">
Contents of CSV will appear here for debugging.
</output>
<div id="gantt" style="height: 600px;"></div>
<script>
var globalJSON = null; // contains the JSON data obtained from reading CSV file
const fileInput = document.getElementById("csv"),
readFile = function () {
const reader = new FileReader();
reader.onload = function () {
document.getElementById('out').innerHTML = reader.result;
console.log(reader.result)
globalJSON = csvJSON(reader.result)
google.charts.load('current', { 'packages': ['corechart', 'gantt'] });
google.charts.setOnLoadCallback(drawChart);
};
// start reading the file. When it is done, calls the onload event defined above.
reader.readAsBinaryString(fileInput.files[0]);
};
fileInput.addEventListener('change', readFile);
</script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
function drawChart() {
const data = new google.visualization.DataTable();
data.addColumn('string', 'Task ID');
data.addColumn('string', 'Task Name');
data.addColumn('string', 'Resource');
data.addColumn('date', 'Start Date');
data.addColumn('date', 'End Date');
data.addColumn('number', 'Duration');
data.addColumn('number', 'Percent Complete');
data.addColumn('string', 'Dependencies');
globalJSON.length = Object.keys(globalJSON).length - 1;
console.log(globalJSON.length)
for (var i = 0; i < globalJSON.length; i++) {
const formattedValues = Object.values(globalJSON[i]).map((value, i) => {
let x = value;
// first three values must be strings
if (i === 0 || i === 1 || i ===2) {
x = String(value)
}
// fourth and fifth values must be dates
if (i === 3 || i === 4) {
x = new Date(value)
}
// sixth values in their example are null https://developers.google.com/chart/interactive/docs/gallery/ganttchart
if (i === 5) {
x = null;
}
// seventh values in their examples are numbers
if (i === 6) {
x = Number(value)
}
// eighth values in their examples are null
if (i === 7) {
x = String(value).replace(/(\r\n|\n|\r)/gm, "")
}
return x;
})
console.log(formattedValues)
data.addRow(formattedValues)
}
const options = { 'title': 'Gantt' };
chart = new google.visualization.Gantt(document.getElementById('gantt'))
// listen for errors
google.visualization.events.addListener(chart, 'error', function (err) {
console.log(err.id, err.message);
});
chart.draw(data, options)
}
// source https://stackoverflow.com/questions/27979002/convert-csv-data-into-json-format-using-javascript
function csvJSON(csv) {
// csv is the CSV file with headers
var lines = csv.split("\n");
var result = [];
var headers = lines[0].split(",");
for (var i = 1; i < lines.length; i++) {
var obj = {};
var currentline = lines[i].split(",");
for (var j = 0; j < headers.length; j++) {
obj[headers[j]] = currentline[j];
}
result.push(obj);
}
return result; //JavaScript object
// return JSON.stringify(result); //JSON
}
</script>
</body>
</html>