Skip to content

Introducing transform plugins #499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Jul 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
53a5971
expose Lib on test dashboard window
etpinard May 3, 2016
9909382
lint
etpinard May 3, 2016
28147e2
add check so that we don't try to destroy gd if it DNE
etpinard May 3, 2016
9ac6d0f
make Plotly.register handle transform modules
etpinard May 3, 2016
06f2e6d
add support for transform plugins:
etpinard May 3, 2016
8ab9d0c
make Plotly.restyle support transforms:
etpinard May 3, 2016
2697975
add two example transform modules
etpinard May 3, 2016
56665f2
add transform test suite
etpinard May 3, 2016
aa274ef
update tests (plots.supplyDataDefaults -> supplyTraceDefaults)
etpinard May 3, 2016
a06441a
[tmp commit] add example transforms to ./lib/ Plotly for testing
etpinard May 3, 2016
0b62e45
Merge branch 'master' into transform-plugins
etpinard Jul 4, 2016
1c651e8
toimage_test: make sure that all graph divs are deleted
etpinard Jul 4, 2016
f93b9c1
plots: rm useless skip-over-step
etpinard Jul 4, 2016
ccbcb58
Merge branch 'master' into transform-plugins
etpinard Jul 18, 2016
37e291b
plots: make relinkPrivateKeys less strict
etpinard Jul 19, 2016
383c6d4
move common code in pushModule func
etpinard Jul 19, 2016
be36402
rm useless comment about fits ;)
etpinard Jul 19, 2016
0e1eb5a
do not add transforms container if fullTrace is no transform present
etpinard Jul 19, 2016
6a8e158
try more useless refs to input container in full trace
etpinard Jul 19, 2016
9a341c1
mutate uid of expanded traces w.r.t to expanded index:
etpinard Jul 19, 2016
1752d30
make uid of expanded trace valid alpha-numerical hashes
etpinard Jul 19, 2016
b5ec01e
add support for transform in plot schema and Plotly.validate
etpinard Jul 20, 2016
a6fda02
improve validate test cases
etpinard Jul 20, 2016
da2c24c
validate transform module on registration:
etpinard Jul 20, 2016
8d7dc46
improve transform tests
etpinard Jul 20, 2016
320d62e
Revert "[tmp commit] add example transforms to ./lib/ Plotly for test…
etpinard Jul 20, 2016
3e6e9c1
rm @assets path shortcut,
etpinard Jul 20, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions devtools/test_dashboard/devtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
var Fuse = require('fuse.js');
var mocks = require('../../build/test_dashboard_mocks.json');
var credentials = require('../../build/credentials.json');
var Lib = require('@src/lib');

