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 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 | 'use strict'; var summary = module.exports = require('datalib/src/stats').summary; require('../globals'); module.exports = compile; var Encoding = require('../Encoding'), axis = compile.axis = require('./axis'), filter = compile.filter = require('./filter'), legend = compile.legend = require('./legend'), marks = compile.marks = require('./marks'), scale = compile.scale = require('./scale'); compile.aggregate = require('./aggregate'); compile.bin = require('./bin'); compile.facet = require('./facet'); compile.group = require('./group'); compile.layout = require('./layout'); compile.sort = require('./sort'); compile.stack = require('./stack'); compile.style = require('./style'); compile.subfacet = require('./subfacet'); compile.template = require('./template'); compile.time = require('./time'); function compile(spec, stats, theme) { return compile.encoding(Encoding.fromSpec(spec, theme), stats); } compile.shorthand = function (shorthand, stats, config, theme) { return compile.encoding(Encoding.fromShorthand(shorthand, config, theme), stats); }; compile.encoding = function (encoding, stats) { // no need to pass stats if you pass in the data Iif (!stats && encoding.hasValues()) { stats = summary(encoding.data('values')).reduce(function(s, p) { s[p.field] = p; return s; }, {}); } var layout = compile.layout(encoding, stats), spec = compile.template(encoding, layout, stats); // .data related stuff var rawTable = spec.data[0], dataTable = spec.data[1]; rawTable = filter.addFilters(rawTable, encoding); // modify rawTable dataTable = compile.bin(dataTable, encoding); // modify dataTable spec = compile.time(spec, encoding); // modify dataTable, add scales var aggResult = compile.aggregate(dataTable, encoding); // modify dataTable var sorting = compile.sort(spec.data, encoding, stats); // append new data // marks var style = compile.style(encoding, stats), group = spec.marks[0], mark = marks[encoding.marktype()], mdefs = marks.def(mark, encoding, layout, style), mdef = mdefs[0]; // TODO: remove this dirty hack by refactoring the whole flow for (var i = 0; i < mdefs.length; i++) { group.marks.push(mdefs[i]); } var lineType = marks[encoding.marktype()].line; // handle subfacets var details = aggResult.details, hasDetails = details && details.length > 0, stack = hasDetails && compile.stack(spec.data, encoding, mdef, aggResult.facets); // modify spec.data, mdef.{from,properties} Eif (hasDetails && (stack || lineType)) { //subfacet to group stack / line together in one group compile.subfacet(group, mdef, details, stack, encoding); } // auto-sort line/area values //TODO(kanitw): have some config to turn off auto-sort for line (for line chart that encodes temporal information) Iif (lineType) { var f = (encoding.isMeasure(X) && encoding.isDimension(Y)) ? Y : X; if (!mdef.from) mdef.from = {}; // TODO: why - ? mdef.from.transform = [{type: 'sort', by: '-' + encoding.field(f)}]; } // Small Multiples Iif (encoding.has(ROW) || encoding.has(COL)) { spec = compile.facet(group, encoding, layout, style, sorting, spec, mdef, stack, stats); spec.legends = legend.defs(encoding); } else { group.scales = scale.defs(scale.names(mdef.properties.update), encoding, layout, stats, style, sorting, {stack: stack}); group.axes = axis.defs(axis.names(mdef.properties.update), encoding, layout, stats); group.legends = legend.defs(encoding); } filter.filterLessThanZero(dataTable, encoding); return spec; }; |