Chris Craik | 1983215 | 2015-04-16 15:43:38 -0700 | [diff] [blame] | 1 | <!DOCTYPE html> |
| 2 | <!-- |
| 3 | Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 4 | Use of this source code is governed by a BSD-style license that can be |
| 5 | found in the LICENSE file. |
| 6 | --> |
| 7 | |
| 8 | <link rel="import" href="/base/base.html"> |
| 9 | |
| 10 | <script> |
| 11 | 'use strict'; |
| 12 | |
| 13 | tv.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 Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 43 | function ChartTransform(displayTransform, axis, trackWidth, |
Chris Craik | 1983215 | 2015-04-16 15:43:38 -0700 | [diff] [blame] | 44 | trackHeight, topPadding, bottomPadding, pixelRatio) { |
| 45 | this.pixelRatio = pixelRatio; |
| 46 | |
| 47 | // X axis. |
| 48 | this.leftViewX = 0; |
| 49 | this.rightViewX = trackWidth; |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 50 | this.leftTimestamp = displayTransform.xViewToWorld(this.leftViewX); |
| 51 | this.rightTimestamp = displayTransform.xViewToWorld(this.rightViewX); |
Chris Craik | 1983215 | 2015-04-16 15:43:38 -0700 | [diff] [blame] | 52 | |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 53 | this.displayTransform_ = displayTransform; |
Chris Craik | 1983215 | 2015-04-16 15:43:38 -0700 | [diff] [blame] | 54 | |
| 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 Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 67 | return this.displayTransform_.xWorldToView(worldX); |
Chris Craik | 1983215 | 2015-04-16 15:43:38 -0700 | [diff] [blame] | 68 | }, |
| 69 | |
| 70 | viewXToWorldX: function(viewX) { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 71 | return this.displayTransform_.xViewToWorld(viewX); |
Chris Craik | 1983215 | 2015-04-16 15:43:38 -0700 | [diff] [blame] | 72 | }, |
| 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> |