OLD | NEW |
| (Empty) |
1 /* | |
2 Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 Use of this source code is governed by a BSD-style license that can be | |
4 found in the LICENSE file. | |
5 */ | |
6 | |
7 /** | |
8 * 'Understands' plot data positioning. | |
9 * @constructor | |
10 * | |
11 * @param {Array} plotData data that will be displayed | |
12 */ | |
13 function Coordinates(plotData, width, height) { | |
14 this.plotData = plotData; | |
15 | |
16 if (!height) | |
17 height = window.innerHeight - 16; | |
18 if (!width) | |
19 width = window.innerWidth - 16; | |
20 | |
21 this.widthMax = width; | |
22 this.heightMax = Math.min(400, height - 85); | |
23 | |
24 this.xMinValue = -0.5; | |
25 this.xMaxValue = (this.plotData[0].length - 1) + 0.5; | |
26 this.processYValues_(); | |
27 } | |
28 | |
29 Coordinates.prototype.processYValues_ = function () { | |
30 var merged = []; | |
31 var mergedErr = []; | |
32 for (var i = 0; i < this.plotData.length; i++) | |
33 for (var j = 0; j < this.plotData[i].length; j++) { | |
34 merged.push(parseFloat(this.plotData[i][j][0])); | |
35 mergedErr.push(parseFloat(this.plotData[i][j][1])); | |
36 } | |
37 var max = Math.max.apply( Math, merged ); | |
38 var min = Math.min.apply( Math, merged ); | |
39 var maxErr = Math.max.apply( Math, mergedErr ); | |
40 | |
41 // If we have a missing value, find the real max and min the hard way. | |
42 if (isNaN(min)) { | |
43 for (var i = 0; i < merged.length; ++i) { | |
44 if (isNaN(min) || merged[i] < min) | |
45 min = merged[i]; | |
46 if (isNaN(max) || merged[i] > max) | |
47 max = merged[i]; | |
48 if (isNaN(maxErr) || mergedErr[i] > maxErr) | |
49 maxErr = mergedErr[i]; | |
50 } | |
51 } | |
52 | |
53 this.yMinValue = min - maxErr; | |
54 this.yMaxValue = max + maxErr; | |
55 }; | |
56 | |
57 /** | |
58 * Difference between horizontal max min values. | |
59 */ | |
60 Coordinates.prototype.xValueRange = function() { | |
61 return this.xMaxValue - this.xMinValue; | |
62 }; | |
63 | |
64 /** | |
65 * Difference between vertical max min values. | |
66 */ | |
67 Coordinates.prototype.yValueRange = function() { | |
68 return this.yMaxValue - this.yMinValue | |
69 }; | |
70 | |
71 /** | |
72 * Converts horizontal data value to pixel value on canvas. | |
73 * @param {number} value horizontal data value | |
74 */ | |
75 Coordinates.prototype.xPoints = function(value) { | |
76 return this.widthMax * ((value - this.xMinValue) / this.xValueRange()); | |
77 }; | |
78 | |
79 /** | |
80 * Converts vertical data value to pixel value on canvas. | |
81 * @param {number} value vertical data value | |
82 */ | |
83 Coordinates.prototype.yPoints = function(value) { | |
84 // yValueRange() can be 0. If it is, place |value| in the middle of | |
85 // the region. | |
86 if (this.yValueRange() == 0) | |
87 return this.heightMax / 2; | |
88 | |
89 // Converts value to canvas Y position in pixels. | |
90 return this.heightMax - this.heightMax * (value - this.yMinValue) / | |
91 this.yValueRange(); | |
92 }; | |
93 | |
94 /** | |
95 * Converts X point on canvas to value it represents. | |
96 * @param {number} position horizontal point on canvas. | |
97 */ | |
98 Coordinates.prototype.xValue = function(position) { | |
99 /* Converts canvas X pixels to value. */ | |
100 return position / this.widthMax * (this.xValueRange()) + this.xMinValue; | |
101 }; | |
102 | |
103 /** | |
104 * Converts Y point on canvas to value it represents. | |
105 * @param {number} position vertical point on canvas. | |
106 */ | |
107 Coordinates.prototype.yValue = function(position) { | |
108 /* Converts canvas Y pixels to value. | |
109 position is point value is from top. | |
110 */ | |
111 var position = this.heightMax - position; | |
112 var ratio = parseFloat(this.heightMax / position); | |
113 return this.yMinValue + this.yValueRange() / ratio; | |
114 }; | |
115 | |
116 /** | |
117 * Converts canvas X pixel to data index. | |
118 * @param {number} xPosition horizontal point on canvas | |
119 */ | |
120 Coordinates.prototype.dataSampleIndex = function(xPosition) { | |
121 var xValue = this.xValue(xPosition); | |
122 var index; | |
123 if (xValue < 0) { | |
124 index = 0; | |
125 } else if (xValue > this.plotData[0].length - 1) { | |
126 index = this.plotData[0].length - 1; | |
127 } else { | |
128 index = xValue.toFixed(0); | |
129 } | |
130 return index; | |
131 }; | |
132 | |
133 Coordinates.prototype.log = function(val) { | |
134 document.getElementById('log').appendChild( | |
135 document.createTextNode(val + '\n')); | |
136 }; | |
OLD | NEW |