Code coverage report for src/compiler/compiler.js

Statements: 96.08% (49 / 51)      Branches: 87.5% (28 / 32)      Functions: 80% (4 / 5)      Lines: 96.08% (49 / 51)      Ignored: none     

All files » src/compiler/ » compiler.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    1   1         1   1           1 1 1 1 1 1 1   1 36     1             1   36 2 2 4 4             36   36                                               36     36       36 36   8     36     36   36   4       36 8 8 4     8       36 37       36 6 6   30 30 30 28   30 27     30     36      
'use strict';
 
var summary = module.exports = require('datalib/src/stats').summary;
 
require('../globals');
 
/**
 * Module for compiling Vega-lite spec into Vega spec.
 */
var compiler = module.exports = {};
 
var Encoding = require('../Encoding'),
  axis = compiler.axis = require('./axis'),
  legend = compiler.legend = require('./legend'),
  marks = compiler.marks = require('./marks'),
  scale = compiler.scale = require('./scale');
 
compiler.data = require('./data');
compiler.facet = require('./facet');
compiler.layout = require('./layout');
compiler.stack = require('./stack');
compiler.style = require('./style');
compiler.subfacet = require('./subfacet');
compiler.time = require('./time');
 
compiler.compile = function (spec, stats, theme) {
  return compiler.compileEncoding(Encoding.fromSpec(spec, theme), stats);
};
 
compiler.shorthand = function (shorthand, stats, config, theme) {
  return compiler.compileEncoding(Encoding.fromShorthand(shorthand, config, theme), stats);
};
 
/**
 * Create a Vega specification from a Vega-lite Encoding object.
 */
compiler.compileEncoding = function (encoding, stats) {
  // no need to pass stats if you pass in the data
  if (!stats) {
    Eif (encoding.hasValues()) {
        stats = summary(encoding.data().values).reduce(function(s, p) {
        s[p.field] = p;
        return s;
      }, {});
    } else {
      console.error('No stats provided and data is not embedded.');
    }
  }
 
  var layout = compiler.layout(encoding, stats);
 
  var spec = {
      width: layout.width,
      height: layout.height,
      padding: 'auto',
      // FIXME(#514): eliminate stats
      data: compiler.data(encoding, stats),
      // global scales contains only time unit scales
      scales: compiler.time.scales(encoding),
      marks: [{
        name: 'cell',
        type: 'group',
        properties: {
          enter: {
            width: layout.cellWidth ?
                     {value: layout.cellWidth} :
                     {field: {group: 'width'}},
            height: layout.cellHeight ?
                    {value: layout.cellHeight} :
                    {field: {group: 'height'}}
          }
        }
      }]
    };
 
  var group = spec.marks[0];
 
  // marks
  var style = compiler.style(encoding, stats),
    mdefs = group.marks = marks.def(encoding, layout, style, stats),
    mdef = mdefs[mdefs.length - 1];  // TODO: remove this dirty hack by refactoring the whole flow
 
  var stack = encoding.stack();
  if (stack) {
    // modify mdef.{from,properties}
    compiler.stack(encoding, mdef, stack);
  }
 
  var lineType = marks[encoding.marktype()].line;
 
  // handle subfacets
  var details = encoding.details();
 
  if (details.length > 0 && lineType) {
    //subfacet to group stack / line together in one group
    compiler.subfacet(group, mdef, details, encoding);
  }
 
  // auto-sort line/area values
  if (lineType && encoding.config('autoSortLine')) {
    var f = (encoding.isMeasure(X) && encoding.isDimension(Y)) ? Y : X;
    if (!mdef.from) {
      mdef.from = {};
    }
    // TODO: why - ?
    mdef.from.transform = [{type: 'sort', by: '-' + encoding.fieldRef(f)}];
  }
 
  // get a flattened list of all scale names that are used in the vl spec
  var singleScaleNames = [].concat.apply([], mdefs.map(function(markProps) {
    return scale.names(markProps.properties.update);
  }));
 
  // Small Multiples
  if (encoding.has(ROW) || encoding.has(COL)) {
    spec = compiler.facet(group, encoding, layout, spec, singleScaleNames, stats);
    spec.legends = legend.defs(encoding, style);
  } else {
    group.scales = scale.defs(singleScaleNames, encoding, layout, stats);
    group.axes = [];
    if (encoding.has(X)) {
      group.axes.push(axis.def(X, encoding, layout, stats));
    }
    if (encoding.has(Y)) {
      group.axes.push(axis.def(Y, encoding, layout, stats));
    }
 
    group.legends = legend.defs(encoding, style);
  }
 
  return spec;
};