vega-view
View component and transforms for Vega visualizations. A View instantiates
an underlying dataflow graph and provides a component for rendering and
interacting with a visualization. When initialized with a container DOM
element, the View adds a Canvas or SVG-based visualization to a web page.
Alternatively, a View can be used either client-side or server-side to export
static SVG or PNG (Canvas) images.
View API Reference
View Construction
Methods for constructing and deconstructing views. In addition to the methods
described below, View instances also inherit all (non-overidden) methods of
the vega-dataflow
Dataflow
parent class.
#
vega.View(runtime[, options])
<>
Constructor that creates a new View instance for the provided
Vega dataflow runtime specification.
If provided, the options argument should be an object with one or more
of the following properties:
- loadOptions: Default options for loading data files or images. See the
load method.
- logLevel: Initial log level to use. See the logLevel
method.
- renderer: The type of renderer to use (
'canvas'
or 'svg'
). See the
renderer method.
The View constructor call is typically followed by a chain of method calls
to setup the desired view configuration. At the end of this chain,
the run method evaluates the underlying dataflow graph to update
and render the visualization.
var view = new vega.View(runtime)
.logLevel(vega.Warn)
.initialize(document.querySelector('#view'))
.renderer('svg')
.hover()
.run();
#
view.finalize()
<>
Prepares the view to be removed from a web page. To prevent unwanted behaviors
and memory leaks, this method removes any event listeners the visualization has
registered on external DOM elements.
View Configuration
Methods for configuring the view state. These methods are often (but not
always) invoked immediately after the View constructor, prior to the first
invocation of the run method.
#
view.initialize([container])
<>
Initializes internal rendering and event handling, then returns this view
instance. If the DOM element container is provided, visualization
elements (such as Canvas or SVG HTML elements) will be added to the web page
under this containing element. If container is not provided, the view
will operate in headless mode, and can still generate static visualization
images using the image export methods.
#
view.logLevel(level)
<>
Sets the current log level and returns this view instance. This method controls
which types of log messages are printed to the JavaScript console, and is
inherited from the
Dataflow
parent class. The valid level values are vega.None
(the default),
vega.Warn
, vega.Info
, vega.Debug
. See the
logger method in
vega-util for more.
#
view.renderer(type)
<>
Sets the renderer type to one of 'canvas'
(the default) or 'svg'
and
returns this view instance. While typically invoked immediately upon view
creation, this method can be called at any time to change the type of renderer
used.
#
view.hover([hoverSet, updateSet])
<>
Enables hover event processing and returns this view instance. The optional
arguments specify which named encoding sets to invoke upon mouseover and
mouseout. The hoverSet defaults to 'hover'
, corresponding to the "hover"
set within a Vega mark specification "encode"
block. The updateSet
defaults to 'update'
, corresponding to the "update"
set within a Vega mark
specification "encode"
block. If this method is never invoked, the view will
not automatically handle hover events. Instead, the underlying dataflow
definition will have to explicitly set up event streams for handling mouseover
and mouseout events.
#
view.background([color])
<>
Gets or sets the view background color. If no arguments are provided, returns
the current background color. If color is specified, this method sets the
background color and returns this view instance. This method does not force
an immediate update to the view: invoke the run method when ready.
#
view.width([width])
<>
Gets or sets the view width, in pixels. If no arguments are provided, returns
the current width value. If width is specified, this method sets the width
and returns this view instance. This method does not force an immediate update
to the view: invoke the run method when ready. This method is
equivalent to view.signal('width'[, width])
.
#
view.height([height])
<>
Gets or sets the view height, in pixels. If no arguments are provided, returns
the current height value. If height is specified, this method sets the
height and returns this view instance. This method does not force an immediate
update to the view: invoke the run method when ready. This method
is equivalent to view.signal('height'[, height])
.
#
view.padding([padding])
<>
Gets or sets the view padding, in pixels. Padding objects take the form
{left: 5, top: 5, right: 5, bottom: 5}
. If no arguments are provided, returns
the current padding value. If padding is specified, this method sets the
padding and returns this view instance. This method does not force an immediate
update to the view: invoke the run method when ready. This method
is equivalent to view.signal('padding'[, padding])
.
Dataflow and Rendering
Methods for invoking dataflow evaluation and view rendering.
#
view.run([encode])
<>
Evaluates the underlying dataflow graph and returns this view instance. The
optional encode argument is a String value indicating the name of a
custom "encode"
set to run in addition to the standard "update"
encoder.
If one or more data sets have been queued to be loaded from external files,
this method will function asynchronously: the method will initiate file loading
and return immediately, and the dataflow graph will be evaluated when file
loading completes. Any scenegraph elements modified during dataflow evaluation
will automatically be re-rendered in the view.
Internally, this method invokes the run
method of the
Dataflow
parent class, and then additionally performs rendering.
#
view.render([update])
<>
Renders the scenegraph and returns this view instance. If no arguments are
provided, the entire scenegraph is redrawn. If provided, the update argument
should be an array of "dirty" scenegraph items to redraw. Incremental rendering
will be perform to redraw only damaged regions of the scenegraph.
During normal execution, this method is automatically invoked by the
run method. However, clients may explicitly call this method to
(re-)render the scene on demand (for example, to aid debugging).
#
view.enqueue(items)
<>
Enqueues "dirty" scenegraph items to be re-drawn the next time dataflow
evaluation completes. This method is typically invoked by dataflow operators
directly to populate a dirty list for incremental rendering.
#
view.scenegraph()
<>
Returns the Vega scenegraph
instance for this view.
Event Handling
Methods for generating new event streams. See also the hover
method.
#
view.events(source, type[, filter])
<>
Returns a new
EventStream
for a specified source, event type, and optional filter function. The
source should be one of "view"
(to specify the current view), "window"
(to specify the browser window object), or a valid CSS selector string
(that will be passed to document.querySelectorAll
). The event type should
be a legal DOM event type. If provided, the option filter argument should
be a function that takes an event object as input and returns true if it
should be included in the produced event stream.
Typically this method is invoked internally to create event streams referenced
within Vega signal definitions. However, callers can use this method to create
custom event streams if desired. This method assumes that the view is running
in a browser environment, otherwise invoking this method may have no effect.
Image Export
Methods for exporting static visualization images. These methods can be invoked
either client-side or server-side.
#
view.toCanvas(items)
<>
Returns a Promise
that resolves to a canvas instance containing a rendered bitmap image of the
view. If invoked in a browser, the returned Promise resolves to an
HTML5 canvas
element. If invoked server-side in node.js, the Promise resolves to a
node-canvas Canvas instance.
#
view.toSVG(items)
<>
Returns a Promise
that resolves to an SVG string, providing a vector graphics image of the view.
#
view.toImageURL(type)
<>
Returns a Promise
that resolves to an image URL for a snapshot of the current view. The type
argument must be one of 'svg'
, 'png'
or 'canvas'
. Both the png and
canvas types result in a PNG image. The generated URL can be used to create
downloadable visualization images.
view.toImageURL('png').then(function(url) {
var link = document.createElement('a');
link.setAttribute('href', url);
link.setAttribute('target', '_blank');
link.setAttribute('download', 'vega-export.png');
var event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, true, document.defaultView,
1, 0, 0, 0, 0, false, false, false, false, 0, null);
link.dispatchEvent(event);
}).catch(function(error) { });
Signals
Methods for accessing and updating dataflow signal values.
#
view.signal(name[, value])
<>
Gets or sets a dataflow signal. If only the name argument is provided,
returns the requested signal value. If value is also specified, updates the
signal and returns this view instance. This method does not force an immediate
update to the view: invoke the run method when ready.
#
view.state([state])
<>
Gets or sets the state of signals in the dataflow graph. If no arguments are
specified, returns an object of name-value mappings for all signals in the
dataflow. If the state argument is provided, this method updates the value
of all signals included in the input state object, invokes the
run method, and returns this view instance.
Data
Methods for accessing data sets and performing streaming updates.
#
view.data(name)
<>
Returns the data set with the given name. The returned array of data
objects is a live array used by the underlying dataflow. Callers that wish
to modify the returned array should first make a defensive copy,
for example using view.data('name').slice()
.
#
view.insert(name, tuples)
<>
Inserts an array of new data tuples into the data set with the given name,
then returns this view instance. The input tuples array should contain one
or more data objects that are not already included in the data set. This
method does not force an immediate update to the view: invoke the
run method when ready.
Internally, this method creates a
ChangeSet
and invokes
Dataflow.pulse.
See vega-dataflow for more.
#
view.remove(name, tuples)
<>
Removes data tuples from the data set with the given name,
then returns this view instance. The tuples argument can either be an
array of tuples already included in the data set, or a predicate function
indicating which tuples should be removed. This method does not force an
immediate update to the view: invoke the run method when ready.
For example, to remove all tuples in the 'table'
data set with a count
property less than five:
view.remove('table', function(d) { return d.count < 5; }).run();
Internally, this method creates a
ChangeSet
and invokes
Dataflow.pulse.
See vega-dataflow for more.