New perf dashboard shows too much space around interesting data points
[WebKit-https.git] / Websites / perf.webkit.org / public / v2 / interactive-chart.js
1 App.InteractiveChartComponent = Ember.Component.extend({
2     chartData: null,
3     showXAxis: true,
4     showYAxis: true,
5     interactive: false,
6     enableSelection: true,
7     classNames: ['chart'],
8     init: function ()
9     {
10         this._super();
11         this._needsConstruction = true;
12         this._eventHandlers = [];
13         $(window).resize(this._updateDimensionsIfNeeded.bind(this));
14     },
15     chartDataDidChange: function ()
16     {
17         var chartData = this.get('chartData');
18         if (!chartData)
19             return;
20         this._needsConstruction = true;
21         this._totalWidth = undefined;
22         this._totalHeight = undefined;
23         this._constructGraphIfPossible(chartData);
24     }.observes('chartData').on('init'),
25     didInsertElement: function ()
26     {
27         var chartData = this.get('chartData');
28         if (chartData)
29             this._constructGraphIfPossible(chartData);
30
31         if (this.get('interactive')) {
32             var element = this.get('element');
33             this._attachEventListener(element, "mousemove", this._mouseMoved.bind(this));
34             this._attachEventListener(element, "mouseleave", this._mouseLeft.bind(this));
35             this._attachEventListener(element, "mousedown", this._mouseDown.bind(this));
36             this._attachEventListener($(element).parents("[tabindex]"), "keydown", this._keyPressed.bind(this));
37         }
38     },
39     willClearRender: function ()
40     {
41         this._eventHandlers.forEach(function (item) {
42             $(item[0]).off(item[1], item[2]);
43         })
44     },
45     _attachEventListener: function(target, eventName, listener)
46     {
47         this._eventHandlers.push([target, eventName, listener]);
48         $(target).on(eventName, listener);
49     },
50     _constructGraphIfPossible: function (chartData)
51     {
52         if (!this._needsConstruction || !this.get('element'))
53             return;
54
55         var element = this.get('element');
56
57         this._x = d3.time.scale();
58         this._y = d3.scale.linear();
59
60         if (this._svgElement)
61             this._svgElement.remove();
62         this._svgElement = d3.select(element).append("svg")
63                 .attr("width", "100%")
64                 .attr("height", "100%");
65
66         var svg = this._svg = this._svgElement.append("g");
67
68         var clipId = element.id + "-clip";
69         this._clipPath = svg.append("defs").append("clipPath")
70             .attr("id", clipId)
71             .append("rect");
72
73         if (this.get('showXAxis')) {
74             this._xAxis = d3.svg.axis().scale(this._x).orient("bottom").ticks(6);
75             this._xAxisLabels = svg.append("g")
76                 .attr("class", "x axis");
77         }
78
79         var isInteractive = this.get('interactive');
80         if (this.get('showYAxis')) {
81             this._yAxis = d3.svg.axis().scale(this._y).orient("left").ticks(6).tickFormat(chartData.formatter);
82             
83             this._yAxisLabels = svg.append('g').attr('class', 'y axis' + (isInteractive ? ' interactive' : ''));
84             if (isInteractive) {
85                 var self = this;
86                 this._yAxisLabels.on('click', function () { self.toggleProperty('showFullYAxis'); });
87             }
88         }
89
90         this._clippedContainer = svg.append("g")
91             .attr("clip-path", "url(#" + clipId + ")");
92
93         var xScale = this._x;
94         var yScale = this._y;
95         this._timeLine = d3.svg.line()
96             .x(function(point) { return xScale(point.time); })
97             .y(function(point) { return yScale(point.value); });
98
99         this._confidenceArea = d3.svg.area()
100 //            .interpolate("cardinal")
101             .x(function(point) { return xScale(point.time); })
102             .y0(function(point) { return point.interval ? yScale(point.interval[0]) : null; })
103             .y1(function(point) { return point.interval ? yScale(point.interval[1]) : null; });
104
105         this._paths = [];
106         this._areas = [];
107         this._dots = [];
108         this._highlights = null;
109
110         this._currentTimeSeries = chartData.current;
111         this._currentTimeSeriesData = this._currentTimeSeries.series();
112         this._baselineTimeSeries = chartData.baseline;
113         this._targetTimeSeries = chartData.target;
114         this._movingAverageTimeSeries = chartData.movingAverage;
115
116         this._yAxisUnit = chartData.unit;
117
118         if (this._baselineTimeSeries) {
119             this._paths.push(this._clippedContainer
120                 .append("path")
121                 .datum(this._baselineTimeSeries.series())
122                 .attr("class", "baseline"));
123         }
124         if (this._targetTimeSeries) {
125             this._paths.push(this._clippedContainer
126                 .append("path")
127                 .datum(this._targetTimeSeries.series())
128                 .attr("class", "target"));
129         }
130
131         var movingAverageIsVisible = this._movingAverageTimeSeries;
132         var foregroundClass = movingAverageIsVisible ? '' : ' foreground';
133         this._areas.push(this._clippedContainer
134             .append("path")
135             .datum(this._currentTimeSeriesData)
136             .attr("class", "area" + foregroundClass));
137
138         this._paths.push(this._clippedContainer
139             .append("path")
140             .datum(this._currentTimeSeriesData)
141             .attr("class", "current" + foregroundClass));
142
143         this._dots.push(this._clippedContainer
144             .selectAll(".dot")
145                 .data(this._currentTimeSeriesData)
146             .enter().append("circle")
147                 .attr("class", "dot" + foregroundClass)
148                 .attr("r", this.get('chartPointRadius') || 1));
149
150         if (movingAverageIsVisible) {
151             this._paths.push(this._clippedContainer
152                 .append("path")
153                 .datum(this._movingAverageTimeSeries.series())
154                 .attr("class", "movingAverage"));
155
156             this._areas.push(this._clippedContainer
157                 .append("path")
158                 .datum(this._movingAverageTimeSeries.series())
159                 .attr("class", "envelope"));
160         }
161
162         if (isInteractive) {
163             this._currentItemLine = this._clippedContainer
164                 .append("line")
165                 .attr("class", "current-item");
166
167             this._currentItemCircle = this._clippedContainer
168                 .append("circle")
169                 .attr("class", "dot current-item")
170                 .attr("r", 3);
171         }
172
173         this._brush = null;
174         if (this.get('enableSelection')) {
175             this._brush = d3.svg.brush()
176                 .x(this._x)
177                 .on("brush", this._brushChanged.bind(this));
178
179             this._brushRect = this._clippedContainer
180                 .append("g")
181                 .attr("class", "x brush");
182         }
183
184         this._updateDomain();
185         this._updateDimensionsIfNeeded();
186
187         // Work around the fact the brush isn't set if we updated it synchronously here.
188         setTimeout(this._selectionChanged.bind(this), 0);
189
190         setTimeout(this._selectedItemChanged.bind(this), 0);
191
192         this._needsConstruction = false;
193
194         this._highlightedItemsChanged();
195         this._rangesChanged();
196     },
197     _updateDomain: function ()
198     {
199         var xDomain = this.get('domain');
200         var intrinsicXDomain = this._computeXAxisDomain(this._currentTimeSeriesData);
201         if (!xDomain)
202             xDomain = intrinsicXDomain;
203         var yDomain = this._computeYAxisDomain(xDomain[0], xDomain[1]);
204
205         var currentXDomain = this._x.domain();
206         var currentYDomain = this._y.domain();
207         if (currentXDomain && App.domainsAreEqual(currentXDomain, xDomain)
208             && currentYDomain && App.domainsAreEqual(currentYDomain, yDomain))
209             return currentDomain;
210
211         this._x.domain(xDomain);
212         this._y.domain(yDomain);
213         return xDomain;
214     },
215     _updateDimensionsIfNeeded: function (newSelection)
216     {
217         var element = $(this.get('element'));
218
219         var newTotalWidth = element.width();
220         var newTotalHeight = element.height();
221         if (this._totalWidth == newTotalWidth && this._totalHeight == newTotalHeight)
222             return;
223
224         this._totalWidth = newTotalWidth;
225         this._totalHeight = newTotalHeight;
226
227         if (!this._rem)
228             this._rem = parseFloat(getComputedStyle(document.documentElement).fontSize);
229         var rem = this._rem;
230
231         var padding = 0.5 * rem;
232         var margin = {top: padding, right: padding, bottom: padding, left: padding};
233         if (this._xAxis)
234             margin.bottom += rem;
235         if (this._yAxis)
236             margin.left += 3 * rem;
237
238         this._margin = margin;
239         this._contentWidth = this._totalWidth - margin.left - margin.right;
240         this._contentHeight = this._totalHeight - margin.top - margin.bottom;
241
242         this._svg.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
243
244         this._clipPath
245             .attr("width", this._contentWidth)
246             .attr("height", this._contentHeight);
247
248         this._x.range([0, this._contentWidth]);
249         this._y.range([this._contentHeight, 0]);
250
251         if (this._xAxis) {
252             this._xAxis.tickSize(-this._contentHeight);
253             this._xAxisLabels.attr("transform", "translate(0," + this._contentHeight + ")");
254         }
255
256         if (this._yAxis)
257             this._yAxis.tickSize(-this._contentWidth);
258
259         if (this._currentItemLine) {
260             this._currentItemLine
261                 .attr("y1", 0)
262                 .attr("y2", margin.top + this._contentHeight);
263         }
264
265         this._relayoutDataAndAxes(this._currentSelection());
266     },
267     _updateBrush: function ()
268     {
269         this._brushRect
270             .call(this._brush)
271         .selectAll("rect")
272             .attr("y", 1)
273             .attr("height", this._contentHeight - 2);
274         this._updateSelectionToolbar();
275     },
276     _relayoutDataAndAxes: function (selection)
277     {
278         var timeline = this._timeLine;
279         this._paths.forEach(function (path) { path.attr("d", timeline); });
280
281         var confidenceArea = this._confidenceArea;
282         this._areas.forEach(function (path) { path.attr("d", confidenceArea); });
283
284         var xScale = this._x;
285         var yScale = this._y;
286         this._dots.forEach(function (dot) {
287             dot
288                 .attr("cx", function(measurement) { return xScale(measurement.time); })
289                 .attr("cy", function(measurement) { return yScale(measurement.value); });
290         });
291         this._updateHighlightPositions();
292         this._updateRangeBarRects();
293
294         if (this._brush) {
295             if (selection)
296                 this._brush.extent(selection);
297             else
298                 this._brush.clear();
299             this._updateBrush();
300         }
301
302         this._updateCurrentItemIndicators();
303
304         if (this._xAxis)
305             this._xAxisLabels.call(this._xAxis);
306         if (!this._yAxis)
307             return;
308
309         this._yAxisLabels.call(this._yAxis);
310         if (this._yAxisUnitContainer)
311             this._yAxisUnitContainer.remove();
312         var x = - 3.2 * this._rem;
313         var y = this._contentHeight / 2;
314         this._yAxisUnitContainer = this._yAxisLabels.append("text")
315             .attr("transform", "rotate(90 0 0) translate(" + y + ", " + (-x) + ")")
316             .style("text-anchor", "middle")
317             .text(this._yAxisUnit);
318     },
319     _updateHighlightPositions: function () {
320         if (!this._highlights)
321             return;
322
323         var xScale = this._x;
324         var yScale = this._y;
325         this._highlights
326             .attr("cy", function(point) { return yScale(point.value); })
327             .attr("cx", function(point) { return xScale(point.time); });
328     },
329     _computeXAxisDomain: function (timeSeries)
330     {
331         var extent = d3.extent(timeSeries, function(point) { return point.time; });
332         var margin = 3600 * 1000; // Use x.inverse to compute the right amount from a margin.
333         return [+extent[0] - margin, +extent[1] + margin];
334     },
335     _computeYAxisDomain: function (startTime, endTime)
336     {
337         var shouldShowFullYAxis = this.get('showFullYAxis');
338         var range = this._minMaxForAllTimeSeries(startTime, endTime, !shouldShowFullYAxis);
339         var min = range[0];
340         var max = range[1];
341         if (max < min)
342             min = max = 0;
343         else if (shouldShowFullYAxis)
344             min = Math.min(min, 0);
345         var diff = max - min;
346         var margin = diff * 0.05;
347
348         yExtent = [min - margin, max + margin];
349         return yExtent;
350     },
351     _minMaxForAllTimeSeries: function (startTime, endTime, ignoreOutliners)
352     {
353         var seriesList = [this._currentTimeSeries, this._movingAverageTimeSeries, this._baselineTimeSeries, this._targetTimeSeries];
354         var min = Infinity;
355         var max = -Infinity;
356         for (var i = 0; i < seriesList.length; i++) {
357             if (seriesList[i]) {
358                 var minMax = seriesList[i].minMaxForTimeRange(startTime, endTime, ignoreOutliners);
359                 min = Math.min(min, minMax[0]);
360                 max = Math.max(max, minMax[1]);
361             }
362         }
363         return [min, max];
364     },
365     _currentSelection: function ()
366     {
367         return this._brush && !this._brush.empty() ? this._brush.extent() : null;
368     },
369     _domainChanged: function ()
370     {
371         var selection = this._currentSelection() || this.get('sharedSelection');
372         var newXDomain = this._updateDomain();
373
374         if (selection && newXDomain && selection[0] <= newXDomain[0] && newXDomain[1] <= selection[1])
375             selection = null; // Otherwise the user has no way of clearing the selection.
376
377         this._relayoutDataAndAxes(selection);
378     }.observes('domain', 'showFullYAxis'),
379     _selectionChanged: function ()
380     {
381         this._updateSelection(this.get('selection'));
382     }.observes('selection'),
383     _updateSelection: function (newSelection)
384     {
385         if (!this._brush)
386             return;
387
388         var currentSelection = this._currentSelection();
389         if (newSelection && currentSelection && App.domainsAreEqual(newSelection, currentSelection))
390             return;
391
392         var domain = this._x.domain();
393         if (!newSelection || App.domainsAreEqual(domain, newSelection))
394             this._brush.clear();
395         else
396             this._brush.extent(newSelection);
397         this._updateBrush();
398
399         this._setCurrentSelection(newSelection);
400     },
401     _brushChanged: function ()
402     {
403         if (this._brush.empty()) {
404             if (!this._brushExtent)
405                 return;
406
407             this._setCurrentSelection(undefined);
408
409             // Avoid locking the indicator in _mouseDown when the brush was cleared in the same mousedown event.
410             this._brushJustChanged = true;
411             var self = this;
412             setTimeout(function () {
413                 self._brushJustChanged = false;
414             }, 0);
415
416             return;
417         }
418
419         this._setCurrentSelection(this._brush.extent());
420     },
421     _keyPressed: function (event)
422     {
423         if (!this._currentItemIndex || this._currentSelection())
424             return;
425
426         var newIndex;
427         switch (event.keyCode) {
428         case 37: // Left
429             newIndex = this._currentItemIndex - 1;
430             break;
431         case 39: // Right
432             newIndex = this._currentItemIndex + 1;
433             break;
434         case 38: // Up
435         case 40: // Down
436         default:
437             return;
438         }
439
440         // Unlike mousemove, keydown shouldn't move off the edge.
441         if (this._currentTimeSeriesData[newIndex])
442             this._setCurrentItem(newIndex);
443     },
444     _mouseMoved: function (event)
445     {
446         if (!this._margin || this._currentSelection() || this._currentItemLocked)
447             return;
448
449         var point = this._mousePointInGraph(event);
450
451         this._selectClosestPointToMouseAsCurrentItem(point);
452     },
453     _mouseLeft: function (event)
454     {
455         if (!this._margin || this._currentItemLocked)
456             return;
457
458         this._selectClosestPointToMouseAsCurrentItem(null);
459     },
460     _mouseDown: function (event)
461     {
462         if (!this._margin || this._currentSelection() || this._brushJustChanged)
463             return;
464
465         var point = this._mousePointInGraph(event);
466         if (!point)
467             return;
468
469         if (this._currentItemLocked) {
470             this._currentItemLocked = false;
471             this.set('selectedItem', null);
472             return;
473         }
474
475         this._currentItemLocked = true;
476         this._selectClosestPointToMouseAsCurrentItem(point);
477     },
478     _mousePointInGraph: function (event)
479     {
480         var offset = $(this.get('element')).offset();
481         if (!offset || !$(event.target).closest('svg').length)
482             return null;
483
484         var point = {
485             x: event.pageX - offset.left - this._margin.left,
486             y: event.pageY - offset.top - this._margin.top
487         };
488
489         var xScale = this._x;
490         var yScale = this._y;
491         var xDomain = xScale.domain();
492         var yDomain = yScale.domain();
493         if (point.x >= xScale(xDomain[0]) && point.x <= xScale(xDomain[1])
494             && point.y <= yScale(yDomain[0]) && point.y >= yScale(yDomain[1]))
495             return point;
496
497         return null;
498     },
499     _selectClosestPointToMouseAsCurrentItem: function (point)
500     {
501         var xScale = this._x;
502         var yScale = this._y;
503         var distanceHeuristics = function (m) {
504             var mX = xScale(m.time);
505             var mY = yScale(m.value);
506             var xDiff = mX - point.x;
507             var yDiff = mY - point.y;
508             return xDiff * xDiff + yDiff * yDiff / 16; // Favor horizontal affinity.
509         };
510         distanceHeuristics = function (m) {
511             return Math.abs(xScale(m.time) - point.x);
512         }
513
514         var newItemIndex;
515         if (point && !this._currentSelection()) {
516             var distances = this._currentTimeSeriesData.map(distanceHeuristics);
517             var minDistance = Number.MAX_VALUE;
518             for (var i = 0; i < distances.length; i++) {
519                 if (distances[i] < minDistance) {
520                     newItemIndex = i;
521                     minDistance = distances[i];
522                 }
523             }
524         }
525
526         this._setCurrentItem(newItemIndex);
527         this._updateSelectionToolbar();
528     },
529     _currentTimeChanged: function ()
530     {
531         if (!this._margin || this._currentSelection() || this._currentItemLocked)
532             return
533
534         var currentTime = this.get('currentTime');
535         if (currentTime) {
536             for (var i = 0; i < this._currentTimeSeriesData.length; i++) {
537                 var point = this._currentTimeSeriesData[i];
538                 if (point.time >= currentTime) {
539                     this._setCurrentItem(i, /* doNotNotify */ true);
540                     return;
541                 }
542             }
543         }
544         this._setCurrentItem(undefined, /* doNotNotify */ true);
545     }.observes('currentTime'),
546     _setCurrentItem: function (newItemIndex, doNotNotify)
547     {
548         if (newItemIndex === this._currentItemIndex) {
549             if (this._currentItemLocked)
550                 this.set('selectedItem', this.get('currentItem') ? this.get('currentItem').measurement.id() : null);
551             return;
552         }
553
554         var newItem = this._currentTimeSeriesData[newItemIndex];
555         this._brushExtent = undefined;
556         this._currentItemIndex = newItemIndex;
557
558         if (!newItem) {
559             this._currentItemLocked = false;
560             this.set('selectedItem', null);
561         }
562
563         this._updateCurrentItemIndicators();
564
565         if (!doNotNotify)
566             this.set('currentTime', newItem ? newItem.time : undefined);
567
568         this.set('currentItem', newItem);
569         if (this._currentItemLocked)
570             this.set('selectedItem', newItem ? newItem.measurement.id() : null);
571     },
572     _selectedItemChanged: function ()
573     {
574         if (!this._margin)
575             return;
576
577         var selectedId = this.get('selectedItem');
578         var currentItem = this.get('currentItem');
579         if (currentItem && currentItem.measurement.id() == selectedId)
580             return;
581
582         var series = this._currentTimeSeriesData;
583         var selectedItemIndex = undefined;
584         for (var i = 0; i < series.length; i++) {
585             if (series[i].measurement.id() == selectedId) {
586                 this._updateSelection(null);
587                 this._currentItemLocked = true;
588                 this._setCurrentItem(i);
589                 this._updateSelectionToolbar();
590                 return;
591             }
592         }
593     }.observes('selectedItem').on('init'),
594     _highlightedItemsChanged: function () {
595         var highlightedItems = this.get('highlightedItems');
596
597         if (!this._clippedContainer || !highlightedItems)
598             return;
599
600         var data = this._currentTimeSeriesData.filter(function (item) { return highlightedItems[item.measurement.id()]; });
601
602         if (this._highlights)
603             this._highlights.remove();
604         this._highlights = this._clippedContainer
605             .selectAll(".highlight")
606                 .data(data)
607             .enter().append("circle")
608                 .attr("class", "highlight")
609                 .attr("r", (this.get('chartPointRadius') || 1) * 1.8);
610
611         this._updateHighlightPositions();
612     }.observes('highlightedItems'),
613     _rangesChanged: function ()
614     {
615         if (!this._currentTimeSeries)
616             return;
617
618         function midPoint(firstPoint, secondPoint) {
619             if (firstPoint && secondPoint)
620                 return (+firstPoint.time + +secondPoint.time) / 2;
621             if (firstPoint)
622                 return firstPoint.time;
623             return secondPoint.time;
624         }
625         var currentTimeSeries = this._currentTimeSeries;
626         var linkRoute = this.get('rangeRoute');
627         this.set('rangeBars', (this.get('ranges') || []).map(function (range) {
628             var start = currentTimeSeries.findPointByMeasurementId(range.get('startRun'));
629             var end = currentTimeSeries.findPointByMeasurementId(range.get('endRun'));
630             return Ember.Object.create({
631                 startTime: midPoint(currentTimeSeries.previousPoint(start), start),
632                 endTime: midPoint(end, currentTimeSeries.nextPoint(end)),
633                 range: range,
634                 left: null,
635                 right: null,
636                 rowIndex: null,
637                 top: null,
638                 bottom: null,
639                 linkRoute: linkRoute,
640                 linkId: range.get('id'),
641                 label: range.get('label'),
642             });
643         }));
644
645         this._updateRangeBarRects();
646     }.observes('ranges'),
647     _updateRangeBarRects: function () {
648         var rangeBars = this.get('rangeBars');
649         if (!rangeBars || !rangeBars.length)
650             return;
651
652         var xScale = this._x;
653         var yScale = this._y;
654
655         // Expand the width of each range as needed and sort ranges by the left-edge of ranges.
656         var minWidth = 3;
657         var sortedBars = rangeBars.map(function (bar) {
658             var left = xScale(bar.get('startTime'));
659             var right = xScale(bar.get('endTime'));
660             if (right - left < minWidth) {
661                 left -= minWidth / 2;
662                 right += minWidth / 2;
663             }
664             bar.set('left', left);
665             bar.set('right', right);
666             return bar;
667         }).sort(function (first, second) { return first.get('left') - second.get('left'); });
668
669         // At this point, left edges of all ranges prior to a range R1 is on the left of R1.
670         // Place R1 into a row in which right edges of all ranges prior to R1 is on the left of R1 to avoid overlapping ranges.
671         var rows = [];
672         sortedBars.forEach(function (bar) {
673             var rowIndex = 0;
674             for (; rowIndex < rows.length; rowIndex++) {
675                 var currentRow = rows[rowIndex];
676                 if (currentRow[currentRow.length - 1].get('right') < bar.get('left')) {
677                     currentRow.push(bar);
678                     break;
679                 }
680             }
681             if (rowIndex >= rows.length)
682                 rows.push([bar]);
683             bar.set('rowIndex', rowIndex);
684         });
685         var rowHeight = 0.6 * this._rem;
686         var firstRowTop = this._contentHeight - rows.length * rowHeight;
687         var barHeight = 0.5 * this._rem;
688
689         $(this.get('element')).find('.rangeBarsContainerInlineStyle').css({
690             left: this._margin.left + 'px',
691             top: this._margin.top + firstRowTop + 'px',
692             width: this._contentWidth + 'px',
693             height: rows.length * barHeight + 'px',
694             overflow: 'hidden',
695             position: 'absolute',
696         });
697
698         var margin = this._margin;
699         sortedBars.forEach(function (bar) {
700             var top = bar.get('rowIndex') * rowHeight;
701             var height = barHeight;
702             var left = bar.get('left');
703             var width = bar.get('right') - left;
704             bar.set('inlineStyle', 'left: ' + left + 'px; top: ' + top + 'px; width: ' + width + 'px; height: ' + height + 'px;');
705         });
706     },
707     _updateCurrentItemIndicators: function ()
708     {
709         if (!this._currentItemLine)
710             return;
711
712         var item = this._currentTimeSeriesData[this._currentItemIndex];
713         if (!item) {
714             this._currentItemLine.attr("x1", -1000).attr("x2", -1000);
715             this._currentItemCircle.attr("cx", -1000);
716             return;
717         }
718
719         var x = this._x(item.time);
720         var y = this._y(item.value);
721
722         this._currentItemLine
723             .attr("x1", x)
724             .attr("x2", x);
725
726         this._currentItemCircle
727             .attr("cx", x)
728             .attr("cy", y);
729     },
730     _setCurrentSelection: function (newSelection)
731     {
732         if (this._brushExtent === newSelection)
733             return;
734
735         var points = null;
736         if (newSelection) {
737             points = this._currentTimeSeriesData
738                 .filter(function (point) { return point.time >= newSelection[0] && point.time <= newSelection[1]; });
739             if (!points.length)
740                 points = null;
741         }
742
743         this._brushExtent = newSelection;
744         this._setCurrentItem(undefined);
745         this._updateSelectionToolbar();
746
747         if (!App.domainsAreEqual(this.get('selection'), newSelection))
748             this.set('selection', newSelection);
749         this.set('selectedPoints', points);
750     },
751     _updateSelectionToolbar: function ()
752     {
753         if (!this.get('interactive'))
754             return;
755
756         var selection = this._currentSelection();
757         var selectionToolbar = $(this.get('element')).children('.selection-toolbar');
758         if (selection) {
759             var left = this._x(selection[0]);
760             var right = this._x(selection[1]);
761             selectionToolbar
762                 .css({left: this._margin.left + right, top: this._margin.top + this._contentHeight})
763                 .show();
764         } else
765             selectionToolbar.hide();
766     },
767     actions: {
768         zoom: function ()
769         {
770             this.sendAction('zoom', this._currentSelection());
771             this.set('selection', null);
772         },
773         openRange: function (range)
774         {
775             this.sendAction('openRange', range);
776         },
777     },
778 });