'use strict';
require('../globals');
var vlEncDef = require('../encdef');
module.exports = function(encoding, stats) {
return {
opacity: estimateOpacity(encoding, stats),
};
};
function estimateOpacity(encoding,stats) {
Iif (!stats) {
return 1;
}
var numPoints = 0;
if (encoding.isAggregate()) { // aggregate plot
numPoints = 1;
// get number of points in each "cell"
// by calculating product of cardinality
// for each non faceting and non-ordinal X / Y fields
// note that ordinal x,y are not include since we can
// consider that ordinal x are subdividing the cell into subcells anyway
encoding.forEach(function(encDef, encType) {
if (encType !== ROW && encType !== COL &&
!((encType === X || encType === Y) &&
vlEncDef.isOrdinalScale(encDef))
) {
numPoints *= encoding.cardinality(encType, stats);
}
});
} else { // raw plot
// TODO: error handling
if (!stats['*'])
return 1;
numPoints = stats['*'].max; // count
// small multiples divide number of points
var numMultiples = 1;
if (encoding.has(ROW)) {
numMultiples *= encoding.cardinality(ROW, stats);
}
if (encoding.has(COL)) {
numMultiples *= encoding.cardinality(COL, stats);
}
numPoints /= numMultiples;
}
var opacity = 0;
if (numPoints <= 25) {
opacity = 1;
} else if (numPoints < 200) {
opacity = 0.8;
} else if (numPoints < 1000 || encoding.is('tick')) {
opacity = 0.7;
} else {
opacity = 0.3;
}
return opacity;
}
|