// put d3 in window scope
var d3 = window.d3 = Plotly.d3;
Expand Down Expand Up @@ -149,6 +150,7 @@ var Tabs = {

// Bind things to the window
window.Tabs = Tabs;
window.Lib = Lib;
setInterval(function() {
window.gd = Tabs.getGraph() || Tabs.fresh();
window.fullLayout = window.gd._fullLayout;
Expand Down
5 changes: 4 additions & 1 deletion devtools/test_dashboard/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var watchify = require('watchify');

var constants = require('../../tasks/util/constants');
var compress = require('../../tasks/util/compress_attributes');
var shortcutPaths = require('../../tasks/util/shortcut_paths');

var PORT = process.argv[2] || 3000;

Expand All @@ -32,7 +33,9 @@ b.on('update', bundlePlotly);

// Bundle devtools code
var devtoolsPath = path.join(constants.pathToRoot, 'devtools/test_dashboard');
var devtools = browserify(path.join(devtoolsPath, 'devtools.js'), {});
var devtools = browserify(path.join(devtoolsPath, 'devtools.js'), {
transform: [shortcutPaths]
});

var firstBundle = true;

Expand Down
20 changes: 11 additions & 9 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -846,9 +846,8 @@ Plotly.newPlot = function(gd, data, layout, config) {
function doCalcdata(gd) {
var axList = Plotly.Axes.list(gd),
fullData = gd._fullData,
fullLayout = gd._fullLayout;

var i, trace, module, cd;
fullLayout = gd._fullLayout,
i;

var calcdata = gd.calcdata = new Array(fullData.length);

Expand All @@ -874,12 +873,12 @@ function doCalcdata(gd) {
}

for(i = 0; i < fullData.length; i++) {
trace = fullData[i];
module = trace._module;
cd = [];
var trace = fullData[i],
_module = trace._module,
cd = [];

if(module && trace.visible === true) {
if(module.calc) cd = module.calc(gd, trace);
if(_module && trace.visible === true) {
if(_module.calc) cd = _module.calc(gd, trace);
}

// make sure there is a first point
Expand Down Expand Up @@ -1551,7 +1550,7 @@ Plotly.restyle = function restyle(gd, astr, val, traces) {

if(isNumeric(traces)) traces = [traces];
else if(!Array.isArray(traces) || !traces.length) {
traces = gd._fullData.map(function(v, i) { return i; });
traces = gd.data.map(function(v, i) { return i; });
}

// recalcAttrs attributes need a full regeneration of calcdata
Expand Down Expand Up @@ -1726,6 +1725,9 @@ Plotly.restyle = function restyle(gd, astr, val, traces) {
continue;
}

// take no chances on transforms
if(ai.substr(0, 10) === 'transforms') docalc = true;

// set attribute in gd.data
undoit[ai] = a0();
for(i = 0; i < traces.length; i++) {
Expand Down
17 changes: 17 additions & 0 deletions src/plot_api/plot_schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var UNDERSCORE_ATTRS = [IS_SUBPLOT_OBJ, IS_LINKED_TO_ARRAY, DEPRECATED];
var plotSchema = {
traces: {},
layout: {},
transforms: {},
defs: {}
};

Expand All @@ -45,7 +46,11 @@ PlotSchema.get = function() {
.forEach(getTraceAttributes);

getLayoutAttributes();

Object.keys(Plots.transformsRegistry).forEach(getTransformAttributes);

getDefs();

return plotSchema;
};

Expand Down Expand Up @@ -136,6 +141,18 @@ function getLayoutAttributes() {
plotSchema.layout = { layoutAttributes: layoutAttributes };
}

function getTransformAttributes(name) {
var _module = Plots.transformsRegistry[name],
_schema = {};

_schema = coupleAttrs(_schema, _module.attributes || {}, 'attributes', '*');
_schema = removeUnderscoreAttrs(_schema);
mergeValTypeAndRole(_schema);
handleLinkedToArray(_schema);

plotSchema.transforms[name] = { attributes: _schema };
}

function getDefs() {
plotSchema.defs = {
valObjects: Lib.valObjects,
Expand Down
38 changes: 37 additions & 1 deletion src/plot_api/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,39 @@ module.exports = function valiate(data, layout) {
}

crawl(traceIn, traceOut, traceSchema, errorList, base);

var transformsIn = traceIn.transforms,
transformsOut = traceOut.transforms;

if(transformsIn) {
if(!isArray(transformsIn)) {
errorList.push(format('array', base, ['transforms']));
}

base.push('transforms');

for(var j = 0; j < transformsIn.length; j++) {
var path = ['transforms', j],
transformType = transformsIn[j].type;

if(!isPlainObject(transformsIn[j])) {
errorList.push(format('object', base, path));
continue;
}

var transformSchema = schema.transforms[transformType] ?
schema.transforms[transformType].attributes :
{};

// add 'type' to transform schema to validate the transform type
transformSchema.type = {
valType: 'enumerated',
values: Object.keys(schema.transforms)
};

crawl(transformsIn[j], transformsOut[j], transformSchema, errorList, base, path);
}
}
}

var layoutOut = gd._fullLayout,
Expand All @@ -121,6 +154,9 @@ function crawl(objIn, objOut, schema, list, base, path) {
for(var i = 0; i < keys.length; i++) {
var k = keys[i];

// transforms are handled separately
if(k === 'transforms') continue;

var p = path.slice();
p.push(k);

Expand Down Expand Up @@ -184,7 +220,7 @@ var code2msgFunc = {
var prefix;

if(base === 'layout' && astr === '') prefix = 'The layout argument';
else if(base[0] === 'data') {
else if(base[0] === 'data' && astr === '') {
prefix = 'Trace ' + base[1] + ' in the data argument';
}
else prefix = inBase(base) + 'key ' + astr;
Expand Down
46 changes: 38 additions & 8 deletions src/plotly.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
require('es6-promise').polyfill();

// lib functions
exports.Lib = require('./lib');
var Lib = exports.Lib = require('./lib');
exports.util = require('./lib/svg_text_utils');
exports.Queue = require('./lib/queue');

Expand Down Expand Up @@ -55,21 +55,51 @@ exports.ModeBar = require('./components/modebar');
exports.register = function register(_modules) {
if(!_modules) {
throw new Error('No argument passed to Plotly.register.');
} else if(_modules && !Array.isArray(_modules)) {
}
else if(_modules && !Array.isArray(_modules)) {
_modules = [_modules];
}

for(var i = 0; i < _modules.length; i++) {
var newModule = _modules[i];

if(newModule && newModule.moduleType !== 'trace') {
if(!newModule) {
throw new Error('Invalid module was attempted to be registered!');
} else {
Plots.register(newModule, newModule.name, newModule.categories, newModule.meta);
}

switch(newModule.moduleType) {
case 'trace':
Plots.register(newModule, newModule.name, newModule.categories, newModule.meta);

if(!Plots.subplotsRegistry[newModule.basePlotModule.name]) {
Plots.registerSubplot(newModule.basePlotModule);
}

break;

case 'transform':
if(typeof newModule.name !== 'string') {
throw new Error('Transform module *name* must be a string.');
}

var prefix = 'Transform module ' + newModule.name;

if(typeof newModule.transform !== 'function') {
throw new Error(prefix + ' is missing a *transform* function.');
}
if(!Lib.isPlainObject(newModule.attributes)) {
Lib.log(prefix + ' registered without an *attributes* object.');
}
if(typeof newModule.supplyDefaults !== 'function') {
Lib.log(prefix + ' registered without a *supplyDefaults* function.');
}

Plots.transformsRegistry[newModule.name] = newModule;

break;

if(!Plots.subplotsRegistry[newModule.basePlotModule.name]) {
Plots.registerSubplot(newModule.basePlotModule);
}
default:
throw new Error('Invalid module was attempted to be registered!');
}
}
};
Expand Down
Loading