npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-vega

v7.6.0

Published

Convert Vega spec into React class conveniently

Downloads

139,789

Readme

react-vega NPM version

Use vega or vega-lite in react application smoothly!

DEMO: http://vega.github.io/react-vega/

Install

npm install react vega vega-lite react-vega --save

Versions

Example code

There are two approaches to use this library.

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

BarChart.js

See the rest of the spec in spec1.ts.

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

export default createClassFromSpec('BarChart', {
  "width": 400,
  "height": 200,
  "data": [{ "name": "table" }],
  "signals": [
    {
      "name": "tooltip",
      "value": {},
      "on": [
        {"events": "rect:mouseover", "update": "datum"},
        {"events": "rect:mouseout",  "update": "{}"}
      ]
    }
  ],
  ... // See the rest in packages/react-vega-demo/stories/vega/spec1.ts
});

main.js

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

const barData = {
  table: [...]
};

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

const signalListeners = { hover: handleHover };

ReactDOM.render(
  <BarChart data={barData} signalListeners={signalListeners} />,
  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,
  "data": [{ "name": "table" }],
  "signals": [
    {
      "name": "tooltip",
      "value": {},
      "on": [
        {"events": "rect:mouseover", "update": "datum"},
        {"events": "rect:mouseout",  "update": "{}"}
      ]
    }
  ],
  ... // See the rest in packages/react-vega-demo/stories/vega/spec1.ts
}

const barData = {
  table: [...]
};

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

const signalListeners = { hover: handleHover };

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

Approach#3 Use <VegaLite> generic class and pass in spec for dynamic component.

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

Also see packages/react-vega-demo/stories/ReactVegaLiteDemo.jsx for details

main.js

import React from 'react'
import ReactDOM from 'react-dom'
import { VegaLite } from 'react-vega'

const spec = {
  width: 400,
  height: 200,
  mark: 'bar',
  encoding: {
    x: { field: 'a', type: 'ordinal' },
    y: { field: 'b', type: 'quantitative' },
  },
  data: { name: 'table' }, // note: vega-lite data attribute is a plain object instead of an array
}

const barData = {
  table: [
    { a: 'A', b: 28 },
    { a: 'B', b: 55 },
    { a: 'C', b: 43 },
    { a: 'D', b: 91 },
    { a: 'E', b: 81 },
    { a: 'F', b: 53 },
    { a: 'G', b: 19 },
    { a: 'H', b: 87 },
    { a: 'I', b: 52 },
  ],
}

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

API

Props

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

Props from vega-embed's API

mode, theme, defaultStyle, renderer, logLovel, tooltip, loader, patch, width, height, padding, actions, scaleFactor, config, editorUrl, sourceHeader, sourceFooter, hover, i18n, downloadFileName

CSS

class and style of the container <div> element

  • className:String
  • style:Object

Data

  • 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. If you are using <VegaLite> make sure to enable your tooltip in the the spec, as described here.

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

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

  • signalListeners:Object

All signals defined in the spec can be listened to via signalListeners. For example, to listen to signal hover, attach a listener like this

// better declare outside of render function
const signalListeners = { hover: handleHover };

<Vega spec={spec} data={barData} signalListeners={signalListeners} />

Event listeners

  • onNewView:Function Dispatched when new vega.View is constructed and pass the newly created view as argument.
  • onParseError:Function Dispatched when vega cannot parse the spec.

Static function

Any class created from createClassFromSpec will have this method.

  • Chart.getSpec() - return spec

Frequently Asked Questions

How to use Vega Tooltip?

You can pass the vega-tooltip handler instance to the tooltip property.

import { Handler } from 'vega-tooltip';

 <Vega spec={spec} data={barData} tooltip={new Handler().call} />