React Wrapper for Vega/Vega-Lite Visualizations

8.0.0 · active · verified Sun Apr 19

react-vega is a lightweight React component library that wraps vega-embed, enabling the declarative integration of Vega and Vega-Lite visualizations within React applications. Its current stable version is 8.0.0, released in 2024. While specific release cadence isn't explicitly stated, the project maintains an active development presence, aligning with the broader Vega ecosystem's evolution. Key differentiators include providing both a `<VegaEmbed />` component for direct JSX usage and a `useVegaEmbed` hook for more imperative control and access to the Vega View API, facilitating dynamic data updates and event subscriptions without full re-renders. It ships with TypeScript types, offering a robust development experience. Unlike directly using `vega-embed`, `react-vega` handles the React lifecycle integration, ensuring proper mounting, unmounting, and updates of the visualization container.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates embedding a static Vega-Lite bar chart using the `VegaEmbed` component.

import React from 'react';
import { VegaEmbed } from 'react-vega';

const spec = {
  $schema: 'https://vega.github.io/schema/vega-lite/v5.json',
  description: 'A simple bar chart with embedded data.',
  data: { values: [
    {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: 12}
  ]},
  mark: 'bar',
  encoding: {
    x: {field: 'a', type: 'nominal', axis: {labelAngle: 0}},
    y: {field: 'b', type: 'quantitative'}
  }
};

const options = {
  actions: false // Disable default actions like 'Export as SVG/PNG'
};

function MyVegaChart() {
  return (
    <div>
      <h1>My Interactive Chart</h1>
      <VegaEmbed spec={spec} options={options} />
    </div>
  );
}

export default MyVegaChart;

view raw JSON →