[Plot] Separate out formatter

Separate out plot label formatter to simplify PlotController,
to simplify test and maintenance. WTD-533
This commit is contained in:
Victor Woeltjen 2014-12-01 14:55:55 -08:00
parent 5bfa9c76a7
commit bb708e9a05
3 changed files with 90 additions and 39 deletions

View File

@ -9,9 +9,10 @@ define(
"./PlotPalette",
"./PlotPanZoomStack",
"./PlotPosition",
"../lib/moment.min.js"
"./PlotTickGenerator",
"./PlotFormatter"
],
function (PlotPreparer, PlotPalette, PlotPanZoomStack, PlotPosition) {
function (PlotPreparer, PlotPalette, PlotPanZoomStack, PlotPosition, PlotTickGenerator, PlotFormatter) {
"use strict";
var AXIS_DEFAULTS = [
@ -29,20 +30,15 @@ define(
var mousePosition,
marqueeStart,
panZoomStack = new PlotPanZoomStack([], []),
formatter = new PlotFormatter(),
domainOffset;
function formatDomainValue(v) {
return moment.utc(v).format("YYYY-DDD HH:mm:ss");
}
function formatRangeValue(v) {
return v.toFixed(1);
}
// Utility, for map/forEach loops. Index 0 is domain,
// index 1 is range.
function formatValue(v, i) {
return (i ? formatRangeValue : formatDomainValue)(v);
return (i ?
formatter.formatRangeValue :
formatter.formatDomainValue)(v);
}
function mousePositionToDomainRange(mousePosition, domainOffset) {
@ -56,20 +52,6 @@ define(
).getPosition();
}
function generateTicks(start, span, count, format) {
var step = span / (count - 1),
result = [],
i;
for (i = 0; i < count; i += 1) {
result.push({
label: format(i * step + start)
});
}
return result;
}
function updateMarqueeBox() {
$scope.draw.boxes = marqueeStart ?
[{
@ -86,9 +68,8 @@ define(
$scope.draw.origin = panZoom.origin;
}
function plotTelemetry() {
var telemetry, prepared, data;
var telemetry, prepared, tickGenerator, data;
telemetry = $scope.telemetry;
@ -104,18 +85,12 @@ define(
($scope.axes[1].active || {}).key
);
$scope.axes[0].ticks = generateTicks(
prepared.getOrigin()[0] + prepared.getDomainOffset(),
prepared.getDimensions()[0],
DOMAIN_TICKS,
formatDomainValue
);
$scope.axes[1].ticks = generateTicks(
prepared.getOrigin()[1],
prepared.getDimensions()[1],
RANGE_TICKS,
formatRangeValue
);
tickGenerator = new PlotTickGenerator(prepared, formatter);
$scope.axes[0].ticks =
tickGenerator.generateDomainTicks(DOMAIN_TICKS);
$scope.axes[1].ticks =
tickGenerator.generateRangeTicks(RANGE_TICKS);
panZoomStack.setBasePanZoom(
prepared.getOrigin(),

View File

@ -0,0 +1,28 @@
/*global define,moment*/
define(
["../lib/moment.min"],
function () {
"use strict";
var DATE_FORMAT = "YYYY-DDD HH:mm:ss";
function PlotFormatter() {
function formatDomainValue(v) {
return moment.utc(v).format(DATE_FORMAT);
}
function formatRangeValue(v) {
return v.toFixed(1);
}
return {
formatDomainValue: formatDomainValue,
formatRangeValue: formatRangeValue
};
}
return PlotFormatter;
}
);

View File

@ -0,0 +1,48 @@
/*global define*/
define(
[],
function () {
"use strict";
function PlotTickGenerator(preparer, formatter) {
function generateTicks(start, span, count, format) {
var step = span / (count - 1),
result = [],
i;
for (i = 0; i < count; i += 1) {
result.push({
label: format(i * step + start)
});
}
return result;
}
return {
generateDomainTicks: function (count) {
return generateTicks(
preparer.getOrigin()[0] + preparer.getDomainOffset(),
preparer.getDimensions()[0],
count,
formatter.formatDomainValue
);
},
generateRangeTicks: function (count) {
return generateTicks(
preparer.getOrigin()[1],
preparer.getDimensions()[1],
count,
formatter.formatRangeValue
);
}
};
}
return PlotTickGenerator;
}
);