-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support top-level width and height (#144)
* feat: support top-level width and height * fix: omit width and height * test: add tests
- Loading branch information
Showing
5 changed files
with
150 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React from 'react'; | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { storiesOf } from '@storybook/react'; | ||
import { VegaLite } from '../../react-vega/src'; | ||
|
||
const DATA = { | ||
myData: [ | ||
{ a: 'A', b: 20 }, | ||
{ a: 'B', b: 34 }, | ||
{ a: 'C', b: 55 }, | ||
{ a: 'D', b: 19 }, | ||
{ a: 'E', b: 40 }, | ||
{ a: 'F', b: 34 }, | ||
{ a: 'G', b: 91 }, | ||
{ a: 'H', b: 78 }, | ||
{ a: 'I', b: 25 }, | ||
], | ||
}; | ||
|
||
const SPEC = { | ||
description: 'A simple bar chart with embedded data.', | ||
layer: [ | ||
{ | ||
data: { name: 'myData' }, | ||
encoding: { | ||
x: { field: 'a', type: 'ordinal' }, | ||
y: { field: 'b', type: 'quantitative' }, | ||
}, | ||
mark: 'bar', | ||
}, | ||
], | ||
}; | ||
|
||
storiesOf('react-vega', module).add('width=300', () => ( | ||
<VegaLite spec={SPEC} data={DATA} width={300} /> | ||
)).add('height=300', () => ( | ||
<VegaLite spec={SPEC} data={DATA} height={300} /> | ||
)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { VisualizationSpec } from 'vega-embed'; | ||
import { VegaEmbedProps } from '../VegaEmbed'; | ||
|
||
export default function combineSpecWithDimension(props: VegaEmbedProps): VisualizationSpec { | ||
const { spec, width, height } = props; | ||
if (typeof width !== 'undefined' && typeof height !== 'undefined') { | ||
return { ...spec, width, height }; | ||
} | ||
if (typeof width !== 'undefined') { | ||
return { ...spec, width }; | ||
} | ||
if (typeof height !== 'undefined') { | ||
return { ...spec, height }; | ||
} | ||
return spec; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/react-vega/test/utils/combineSpecWithDimension.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import combineSpecWithDimension from '../../src/utils/combineSpecWithDimension'; | ||
|
||
describe('combineSpecWithDimension(props)', () => { | ||
const spec = { | ||
$schema: 'https://vega.github.io/schema/vega-lite/v4.json', | ||
data: { | ||
values: [], | ||
name: 'source', | ||
}, | ||
mark: 'bar', | ||
encoding: { | ||
y: { | ||
field: 'b', | ||
type: 'quantitative', | ||
}, | ||
}, | ||
} as const; | ||
|
||
it('adds width and height', () => { | ||
expect(combineSpecWithDimension({ spec, width: 100, height: 100 })).toEqual({ | ||
$schema: 'https://vega.github.io/schema/vega-lite/v4.json', | ||
width: 100, | ||
height: 100, | ||
data: { | ||
values: [], | ||
name: 'source', | ||
}, | ||
mark: 'bar', | ||
encoding: { | ||
y: { | ||
field: 'b', | ||
type: 'quantitative', | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('adds width', () => { | ||
expect(combineSpecWithDimension({ spec, width: 100 })).toEqual({ | ||
$schema: 'https://vega.github.io/schema/vega-lite/v4.json', | ||
width: 100, | ||
data: { | ||
values: [], | ||
name: 'source', | ||
}, | ||
mark: 'bar', | ||
encoding: { | ||
y: { | ||
field: 'b', | ||
type: 'quantitative', | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('adds height', () => { | ||
expect(combineSpecWithDimension({ spec, height: 100 })).toEqual({ | ||
$schema: 'https://vega.github.io/schema/vega-lite/v4.json', | ||
height: 100, | ||
data: { | ||
values: [], | ||
name: 'source', | ||
}, | ||
mark: 'bar', | ||
encoding: { | ||
y: { | ||
field: 'b', | ||
type: 'quantitative', | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('returns original if no width or height are defined', () => { | ||
expect(combineSpecWithDimension({ spec })).toBe(spec); | ||
}); | ||
}); |