• Stars
    star
    352
  • Rank 116,135 (Top 3 %)
  • Language
    TypeScript
  • License
    BSD 3-Clause "New...
  • Created over 8 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Publish Vega visualizations as embedded web components with interactive parameters.

Vega-Embed

npm version Build Status codecov code style: prettier

Vega-Embed makes it easy to embed interactive Vega and Vega-Lite views into web pages. With Vega Embed, you can:

  • Load Vega/Vega-Lite specs from source text, parsed JSON, or URLs.
  • Patch Vega specs (even ones generated from Vega-Lite) to add additional functionality; for example, see Rotating Earth.
  • Add action links such as "View Source" and "Open in Vega Editor".
  • Includes Vega Tooltip.
  • Includes Vega Themes. Experimental: themes are not stable yet

Vega-Lite works well with Observable. Learn how to use it in our example notebook.

Basic Examples

Directly in the Browser

You can import Vega-Embed from a local copy or (as shown below) from jsDelivr. Please replace [VERSION] with the correct Vega, Vega-Lite, and Vega-Embed versions. We recommend that you specify the major versions (vega@5, vega-lite@5, vega-embed@6).

<!DOCTYPE html>
<html>
<head>
  <!-- Import Vega & Vega-Lite (does not have to be from CDN) -->
  <script src="https://cdn.jsdelivr.net/npm/vega@[VERSION]"></script>
  <script src="https://cdn.jsdelivr.net/npm/vega-lite@[VERSION]"></script>
  <!-- Import vega-embed -->
  <script src="https://cdn.jsdelivr.net/npm/vega-embed@[VERSION]"></script>
</head>
<body>

<div id="vis"></div>

<script type="text/javascript">
  var spec = "https://raw.githubusercontent.com/vega/vega/master/docs/examples/bar-chart.vg.json";
  vegaEmbed('#vis', spec).then(function(result) {
    // Access the Vega view instance (https://vega.github.io/vega/docs/api/view/) as result.view
  }).catch(console.error);
</script>
</body>
</html>

Look at the example online in the Vega-Embed Block.

JavaScript or TypeScript

The basic example below needs to be transpiled and bundled (using rollup, webpack, etc..) before it can be loaded in a browser.

import embed from 'vega-embed';

const spec = {
  ...
}

const result = await embed('#vis', spec);

console.log(result.view);

In Observable

You can require embed with embed = require('vega-embed@6') and then embed a chart with viewof view = embed(...). Check the our example notebook for more details.

API Reference

When using a script tag, the default export of Vega-Embed is a wrapper function that automatically chooses between embed and container based on the provided arguments. Vega-Embed provides this convenience for imports in interactive environments like Observable. When using the Vega-Embed npm package, the default export is the embed function.

# embed(el, spec[, opt]) <>

Returns a Promise that resolves to a result object. The result object contains:

Property Type Description
view String The instantiated Vega View instance.
spec Object A copy of the parsed JSON Vega or Vega-Lite spec.
vgSpec Object The compiled Vega spec.
finalize Function A method to prepare embed to be removed. To prevent unwanted behaviors and memory leaks, this method unregisters any timers and removes any event listeners the visualization has registered on external DOM elements. Applications should invoke this method when a Embed or the View instance is no longer needed. This method calls view.finalize.

The embed function accepts the following arguments:

Property Type Description
el String A DOM element or CSS selector indicating the element on the page in which to add the embedded view.
spec String / Object String : A URL string from which to load the Vega specification. This URL will be subject to standard browser security restrictions. Typically this URL will point to a file on the same host and port number as the web page itself.
Object : The Vega/Vega-Lite specification as a parsed JSON object.
opt Object (Optional) A JavaScript object containing options for embedding.

Note: Internet Explorer does not support the ES6 Promise feature. To make it work correctly, please follow the instructions on the Vega website.

# container(spec[, opt]) <>

Returns a Promise* that resolves to an HTML element with the Vega View instance as the value property. The function is designed to work with Observable. The container function accepts the following arguments:

Property Type Description
spec String / Object String : A URL string from which to load the Vega specification. This URL will be subject to standard browser security restrictions. Typically this URL will point to a file on the same host and port number as the web page itself.
Object : The Vega/Vega-Lite specification as a parsed JSON object.
opt Object (Optional) A JavaScript object containing options for embedding.

Options

You can configure Vega Embed with an options object. You can pass options as an argument to the embed function or as usermeta.embedOptions as part of the Vega or Vega-Lite specification.

