1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | 1 1 1 1 10 10 10 10 1 7 7 1 9 3 3 31 5 1 1 1 9 9 1 1 3 3 9 4 31 5 1 37 90 90 6 6 90 37 1 1 6 6 1 5 1 17 17 1 5 5 1 9 9 2 7 1 6 1 5 | 'use strict'; var util = require('../util'), d3_time_format = require('d3-time-format'); var time = module.exports = {}; // 'Wednesday September 17 04:00:00 2014' // Wednesday is the longest date // September is the longest month (8 in javascript as it is zero-indexed). var LONG_DATE = new Date(Date.UTC(2014, 8, 17)); time.cardinality = function(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; }; time.formula = function(timeUnit, fieldRef) { // TODO(kanitw): add formula to other time format var fn = 'utc' + timeUnit; return fn + '(' + fieldRef + ')'; }; time.maxLength = function(timeUnit, encoding) { switch (timeUnit) { case 'seconds': case 'minutes': case 'hours': case 'date': return 2; case 'month': case 'day': var range = time.range(timeUnit, encoding); Eif (range) { // return the longest name in the range return Math.max.apply(null, range.map(function(r) {return r.length;})); } return 2; case 'year': return 4; //'1998' } // TODO(#600) revise this // no time unit var timeFormat = encoding.config('timeFormat'); return d3_time_format.utcFormat(timeFormat)(LONG_DATE).length; }; time.range = function(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; }; /** * @param {Object} encoding * @return {Array} scales for time unit names */ time.scales = function(encoding) { var scales = encoding.reduce(function(scales, encDef) { var timeUnit = encDef.timeUnit; if (encDef.type === T && timeUnit && !scales[timeUnit]) { var scale = time.scale.def(encDef.timeUnit, encoding); if (scale) scales[timeUnit] = scale; } return scales; }, {}); return util.vals(scales); }; time.scale = {}; /** append custom time scales for axis label */ time.scale.def = function(timeUnit, encoding) { var range = time.range(timeUnit, encoding); if (range) { return { name: 'time-'+timeUnit, type: 'ordinal', domain: time.scale.domain(timeUnit), range: range }; } return null; }; time.isOrdinalFn = function(timeUnit) { switch (timeUnit) { case 'seconds': case 'minutes': case 'hours': case 'day': case 'date': case 'month': return true; } return false; }; time.scale.type = function(timeUnit, name) { Iif (name === COLOR) { return 'linear'; // time has order, so use interpolated ordinal color scale. } return time.isOrdinalFn(timeUnit) || name === COL || name === ROW ? 'ordinal' : 'linear'; }; time.scale.domain = function(timeUnit, name) { var isColor = name === 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; }; /** whether a particular time function has custom scale for labels implemented in time.scale */ time.hasScale = function(timeUnit) { switch (timeUnit) { case 'day': case 'month': return true; } return false; }; |