Getting Started with Vega
Last updated
Last updated
Source code is located at the end of the tutorial.
This tutorial uses the same visualization as Vega at a Glance but elaborates on the runtime environment and implementation steps. The Vega usage pattern described here applies to all Vega implementations. Subsequent tutorials differ only in describing more advanced Vega features.
This visualization maps a continuous, quantitative input domain to a continuous output range. Again, the visualization shows tweets in the EMEA region, from a tweets data table:
Backend rendering using Vega involves the following steps:
You can create the Vega specification statically, as shown in this tutorial, or programmatically. See the Poly Map with Backend Rendering charting example for a programmatic implementation. Here is the programmatic source code:
A Vega JSON specification has the following general structure:
The width
and height
properties define the width and height of your visualization area, in pixels:
This example uses the following SQL statement to get the tweets data:
The input data are the latitude and longitude coordinates of tweets from the tweets_nov_feb
data table. The coordinates are labeled x
and y
for Field Reference in the marks
property, which references the data using the tweets
name.
The marks
property specifies the graphical attributes of how each data item is rendered:
In this example, each data item from the tweets
data table is rendered as a point. The points
marks type includes position, fill color, and size attributes. The marks property specifies how to visually encode points according to these attributes. Points in this example are three pixels in diameter and colored blue.
Points are scaled to the visualization area using the scales property.
The following scales
specification maps marks
to the visualization area.
Both x
and y
scales specify a linear
mapping of the continuous, quantitative input domain to a continuous output range. In this example, input data values are transformed to predefined width
and height
range
values.
Later tutorials show how to specify data transformation using discrete domain-to-range mapping.
Use the browser-connector.js renderVega()
API to communicate with the backend. The connector is layered on Apache Thrift for cross-language client communication with the server.
Follow these steps to instantiate the connector and to connect to the backend:
Include browser-connector.js
located at https://github.com/omnisci/mapd-connector/tree/master/dist to include the MapD connector and Thrift interface APIs.
Instantiate the MapdCon()
connector and set the server name, protocol information, and your authentication credentials, as described in the MapD Connector API:
Property
Description
dbName
OmniSci database name.
host
OmniSci web server name.
password
OmniSci user password.
port
OmniSci web server port
protocol
Communication protocol: http
, https
user
OmniSci user name.
Finally, call the MapD connector API connect() function to initiate a connect request, passing a callback function with a (error, success)
signature as the parameter.
For example,
The connect()
function generates client and session IDs for this connection instance, which are unique for each instance and are used in subsequent API calls for the session.
On a successful connection, the callback function is called. The callback function in this example calls the renderVega()
function.
The MapD connector API renderVega() function sends the Vega JSON to the backend, and has the following parameters:
Parameter
Type
Required
Description
widgetid
number
X
Calling widget ID.
vega
string
X
options
number
Render query options.
compressionLevel
:PNG compression level. 1
(low, fast) to 10
(high, slow). Default = 3
callback
function
Callback function with (error, success)
signature.
Return
Description
Base64 image
PNG image rendered on server
The backend returns the rendered Base64 image in results.image
, which you can display in the browser window using a data URI.
Getting Started Directory Structure
Getting Started index.html
Getting Started vegademo.js
Getting Started vegaspec.js
Vega JSON object, as described in .