var opt = {
  mode: ...,

  config: ...,
  theme: ...,

  defaultStyle: ...,

  bind: ...,

  // view config options
  renderer: ...,
  loader: ...,
  logLevel: ...,

  tooltip: ...,

  patch: ...,

  width: ...,
  height: ...,
  padding: ...,

  actions: {
    export: ...,
    source: ...,
    compiled: ...,
    editor: ...
  },

  scaleFactor: ...,

  editorUrl: ...,

  sourceHeader: ...,
  sourceFooter: ...,

  hover: {
    hoverSet: ...,
    updateSet: ...,
  },

  downloadFileName: ...,

  formatLocale: ...,
  timeFormatLocale: ...,
  expressionFunctions: ...,

  ast: ...,
  expr: ...,

  i18n: {
    COMPILED_ACTION: ...,
    EDITOR_ACTION: ...,
    PNG_ACTION: ...,
    SOURCE_ACTION: ...,
    SVG_ACTION: ...
  }
}
Property Type Description
mode String If specified, tells Vega-Embed to parse the spec as vega or vega-lite. Vega-Embed will parse the $schema url if the mode is not specified. Vega-Embed will default to vega if neither mode, nor $schema are specified.
config String / Object String : A URL string from which to load a Vega/Vega-Lite or Vega-Lite configuration file. This URL will be subject to standard browser security restrictions. Typically this URL will point to a file on the same host and port number as the web page itself.
Object : A Vega/Vega-Lite configuration as a parsed JSON object to override the default configuration options.
theme String If specified, tells Vega-Embed use the theme from Vega Themes. Experimental: we may update themes with minor version updates of Vega-Embed.
defaultStyle Boolean or String If set to true (default), the embed actions are shown in a menu. Set to false to use simple links. Provide a string to set the style sheet (not supported in usermeta).
forceActionsMenu Boolean If set to true, the embed actions are shown in a menu like they would be if the defaultStyle option were truthy. This can be useful when setting defaultStyle to false and defining menu styles in the parent application.
bind String or Element The element that should contain any input elements bound to signals.
renderer String The renderer to use for the view. One of "canvas" (default) or "svg". See Vega docs for details. May be a custom value if passing your own viewClass option.
logLevel Level Sets the current log level. See Vega docs for details.
tooltip Handler or Boolean or Object Provide a tooltip handler, customize the default Vega Tooltip handler, or disable the default handler.
loader Loader / Object Loader : Sets a custom Vega loader. Object : Vega loader options for a loader that will be created.
See Vega docs for details.
patch Function / Object[] / String A function to modify the Vega specification before it is parsed. Alternatively, a JSON-Patch RFC6902 to modify the Vega specification. If you use Vega-Lite, the compiled Vega will be patched. Alternatively to the function or the object, a URL string from which to load the patch can be provided. This URL will be subject to standard browser security restrictions. Typically this URL will point to a file on the same host and port number as the web page itself.
width Number Sets the view width in pixels. See Vega docs for details. Note that Vega-Lite overrides this option.
height Number Sets the view height in pixels. See Vega docs for details. Note that Vega-Lite overrides this option.
padding Number / Object Sets the view padding in pixels. See Vega docs for details.
actions Boolean / Object Determines if action links ("Export as PNG/SVG", "View Source", "View Vega" (only for Vega-Lite), "Open in Vega Editor") are included with the embedded view. If the value is true, all action links will be shown and none if the value is false. This property can take a key-value mapping object that maps keys (export, source, compiled, editor) to boolean values for determining if each action link should be shown. By default, export, source, and editor are true and compiled is false. These defaults can be overridden: for example, if actions is {export: false, source: true}, the embedded visualization will have two links – "View Source" and "Open in Vega Editor". The export property can take a key-value mapping object that maps keys (svg, png) to boolean values for determining if each export action link should be shown. By default, svg and png are true.
scaleFactor Number / Object Number: The number by which to multiply the width and height (default 1) of an exported PNG or SVG image.
Object: The different multipliers for each format ({ svg: <Number>, png: <Number> }). If one of the formats is omitted, the default value is used.
editorUrl String The URL at which to open embedded Vega specs in a Vega editor. Defaults to "http://vega.github.io/editor/". Internally, Vega-Embed uses HTML5 postMessage to pass the specification information to the editor.
sourceHeader String HTML to inject into the head tag of the page generated by the "View Source" and "View Vega" action link. For example, this can be used to add code for syntax highlighting.
sourceFooter String HTML to inject into the end of the page generated by the "View Source" and "View Vega" action link. The text will be added immediately before the closing body tag.
hover Boolean or Object Enable hover event processing. Hover event processing is enabled on Vega by default.
Boolean: Enables/disables hover event processing.
Object: Optional keys (hoverSet, updateSet) to specify which named encoding sets to invoke upon mouseover and mouseout.
i18n Object This property maps keys (COMPILED_ACTION, EDITOR_ACTION, PNG_ACTION, SOURCE_ACTION, SVG_ACTION) to string values for the action's text. By default, the text is in English.
downloadFileName String Sets the file name (default: visualization) for charts downloaded using the png or svg action.
formatLocale Object Sets the default locale definition for number formatting. See the d3-format locale collection for definition files for a variety of languages. Note that this is a global setting.
timeFormatLocale Object Sets the default locale definition for date/time formatting. See the d3-time-format locale collection for definition files for a variety of languages. Note that this is a global setting.
expressionFunctions Object Sets custom expression functions. Maps a function name to a JavaScript function, or an Object with the fn, and visitor parameters. See Vega Expression Functions for more information.
ast Boolean Generate an Abstract Syntax Tree (AST) instead of expressions and use an interpreter instead of native evaluation. While the interpreter is slower, it adds support for Vega expressions that are Content Security Policy (CSP)-compliant.
expr Object Custom Vega Expression interpreter.
viewClass Class Class which extends Vega View for custom rendering.

