|
| 1 | +/***************************************************************************** |
| 2 | + * Open MCT Web, Copyright (c) 2014-2015, United States Government |
| 3 | + * as represented by the Administrator of the National Aeronautics and Space |
| 4 | + * Administration. All rights reserved. |
| 5 | + * |
| 6 | + * Open MCT Web is licensed under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0. |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | + * License for the specific language governing permissions and limitations |
| 15 | + * under the License. |
| 16 | + * |
| 17 | + * Open MCT Web includes source code licensed under additional open source |
| 18 | + * licenses. See the Open Source Licenses file (LICENSES.md) included with |
| 19 | + * this source code distribution or the Licensing information page available |
| 20 | + * at runtime from the About dialog for additional information. |
| 21 | + *****************************************************************************/ |
| 22 | + |
| 23 | +define( |
| 24 | + [ |
| 25 | + "d3" |
| 26 | + ], |
| 27 | + function (d3) { |
| 28 | + var PADDING = 1; |
| 29 | + |
| 30 | + /** |
| 31 | + * The mct-conductor-axis renders a horizontal axis with regular |
| 32 | + * labelled 'ticks'. It requires 'start' and 'end' integer values to |
| 33 | + * be specified as attributes. |
| 34 | + */ |
| 35 | + function ConductorAxisController(conductor, formatService) { |
| 36 | + // Dependencies |
| 37 | + this.d3 = d3; |
| 38 | + this.formatService = formatService; |
| 39 | + this.conductor = conductor; |
| 40 | + |
| 41 | + // Runtime properties (set by 'link' function) |
| 42 | + this.target = undefined; |
| 43 | + this.xScale = undefined; |
| 44 | + this.xAxis = undefined; |
| 45 | + this.axisElement = undefined; |
| 46 | + this.initialized = false; |
| 47 | + this.msPerPixel = undefined; |
| 48 | + |
| 49 | + this.setScale = this.setScale.bind(this); |
| 50 | + this.changeBounds = this.changeBounds.bind(this); |
| 51 | + this.changeTimeSystem = this.changeTimeSystem.bind(this); |
| 52 | + |
| 53 | + this.bounds = conductor.bounds(); |
| 54 | + this.timeSystem = conductor.timeSystem(); |
| 55 | + } |
| 56 | + |
| 57 | + ConductorAxisController.prototype.changeBounds = function (bounds) { |
| 58 | + this.bounds = bounds; |
| 59 | + if (this.initialized) { |
| 60 | + this.setScale(); |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + ConductorAxisController.prototype.setScale = function () { |
| 65 | + var width = this.target.offsetWidth; |
| 66 | + var timeSystem = this.conductor.timeSystem(); |
| 67 | + var bounds = this.bounds; |
| 68 | + |
| 69 | + if (timeSystem.isUTCBased()) { |
| 70 | + this.xScale = this.xScale || this.d3.scaleUtc(); |
| 71 | + this.xScale.domain([new Date(bounds.start), new Date(bounds.end)]); |
| 72 | + } else { |
| 73 | + this.xScale = this.xScale || this.d3.scaleLinear(); |
| 74 | + this.xScale.domain([bounds.start, bounds.end]); |
| 75 | + } |
| 76 | + |
| 77 | + this.xScale.range([PADDING, width - PADDING * 2]); |
| 78 | + this.axisElement.call(this.xAxis); |
| 79 | + |
| 80 | + this.msPerPixel = (bounds.end - bounds.start) / width; |
| 81 | + }; |
| 82 | + |
| 83 | + ConductorAxisController.prototype.changeTimeSystem = function (timeSystem) { |
| 84 | + this.timeSystem = timeSystem; |
| 85 | + |
| 86 | + var key = timeSystem.formats()[0]; |
| 87 | + if (this.initialized && key !== undefined) { |
| 88 | + var format = this.formatService.getFormat(key); |
| 89 | + var bounds = this.conductor.bounds(); |
| 90 | + |
| 91 | + if (timeSystem.isUTCBased()) { |
| 92 | + this.xScale = this.d3.scaleUtc(); |
| 93 | + } else { |
| 94 | + this.xScale = this.d3.scaleLinear(); |
| 95 | + } |
| 96 | + |
| 97 | + this.xAxis.scale(this.xScale); |
| 98 | + //Define a custom format function |
| 99 | + this.xAxis.tickFormat(function (tickValue) { |
| 100 | + // Normalize date representations to numbers |
| 101 | + if (tickValue instanceof Date) { |
| 102 | + tickValue = tickValue.getTime(); |
| 103 | + } |
| 104 | + return format.format(tickValue, { |
| 105 | + min: bounds.start, |
| 106 | + max: bounds.end |
| 107 | + }); |
| 108 | + }); |
| 109 | + this.axisElement.call(this.xAxis); |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + ConductorAxisController.prototype.link = function (scope, element) { |
| 114 | + this.target = element[0].firstChild; |
| 115 | + this.scope = scope; |
| 116 | + var height = this.target.offsetHeight; |
| 117 | + var vis = this.d3.select(this.target) |
| 118 | + .append("svg:svg") |
| 119 | + .attr("width", "100%") |
| 120 | + .attr("height", height); |
| 121 | + |
| 122 | + this.xAxis = this.d3.axisTop(); |
| 123 | + |
| 124 | + // draw x axis with labels and move to the bottom of the chart area |
| 125 | + this.axisElement = vis.append("g") |
| 126 | + .attr("transform", "translate(0," + (height - PADDING) + ")"); |
| 127 | + |
| 128 | + this.initialized = true; |
| 129 | + |
| 130 | + if (this.timeSystem !== undefined) { |
| 131 | + this.changeTimeSystem(this.timeSystem); |
| 132 | + this.setScale(this.bounds); |
| 133 | + } |
| 134 | + |
| 135 | + //Respond to changes in conductor |
| 136 | + this.conductor.on("timeSystem", this.changeTimeSystem); |
| 137 | + this.conductor.on("bounds", this.changeBounds); |
| 138 | + }; |
| 139 | + |
| 140 | + ConductorAxisController.prototype.panEnd = function () { |
| 141 | + //resync view bounds with time conductor bounds |
| 142 | + this.conductor.bounds(this.bounds); |
| 143 | + this.scope.$emit("pan-stop"); |
| 144 | + }; |
| 145 | + |
| 146 | + ConductorAxisController.prototype.pan = function (delta) { |
| 147 | + if (!this.conductor.follow()) { |
| 148 | + var deltaInMs = delta[0] * this.msPerPixel; |
| 149 | + var bounds = this.conductor.bounds(); |
| 150 | + var start = Math.floor((bounds.start - deltaInMs) / 1000) * 1000; |
| 151 | + var end = Math.floor((bounds.end - deltaInMs) / 1000) * 1000; |
| 152 | + this.bounds = { |
| 153 | + start: start, |
| 154 | + end: end |
| 155 | + }; |
| 156 | + this.setScale(); |
| 157 | + this.scope.$emit("pan", this.bounds); |
| 158 | + } |
| 159 | + }; |
| 160 | + |
| 161 | + ConductorAxisController.prototype.resize = function () { |
| 162 | + if (this.initialized) { |
| 163 | + this.setScale(); |
| 164 | + } |
| 165 | + }; |
| 166 | + |
| 167 | + return ConductorAxisController; |
| 168 | + } |
| 169 | +); |
0 commit comments