-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtimeline.js
More file actions
248 lines (223 loc) · 6.43 KB
/
timeline.js
File metadata and controls
248 lines (223 loc) · 6.43 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
Timeline
Javascript library for visualising event data.
See demo.html for how to use and timeline.md for detailed API.
Copyright Guru Systems Ltd 2014.
Tom Young <tom.young@gurusystems.com>
*/
function Timeline(domID, data, conf)
{
if (conf.sortLines)
sortLines(data, conf);
var d = makeFlotData(data, conf);
var flotConfig = makeFlotConfig(conf);
var flotObject = $.plot(domID, d.flotData, flotConfig);
// Pull the Y axis up to the top so that if the tick labels are links
// we can click on them
//$('.flot-y-axis').css('zIndex',2);
// If someone selects a portion, zoom in
$(domID).bind("plotselected", function (event, ranges) {
flotObject.clearSelection();
flotObject.getOptions().xaxes[0].min = ranges.xaxis.from
flotObject.getOptions().xaxes[0].max = ranges.xaxis.to
flotObject.setupGrid();
flotObject.draw();
});
// If someone double clicks the plot, reset to default view
function reset() {
flotObject.getOptions().xaxes[0].min = null
flotObject.getOptions().xaxes[0].max = null
flotObject.setupGrid();
flotObject.draw();
}
$(domID).dblclick(reset);
// If someone hovers over a point, display some info
var previousPoint = null;
$(domID).bind("plothover", function (event, pos, item) {
if (item) {
if (previousPoint != item.dataIndex) {
previousPoint = item.dataIndex;
$("#timelineTooltip").remove();
var tooltipHtml = d.tooltips[item.seriesIndex][item.dataIndex];
if(tooltipHtml)
showTooltip(item.pageX, item.pageY, tooltipHtml);
}
}
else {
$("#timelineTooltip").fadeOut(400, function () {this.remove(); });;
previousPoint = null;
}
});
return flotObject;
}
function sortLines(data, conf)
/*
There are currently two types of sorting supported:
- specify conf.sortLines.categories = ["one", "two"] to sort lines
by the number of events of category "one" and then by the number of
of category "two" (etc).
- if you set conf.sortLines but don't set conf.sortLines.categories then
we will sort by the total number of events on each line
Specify conf.sortLines.direction = "ascending" or "descending" (default descending)
*/
{
// Score each line based on a weighted count of events in the categories
// specified in conf.sortLines.categories, then modify the conf.lines
// array to have the new order.
// Before we modify conf.lines, make a deep copy in case it has been
// shallow-copied from a client's data.
if (conf.lines === undefined)
conf.lines = [];
else
conf.lines = conf.lines.slice(0);
var scores = [];
for (i=0; i<conf.lines.length; ++i)
scores[i] = 0;
for (i=0; i<data.length; ++i)
{
// Check that the lines array contains the line from this event
if (conf.lines.indexOf(data[i].line) < 0)
{
conf.lines.push(data[i].line);
scores.push(0);
}
if (conf.sortLines.categories)
{
// If the event is in one of the specified sort categories,
// increase the score, weighting categories earlier in the
// list more highly (by a ratio of data.length for each place).
s = conf.sortLines.categories.indexOf(data[i].category);
if (s >= 0)
{
lineIndex = conf.lines.indexOf(data[i].line);
scores[lineIndex] += Math.pow(data.length, (conf.sortLines.categories.length - s - 1));
}
}
else
// Sort by total number of events on line
{
lineIndex = conf.lines.indexOf(data[i].line);
scores[lineIndex]++;
}
}
// work out the new indices
var order = [];
for (i=0; i<scores.length; ++i)
order[i] = i;
if (conf.sortLines.direction == "ascending")
order.sort(function (a, b) { return scores[b] - scores[a]; });
else
order.sort(function (a, b) { return scores[a] - scores[b]; });
// Sort the lines
var oldLines = conf.lines.slice(0);
for (i=0; i<conf.lines.length; ++i)
conf.lines[i] = oldLines[order[i]];
// Sort the line labels
if (conf.lineLabels)
{
var oldLineLabels = conf.lineLabels.slice(0);
for (i=0; i<conf.lineLabels.length; ++i)
conf.lineLabels[i] = oldLineLabels[order[i]];
}
}
function makeFlotData(data, conf)
{
var categories = [];
if (conf.lines === undefined)
conf.lines = [];
var flotData = []; // one object per category
var tooltips = [];
function getCategoryIndex(category)
{
var y = categories.indexOf(category);
if (y >= 0)
return y;
else
{
jQuery.extend(conf.categories[category], {'data': []});
flotData.push(conf.categories[category]);
tooltips.push([]);
return categories.push(category) - 1;
}
}
function getLineIndex(line)
{
var y = conf.lines.indexOf(line);
if (y >= 0)
return y;
else
return conf.lines.push(line) - 1;
}
for (i=0; i<data.length; ++i)
{
var iCategory = getCategoryIndex(data[i].category);
var iLine = getLineIndex(data[i].line);
flotData[iCategory].data.push([data[i].time, iLine]);
if (data[i].endTime !== undefined)
{
tooltips[iCategory].push(data[i].label + " started");
flotData[iCategory].data.push([data[i].endTime, iLine]);
tooltips[iCategory].push(data[i].label + " ended");
}
else
tooltips[iCategory].push(data[i].label);
flotData[iCategory].data.push(null);
tooltips[iCategory].push(null);
}
return {"flotData": flotData, "tooltips": tooltips};
}
function makeFlotConfig(conf)
{
var tz = 'browser';
if (conf.timezone !== undefined)
tz = conf.timezone;
var d = {
xaxis: {
mode: "time",
timezone: tz,
tickLength: 5,
position: "top",
},
yaxis: { max: conf.lines.length },
selection: {mode: "x", color: "green" },
lines: { show: true , lineWidth: 3},
points: { show: true, fillColor: false},
shadowSize: 0,
grid: {
borderWidth: {top:1, bottom:0, left:0, right:0},
labelMargin: 10,
hoverable: true,
clickable: true
},
}
if (conf.lineLabels)
{
d.yaxis.ticks = [];
for (iLine=0; iLine<conf.lines.length; iLine++)
if (iLine < conf.lineLabels.length && conf.lineLabels[iLine] !== undefined)
d.yaxis.ticks.push([iLine, conf.lineLabels[iLine]]);
else
// Run out of labels, try using the line index itself (as a string)
d.yaxis.ticks.push([iLine, "" + conf.lines[iLine]]);
}
else
{
// disable labeling
d.yaxis.tickSize = 1;
d.yaxis.tickFormatter = function(val, axis) { return ''; };
}
return d;
};
function showTooltip(x, y, contents) {
$('<div id="timelineTooltip">' + contents + '</div>').css( {
position: 'absolute',
display: 'none',
top: y + 5,
left: x + 5,
border: "1px solid #ddf",
padding: "10px",
"background-color": "#efd",
margin: 0,
"font-size": "smaller",
}).appendTo("body").fadeIn(200);
}