Skip to content

Commit

Permalink
[geo] JS function to receive the whole data array instead of individu…
Browse files Browse the repository at this point in the history
…al object (#4262)

Moving from having the user define an interceptor function that operates
on one object at a time.

By passing the entire array, it's possible to do multiple pass where
needed. A common pattern might be to figure out the max value in order
to define a scaler function. That's only possible if dealing with the
whole array.
  • Loading branch information
mistercrunch authored Jan 24, 2018
1 parent 9cf16a4 commit ff2f85f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 26 deletions.
9 changes: 5 additions & 4 deletions superset/assets/javascripts/explore/stores/controls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1881,10 +1881,11 @@ export const controls = {
},
},

js_datapoint_mutator: jsFunctionControl(
t('Javascript data point mutator'),
t('Define a javascript function that receives each data point and can alter it ' +
'before getting sent to the deck.gl layer'),
js_data_mutator: jsFunctionControl(
t('Javascript data interceptor'),
t('Define a javascript function that receives the data array used in the visualization ' +
'and is expected to return a modified version of that array. This can be used ' +
'to alter properties of the data, filter, or enrich the array.'),
),

js_data: jsFunctionControl(
Expand Down
8 changes: 4 additions & 4 deletions superset/assets/javascripts/explore/stores/visTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ export const visTypes = {
label: t('Advanced'),
controlSetRows: [
['js_columns'],
['js_datapoint_mutator'],
['js_data_mutator'],
['js_tooltip'],
['js_onclick_href'],
],
Expand Down Expand Up @@ -509,7 +509,7 @@ export const visTypes = {
label: t('Advanced'),
controlSetRows: [
['js_columns'],
['js_datapoint_mutator'],
['js_data_mutator'],
['js_tooltip'],
['js_onclick_href'],
],
Expand Down Expand Up @@ -549,7 +549,7 @@ export const visTypes = {
label: t('Advanced'),
controlSetRows: [
['js_columns'],
['js_datapoint_mutator'],
['js_data_mutator'],
['js_tooltip'],
['js_onclick_href'],
],
Expand Down Expand Up @@ -621,7 +621,7 @@ export const visTypes = {
label: t('Advanced'),
controlSetRows: [
['js_columns'],
['js_datapoint_mutator'],
['js_data_mutator'],
['js_tooltip'],
['js_onclick_href'],
],
Expand Down
17 changes: 8 additions & 9 deletions superset/assets/visualizations/deckgl/layers/geojson.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,17 @@ const alterProps = (props, propOverrides) => {
};
};
let features;
const recurseGeoJson = (node, propOverrides, jsFnMutator, extraProps) => {
const recurseGeoJson = (node, propOverrides, extraProps) => {
if (node && node.features) {
node.features.forEach((obj) => {
recurseGeoJson(obj, propOverrides, jsFnMutator, node.extraProps || extraProps);
recurseGeoJson(obj, propOverrides, node.extraProps || extraProps);
});
}
if (node && node.geometry) {
const newNode = {
...node,
properties: alterProps(node.properties, propOverrides),
};
if (jsFnMutator) {
jsFnMutator(newNode);
}
if (!newNode.extraProps) {
newNode.extraProps = extraProps;
}
Expand All @@ -70,14 +67,16 @@ export default function geoJsonLayer(formData, payload, slice) {
propOverrides.strokeColor = strokeColor;
}

features = [];
recurseGeoJson(payload.data, propOverrides);

let jsFnMutator;
if (fd.js_datapoint_mutator) {
if (fd.js_data_mutator) {
// Applying user defined data mutator if defined
jsFnMutator = sandboxedEval(fd.js_datapoint_mutator);
jsFnMutator = sandboxedEval(fd.js_data_mutator);
features = jsFnMutator(features);
}

features = [];
recurseGeoJson(payload.data, propOverrides, jsFnMutator);
return new GeoJsonLayer({
id: `geojson-layer-${fd.slice_id}`,
filled: fd.filled,
Expand Down
6 changes: 3 additions & 3 deletions superset/assets/visualizations/deckgl/layers/path.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export default function getLayer(formData, payload, slice) {
color: fixedColor,
}));

if (fd.js_datapoint_mutator) {
const jsFnMutator = sandboxedEval(fd.js_datapoint_mutator);
data = data.map(jsFnMutator);
if (fd.js_data_mutator) {
const jsFnMutator = sandboxedEval(fd.js_data_mutator);
data = jsFnMutator(data);
}

return new PathLayer({
Expand Down
6 changes: 3 additions & 3 deletions superset/assets/visualizations/deckgl/layers/polygon.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export default function polygonLayer(formData, payload, slice) {
fillColor: [fc.r, fc.g, fc.b, 255 * fc.a],
}));

if (fd.js_datapoint_mutator) {
if (fd.js_data_mutator) {
// Applying user defined data mutator if defined
const jsFnMutator = sandboxedEval(fd.js_datapoint_mutator);
data = data.map(jsFnMutator);
const jsFnMutator = sandboxedEval(fd.js_data_mutator);
data = jsFnMutator(data);
}

return new PolygonLayer({
Expand Down
6 changes: 3 additions & 3 deletions superset/assets/visualizations/deckgl/layers/scatter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export default function getLayer(formData, payload, slice) {
};
});

if (fd.js_datapoint_mutator) {
if (fd.js_data_mutator) {
// Applying user defined data mutator if defined
const jsFnMutator = sandboxedEval(fd.js_datapoint_mutator);
data = data.map(jsFnMutator);
const jsFnMutator = sandboxedEval(fd.js_data_mutator);
data = jsFnMutator(data);
}

return new ScatterplotLayer({
Expand Down

0 comments on commit ff2f85f

Please sign in to comment.