Vega-Lite
Updated 6 years ago to v1.3.0 by Flourish team
How to use this template
Vega-Lite
A template to unlock the power of Vega-Lite visualisations in Flourish.
Vega-Lite lets you concisely specify interactive visualisations in JSON syntax, and has a wide range of chart types.
To build your own Vega-Lite charts in Flourish:
- Upload your data, and update the
Data
binding to point to the columns for use in the visualisation. - Choose an example from the Vega-Lite examples gallery. Copy the JSON used in the example, and paste it into the
Vega-lite code
setting field. - Delete the
$schema
,description
,height
,width
anddata
fields. - In the
encoding
field, update thefield
names to point at the names of your bound columns.
For example, if you were building a Vega-lite simple bar chart, and you had CSV data like this:
customer,sales
robin,17825
duncan,78124
tim,98724
You would need to bind columns A-B in your data, then use the following JSON:
{
"mark": "bar",
"encoding": {
"x": {"field": "customer", "type": "ordinal"},
"y": {"field": "sales", "type": "quantitative"}
}
}
For more advanced options, see the blog post and the Vega-lite documentation.
API information
This section documents API usage specific to this template, so for an introduction we suggest you refer to the generic API documentation instead.
template: @flourish/vega-lite
version: 1
Template data
There are three different formats in which you can supply data to this template. The most convenient for you to use likely depends on the source of your data, as described below.
1. Array of arrays, and a bindings object
You can supply arrays of arrays to opts.data
, which might look
like:
{ data: { values: [ [ "ValuesColumn1Value1", "ValuesColumn2Value1", [ "ValuesColumn1Value2", "ValuesColumn2Value2", [ "ValuesColumn1Value3", "ValuesColumn2Value3", ... ] } }
where each array of arrays represents the rows in a data sheet.
To tell the API how the values from each column should be
associated with the keys that the template is expecting, you must also supply
an object attached to opts.bindings
. (The meanings of the
keys in the bindings object are documented
below.) The minimal bindings you can
supply for this template are as shown in this example:
{ template: "@flourish/vega-lite", version: "1", bindings: { values: { } }, data: { values: [ [ "ValuesColumn1Value1", "ValuesColumn2Value1", [ "ValuesColumn1Value2", "ValuesColumn2Value2", [ "ValuesColumn1Value3", "ValuesColumn2Value3", ... ] } }
All possible bindings that you can supply are shown in this example:
{ template: "@flourish/vega-lite", version: "1", bindings: { values: { cols: [0, 1, ...], // index(es) of column(s) in your data } }, data: { values: [ [ "ValuesColumn1Value1", "ValuesColumn2Value1", [ "ValuesColumn1Value2", "ValuesColumn2Value2", [ "ValuesColumn1Value3", "ValuesColumn2Value3", ... ] } }
2. Array of objects with arbitrary keys, and a bindings object
This format is most likely useful when you have data from an external source,
such as CSV data loaded from d3-dsv.
You should supply this attached to the opts.data
, which might look
like:
{ values: [ { "ValuesHeader1": ..., "ValuesHeader2": ..., ... }, { "ValuesHeader1": ..., "ValuesHeader2": ..., ... }, { "ValuesHeader1": ..., "ValuesHeader2": ..., ... }, ... ] }
... but with the keys being the column headers from your
source data instead. You must also supply an object attached to
opts.bindings
. The minimal bindings you can
supply for this template are as shown in this example:
{ template: "@flourish/vega-lite", version: "1", bindings: { values: { } }, data: { values: [ { "ValuesHeader1": ..., "ValuesHeader2": ..., ... }, { "ValuesHeader1": ..., "ValuesHeader2": ..., ... }, { "ValuesHeader1": ..., "ValuesHeader2": ..., ... }, ... ] } }
All possible bindings that you can supply are shown in this example:
{ template: "@flourish/vega-lite", version: "1", bindings: { values: { cols: ["ValuesHeader1", "ValuesHeader2", ...], } }, data: { values: [ { "ValuesHeader1": ..., "ValuesHeader2": ..., ... }, { "ValuesHeader1": ..., "ValuesHeader2": ..., ... }, { "ValuesHeader1": ..., "ValuesHeader2": ..., ... }, ... ] } }
(As before, the keys containing "Header" would be replaced by column names from your data source.)
3. Array of objects with template-defined keys
There is an alternative format you can use, which is likely to be easier to
use if your data is not from a spreadsheet source. With this alternative format
you supply your data to the template as
an array of objects, attached to opts.data
, where the keys must
be those used by the template, as documented below. In this case
there is no need to supply a bindings object, since the key names are already
those expected by the template. The required properties in the data
object are as follows (scroll down for a
description of what each property is):
{ template: "@flourish/vega-lite", version: "1", data: { values: [ { cols: [...] }, ... ] }, ... }
And the full list of all possible properties is as follows:
{ template: "@flourish/vega-lite", version: "1", data: { values: [ { cols: [...] }, ... ] }, ... }
Meanings of the template data keys:
- values.cols: Columns for use in the visualisation
Template settings
Options for opts.state
.
Settings
spec code
Vega-Lite code. The Vega-Lite configuration code (in JSON format). To copy a example from the Vega-Lite gallery just paste in the code here, delete the “data”, “width” and “height” properties and update the “field” properties to match column names in your spreadsheet.
renderer string
Renderer. Canvas is recommended for online use as it is faster and can handle a larger numbers of data points. SVG is best if you want to download the visualisation to edit in a vector graphics application such as Illustrator. Allowed values:
header_title string
Title.
header_subtitle string
Subtitle.
header_margin number
Margin.
header_color color
Color.
header_align string
Text align. Allowed values:
Multi-chart layout
multichart_margin_horizontal number
Horizontal space. How much horizontal space to leave for axes, legends, etc
multichart_margin_vertical number
Vertical space. How much vertical space to leave for axes, legends, etc;
Footer
footer_source_name string
Source name.
footer_source_url string
Source url.
footer_multiple_sources boolean
Multiple sources.
footer_source_name_2 string
Source name.
footer_source_url_2 string
Source url.
footer_source_name_3 string
Source name.
footer_source_url_3 string
Source url.
footer_source_label_other boolean
Change source label.
footer_source_label string
Source label.
footer_note string
Note.
footer_size number
Size.
footer_color color
Color.
footer_align string
Alignment. Allowed values:
footer_margin number
Overall.
footer_margin_top number
Top.
footer_margin_right number
Right.
footer_margin_bottom number
Bottom.
footer_margin_left number
Left.
footer_margin_advanced boolean
Advanced.