blob: 6ba29ec324f23a4c09aff326e737a39e94799dca [file] [log] [blame]
Chris Craik19832152015-04-16 15:43:38 -07001<!DOCTYPE html>
2<!--
3Copyright (c) 2015 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7
8<link rel="import" href="/base/base.html">
9
10<script>
11'use strict';
12
13tv.exportTo('tv.c.tracks', function() {
14
15 /**
16 * A helper object encapsulating all parameters necessary to draw a chart
17 * series and provides conversion between world coordinates and physical
18 * pixels.
19 *
20 * All parameters (except for pixelRatio) are assumed to be in physical pixels
21 * (i.e. already pre-multiplied with pixelRatio).
22 *
23 * The diagram below explains the meaning of the resulting fields with
24 * respect to a chart track:
25 *
26 * outerTopViewY -> +--------------------/-\-------+ <- Top padding
27 * innerTopViewY -> + - - - - - - - - - -| |- - - -+ <- Axis max
28 * | .. ==\-/== |
29 * | == Series == |
30 * | ==/-\== .. |
31 * innerBottomViewY -> + - - -Point- - - - - - - - - -+ <- Axis min
32 * outerBottomViewY -> +-------\-/--------------------+ <- Bottom padding
33 * ^ ^
34 * leftViewX rightViewX
35 * leftTimeStamp rightTimestamp
36 *
37 * Labels starting with a lower case letter are the resulting fields of the
38 * transform object. Labels starting with an upper case letter correspond
39 * to the relevant chart track concepts.
40 *
41 * @constructor
42 */
Chris Craik44c28202015-05-12 17:25:16 -070043 function ChartTransform(displayTransform, axis, trackWidth,
Chris Craik19832152015-04-16 15:43:38 -070044 trackHeight, topPadding, bottomPadding, pixelRatio) {
45 this.pixelRatio = pixelRatio;
46
47 // X axis.
48 this.leftViewX = 0;
49 this.rightViewX = trackWidth;
Chris Craik44c28202015-05-12 17:25:16 -070050 this.leftTimestamp = displayTransform.xViewToWorld(this.leftViewX);
51 this.rightTimestamp = displayTransform.xViewToWorld(this.rightViewX);
Chris Craik19832152015-04-16 15:43:38 -070052
Chris Craik44c28202015-05-12 17:25:16 -070053 this.displayTransform_ = displayTransform;
Chris Craik19832152015-04-16 15:43:38 -070054
55 // Y axis.
56 this.outerTopViewY = 0;
57 this.innerTopViewY = topPadding;
58 this.innerBottomViewY = trackHeight - bottomPadding;
59 this.outerBottomViewY = trackHeight;
60
61 this.axis_ = axis;
62 this.innerHeight_ = this.innerBottomViewY - this.innerTopViewY;
63 };
64
65 ChartTransform.prototype = {
66 worldXToViewX: function(worldX) {
Chris Craik44c28202015-05-12 17:25:16 -070067 return this.displayTransform_.xWorldToView(worldX);
Chris Craik19832152015-04-16 15:43:38 -070068 },
69
70 viewXToWorldX: function(viewX) {
Chris Craik44c28202015-05-12 17:25:16 -070071 return this.displayTransform_.xViewToWorld(viewX);
Chris Craik19832152015-04-16 15:43:38 -070072 },
73
74 worldYToViewY: function(worldY) {
75 var innerHeightCoefficient = 1 - this.axis_.valueToUnitRange(worldY);
76 return innerHeightCoefficient * this.innerHeight_ + this.innerTopViewY;
77 }
78 };
79
80 return {
81 ChartTransform: ChartTransform
82 };
83});
84</script>