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 * @fileoverview Class and functions to handle positioning of plot data points. |
| 9 */ |
| 10 |
| 11 /** |
| 12 * Class that handles plot data positioning. |
| 13 * @constructor |
| 14 * @param {Array.<Array.<Array.<number>>>>} plotData Data that will be plotted. |
| 15 * It is an array of lines, where each line is an array of points, and each |
| 16 * point is a length-2 array representing an (x, y) pair. |
| 17 */ |
| 18 function Coordinates(plotData) { |
| 19 this.plotData = plotData; |
| 20 |
| 21 height = window.innerHeight - 16; |
| 22 width = window.innerWidth - 16; |
| 23 |
| 24 this.widthMax = width; |
| 25 this.heightMax = Math.min(400, height - 85); |
| 26 |
| 27 this.processValues_('x'); |
| 28 this.processValues_('y'); |
| 29 } |
| 30 |
| 31 /** |
| 32 * Determines the min/max x or y values in the plot, accounting for some extra |
| 33 * buffer space. |
| 34 * @param {string} type The type of value to process, either 'x' or 'y'. |
| 35 */ |
| 36 Coordinates.prototype.processValues_ = function (type) { |
| 37 var merged = []; |
| 38 for (var i = 0; i < this.plotData.length; i++) |
| 39 for (var j = 0; j < this.plotData[i].length; j++) { |
| 40 if (type == 'x') |
| 41 merged.push(parseFloat(this.plotData[i][j][0])); // Index 0 is x value. |
| 42 else |
| 43 merged.push(parseFloat(this.plotData[i][j][1])); // Index 1 is y value. |
| 44 } |
| 45 |
| 46 min = merged[0]; |
| 47 max = merged[0]; |
| 48 for (var i = 1; i < merged.length; ++i) { |
| 49 if (isNaN(min) || merged[i] < min) |
| 50 min = merged[i]; |
| 51 if (isNaN(max) || merged[i] > max) |
| 52 max = merged[i]; |
| 53 } |
| 54 |
| 55 var bufferSpace = 0.02 * (max - min); |
| 56 |
| 57 if (type == 'x') { |
| 58 this.xBufferSpace_ = bufferSpace; |
| 59 this.xMinValue_ = min; |
| 60 this.xMaxValue_ = max; |
| 61 } else { |
| 62 this.yBufferSpace_ = bufferSpace; |
| 63 this.yMinValue_ = min; |
| 64 this.yMaxValue_ = max; |
| 65 } |
| 66 }; |
| 67 |
| 68 /** |
| 69 * Difference between horizontal upper and lower limit values. |
| 70 * @return {number} The x value range. |
| 71 */ |
| 72 Coordinates.prototype.xValueRange = function() { |
| 73 return this.xUpperLimitValue() - this.xLowerLimitValue(); |
| 74 }; |
| 75 |
| 76 /** |
| 77 * Difference between vertical upper and lower limit values. |
| 78 * @return {number} The y value range. |
| 79 */ |
| 80 Coordinates.prototype.yValueRange = function() { |
| 81 return this.yUpperLimitValue() - this.yLowerLimitValue(); |
| 82 }; |
| 83 |
| 84 /** |
| 85 * Converts horizontal data value to pixel value on canvas. |
| 86 * @param {number} value The x data value. |
| 87 * @return {number} The corresponding x pixel value on the canvas. |
| 88 */ |
| 89 Coordinates.prototype.xPixel = function(value) { |
| 90 return this.widthMax * |
| 91 ((value - this.xLowerLimitValue()) / this.xValueRange()); |
| 92 }; |
| 93 |
| 94 /** |
| 95 * Converts vertical data value to pixel value on canvas. |
| 96 * @param {number} value The y data value. |
| 97 * @return {number} The corresponding y pixel value on the canvas. |
| 98 */ |
| 99 Coordinates.prototype.yPixel = function(value) { |
| 100 if (this.yValueRange() == 0) { |
| 101 // Completely horizontal lines should be centered horizontally. |
| 102 return this.heightMax / 2; |
| 103 } else { |
| 104 return this.heightMax - |
| 105 (this.heightMax * |
| 106 (value - this.yLowerLimitValue()) / this.yValueRange()); |
| 107 } |
| 108 }; |
| 109 |
| 110 /** |
| 111 * Converts x point on canvas to data value it represents. |
| 112 * @param {number} position The x pixel value on the canvas. |
| 113 * @return {number} The corresponding x data value. |
| 114 */ |
| 115 Coordinates.prototype.xValue = function(position) { |
| 116 return this.xLowerLimitValue() + |
| 117 (position / this.widthMax * this.xValueRange()); |
| 118 }; |
| 119 |
| 120 /** |
| 121 * Converts y point on canvas to data value it represents. |
| 122 * @param {number} position The y pixel value on the canvas. |
| 123 * @return {number} The corresponding y data value. |
| 124 */ |
| 125 Coordinates.prototype.yValue = function(position) { |
| 126 var ratio = this.heightMax / (this.heightMax - position); |
| 127 return this.yLowerLimitValue() + (this.yValueRange() / ratio); |
| 128 }; |
| 129 |
| 130 /** |
| 131 * Returns the minimum x value of all the data points. |
| 132 * @return {number} The minimum x value of all the data points. |
| 133 */ |
| 134 Coordinates.prototype.xMinValue = function() { |
| 135 return this.xMinValue_; |
| 136 }; |
| 137 |
| 138 /** |
| 139 * Returns the maximum x value of all the data points. |
| 140 * @return {number} The maximum x value of all the data points. |
| 141 */ |
| 142 Coordinates.prototype.xMaxValue = function() { |
| 143 return this.xMaxValue_; |
| 144 }; |
| 145 |
| 146 /** |
| 147 * Returns the minimum y value of all the data points. |
| 148 * @return {number} The minimum y value of all the data points. |
| 149 */ |
| 150 Coordinates.prototype.yMinValue = function() { |
| 151 return this.yMinValue_; |
| 152 }; |
| 153 |
| 154 /** |
| 155 * Returns the maximum y value of all the data points. |
| 156 * @return {number} The maximum y value of all the data points. |
| 157 */ |
| 158 Coordinates.prototype.yMaxValue = function() { |
| 159 return this.yMaxValue_; |
| 160 }; |
| 161 |
| 162 /** |
| 163 * Returns the x value at the lower limit of the bounding box of the canvas. |
| 164 * @return {number} The x value at the lower limit of the bounding box of |
| 165 * the canvas. |
| 166 */ |
| 167 Coordinates.prototype.xLowerLimitValue = function() { |
| 168 return this.xMinValue_ - this.xBufferSpace_; |
| 169 }; |
| 170 |
| 171 /** |
| 172 * Returns the x value at the upper limit of the bounding box of the canvas. |
| 173 * @return {number} The x value at the upper limit of the bounding box of |
| 174 * the canvas. |
| 175 */ |
| 176 Coordinates.prototype.xUpperLimitValue = function() { |
| 177 return this.xMaxValue_ + this.xBufferSpace_; |
| 178 }; |
| 179 |
| 180 /** |
| 181 * Returns the y value at the lower limit of the bounding box of the canvas. |
| 182 * @return {number} The y value at the lower limit of the bounding box of |
| 183 * the canvas. |
| 184 */ |
| 185 Coordinates.prototype.yLowerLimitValue = function() { |
| 186 return this.yMinValue_ - this.yBufferSpace_; |
| 187 }; |
| 188 |
| 189 /** |
| 190 * Returns the y value at the upper limit of the bounding box of the canvas. |
| 191 * @return {number} The y value at the upper limit of the bounding box of |
| 192 * the canvas. |
| 193 */ |
| 194 Coordinates.prototype.yUpperLimitValue = function() { |
| 195 return this.yMaxValue_ + this.yBufferSpace_; |
| 196 }; |
OLD | NEW |