Skip to content

Commit 16314f7

Browse files
committed
Fix vega expression format() call
Signed-off-by: Itay Dafna <[email protected]>
1 parent 44724c4 commit 16314f7

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

js/vegaexpr.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@
22
// Distributed under the terms of the Modified BSD License.
33

44
const vegaExpressions: any = require('vega-expression');
5+
const vegaFormat: any = require('vega-format');
56
const vegaFunctions: any = require('vega-functions');
6-
const d3Format: any = require('d3-format');
77

88
import { WidgetModel, WidgetView } from '@jupyter-widgets/base';
9-
109
import { CellRenderer } from '@lumino/datagrid';
11-
1210
import { MODULE_NAME, MODULE_VERSION } from './version';
1311

12+
// Newer versions of vega-functions require a locale property.
13+
// When using vega-functions from within Vega, this is provided,
14+
// but because we're using vega-functions separately, we need to
15+
// manually create and bind that locale context to the format function.
16+
const locale = vegaFormat.defaultLocale();
17+
const dataflow = { context: { dataflow: { locale: () => locale } } };
18+
vegaFunctions.functionContext.format =
19+
vegaFunctions.functionContext.format.bind(dataflow);
20+
1421
export class VegaExprModel extends WidgetModel {
1522
defaults() {
1623
return {
@@ -30,7 +37,6 @@ export class VegaExprModel extends WidgetModel {
3037
globalvar: 'cell',
3138
functions: (codegen: any) => {
3239
const fn = vegaExpressions.functions(codegen);
33-
fn.format = this.d3FormatFunc;
3440
for (const name in vegaFunctions.functionContext) {
3541
fn[name] = 'functions.' + name;
3642
}
@@ -44,14 +50,6 @@ export class VegaExprModel extends WidgetModel {
4450
this.on('change:value', this.updateFunction.bind(this));
4551
}
4652

47-
// Newer versions of vega introduced a locale requirement, which we do
48-
// not use (older vega dependency) in ipydatagrid. The .format() function
49-
// specifically won't work without that context. Therefore, as that function
50-
// uses d3-format, we're monkey patching any .format() calls to use d3 directly.
51-
d3FormatFunc(value: any, spec: any) {
52-
return d3Format.format(spec)(value);
53-
}
54-
5553
process(config: CellRenderer.CellConfig, defaultValue: any) {
5654
return this._function(config, defaultValue, vegaFunctions.functionContext);
5755
}

0 commit comments

Comments
 (0)