<commit_before>'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) {
    if (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 output = {
      width: layout.width,
      height: layout.height,
      padding: 'auto',
      data: compiler.data(encoding),
      // 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 = output.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 area / line together in one group
    compiler.subfacet(group, mdef, details);
  }

  // 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)) {
    output = compiler.facet(group, encoding, layout, output, singleScaleNames, stats);
    output.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 output;
};<commit_msg>only produces scales, axes, legends when they are non-empty<commit_after>'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) {
    if (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 output = {
      width: layout.width,
      height: layout.height,
      padding: 'auto',
      data: compiler.data(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'}}
          }
        }
      }]
    };

  // global scales contains only time unit scales
  var timeScales = compiler.time.scales(encoding);
  if (timeScales.length > 0) {
    output.scales = timeScales;
  }

  var group = output.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 area / line together in one group
    compiler.subfacet(group, mdef, details);
  }

  // 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);
  }));

  var legends = legend.defs(encoding, style);

  // Small Multiples
  if (encoding.has(ROW) || encoding.has(COL)) {
    output = compiler.facet(group, encoding, layout, output, singleScaleNames, stats);
    if (legends) {
      output.legends = legends;
    }
  } else {
    group.scales = scale.defs(singleScaleNames, encoding, layout, stats);

    var axes = [];
    if (encoding.has(X)) {
      axes.push(axis.def(X, encoding, layout, stats));
    }
    if (encoding.has(Y)) {
      axes.push(axis.def(Y, encoding, layout, stats));
    }
    if (axes) {
      group.axes = axes;
    }

    if (legends) {
      group.legends = legends;
    }
  }

  return output;
};
{'fraction_non_alphanumeric': 0.1535393818544367, 'fraction_numerical': 0.000872382851445663, 'mean_word_length': 2.7925330812854443, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7187444', 'n_tokens_mistral': 2484, 'n_tokens_neox': 2412, 'n_words': 621}