GithubHelp home page GithubHelp logo

kryndex / react-vega Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vega/react-vega

0.0 2.0 0.0 898 KB

Convert Vega spec into React class conveniently

Home Page: http://kristw.github.io/react-vega/

License: Other

JavaScript 100.00%

react-vega's Introduction

react-vega NPM version Dependency Status

Convert Vega spec into React class conveniently, inspired by this tutorial by @pbeshai

Examples

Install

# via npm
npm install react-vega --save
# or via bower
bower install react-vega --save

Example code

There are two approaches to use this libary.

Approach#1 Create class from spec, then get a React class to use

BarChart.js

See the rest of the spec in barChart.json.

import React, { PropTypes } from 'react';
import {createClassFromSpec} from 'react-vega';

export default createClassFromSpec('BarChart', {
  "width": 400,
  "height": 200,
  "padding": {"top": 10, "left": 30, "bottom": 30, "right": 10},
  "data": [{ "name": "table" }],
  "signals": [
    {
      "name": "hover", "init": null,
      "streams": [
        {"type": "@bar:mouseover", "expr": "datum"},
        {"type": "@bar:mouseout", "expr": "null"}
      ]
    }
  ],
  ... // See the rest in barChart.json
});

main.js

import React from 'react';
import ReactDOM from 'react-dom';
import BarChart from './BarChart.js';

const barData = {
  table: [
    {"x": 1,  "y": 28}, {"x": 2,  "y": 55},
    {"x": 3,  "y": 43}, {"x": 4,  "y": 91},
    {"x": 5,  "y": 81}, {"x": 6,  "y": 53},
    ...
  ]
};

function handleHover(...args){
  console.log(args);
}

ReactDOM.render(
  <BarChart data={barData} onSignalHover={handleHover}/>,
  document.getElementById('bar-container')
);

Approach#2 Use <Vega> generic class and pass in spec for dynamic component.

Provides a bit more flexibility, but at the cost of extra checks for spec changes.

main.js

import React from 'react';
import ReactDOM from 'react-dom';
import Vega from 'react-vega';

const spec = {
  "width": 400,
  "height": 200,
  "padding": {"top": 10, "left": 30, "bottom": 30, "right": 10},
  "data": [{ "name": "table" }],
  "signals": [
    {
      "name": "hover", "init": null,
      "streams": [
        {"type": "@bar:mouseover", "expr": "datum"},
        {"type": "@bar:mouseout", "expr": "null"}
      ]
    }
  ],
  ... // See the rest in barChart.json
}

const barData = {
  table: [
    {"x": 1,  "y": 28}, {"x": 2,  "y": 55},
    {"x": 3,  "y": 43}, {"x": 4,  "y": 91},
    {"x": 5,  "y": 81}, {"x": 6,  "y": 53},
    ...
  ]
};

function handleHover(...args){
  console.log(args);
}

ReactDOM.render(
  <Vega spec={spec} data={barData} onSignalHover={handleHover}/>,
  document.getElementById('bar-container')
);

Props

React class Vega and any output class from createClassFromSpec have these properties:

  • width:Number
  • height:Number
  • padding:Object
  • viewport:Array
  • renderer:String
  • className:String
  • style:Object

These five properties above correspond to Vega's View Component API

  • data:Object

For data, this property takes an Object with keys being dataset names defined in the spec's data field, such as:

var barData = {
  table: [{"x": 1,  "y": 28}, {"x": 2,  "y": 55}, ...]
};

Each value can be an array or function(dataset){...}. If the value is a function, Vega's vis.data(dataName) will be passed as the argument dataset.

var barData = {
  table: function(dataset){...}
};

In the example above, vis.data('table') will be passed as dataset.

  • onSignalXXX - Include all signals defined in the spec automatically.

All signals defined in the spec can be listened to via these properties. For example, to listen to signal hover, attach a listener to onSignal+capitalize('hover')

 <Vega spec={spec} data={barData} onSignalHover={handleHover}/>

Static function

Any class created from createClassFromSpec will have this method.

  • Chart.getSpec() - return spec

License

© 2016 Krist Wongsuphasawat (@kristw) Apache-2.0 License

react-vega's People

Contributors

kristw avatar npmcdn-to-unpkg-bot avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.