question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom fuction in vega expression

See original GitHub issue

I’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:closed
  • Created 8 years ago
  • Reactions:4
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
jheercommented, Apr 5, 2018

Good question! The this context bound to the function includes a context 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:

vega.expressionFunction('foo', function() { console.log('foo!', this); });
var spec = {signals: [{name:'bar', update:'foo()'}]};
var view = new vega.View(vega.parse(spec)).run();

Use this.context.dataflow to get a reference to the current View instance.

1reaction
jheercommented, Apr 5, 2018

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:

expressionFunction(name, fn, visitor)
  • The name argument is a string indicating the name of the function as used in the Vega expression language.
  • The fn argument is the actual function to invoke.
  • The visitor argument is an expression AST visitor which can be used to perform dependency analysis (e.g., for scale or data source lookups). For most basic functions no visitor is needed, in which case this argument can be omitted.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found