Common questions

How do I send cookies when loading data?

By default, the Vega loader does not send the credentials of the current page with requests. You can override this behavior by passing {loader: { http: { credentials: 'same-origin' }}} as the embed option.

What CSS should I use to support container sizing?

When using container sizing in Vega-Lite, make sure to set the width of the DOM element you passed to Embed.

Build Process

To build vega-embed.js and view the test examples, you must have Yarn 1 installed.

  1. Run yarn in the Vega-Embed folder to install dependencies.
  2. Run yarn build. This will create vega-embed.js and the minified vega-embed.min.js.
  3. Run a local webserver with yarn start then point your web browser at the test page (e.g., http://localhost:8000/test-vg.html(Vega) or http://localhost:8000/test-vl.html(Vega-Lite)).

Publishing

To make a release, run npm run release.

More Repositories

1

vega

A visualization grammar.
JavaScript
10,564
star
2

vega-lite

A concise grammar of interactive graphics, built on Vega.
TypeScript
4,260
star
3

voyager

Visualization Tool for Data Exploration
TypeScript
1,405
star
4

ts-json-schema-generator

Generate JSON schema from your Typescript sources
TypeScript
1,235
star
5

lyra

An interactive, graphical Visualization Design Environment (VDE)
TypeScript
1,042
star
6

falcon

Brushing and linking for big data
Jupyter Notebook
919
star
7

datalib

JavaScript data utility library.
JavaScript
727
star
8

polestar

Lightweight Tableau-style interface for visual analysis, built on Vega-lite.
JavaScript
370
star
9

ipyvega

IPython/Jupyter notebook module for Vega and Vega-Lite
Jupyter Notebook
369
star
10

react-vega

Convert Vega spec into React class conveniently
TypeScript
363
star
11

compassql

CompassQL Query Language for visualization recommendation.
TypeScript
258
star
12

vega-datasets

Common repository for example datasets used by Vega-related projects
TypeScript
234
star
13

vega-lite-api

A JavaScript API for Vega-Lite.
JavaScript
198
star
14

editor

Editor/IDE for Vega and Vega-Lite
TypeScript
134
star
15

vega-themes

Themes for stylized Vega and Vega-Lite visualizations.
TypeScript
104
star
16

vega-desktop

App for viewing visualizations created in Vega or Vega-lite
JavaScript
87
star
17

vega-tooltip

Tooltip Plugin for Vega-Lite
TypeScript
77
star
18

vega.github.io

The Vega landing page.
HTML
70
star
19

react-vega-lite

react + vega-lite
JavaScript
68
star
20

vl-convert

Utilities for converting Vega-Lite specs from the command line and Python
Rust
65
star
21

svelte-vega

Svelte component for Vega and Vega-Lite
Svelte
64
star
22

compass

Visualization Recommendation Engine, powered by Vega-Lite Specification Language
JavaScript
56
star
23

vega-loader-arrow

Data loader for the Apache Arrow format.
JavaScript
51
star
24

scalable-vega

A demo of scaling Vega to millions of records
TypeScript
45
star
25

vega-webgl-renderer

WebGL renderer for Vega.
JavaScript
41
star
26

vega-dataflow

Reactive dataflow processing.
JavaScript
39
star
27

vega-editor

[Deprecated] Please use https://github.com/vega/editor! (Link to deployed old editor: http://vega.github.io/vega-editor)
JavaScript
39
star
28

dataflow-api

JavaScript API for dataflow processing.
JavaScript
38
star
29

vega-plus

Make Vega charts of large datasets
TypeScript
35
star
30

vega-scenegraph

Vega scenegraph and renderers.
JavaScript
34
star
31

voyager2

Deprecated version of Voyager 2 (in Angular), please use https://github.com/vega/voyager.
JavaScript
30
star
32

schema

JSON schema for Vega and Vega-Lite
27
star
33

vega-lite-tutorials

Compilation of Vega-Lite & Altair Tutorials
Jupyter Notebook
24
star
34

vega-expression

Vega expression parser and code generator.
JavaScript
24
star
35

vega-lib

Include Vega in projects using minimal dependencies.
HTML
21
star
36

dataflow-vis

Experimental Vega Dataflow Visualization
JavaScript
20
star
37

vega-view

View component for Vega visualizations.
JavaScript
20
star
38

vega-label

Labeling algorithm for Vega.
JavaScript
19
star
39

vega-render-service

A service to render Vega visualizations
TypeScript
19
star
40

datalib-sketch

Probabilistic data structures for large or streaming data sets.
JavaScript
19
star
41

editor-backend

Backend for the Vega Editor
TypeScript
18
star
42

vega-bundler

Compile optimized Vega and Vega-Lite bundles.
JavaScript
17
star
43

vega-lite-ui

Common UI Library that powers Polestar and Voyager
JavaScript
13
star
44

vega-renderer-webgl

WebGL Renderer extension for Vega
JavaScript
13
star
45

vega-tutorials

Interactive tutorials for learning Vega.
JavaScript
13
star
46

vega-parser

Parse Vega specifications to runtime dataflows.
JavaScript
13
star
47

vega-vscode

Vega Language Plug-in for Visual Studio Code
TypeScript
12
star
48

vega-statistics

Statistical routines and probability distributions.
JavaScript
12
star
49

voyager-server

TypeScript
11
star
50

roadmap

The Vega and Vega-Lite Roadmap
9
star
51

vega-geo

Geographic data transforms for Vega dataflows.
JavaScript
8
star
52

vega-typings

Typings for Vega
TypeScript
8
star
53

vega-lite-transforms2sql

Convert extracted Vega-Lite transforms to SQL for scalable visualizations
TypeScript
6
star
54

vega-runtime

Runtime support for Vega dataflows.
JavaScript
6
star
55

vega-webgpu

WebGPU Renderer Extension for Vega
TypeScript
6
star
56

vega-crossfilter

Indexed cross-filtering for Vega dataflows.
JavaScript
6
star
57

vega-lite-to-api

Convert Vega-Lite JSON spec to Vega-Lite JS API
TypeScript
6
star
58

vega-hierarchy

Hierarchical layout transforms for Vega dataflows.
JavaScript
5
star
59

vega-logging

Vega logging utilities.
JavaScript
5
star
60

vega-util

JavaScript utilities for Vega.
JavaScript
4
star
61

vue-vega

Vue component for Vega and Vega-Lite
TypeScript
4
star
62

voyager-electron

JavaScript
4
star
63

vl-convert-service

Python
4
star
64

vega-loader

Network request and file loading utilities.
JavaScript
3
star
65

vega-wordcloud

Wordcloud layout algorithm for Vega dataflows.
JavaScript
3
star
66

vega-dataflow-examples

Example applications driven by Vega dataflows.
JavaScript
3
star
67

voyager-docs

3
star
68

vega-projection

Projections for cartographic mapping.
JavaScript
3
star
69

vega-event-selector

A CSS-inspired language to select, sequence, and compose DOM events into event streams.
JavaScript
3
star
70

vega-lite-v1

Copy of Vega-Lite 1.x website
TypeScript
2
star
71

vega-lite-params-proposal

2
star
72

vega-transforms

Data processing transforms for Vega dataflows.
JavaScript
2
star
73

vega-lite-dev-config

Version-controlled build config for easy re-use and sharing
TypeScript
2
star
74

vega-force

Force simulation transform for Vega dataflows.
JavaScript
2
star
75

vega-benchmarks

Scripts to benchmark Vega's performance
HTML
2
star
76

vega-lite-v4

Copy of the Vega-Lite 4 Website
JavaScript
2
star
77

ts-api

Typescript to API generator
TypeScript
2
star
78

vega-lite-shorthand

Shorthand Syntax for Vega-Lite
2
star
79

logos

Vega and Vega-Lite Logos
2
star
80

vega-embed-v2

Vega-Embed for Vega 2 and Vega-Lite 1
JavaScript
1
star
81

vega-gist

Client-side library for managing Vega GitHub gists.
1
star
82

vega-voronoi

Voronoi diagram transform for Vega dataflows.
JavaScript
1
star
83

vega-scale

Scales and color schemes for visual encoding.
JavaScript
1
star
84

vega-lite-v3

Copy of the Vega-Lite 3 Website
TypeScript
1
star
85

vega-view-transforms

View-specific transforms for Vega dataflows.
JavaScript
1
star
86

vega-canvas

Canvas and Image object instantiation utilities.
JavaScript
1
star
87

vega-encode

Visual encoding transforms for Vega dataflows.
JavaScript
1
star