-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomData.js
133 lines (100 loc) · 3.21 KB
/
randomData.js
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
(function() {
"use strict";
var RandomData = function() {
var addToLane = function (chart, item) {
var name = item.lane;
if (!chart.lanes[name])
chart.lanes[name] = [];
var lane = chart.lanes[name];
var sublane = 0;
while(isOverlapping(item, lane[sublane]))
sublane++;
if (!lane[sublane]) {
lane[sublane] = [];
}
lane[sublane].push(item);
};
var isOverlapping = function(item, lane) {
if (lane) {
for (var i = 0; i < lane.length; i++) {
var t = lane[i];
if (item.start < t.end && item.end > t.start) {
return true;
}
}
}
return false;
};
var parseData = function (data) {
var i = 0, length = data.length, node;
chart = { lanes: {} };
for (i; i < length; i++) {
var item = data[i];
addToLane(chart, item);
}
return collapseLanes(chart);
};
var collapseLanes = function (chart) {
var lanes = [], items = [], laneId = 0;
var now = new Date();
for (var laneName in chart.lanes) {
var lane = chart.lanes[laneName];
for (var i = 0; i < lane.length; i++) {
var subLane = lane[i];
lanes.push({
id: laneId,
label: i === 0 ? laneName : ''
});
for (var j = 0; j < subLane.length; j++) {
var item = subLane[j];
items.push({
id: item.id,
lane: laneId,
start: item.start,
end: item.end,
class: item.end > now ? 'future' : 'past',
desc: item.desc
});
}
laneId++;
}
}
return {lanes: lanes, items: items};
}
var randomNumber = function(min, max) {
return Math.floor(Math.random(0, 1) * (max - min)) + min;
};
var generateRandomWorkItems = function () {
var data = [];
var laneCount = randomNumber(5,7)
, totalWorkItems = randomNumber(20,30)
, startMonth = randomNumber(0,1)
, startDay = randomNumber(1,28)
, totalMonths = randomNumber(4,10);
for (var i = 0; i < laneCount; i++) {
var dt = new Date(2012, startMonth, startDay);
for (var j = 0; j < totalWorkItems; j++) {
var dtS = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate() + randomNumber(1,5), randomNumber(8, 16), 0, 0);
var dateOffset = randomNumber(0,7);
var dt = new Date(dtS.getFullYear(), dtS.getMonth(), dtS.getDate() + dateOffset, randomNumber(dateOffset === 0 ? dtS.getHours() + 2 : 8, 18), 0, 0);
var workItem = {
id: i * totalWorkItems + j,
name: 'work item ' + j,
lane: 'lane ' + i,
start: dtS,
end: dt,
desc: 'This is a description.'
};
data.push(workItem);
}
}
return data;
};
return parseData(generateRandomWorkItems());
};
/**
* Allow library to be used within both the browser and node.js
*/
var root = typeof exports !== "undefined" && exports !== null ? exports : window;
root.randomData = RandomData;
}).call(this);