Code coverage report for src/Encoding.js

Statements: 74.57% (129 / 173)      Branches: 62.4% (78 / 125)      Functions: 72.22% (39 / 54)      Lines: 75% (129 / 172)      Ignored: none     

All files » src/ » Encoding.js
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392    1   1           1 1 88     88 88 88 88     1   1 1         1               1 87     1           1           1       1                                             1 160     1 273     1   1618     1 2398       1 431 431           1 44     1 94 5   89 89 33   56       1 312     1 363     1 88           88         1 160 160   160 12     148     1 66     1 74   74     1                                                 1       1 167     1 161     1 101     1 500 500       1 43 43     1 106     1 182     1 135     1 106     1 189     1 144     1 202     1 119     1       1         1                       1 166       166       166   41 41   41 19           22 22               125         1 36 36 89 20   89       1 8 8 25 2   25       1 98     1       1 209       1 43 43     1 1102     1                         1             1           1       1                           1    
'use strict';
 
require('./globals');
 
var consts = require('./consts'),
  util = require('./util'),
  vlEncDef = require('./encdef'),
  vlenc = require('./enc'),
  schema = require('./schema/schema');
 
module.exports = (function() {
  function Encoding(spec, theme) {
    var defaults = schema.instantiate(),
      specExtended = schema.util.merge(defaults, theme || {}, spec) ;
 
    this._data = specExtended.data;
    this._marktype = specExtended.marktype;
    this._enc = specExtended.encoding;
    this._config = specExtended.config;
  }
 
  var proto = Encoding.prototype;
 
  Encoding.fromShorthand = function(shorthand, data, config, theme) {
    var c = consts.shorthand,
        split = shorthand.split(c.delim),
        marktype = split.shift().split(c.assign)[1].trim(),
        enc = vlenc.fromShorthand(split);
 
    return new Encoding({
      data: data,
      marktype: marktype,
      encoding: enc,
      config: config
    }, theme);
  };
 
  Encoding.fromSpec = function(spec, theme) {
    return new Encoding(spec, theme);
  };
 
  proto.toShorthand = function() {
    var c = consts.shorthand;
    return 'mark' + c.assign + this._marktype +
      c.delim + vlenc.shorthand(this._enc);
  };
 
  Encoding.shorthand = function (spec) {
    var c = consts.shorthand;
    return 'mark' + c.assign + spec.marktype +
      c.delim + vlenc.shorthand(spec.encoding);
  };
 
  Encoding.specFromShorthand = function(shorthand, data, config, excludeConfig) {
    return Encoding.fromShorthand(shorthand, data, config).toSpec(excludeConfig);
  };
 
  proto.toSpec = function(excludeConfig, excludeData) {
    var enc = util.duplicate(this._enc),
      spec;
 
    spec = {
      marktype: this._marktype,
      encoding: enc
    };
 
    if (!excludeConfig) {
      spec.config = util.duplicate(this._config);
    }
 
    if (!excludeData) {
      spec.data = util.duplicate(this._data);
    }
 
    // remove defaults
    var defaults = schema.instantiate();
    return schema.util.subtract(spec, defaults);
  };
 
 
  proto.marktype = function() {
    return this._marktype;
  };
 
  proto.is = function(m) {
    return this._marktype === m;
  };
 
  proto.has = function(encType) {
    // equivalent to calling vlenc.has(this._enc, encType)
    return this._enc[encType].name !== undefined;
  };
 
  proto.encDef = function(et) {
    return this._enc[et];
  };
 
  // get "field" reference for vega
  proto.fieldRef = function(et, opt) {
    opt = opt || {};
    return vlEncDef.fieldRef(this._enc[et], opt);
  };
 
  /*
   * return key-value pairs of field name and list of fields of that field name
   */
  proto.fields = function() {
    return vlenc.fields(this._enc);
  };
 
  proto.fieldTitle = function(et) {
    if (vlEncDef.isCount(this._enc[et])) {
      return vlEncDef.count.displayName;
    }
    var fn = this._enc[et].aggregate || this._enc[et].timeUnit || (this._enc[et].bin && 'bin');
    if (fn) {
      return fn.toUpperCase() + '(' + this._enc[et].name + ')';
    } else {
      return this._enc[et].name;
    }
  };
 
  proto.scale = function(et) {
    return this._enc[et].scale || {};
  };
 
  proto.axis = function(et) {
    return this._enc[et].axis || {};
  };
 
  proto.bandSize = function(encType, useSmallBand) {
    useSmallBand = useSmallBand ||
      //isBandInSmallMultiples
      (encType === Y && this.has(ROW) && this.has(Y)) ||
      (encType === X && this.has(COL) && this.has(X));
 
    // if band.size is explicitly specified, follow the specification, otherwise draw value from config.
    return this.encDef(encType).band.size ||
      this.config(useSmallBand ? 'smallBandSize' : 'largeBandSize');
  };
 
  // returns false if binning is disabled, otherwise an object with binning properties
  proto.bin = function(et) {
    var bin = this._enc[et].bin;
    Iif (bin === {})
      return false;
    if (bin === true)
      return {
        maxbins: schema.MAXBINS_DEFAULT
      };
    return bin;
  };
 
  proto.value = function(et) {
    return this._enc[et].value;
  };
 
  proto.numberFormat = function(fieldStats) {
    var formatConfig = fieldStats.max > this.config('maxSmallNumber') ?
      'largeNumberFormat': 'smallNumberFormat';
    return this.config(formatConfig);
  };
 
  proto.sort = function(et, stats) {
    var sort = this._enc[et].sort,
      enc = this._enc,
      isTypes = vlEncDef.isTypes;
 
    if ((!sort || sort.length===0) &&
        // FIXME
        Encoding.toggleSort.support({encoding:this._enc}, stats, true) && //HACK
        this.config('toggleSort') === Q
      ) {
      var qField = isTypes(enc.x, [N, O]) ? enc.y : enc.x;
 
      if (isTypes(enc[et], [N, O])) {
        sort = [{
          name: qField.name,
          aggregate: qField.aggregate,
          type: qField.type,
          reverse: true
        }];
      }
    }
 
    return sort;
  };
 
  proto.map = function(f) {
    return vlenc.map(this._enc, f);
  };
 
  proto.reduce = function(f, init) {
    return vlenc.reduce(this._enc, f, init);
  };
 
  proto.forEach = function(f) {
    return vlenc.forEach(this._enc, f);
  };
 
  proto.type = function(et) {
    return this.has(et) ? this._enc[et].type : null;
  };
 
  proto.isType = function(et, type) {
    var encDef = this.encDef(et);
    return encDef && vlEncDef.isType(encDef, type);
  };
 
 
  proto.isTypes = function(et, type) {
    var encDef = this.encDef(et);
    return encDef && vlEncDef.isTypes(encDef, type);
  };
 
  Encoding.isOrdinalScale = function(encoding, encType) {
    return vlEncDef.isOrdinalScale(encoding.encDef(encType));
  };
 
  Encoding.isDimension = function(encoding, encType) {
    return vlEncDef.isDimension(encoding.encDef(encType));
  };
 
  Encoding.isMeasure = function(encoding, encType) {
    return vlEncDef.isMeasure(encoding.encDef(encType));
  };
 
  proto.isOrdinalScale = function(encType) {
    return this.has(encType) && Encoding.isOrdinalScale(this, encType);
  };
 
  proto.isDimension = function(encType) {
    return this.has(encType) && Encoding.isDimension(this, encType);
  };
 
  proto.isMeasure = function(encType) {
    return this.has(encType) && Encoding.isMeasure(this, encType);
  };
 
  proto.isAggregate = function() {
    return vlenc.isAggregate(this._enc);
  };
 
  proto.dataTable = function() {
    return this.isAggregate() ? AGGREGATE : RAW;
  };
 
  Encoding.isAggregate = function(spec) {
    return vlenc.isAggregate(spec.encoding);
  };
 
  Encoding.alwaysNoOcclusion = function(spec) {
    // FIXME raw OxQ with # of rows = # of O
    return vlenc.isAggregate(spec.encoding);
  };
 
  Encoding.isStack = function(spec) {
    // FIXME update this once we have control for stack ...
    return (spec.marktype === 'bar' || spec.marktype === 'area') &&
      spec.encoding.color;
  };
 
  /**
   * Check if the encoding should be stacked and return the stack dimenstion and value fields.
   * @return {Object} An object containing two properties:
   * - dimension - the dimension field
   * - value - the value field
   */
  proto.stack = function() {
    var stack = (this.has(COLOR) && this.encDef(COLOR).stack) ? COLOR :
          (this.has(DETAIL) && this.encDef(DETAIL).stack) ? DETAIL :
          null;
 
    var properties = stack && this.encDef(stack).stack !== true ?
                       this.encDef(stack).stack :
                       {};
 
    if ((this.is('bar') || this.is('area')) && stack && this.isAggregate()) {
 
      var isXMeasure = this.isMeasure(X);
      var isYMeasure = this.isMeasure(Y);
 
      if (isXMeasure && !isYMeasure) {
        return {
          groupby: Y,
          value: X,
          stack: stack,
          properties: properties
        };
      } else Eif (isYMeasure && !isXMeasure) {
        return {
          groupby: X,
          value: Y,
          stack: stack,
          properties: properties
        };
      }
    }
    return null; // no stack encoding
  };
 
 
 
  proto.details = function() {
    var encoding = this;
    return this.reduce(function(refs, field, encType) {
      if (!field.aggregate && (encType !== X && encType !== Y)) {
        refs.push(encoding.fieldRef(encType));
      }
      return refs;
    }, []);
  };
 
  proto.facets = function() {
    var encoding = this;
    return this.reduce(function(refs, field, encType) {
      if (!field.aggregate && (encType == ROW || encType == COL)) {
        refs.push(encoding.fieldRef(encType));
      }
      return refs;
    }, []);
  };
 
  proto.cardinality = function(encType, stats) {
    return vlEncDef.cardinality(this.encDef(encType), stats, this.config('filterNull'));
  };
 
  proto.isRaw = function() {
    return !this.isAggregate();
  };
 
  proto.data = function() {
    return this._data;
  };
 
   // returns whether the encoding has values embedded
  proto.hasValues = function() {
    var vals = this.data().values;
    return vals && vals.length;
  };
 
  proto.config = function(name) {
    return this._config[name];
  };
 
  Encoding.transpose = function(spec) {
    var oldenc = spec.encoding,
      enc = util.duplicate(spec.encoding);
    enc.x = oldenc.y;
    enc.y = oldenc.x;
    enc.row = oldenc.col;
    enc.col = oldenc.row;
    spec.encoding = enc;
    return spec;
  };
 
  // FIXME: REMOVE everything below here
 
  Encoding.toggleSort = function(spec) {
    spec.config = spec.config || {};
    spec.config.toggleSort = spec.config.toggleSort === Q ? N : Q;
    return spec;
  };
 
 
  Encoding.toggleSort.direction = function(spec) {
    if (!Encoding.toggleSort.support(spec)) { return; }
    var enc = spec.encoding;
    return enc.x.type === N ? 'x' : 'y';
  };
 
  Encoding.toggleSort.mode = function(spec) {
    return spec.config.toggleSort;
  };
 
  Encoding.toggleSort.support = function(spec, stats) {
    var enc = spec.encoding,
      isTypes = vlEncDef.isTypes;
 
    if (vlenc.has(enc, ROW) || vlenc.has(enc, COL) ||
      !vlenc.has(enc, X) || !vlenc.has(enc, Y) ||
      !Encoding.alwaysNoOcclusion(spec, stats)) {
      return false;
    }
 
    return ( isTypes(enc.x, [N,O]) && vlEncDef.isMeasure(enc.y)) ? 'x' :
      ( isTypes(enc.y, [N,O]) && vlEncDef.isMeasure(enc.x)) ? 'y' : false;
  };
 
  return Encoding;
})();