[Plot] Separate out positioning

Separate out pixel-to-plot-coordinate conversion into its
own script, to simplify PlotController for testing and
maintainability. WTD-533.
This commit is contained in:
Victor Woeltjen 2014-12-01 14:14:31 -08:00
parent 1b4286344d
commit 5bfa9c76a7
2 changed files with 44 additions and 19 deletions
platform/features/plot/src

@ -8,9 +8,10 @@ define(
"./PlotPreparer",
"./PlotPalette",
"./PlotPanZoomStack",
"./PlotPosition",
"../lib/moment.min.js"
],
function (PlotPreparer, PlotPalette, PlotPanZoomStack) {
function (PlotPreparer, PlotPalette, PlotPanZoomStack, PlotPosition) {
"use strict";
var AXIS_DEFAULTS = [
@ -44,30 +45,15 @@ define(
return (i ? formatRangeValue : formatDomainValue)(v);
}
function pixelToDomainRange(x, y, width, height, domainOffset) {
var panZoom = panZoomStack.getPanZoom(),
offset = [ domainOffset || 0, 0 ],
origin = panZoom.origin,
dimensions = panZoom.dimensions;
if (!dimensions || !origin) {
return [];
}
return [ x / width, (height - y) / height ].map(function (v, i) {
return v * dimensions[i] + origin[i] + offset[i];
});
}
function mousePositionToDomainRange(mousePosition, domainOffset) {
return pixelToDomainRange(
return new PlotPosition(
mousePosition.x,
mousePosition.y,
mousePosition.width,
mousePosition.height,
panZoomStack,
domainOffset
);
).getPosition();
}
function generateTicks(start, span, count, format) {

@ -0,0 +1,39 @@
/*global define*/
define(
[],
function () {
"use strict";
function PlotPosition(x, y, width, height, panZoomStack, domainOffset) {
var panZoom = panZoomStack.getPanZoom(),
offset = [ domainOffset || 0, 0 ],
origin = panZoom.origin,
dimensions = panZoom.dimensions,
position;
if (!dimensions || !origin) {
position = [];
} else {
position = [ x / width, (height - y) / height ].map(function (v, i) {
return v * dimensions[i] + origin[i] + offset[i];
});
}
return {
getDomain: function () {
return position[0];
},
getRange: function () {
return position[1];
},
getPosition: function () {
return position;
}
};
}
return PlotPosition;
}
);