Custom fuction in vega expression
See original GitHub issueI’m wondering if it is possible to add a new function to be used in vega expressions like this
...
"transform": {"type": "formula", "field": "myNewField", "expr": "myFunction(datum.mySourceField)"},
...
After looking at how things are done in vega/vega-expression
, I tried the following but it did not work:
// Include vega.js and dependencices
window.myFunctionInRealLife = function(value) {
return value * 42;
}
vg.parse.expr.codegen.functions["myFunction"] = function(args) {
var a = args.map(vg.parse.expr.codegen);
return 'window.myFunctionInRealLife('+a[0]+')';
}
// Use myFunction in a vega spec
It would be nice to see a temporal workaround and a proper solution in future releases.
Just in case: my specific goal is to calculate centroids for geopolygons after they have been projected (i.e convert layout_path to some [x,y]). This can be done using d3.path.centroid().
Issue Analytics
- State:
- Created 8 years ago
- Reactions:4
- Comments:10 (10 by maintainers)
Top Results From Across the Web
Expressions | Vega
To enable custom calculations, Vega includes its own expression language for writing basic formulas. For example, these expressions are used by the filter...
Read more >Registering custom vega formatters in jupyterlab with altair
I cannot figure out how to register the vega expression function in jupyterlab. It works when using the vega-editor and injecting the expression...
Read more >How to do a simple (but useful) Data Transform in Vega
For its expression language Vega offers many special expressions – the one we use here is data() . It allows to access the...
Read more >vega-functions | Yarn - Package Manager
Unlike the basic utility functions included in the vega-expression package, this package includes custom expression functions, many of which are specific to ...
Read more >How to use the vega-functions.codeGenerator function in vega ...
To help you get started, we've selected a few vega-functions.codeGenerator examples ... vega-functions. Custom functions for the Vega expression language.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Good question! The
this
context bound to the function includes acontext
property that is the runtime context object for the Vega View. To explore this yourself, go to the Vega home page, open the console, and try this:Use
this.context.dataflow
to get a reference to the currentView
instance.The original issue was specific to Vega 2.
In Vega 3, you can add new expression functions using
vega.expressionFunction
, a method exported by vega-parser. The signature is:New expressions must be added before parsing a spec that uses the custom function. Also this is a global operation, and all subsequent Vega views will have access to the function.
We will update the documentation with the above information; closing out this issue now.