OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 /** | |
6 * @constructor | |
7 * @extends {WebInspector.Object} | |
8 */ | |
9 WebInspector.TimelinePowerOverviewDataProvider = function() | |
10 { | |
11 this._records = []; | |
12 if (WebInspector.experimentsSettings.powerProfiler.isEnabled() && Capabiliti es.canProfilePower) | |
13 WebInspector.powerProfiler.addEventListener(WebInspector.PowerProfiler.E ventTypes.PowerEventRecorded, this._onRecordAdded, this); | |
14 } | |
15 | |
16 WebInspector.TimelinePowerOverviewDataProvider.prototype = { | |
17 /** | |
18 * @return {Array.<PowerEvent>} | |
19 */ | |
20 records : function() | |
21 { | |
22 return this._records; | |
23 }, | |
24 | |
25 _onRecordAdded: function(event) | |
26 { | |
27 this._records.push(event.data); | |
28 }, | |
29 | |
30 __proto__: WebInspector.Object.prototype | |
31 } | |
32 | |
33 /** | |
34 * @constructor | |
35 * @extends {WebInspector.CounterUIBase} | |
pfeldman
2014/03/24 13:41:25
We should figure out whether we want a) power over
Pan
2014/03/25 06:12:31
Previously, I tried to reuse some code from a memo
| |
36 * @param {!WebInspector.TimelinePowerOverview} powerOverView | |
37 * @param {string} graphColor | |
38 * @param {!WebInspector.Counter} counter | |
39 */ | |
40 WebInspector.PowerCounterUI = function(powerOverView, color, counter) | |
41 { | |
42 WebInspector.CounterUIBase.call(this, color, counter); | |
43 | |
44 this._value = powerOverView.element.createChild("span", "timeline-power-text "); | |
45 this._value.style.color = color; | |
46 this._currentValueLabel = WebInspector.UIString("%.2f\u2009watts"); | |
47 this._marker = powerOverView.element.createChild("div", "timeline-power-mark er"); | |
48 this._marker.style.backgroundColor = color; | |
49 this.clearCurrentValueAndMarker(); | |
50 } | |
51 | |
52 WebInspector.PowerCounterUI.prototype = { | |
53 __proto__: WebInspector.CounterUIBase.prototype | |
54 } | |
55 | |
56 /** | |
57 * @constructor | |
58 * @param {!WebInspector.TimelineModel} model | |
59 */ | |
60 WebInspector.TimelinePowerOverviewCalculator = function(model) | |
61 { | |
62 this._model = model; | |
63 } | |
64 | |
65 WebInspector.TimelinePowerOverviewCalculator.prototype = { | |
66 /** | |
67 * @return {number} | |
68 */ | |
69 minimumBoundary : function () | |
70 { | |
71 return this._model.minimumRecordTime(); | |
72 }, | |
73 | |
74 /** | |
75 * @return {number} | |
76 */ | |
77 maximumBoundary : function () | |
78 { | |
79 return this._model.maximumRecordTime(); | |
80 }, | |
81 } | |
82 | |
83 /** | |
84 * @constructor | |
85 * @extends {WebInspector.TimelineOverviewBase} | |
86 * @param {!WebInspector.TimelineModel} model | |
87 */ | |
88 WebInspector.TimelinePowerOverview = function(model) | |
89 { | |
90 WebInspector.TimelineOverviewBase.call(this, model); | |
91 this.element.id = "timeline-overview-power"; | |
92 this._dataProvider = new WebInspector.TimelinePowerOverviewDataProvider(); | |
93 this._counter = new WebInspector.Counter(WebInspector.UIString("value")); | |
94 this._counterUI = new WebInspector.PowerCounterUI(this, "rgba(20,0,0,0.8)", this._counter); | |
95 this._calculator = new WebInspector.TimelinePowerOverviewCalculator(this._mo del); | |
96 | |
97 this._canvas.addEventListener("mousemove", this._onMouseMove.bind(this)); | |
98 } | |
99 | |
100 WebInspector.TimelinePowerOverview.prototype = { | |
101 startTimeline: function() | |
102 { | |
103 if (WebInspector.experimentsSettings.powerProfiler.isEnabled() && Capabi lities.canProfilePower) | |
104 WebInspector.powerProfiler.startProfile(); | |
105 }, | |
106 | |
107 stopTimeline: function() | |
108 { | |
109 if (WebInspector.experimentsSettings.powerProfiler.isEnabled() && Capabi lities.canProfilePower) | |
110 WebInspector.powerProfiler.stopProfile(); | |
111 }, | |
112 | |
113 _onMouseMove: function(event) | |
114 { | |
115 this._counterUI.updateCurrentValue(event.offsetX); | |
116 }, | |
117 | |
118 update: function() | |
119 { | |
120 this.resetCanvas(); | |
121 | |
122 var records = this._dataProvider.records(); | |
123 if (records.length < 2) | |
124 return; | |
125 | |
126 this._counter.reset(); | |
127 | |
128 for(var i = 0; i < records.length - 1; i++) | |
129 this._counter.appendSample(records[i].timestamp, records[i + 1]); | |
130 | |
131 this._counter._calculateVisibleIndexes(this._calculator); | |
132 this._counter._calculateXValues(this._canvas.offsetWidth); | |
133 | |
134 var canvas = this._canvas; | |
135 var ctx = canvas.getContext("2d"); | |
136 const lowerOffset = 3; | |
137 var width = canvas.width; | |
138 var counter = this._counterUI.counter; | |
139 var values = counter.values; | |
140 | |
141 var maxValue; | |
142 var minValue; | |
143 for (var i = counter._minimumIndex; i <= counter._maximumIndex; i++) { | |
144 var value = values[i]; | |
145 if (minValue === undefined || value < minValue) | |
146 minValue = value; | |
147 if (maxValue === undefined || value > maxValue) | |
148 maxValue = value; | |
149 } | |
150 minValue = minValue || 0; | |
151 maxValue = maxValue || 1; | |
152 | |
153 var yValues = this._counterUI.graphYValues; | |
154 | |
155 var maxYRange = maxValue - minValue; | |
156 var yFactor = maxYRange ? (this._canvas.height - lowerOffset) / (maxYRan ge) : 1; | |
157 | |
158 ctx.save(); | |
159 ctx.beginPath(); | |
160 var currentX = 0; | |
161 var initialValue = values[counter._minimumIndex]; | |
162 var currentY = Math.round(this._canvas.height - (initialValue - minValue ) * yFactor); | |
163 ctx.moveTo(currentX, currentY); | |
164 for (var i = counter._minimumIndex; i <= counter._maximumIndex; i++) { | |
165 var x = Math.round(counter.x[i] * this._canvas.width / this._canvas .offsetWidth); | |
166 ctx.lineTo(x, currentY); | |
167 var currentValue = values[i]; | |
168 | |
169 currentY = Math.round(this._canvas.height - (currentValue - minValu e) * yFactor); | |
170 ctx.lineTo(x, currentY); | |
171 yValues[i] = currentY * this._canvas.offsetHeight / this._canvas.he ight; | |
172 } | |
173 ctx.lineTo(width, currentY); | |
174 ctx.lineWidth = 1; | |
175 ctx.strokeStyle = this._counterUI.graphColor; | |
176 ctx.stroke(); | |
177 | |
178 ctx.lineTo(width, this._canvas.height); | |
179 ctx.lineTo(0, this._canvas.height); | |
180 ctx.fillStyle = "rgba(255,192,0, 0.8)"; | |
181 ctx.fill(); | |
182 ctx.closePath(); | |
183 | |
184 ctx.restore(); | |
185 }, | |
186 | |
187 __proto__: WebInspector.TimelineOverviewBase.prototype | |
188 } | |
189 | |
190 | |
OLD | NEW |