summaryrefslogtreecommitdiff
path: root/timings/js/coordinates.js
blob: 69cb4c22d5b4515bc078d29656147fb4eafb90c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
  Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
  Use of this source code is governed by a BSD-style license that can be
  found in the LICENSE file.
*/

/**
 * 'Understands' plot data positioning.
 *  @constructor
 *
 * @param {Array} plotData data that will be displayed
 */
function Coordinates(plotData) {
  this.plotData = plotData;
  
  height = window.innerHeight - 16;
  width = window.innerWidth - 16;

  this.widthMax = width;
  this.heightMax = Math.min(400, height - 85);

  this.xMinValue = -0.5;
  this.xMaxValue = (this.plotData[0].length - 1)+ 0.5;
  this.processYValues_();
}

Coordinates.prototype.processYValues_ = function () {
  var merged = [];
  for (var i = 0; i < this.plotData.length; i++)
    for (var j = 0; j < this.plotData[i].length; j++)
      merged.push(this.plotData[i][j][0]);
  var max = Math.max.apply( Math, merged );
  var min = Math.min.apply( Math, merged );

  // If we have a missing value, find the real max and min the hard way.
  if (isNaN(min)) {
    for (var i = 0; i < merged.length; ++i) {
      if (isNaN(min) || merged[i] < min)
        min = merged[i];
      if (isNaN(max) || merged[i] > max)
        max = merged[i];
    }
  }
  var yd = (max - min) / 10.0;
  if (yd == 0)
    yd = max / 10;
  this.yMinValue = min - yd;
  this.yMaxValue = max + yd;
};

/**
 * Difference between horizontal max min values.
 */
Coordinates.prototype.xValueRange = function() {
  return this.xMaxValue - this.xMinValue;
};

/**
 * Difference between vertical max min values.
 */
Coordinates.prototype.yValueRange = function() {
  return this.yMaxValue - this.yMinValue
};

/**
 * Converts horizontal data value to pixel value on canvas.
 * @param {number} value horizontal data value
 */
Coordinates.prototype.xPoints = function(value) {
  return this.widthMax * ((value - this.xMinValue) / this.xValueRange());
};

/**
 * Converts vertical data value to pixel value on canvas.
 * @param {number} value vertical data value
 */
Coordinates.prototype.yPoints = function(value) {
  /* Converts value to canvas Y position in pixels. */
  return this.heightMax  - this.heightMax * (value - this.yMinValue) / 
    this.yValueRange();
};

/**
 * Converts X point on canvas to value it represents.
 * @param {number} position horizontal point on canvas.
 */
Coordinates.prototype.xValue = function(position) {
  /* Converts canvas X pixels to value. */
  return position / this.widthMax * (this.xValueRange()) + this.xMinValue;
};

/**
 * Converts Y point on canvas to value it represents.
 * @param {number} position vertical point on canvas.
 */
Coordinates.prototype.yValue = function(position) {
  /* Converts canvas Y pixels to value. 
  position is point value is from top.
  */
  var position = this.heightMax - position;
  var ratio = parseFloat(this.heightMax / position);
  return  this.yMinValue + this.yValueRange() / ratio;
};

/**
 * Converts canvas X pixel to data index.
 * @param {number} xPosition horizontal point on canvas
 */
Coordinates.prototype.dataSampleIndex = function(xPosition) {
  var xValue = this.xValue(xPosition);
  var index;
  if (xValue < 0) {
    index = 0;
  } else if (xValue > this.plotData[0].length - 1) {
    index = this.plotData[0].length - 1;
  } else {
    index = xValue.toFixed(0);
  }
  return index;
};

Coordinates.prototype.log = function(val) {
  document.getElementById('log').appendChild(
    document.createTextNode(val + '\n'));
};