/// <reference path="../../typings/d3-time-format.d.ts"/>
var d3_time_format_1 = require('d3-time-format');
var util = require('../util');
var consts_1 = require('../consts');
var LONG_DATE = new Date(Date.UTC(2014, 8, 17));
function cardinality(encDef, stats, filterNull, type) {
var timeUnit = encDef.timeUnit;
switch (timeUnit) {
case 'seconds': return 60;
case 'minutes': return 60;
case 'hours': return 24;
case 'day': return 7;
case 'date': return 31;
case 'month': return 12;
case 'year':
var stat = stats[encDef.name], yearstat = stats['year_' + encDef.name];
Eif (!yearstat) {
return null;
}
return yearstat.distinct -
(stat.missing > 0 && filterNull[type] ? 1 : 0);
}
return null;
}
exports.cardinality = cardinality;
;
function formula(timeUnit, fieldRef) {
var fn = 'utc' + timeUnit;
return fn + '(' + fieldRef + ')';
}
exports.formula = formula;
;
function maxLength(timeUnit, encoding) {
switch (timeUnit) {
case 'seconds':
case 'minutes':
case 'hours':
case 'date':
return 2;
case 'month':
case 'day':
var rng = range(timeUnit, encoding);
Eif (rng) {
return Math.max.apply(null, rng.map(function (r) { return r.length; }));
}
return 2;
case 'year':
return 4;
}
var timeFormat = encoding.config('timeFormat');
return d3_time_format_1.utcFormat(timeFormat)(LONG_DATE).length;
}
exports.maxLength = maxLength;
;
function range(timeUnit, encoding) {
var labelLength = encoding.config('timeScaleLabelLength'), scaleLabel;
switch (timeUnit) {
case 'day':
scaleLabel = encoding.config('dayScaleLabel');
break;
case 'month':
scaleLabel = encoding.config('monthScaleLabel');
break;
}
if (scaleLabel) {
return labelLength ? scaleLabel.map(function (s) { return s.substr(0, labelLength); }) : scaleLabel;
}
return;
}
exports.range = range;
;
function scales(encoding) {
var scales = encoding.reduce(function (scales, encDef) {
var timeUnit = encDef.timeUnit;
if (encDef.type === consts_1.Type.T && timeUnit && !scales[timeUnit]) {
var scaleDef = scale.def(encDef.timeUnit, encoding);
if (scaleDef)
scales[timeUnit] = scaleDef;
}
return scales;
}, {});
return util.vals(scales);
}
exports.scales = scales;
;
function isOrdinalFn(timeUnit) {
switch (timeUnit) {
case 'seconds':
case 'minutes':
case 'hours':
case 'day':
case 'date':
case 'month':
return true;
}
return false;
}
exports.isOrdinalFn = isOrdinalFn;
;
var scale;
(function (scale) {
function def(timeUnit, encoding) {
var rangeDef = range(timeUnit, encoding);
if (rangeDef) {
return {
name: 'time-' + timeUnit,
type: 'ordinal',
domain: scale.domain(timeUnit),
range: rangeDef
};
}
return null;
}
scale.def = def;
function type(timeUnit, name) {
Iif (name === consts_1.Enctype.COLOR) {
return 'linear';
}
return isOrdinalFn(timeUnit) || name === consts_1.Enctype.COL || name === consts_1.Enctype.ROW ? 'ordinal' : 'linear';
}
scale.type = type;
function domain(timeUnit, name) {
var isColor = name === consts_1.Enctype.COLOR;
switch (timeUnit) {
case 'seconds':
case 'minutes': return isColor ? [0, 59] : util.range(0, 60);
case 'hours': return isColor ? [0, 23] : util.range(0, 24);
case 'day': return isColor ? [0, 6] : util.range(0, 7);
case 'date': return isColor ? [1, 31] : util.range(1, 32);
case 'month': return isColor ? [0, 11] : util.range(0, 12);
}
return null;
}
scale.domain = domain;
})(scale = exports.scale || (exports.scale = {}));
;
function hasScale(timeUnit) {
switch (timeUnit) {
case 'day':
case 'month':
return true;
}
return false;
}
exports.hasScale = hasScale;
;
//# sourceMappingURL=time.js.map |