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.
8 * @fileoverview Class and functions to handle positioning of plot data points.
12 * Class that handles plot data positioning.
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.
19 function Coordinates(plotData) {
20 this.plotData = plotData;
22 height = window.innerHeight - 16;
23 width = window.innerWidth - 16;
25 this.widthMax = width;
26 this.heightMax = Math.min(400, height - 85);
28 this.processValues_('x');
29 this.processValues_('y');
33 * Determines the min/max x or y values in the plot, accounting for some extra
36 * @param {string} type The type of value to process, either 'x' or 'y'.
38 Coordinates.prototype.processValues_ = function (type) {
40 for (var i = 0; i < this.plotData.length; i++)
41 for (var j = 0; j < this.plotData[i].length; j++) {
43 merged.push(parseFloat(this.plotData[i][j][0])); // Index 0 is x value.
45 merged.push(parseFloat(this.plotData[i][j][1])); // Index 1 is y value.
50 for (var i = 1; i < merged.length; ++i) {
51 if (isNaN(min) || merged[i] < min)
53 if (isNaN(max) || merged[i] > max)
57 var bufferSpace = 0.02 * (max - min);
60 this.xBufferSpace_ = bufferSpace;
61 this.xMinValue_ = min;
62 this.xMaxValue_ = max;
64 this.yBufferSpace_ = bufferSpace;
65 this.yMinValue_ = min;
66 this.yMaxValue_ = max;
71 * Difference between horizontal upper and lower limit values.
73 * @return {number} The x value range.
75 Coordinates.prototype.xValueRange = function() {
76 return this.xUpperLimitValue() - this.xLowerLimitValue();
80 * Difference between vertical upper and lower limit values.
82 * @return {number} The y value range.
84 Coordinates.prototype.yValueRange = function() {
85 return this.yUpperLimitValue() - this.yLowerLimitValue();
89 * Converts horizontal data value to pixel value on canvas.
91 * @param {number} value The x data value.
92 * @return {number} The corresponding x pixel value on the canvas.
94 Coordinates.prototype.xPixel = function(value) {
95 return this.widthMax *
96 ((value - this.xLowerLimitValue()) / this.xValueRange());
100 * Converts vertical data value to pixel value on canvas.
102 * @param {number} value The y data value.
103 * @return {number} The corresponding y pixel value on the canvas.
105 Coordinates.prototype.yPixel = function(value) {
106 if (this.yValueRange() == 0) {
107 // Completely horizontal lines should be centered horizontally.
108 return this.heightMax / 2;
110 return this.heightMax -
112 (value - this.yLowerLimitValue()) / this.yValueRange());
117 * Converts x point on canvas to data value it represents.
119 * @param {number} position The x pixel value on the canvas.
120 * @return {number} The corresponding x data value.
122 Coordinates.prototype.xValue = function(position) {
123 return this.xLowerLimitValue() +
124 (position / this.widthMax * this.xValueRange());
128 * Converts y point on canvas to data value it represents.
130 * @param {number} position The y pixel value on the canvas.
131 * @return {number} The corresponding y data value.
133 Coordinates.prototype.yValue = function(position) {
134 var ratio = this.heightMax / (this.heightMax - position);
135 return this.yLowerLimitValue() + (this.yValueRange() / ratio);
139 * Returns the minimum x value of all the data points.
141 * @return {number} The minimum x value of all the data points.
143 Coordinates.prototype.xMinValue = function() {
144 return this.xMinValue_;
148 * Returns the maximum x value of all the data points.
150 * @return {number} The maximum x value of all the data points.
152 Coordinates.prototype.xMaxValue = function() {
153 return this.xMaxValue_;
157 * Returns the minimum y value of all the data points.
159 * @return {number} The minimum y value of all the data points.
161 Coordinates.prototype.yMinValue = function() {
162 return this.yMinValue_;
166 * Returns the maximum y value of all the data points.
168 * @return {number} The maximum y value of all the data points.
170 Coordinates.prototype.yMaxValue = function() {
171 return this.yMaxValue_;
175 * Returns the x value at the lower limit of the bounding box of the canvas.
177 * @return {number} The x value at the lower limit of the bounding box of
180 Coordinates.prototype.xLowerLimitValue = function() {
181 return this.xMinValue_ - this.xBufferSpace_;
185 * Returns the x value at the upper limit of the bounding box of the canvas.
187 * @return {number} The x value at the upper limit of the bounding box of
190 Coordinates.prototype.xUpperLimitValue = function() {
191 return this.xMaxValue_ + this.xBufferSpace_;
195 * Returns the y value at the lower limit of the bounding box of the canvas.
197 * @return {number} The y value at the lower limit of the bounding box of
200 Coordinates.prototype.yLowerLimitValue = function() {
201 return this.yMinValue_ - this.yBufferSpace_;
205 * Returns the y value at the upper limit of the bounding box of the canvas.
207 * @return {number} The y value at the upper limit of the bounding box of
210 Coordinates.prototype.yUpperLimitValue = function() {
211 return this.yMaxValue_ + this.yBufferSpace_;