Skip to content

Commit

Permalink
fix: make datum work with expr
Browse files Browse the repository at this point in the history
  • Loading branch information
kanitw committed Sep 13, 2020
1 parent 35d17ee commit b404b86
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/channeldef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ import {isSignalRef} from './vega.schema';

export type PrimitiveValue = number | string | boolean | null;

export type Value = PrimitiveValue | number[] | Gradient | Text | ExprRef | SignalRef;
export type Value<ES extends ExprRef | SignalRef = ExprRef | SignalRef> =
| PrimitiveValue
| number[]
| Gradient
| Text
| ES;

/**
* Definition object for a constant value (primitive value or gradient definition) of an encoding channel.
Expand Down
3 changes: 3 additions & 0 deletions src/compile/mark/encode/valueref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from '../../../channeldef';
import {Config} from '../../../config';
import {dateTimeToExpr, isDateTime} from '../../../datetime';
import {isExprRef} from '../../../expr';
import * as log from '../../../log';
import {isPathMark, Mark, MarkDef} from '../../../mark';
import {fieldValidPredicate} from '../../../predicate';
Expand Down Expand Up @@ -138,6 +139,8 @@ export function valueRefForFieldOrDatumDef(
ref.signal = dateTimeToExpr(datum);
} else if (isSignalRef(datum)) {
ref.signal = datum.signal;
} else if (isExprRef(datum)) {
ref.signal = datum.expr;
} else {
ref.value = datum;
}
Expand Down
2 changes: 1 addition & 1 deletion src/vega.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function isSignalRef(o: any): o is SignalRef {

// TODO: add type of value (Make it VgValueRef<V extends ValueOrGradient> {value?:V ...})
export interface VgValueRef {
value?: Value | number[];
value?: Value<null>;
field?:
| string
| {
Expand Down
13 changes: 13 additions & 0 deletions test/compile/mark/point.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,17 @@ describe('Mark: Circle', () => {

expect(filledCircleProps.stroke).toEqual({signal: "'red'"});
});

it('converts expression in mark properties into signal', () => {
const filledCircleModel = parseUnitModelWithScaleAndLayoutSize({
mark: {type: 'circle'},
encoding: {
x: {datum: {expr: 'myX'}, type: 'quantitative'}
}
});

const filledCircleProps = circle.encodeEntry(filledCircleModel);

expect(filledCircleProps.x).toEqual({scale: 'x', signal: 'myX'});
});
});

0 comments on commit b404b86

Please sign in to comment.