Search…
Getting More from Your Data
Source code can be found at the end of this tutorial.
This tutorial builds on the Getting Started with Vega tutorial by color-coding tweets according to language:
    Tweets in English are blue.
    Tweets in French are orange.
    Tweets in Spanish are green.
    All other tweets are light or dark gray.
To highlight language in the visualization, the example specifies the language column query in the Vega data property, and associates language with color.
1
"data:" [
2
{
3
"name": "tweets",
4
"sql": "SELECT goog_x as x, goog_y as y, lang as color, tweets_nov_feb.rowid FROM tweets_nov_feb"
5
}
Copied!
The Scales property maps the language abbreviation string to a color value. Because we want to map discrete domain values to discrete range values, we specify a color scale with an ordinal type scale:
1
"scales:" [
2
.
3
.
4
.
5
{
6
"name": "color",
7
"type": "ordinal",
8
"domain": ["en", "es", "fr"],
9
"range": ["#27aeef", "#87bc45", "#ef9b20"],
10
"default": "gray",
11
"nullValue": "#cacaca"
12
}
13
]
Copied!
You can specify a default color values for values not specified in range and for data items with a value of null. In this example, tweets in languages other than English, Spanish, or French are colored gray and tweets with a language value of null are colored light gray (#cacaca).
Similar to using x and y scales to map Marks property x and y fields to the visualization area, you can scale the fillColor property to the visualization area.
In previous examples the fill color of points representing tweets was statically specified as blue:
1
"marks:" [
2
{
3
"type:" "points",
4
"from:" {
5
"data:" "tweets"
6
},
7
"properties:" {
8
.
9
.
10
.
11
},
12
"fillColor": "blue",
13
"size:" {"value:" 3}
14
}
15
}
16
]
Copied!
This example, uses Value Reference to specify the fill color:
1
"marks:" [
2
{
3
"type:" "points",
4
"from:" {
5
"data:" "tweets"
6
},
7
"properties:" {
8
.
9
.
10
.
11
},
12
"fillColor:" {
13
"scale": "color",
14
"field": "color"
15
},
16
"size:" 3
17
}
18
}
19
]
Copied!
The fillColor references the color scale and performs a lookup on the current language value, from the color data table field.

Source Code

Getting More Insight Tutorial Directory Structure
1
index.html
2
/js
3
browser-connector.js
4
vegaspec.js
5
vegademo.js
Copied!

HTML

Getting More Insight Tutorial index.html
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<title>OmniSci</title>
5
<meta charset="UTF-8">
6
</head>
7
<body>
8
<script src="js/browser-connector.js"></script>
9
<script src="js/vegaspec.js"></script>
10
<script src="js/vegademo.js"></script>
11
12
<script>
13
document.addEventListener('DOMContentLoaded', init, false);
14
</script>
15
</body>
16
</html>
Copied!

JavaScript

Getting More Insight Tutorial vegademo.js
1
function init() {
2
var vegaOptions = {}
3
var connector = new MapdCon()
4
.protocol("http")
5
.host("my.host.com")
6
.port("6273")
7
.dbName("omnisci")
8
.user("omnisci")
9
.password("changeme")
10
.connect(function(error, con) {
11
con.renderVega(1, JSON.stringify(exampleVega), vegaOptions, function(error, result) {
12
if (error) {
13
console.log(error.message);
14
}
15
else {
16
var blobUrl = `data:image/png;base64,${result.image}`
17
var body = document.querySelector('body')
18
var vegaImg = new Image()
19
vegaImg.src = blobUrl
20
body.append(vegaImg)
21
}
22
});
23
});
24
}
Copied!
Getting More Insight Tutorial vegaspec.js
1
const exampleVega = {
2
"width": 384,
3
"height": 564,
4
"data": [
5
{
6
"name": "tweets",
7
"sql": "SELECT goog_x as x, goog_y as y, lang as color, tweets_data_table.rowid FROM tweets_data_table"
8
}
9
],
10
"scales": [
11
{
12
"name": "x",
13
"type": "linear",
14
"domain": [
15
-3650484.1235206556,
16
7413325.514451755
17
],
18
"range": "width"
19
},
20
{
21
"name": "y",
22
"type": "linear",
23
"domain": [
24
-5778161.9183506705,
25
10471808.487466192
26
],
27
"range": "height"
28
},
29
{
30
"name": "color",
31
"type": "ordinal",
32
"domain": ["en", "es", "fr"],
33
"range": ["#27aeef", "#87bc45", "#ef9b20"],
34
"default": "gray",
35
"nullValue": "#cacaca"
36
}
37
],
38
"marks": [
39
{
40
"type": "points",
41
"from": {
42
"data": "tweets"
43
},
44
"properties": {
45
"x": {
46
"scale": "x",
47
"field": "x"
48
},
49
"y": {
50
"scale": "y",
51
"field": "y"
52
},
53
"fillColor": {
54
"scale": "color",
55
"field": "color"
56
},
57
"size": 3
58
}
59
}
60
]
61
};
Copied!
Last modified 1yr ago
Export as PDF
Copy link