Skip to content

Commit

Permalink
feat: add support for signals in mark clip (#9282)
Browse files Browse the repository at this point in the history
## PR Description

In Vega, [mark supports a signal for the `clip`
property](https://vega.github.io/vega/docs/marks/#mark-clipping). To
round out the Vega-Lite API, we should support it here as well,
especially since it is trivial to add.

## Checklist

- [x] This PR is atomic (i.e., it fixes one issue at a time).
- [x] The title is a concise [semantic commit
message](https://www.conventionalcommits.org/) (e.g. "fix: correctly
handle undefined properties").
- [x] `yarn test` runs successfully
- For new features:
  - [x] Has unit tests.
  - [x] Has documentation under `site/docs/` + examples.

Tips:

-
https://medium.com/@greenberg/writing-pull-requests-your-coworkers-might-enjoy-reading-9d0307e93da3
is a nice article about writing a nice PR.
- Use draft PR for work in progress PRs / when you want early feedback
(https://github.blog/2019-02-14-introducing-draft-pull-requests/).

---------

Co-authored-by: GitHub Actions Bot <vega-actions-bot@users.noreply.github.com>
  • Loading branch information
lsh and GitHub Actions Bot authored Mar 11, 2024
1 parent 3bc99d8 commit bf14024
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 6 deletions.
22 changes: 18 additions & 4 deletions build/vega-lite-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -16749,8 +16749,15 @@
]
},
"clip": {
"description": "Whether a mark be clipped to the enclosing group’s width and height.",
"type": "boolean"
"anyOf": [
{
"type": "boolean"
},
{
"$ref": "#/definitions/ExprRef"
}
],
"description": "Whether a mark be clipped to the enclosing group’s width and height."
},
"color": {
"anyOf": [
Expand Down Expand Up @@ -18300,8 +18307,15 @@
]
},
"clip": {
"description": "Whether a mark be clipped to the enclosing group’s width and height.",
"type": "boolean"
"anyOf": [
{
"type": "boolean"
},
{
"$ref": "#/definitions/ExprRef"
}
],
"description": "Whether a mark be clipped to the enclosing group’s width and height."
},
"color": {
"anyOf": [
Expand Down
2 changes: 1 addition & 1 deletion src/compile/mark/mark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ function getMarkGroup(model: UnitModel, opt: {fromPrefix: string} = {fromPrefix:
{
name: model.getName('marks'),
type: markCompiler[mark].vgMark,
...(clip ? {clip: true} : {}),
...(clip ? {clip} : {}),
...(style ? {style} : {}),
...(key ? {key: key.field} : {}),
...(sort ? {sort} : {}),
Expand Down
2 changes: 1 addition & 1 deletion src/mark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ export interface MarkDefMixins<ES extends ExprRef | SignalRef> {
/**
* Whether a mark be clipped to the enclosing group’s width and height.
*/
clip?: boolean;
clip?: boolean | ES;

// Offset properties should not be a part of config

Expand Down
62 changes: 62 additions & 0 deletions test/compile/mark/mark.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,4 +514,66 @@ describe('Mark', () => {
expect(mark[0].clip).toBe(true);
});
});

describe('signal clipping', () => {
it('should pass clip as a signal if clip is an expression', () => {
const model = parseUnitModelWithScaleAndLayoutSize({
mark: {
type: 'bar',
clip: {expr: "datum['foo'] > 10"}
},
encoding: {
x: {type: 'quantitative', field: 'foo'}
}
});

const markGroup = parseMarkGroups(model);
expect(markGroup[0].clip).toEqual({signal: "datum['foo'] > 10"});
});

it('should pass clip as a signal if clip is a signal', () => {
const model = parseUnitModelWithScaleAndLayoutSize({
mark: {
type: 'bar',
clip: {signal: "datum['foo'] > 10"}
},
encoding: {
x: {type: 'quantitative', field: 'foo'}
}
});

const markGroup = parseMarkGroups(model);
expect(markGroup[0].clip).toEqual({signal: "datum['foo'] > 10"});
});

it('should pass clip as a boolean if clip is true', () => {
const model = parseUnitModelWithScaleAndLayoutSize({
mark: {
type: 'bar',
clip: true
},
encoding: {
x: {type: 'quantitative', field: 'foo'}
}
});

const markGroup = parseMarkGroups(model);
expect(markGroup[0].clip).toBe(true);
});

it('should not have clip defined if clip is false', () => {
const model = parseUnitModelWithScaleAndLayoutSize({
mark: {
type: 'bar',
clip: false
},
encoding: {
x: {type: 'quantitative', field: 'foo'}
}
});

const markGroup = parseMarkGroups(model);
expect(markGroup[0].clip).toBeUndefined();
});
});
});

0 comments on commit bf14024

Please sign in to comment.