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 * |
| 15 * @param {Array} plotData Data that will be plotted. It is an array of lines, |
| 16 * where each line is an array of points, and each point is a length-2 array |
| 17 * representing an (x, y) pair. |
| 18 */ |
| 19 function Coordinates(plotData) { |
| 20 this.plotData = plotData; |
| 21 |
| 22 height = window.innerHeight - 16; |
| 23 width = window.innerWidth - 16; |
| 24 |
| 25 this.widthMax = width; |
| 26 this.heightMax = Math.min(400, height - 85); |
| 27 |
| 28 this.processValues_('x'); |
| 29 this.processValues_('y'); |
| 30 } |
| 31 |
| 32 /** |
| 33 * Determines the min/max x or y values in the plot, accounting for some extra |
| 34 * buffer space. |
| 35 * |
| 36 * @param {string} type The type of value to process, either 'x' or 'y'. |
| 37 */ |
| 38 Coordinates.prototype.processValues_ = function (type) { |
| 39 var merged = []; |
| 40 for (var i = 0; i < this.plotData.length; i++) |
| 41 for (var j = 0; j < this.plotData[i].length; j++) { |
| 42 if (type == 'x') |
| 43 merged.push(parseFloat(this.plotData[i][j][0])); // Index 0 is x value. |
| 44 else |
| 45 merged.push(parseFloat(this.plotData[i][j][1])); // Index 1 is y value. |
| 46 } |
| 47 |
| 48 min = merged[0]; |
| 49 max = merged[0]; |
| 50 for (var i = 1; i < merged.length; ++i) { |
| 51 if (isNaN(min) || merged[i] < min) |
| 52 min = merged[i]; |
| 53 if (isNaN(max) || merged[i] > max) |
| 54 max = merged[i]; |
| 55 } |
| 56 |
| 57 var bufferSpace = 0.02 * (max - min); |
| 58 |
| 59 if (type == 'x') { |
| 60 this.xMinValue = min - bufferSpace; |
| 61 this.xMaxValue = max + bufferSpace; |
| 62 } else { |
| 63 this.yMinValue = min - bufferSpace; |
| 64 this.yMaxValue = max + bufferSpace; |
| 65 } |
| 66 }; |
| 67 |
| 68 /** |
| 69 * Difference between horizontal max and min values. |
| 70 * |
| 71 * @return {number} The x value range. |
| 72 */ |
| 73 Coordinates.prototype.xValueRange = function() { |
| 74 return this.xMaxValue - this.xMinValue; |
| 75 }; |
| 76 |
| 77 /** |
| 78 * Difference between vertical max and min values. |
| 79 * |
| 80 * @return {number} The y value range. |
| 81 */ |
| 82 Coordinates.prototype.yValueRange = function() { |
| 83 return this.yMaxValue - this.yMinValue |
| 84 }; |
| 85 |
| 86 /** |
| 87 * Converts horizontal data value to pixel value on canvas. |
| 88 * |
| 89 * @param {number} value The x data value. |
| 90 * @return {number} The corresponding x pixel value on the canvas. |
| 91 */ |
| 92 Coordinates.prototype.xPixel = function(value) { |
| 93 return this.widthMax * ((value - this.xMinValue) / this.xValueRange()); |
| 94 }; |
| 95 |
| 96 /** |
| 97 * Converts vertical data value to pixel value on canvas. |
| 98 * |
| 99 * @param {number} value The y data value. |
| 100 * @return {number} The corresponding y pixel value on the canvas. |
| 101 */ |
| 102 Coordinates.prototype.yPixel = function(value) { |
| 103 if (this.yValueRange() == 0) { |
| 104 // Completely horizontal lines should be centered horizontally. |
| 105 return this.heightMax / 2; |
| 106 } else { |
| 107 return this.heightMax - |
| 108 (this.heightMax * (value - this.yMinValue) / this.yValueRange()); |
| 109 } |
| 110 }; |
| 111 |
| 112 /** |
| 113 * Converts x point on canvas to data value it represents. |
| 114 * |
| 115 * @param {number} position The x pixel value on the canvas. |
| 116 * @return {number} The corresponding x data value. |
| 117 */ |
| 118 Coordinates.prototype.xValue = function(position) { |
| 119 return this.xMinValue + (position / this.widthMax * this.xValueRange()); |
| 120 }; |
| 121 |
| 122 /** |
| 123 * Converts y point on canvas to data value it represents. |
| 124 * |
| 125 * @param {number} position The y pixel value on the canvas. |
| 126 * @return {number} The corresponding y data value. |
| 127 */ |
| 128 Coordinates.prototype.yValue = function(position) { |
| 129 var ratio = this.heightMax / (this.heightMax - position); |
| 130 return this.yMinValue + (this.yValueRange() / ratio); |
| 131 }; |
OLD | NEW |