Skip to content

Commit

Permalink
feat: add data support for layer (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
kristw committed Oct 18, 2019
1 parent 6d409a4 commit b63e3ae
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
5 changes: 3 additions & 2 deletions packages/react-vega-demo/stories/index.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import ReactVegaDemo from './ReactVegaDemo';
import ReactVegaLiteDemo from './ReactVegaLiteDemo';
import './style.css';

storiesOf('react-vega', module).add('Vega', () => <ReactVegaDemo />);
storiesOf('react-vega', module).add('VegaLite', () => <ReactVegaLiteDemo />);
storiesOf('react-vega', module)
.add('Vega', () => <ReactVegaDemo />)
.add('VegaLite', () => <ReactVegaLiteDemo />);
1 change: 0 additions & 1 deletion packages/react-vega-demo/stories/style.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
body{
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
text-align: center;
}

h1, h2, h3{
Expand Down
36 changes: 36 additions & 0 deletions packages/react-vega-demo/stories/vegaLiteWithLayers.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-disable react/jsx-filename-extension */
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('VegaLite with layers', () => <><VegaLite spec={SPEC} data={DATA} /><div><pre>{JSON.stringify(SPEC, null, 2)}</pre></div></>);
5 changes: 2 additions & 3 deletions packages/react-vega/src/Vega.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import isFunction from './utils/isFunction';
import { PlainObject, View, ViewListener } from './types';
import shallowEqual from './utils/shallowEqual';
import { NOOP } from './constants';
import getDatasetNamesFromSpec from './utils/getDatasetNamesFromSpec';

export type VegaProps = VegaEmbedProps & {
data?: PlainObject;
Expand Down Expand Up @@ -53,10 +52,10 @@ export default class Vega extends React.PureComponent<VegaProps> {
};

update() {
const { data, spec } = this.props;
const { data } = this.props;

if (data) {
const datasetNames = getDatasetNamesFromSpec(spec).filter(name => data[name]);
const datasetNames = Object.keys(data);

if (this.vegaEmbed.current && datasetNames.length > 0) {
this.vegaEmbed.current.modifyView(view => {
Expand Down

0 comments on commit b63e3ae

Please sign in to comment.