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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
window.SonarWidgets = {}
//******************* STACK AREA CHART ******************* //
SonarWidgets.StackArea = function (divId) {
this.wDivId = divId;
this.wHeight;
this.wData;
this.wSnapshots;
this.wMetrics;
this.wColors;
this.height = function(height) {
this.wHeight = height;
return this;
}
this.data = function(data) {
this.wData = data;
return this;
}
this.snapshots = function(snapshots) {
this.wSnapshots = snapshots;
return this;
}
this.metrics = function(metrics) {
this.wMetrics = metrics;
return this;
}
this.colors = function(colors) {
this.wColors = colors;
return this;
}
}
SonarWidgets.StackArea.prototype.render = function() {
var trendData = this.wData;
var metrics = this.wMetrics;
var snapshots = this.wSnapshots;
var colors = this.wColors;
var widgetDiv = $(this.wDivId);
var headerFont = "10.5px Arial,Helvetica,sans-serif";
/* Sizing and scales. */
var headerHeight = 40;
var w = widgetDiv.getOffsetParent().getWidth() - 60;
var h = (this.wHeight == null ? 120 : this.wHeight) + headerHeight;
var x = pv.Scale.linear(pv.blend(pv.map(trendData, function(d) {return d;})), function(d) {return d.x}).range(0, w);
var maxY = 0;
for (i=0; i<trendData[0].size(); i++) {
var currentYSum = 0;
for (j=0; j<trendData.size(); j++) {
currentYSum += trendData[j][i].y;
}
if (currentYSum > maxY) { maxY = currentYSum;}
}
var y = pv.Scale.linear(0, maxY).range(0, h-headerHeight);
var idx_numbers = trendData[0].size();
var idx = idx_numbers - 1;
/* The root panel. */
var vis = new pv.Panel()
.canvas(widgetDiv)
.width(w)
.height(h)
.left(20)
.right(20)
.bottom(30)
.top(20)
.strokeStyle("#CCC");
/* X-axis */
vis.add(pv.Rule)
.data(x.ticks())
.left(x)
.bottom(-10)
.height(10)
.anchor("bottom")
.add(pv.Label)
.text(x.tickFormat);
/* Y-axis and ticks. */
vis.add(pv.Rule)
.data(y.ticks())
.bottom(y)
.strokeStyle("rgba(128,128,128,.2)")
.anchor("left")
.add(pv.Label)
.text(y.tickFormat);
/* The stack layout */
var area = vis.add(pv.Layout.Stack)
.layers(trendData)
.x(function(d) {return x(d.x);})
.y(function(d) {return y(d.y);})
.layer
.add(pv.Area)
.fillStyle(function() {return colors[this.parent.index % colors.size()][0];})
.strokeStyle("rgba(128,128,128,.8)")
.add(pv.Dot)
.radius(1);
/* Stack labels. */
vis.add(pv.Panel)
.extend(area.parent)
.add(pv.Area)
.extend(area)
.fillStyle(null)
.strokeStyle(null)
.anchor(function() {return idx == 0 ? "left" : (idx == idx_numbers-1 ? "right" : "center");})
.add(pv.Label)
.visible(function(d) {return this.index == idx && d.y != 0;})
.font(function(d) { return Math.round(5 + Math.sqrt(y(d.y))) + "px sans-serif";})
.textStyle("#FFF")
.text(function(d) {return metrics[this.parent.index] + ": " + d.y;});
/* The date of the selected dot in the header. */
vis.add(pv.Label)
.left(8)
.top(16)
.font(headerFont)
.text(function() {return snapshots[idx].ld;});
/* The event labels */
if (true) {
eventColor = "rgba(75,159,213,1)";
eventHoverColor = "rgba(202,227,242,1)";
vis.add(pv.Line)
.strokeStyle("rgba(0,0,0,.001)")
.data(snapshots)
.left(function(s) {return x(s.d);})
.bottom(0)
.anchor("top")
.add(pv.Dot)
.bottom(-6)
.shape("triangle")
.angle(3.14159265)
.strokeStyle("grey")
.visible(function(s) {return s.e.size() > 0;})
.fillStyle(function() {return this.index == idx ? eventHoverColor : eventColor;})
.add(pv.Dot)
.radius(3)
.visible(function(s) {return s.e.size() > 0 && this.index == idx;})
.left(16)
.top(24)
.shape("triangle")
.fillStyle(function() {return this.index == idx ? eventHoverColor : eventColor;})
.strokeStyle("grey")
.anchor("right")
.add(pv.Label)
.font(headerFont)
.text(function(s) {return s.e.size() == 0 ? "" : s.e[0] + ( s.e[1] ? " (... +" + (s.e.size()-1) + ")" : "");});
}
/* An invisible bar to capture events (without flickering). */
vis.add(pv.Bar)
.fillStyle("rgba(0,0,0,.001)")
.width(w+30)
.height(h+30)
.event("mouseout", function() {
i = -1;
return vis;
})
.event("mousemove", function() {
var mx = x.invert(vis.mouse().x);
idx = pv.search(trendData[0].map(function(d) {return d.x;}), mx);
idx = idx < 0 ? (-idx - 2) : idx;
idx = idx < 0 ? 0 : idx;
return vis;
});
vis.render();
}
//******************* TIMELINE CHART ******************* //
/*
* Displays the evolution of metrics on a line chart, displaying related events.
*
* Parameters of the Timeline class:
* - data: array of arrays, each containing maps {x,y} where x is a (JS) date and y is a number value (representing a metric value at
* a given time). The {x,y} maps must be sorted by ascending date.
* - metrics: array of metric names. The order is important as it defines which array of the "data" parameter represents which metric.
* - snapshots: array of maps {sid,d} where sid is the snapshot id and d is the locale-formatted date of the snapshot. The {sid,d}
* maps must be sorted by ascending date.
* - events: array of maps {sid,d,l[{n}]} where sid is the snapshot id corresponding to an event, d is the (JS) date of the event, and l
* is an array containing the different event names for this date.
* - height: height of the chart area (notice header excluded). Defaults to 80.
*
* Example: displays 2 metrics:
*
<code>
function d(y,m,d,h,min,s) {
return new Date(y,m,d,h,min,s);
}
var data = [
[{x:d(2011,5,15,0,1,0),y:912.00},{x:d(2011,6,21,0,1,0)],
[{x:d(2011,5,15,0,1,0),y:52.20},{x:d(2011,6,21,0,1,0),y:52.10}]
];
var metrics = ["Lines of code","Rules compliance"];
var snapshots = [{sid:1,d:"June 15, 2011 00:01"},{sid:30,d:"July 21, 2011 00:01"}];
var events = [
{sid:1,d:d(2011,5,15,0,1,0),l:[{n:"0.6-SNAPSHOT"},{n:"Sun checks"}]},
{sid:30,d:d(2011,6,21,0,1,0),l:[{n:"0.7-SNAPSHOT"}]}
];
var timeline = new SonarWidgets.Timeline('timeline-chart-20')
.height(160)
.data(data)
.snapshots(snapshots)
.metrics(metrics)
.events(events);
timeline.render();
</code>
*
*/
SonarWidgets.Timeline = function (divId) {
this.wDivId = divId;
this.wHeight;
this.wData;
this.wSnapshots;
this.wMetrics;
this.wEvents;
this.height = function(height) {
this.wHeight = height;
return this;
}
this.data = function(data) {
this.wData = data;
return this;
}
this.snapshots = function(snapshots) {
this.wSnapshots = snapshots;
return this;
}
this.metrics = function(metrics) {
this.wMetrics = metrics;
return this;
}
this.events = function(events) {
this.wEvents = events;
return this;
}
}
SonarWidgets.Timeline.prototype.render = function() {
var trendData = this.wData;
var metrics = this.wMetrics;
var snapshots = this.wSnapshots;
var events = this.wEvents;
var widgetDiv = $(this.wDivId);
var headerFont = "10.5px Arial,Helvetica,sans-serif";
/* Sizing and scales. */
var headerHeight = 4 + Math.max(this.wMetrics.size(), events ? 2 : 1) * 18;
var w = widgetDiv.getOffsetParent().getWidth() - 60;
var h = (this.wHeight == null ? 80 : this.wHeight) + headerHeight;
var yMaxHeight = h-headerHeight;
var x = pv.Scale.linear(pv.blend(pv.map(trendData, function(d) {return d;})), function(d) {return d.x}).range(0, w);
var y = new Array(trendData.size());
for(var i = 0; i < trendData.size(); i++){
y[i]=pv.Scale.linear(trendData[i], function(d) {return d.y;}).range(20, yMaxHeight);
}
var interpolate = "linear"; /* cardinal or linear */
var idx = trendData[0].size() - 1;
/* The root panel. */
var vis = new pv.Panel()
.canvas(widgetDiv)
.width(w)
.height(h)
.left(20)
.right(20)
.bottom(20)
.top(5)
.strokeStyle("#CCC");
/* X-axis */
vis.add(pv.Rule)
.data(x.ticks())
.left(x)
.bottom(-5)
.height(5)
.anchor("bottom")
.add(pv.Label)
.text(x.tickFormat);
/* A panel for each data series. */
var panel = vis.add(pv.Panel)
.data(trendData);
/* The line. */
var line = panel.add(pv.Line)
.data(function(array) {return array;})
.left(function(d) {return x(d.x);})
.bottom(function(d) {var yAxis = y[this.parent.index](d.y); return isNaN(yAxis) ? yMaxHeight : yAxis;})
.interpolate(function() {return interpolate;})
.lineWidth(2);
/* The mouseover dots and label in footer. */
line.add(pv.Dot)
.data(function(d) {return [d[idx]];})
.fillStyle(function() {return line.strokeStyle();})
.strokeStyle("#000")
.size(20)
.lineWidth(1)
.add(pv.Dot)
.radius(3)
.left(10)
.top(function() {return 10 + this.parent.index * 14;})
.anchor("right").add(pv.Label)
.font(headerFont)
.text(function(d) {return metrics[this.parent.index] + ": " + d.y.toFixed(2);});
/* The date of the selected dot in the header. */
vis.add(pv.Label)
.left(w/2)
.top(16)
.font(headerFont)
.text(function() {return snapshots[idx].d;});
/* The event labels */
if (events) {
eventColor = "rgba(75,159,213,1)";
eventHoverColor = "rgba(202,227,242,1)";
vis.add(pv.Line)
.strokeStyle("rgba(0,0,0,.001)")
.data(events)
.left(function(e) {return x(e.d);})
.bottom(0)
.anchor("top")
.add(pv.Dot)
.bottom(6)
.shape("triangle")
.strokeStyle("grey")
.fillStyle(function(e) {return e.sid == snapshots[idx].sid ? eventHoverColor : eventColor;})
.add(pv.Dot)
.radius(3)
.visible(function(e) { return e.sid == snapshots[idx].sid;})
.left(w/2+8)
.top(24)
.shape("triangle")
.fillStyle(function(e) {return e.sid == snapshots[idx].sid ? eventHoverColor : eventColor;})
.strokeStyle("grey")
.anchor("right")
.add(pv.Label)
.font(headerFont)
.text(function(e) {return e.l[0].n + ( e.l[1] ? " (... +" + (e.l.size()-1) + ")" : "");});
}
/* An invisible bar to capture events (without flickering). */
vis.add(pv.Bar)
.fillStyle("rgba(0,0,0,.001)")
.width(w+10)
.event("mouseout", function() {
i = -1;
return vis;
})
.event("mousemove", function() {
var mx = x.invert(vis.mouse().x);
idx = pv.search(trendData[0].map(function(d) {return d.x;}), mx);
idx = idx < 0 ? (-idx - 2) : idx;
idx = idx < 0 ? 0 : idx;
return vis;
});
vis.render();
}
|