Code coverage report for src/compile/stack.js

Statements: 84.62% (22 / 26)      Branches: 75% (12 / 16)      Functions: 100% (1 / 1)      Lines: 91.67% (22 / 24)      Ignored: none     

All files » src/compile/ » stack.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    1   1   1   1 2     2   2       2 1 1 1 1 1 1 1           2                   2                     2     2               2 2   2    
'use strict';
 
require('../globals');
 
var  marks = require('./marks');
 
module.exports = stacking;
 
function stacking(data, encoding, mdef, facets) {
  Iif (!marks[encoding.marktype()].stack) return false;
 
  // TODO: add || encoding.has(LOD) here once LOD is implemented
  Iif (!encoding.has(COLOR)) return false;
 
  var dim=null, val=null, idx =null,
    isXMeasure = encoding.isMeasure(X),
    isYMeasure = encoding.isMeasure(Y);
 
  if (isXMeasure && !isYMeasure) {
    dim = Y;
    val = X;
    idx = 0;
  } else Eif (isYMeasure && !isXMeasure) {
    dim = X;
    val = Y;
    idx = 1;
  } else {
    return null; // no stack encoding
  }
 
  // add transform to compute sums for scale
  var stacked = {
    name: STACKED,
    source: TABLE,
    transform: [{
      type: 'aggregate',
      groupby: [encoding.field(dim)].concat(facets), // dim and other facets
      fields: [{op: 'sum', field: encoding.field(val)}] // TODO check if field with aggregate is correct?
    }]
  };
 
  Iif (facets && facets.length > 0) {
    stacked.transform.push({ //calculate max for each facet
      type: 'aggregate',
      groupby: facets,
      fields: [{
        op: 'max',
        field: encoding.fieldName(val, {fn: 'sum'})
      }]
    });
  }
 
  data.push(stacked);
 
  // add stack transform to mark
  mdef.from.transform = [{
    type: 'stack',
    point: encoding.field(dim),
    height: encoding.field(val),
    output: {y1: val, y0: val + '2'}
  }];
 
  // TODO: This is super hack-ish -- consolidate into modular mark properties?
  mdef.properties.update[val] = mdef.properties.enter[val] = {scale: val, field: val};
  mdef.properties.update[val + '2'] = mdef.properties.enter[val + '2'] = {scale: val, field: val + '2'};
 
  return val; //return stack encoding
}