Search…
Using Polys Marks Type
Source code can be found at the end of this tutorial.
This tutorial introduces you to the Polys Type marks, which uses an implicit polygon data table format. The visualization is a map of zip codes color-coded according to average contribution amount. The data table encodes polygons representing zip code areas.
See the Poly Map with Backend Rendering charting example for a programmatic rendering of this visualization.
The following data property extracts the average contribution amount from the contributions_donotmodify data table, omitting rows that do not have a contribution amount:
1
"data": [
2
{
3
"name": "polys",
4
"format": "polys",
5
"sql": "SELECT zipcodes.rowid,AVG(contributions_donotmodify.amount) AS avgContrib FROM contributions_donotmodify,zipcodes WHERE contributions_donotmodify.amount IS NOT NULL AND contributions_donotmodify.contributor_zipcode = zipcodes.ZCTA5CE10 GROUP BY zipcodes.rowid ORDER BY avgContrib DESC"
6
}
7
]
Copied!
When working with polygon data, the "format": "polys" property must be specified.
The scales specification scales x values to the visualization area width and y values to the height. A color scale, polys_fillColor is also specified that linearly scales nine contribution amount ranges to nine colors:
1
"scales": [
2
{
3
"name": "x",
4
"type": "linear",
5
"domain": [-19646150.75527339, 19646150.755273417],
6
"range": "width"
7
},
8
{
9
"name": "y",
10
"type": "linear",
11
"domain": [-3071257.58106188, 10078357.267122284],
12
"range": "height"
13
},
14
{
15
"name": "polys_fillColor",
16
"type": "linear",
17
"domain": [0, 325, 650, 975, 1300, 1625, 1950, 2275, 2600],
18
"range": ["#115f9a", "#1984c5", "#22a7f0", "#48b5c4", "#76c68f", "#a6d75b", "#c9e52f", "#d0ee11", "#d0f400"],
19
"default": "green"
20
}
21
]
Copied!
Zip code areas for which average contribution amounts are not specified by the domain are color-coded green.
The marks property specifies visually encoding the data from the polys data table as polygons:
1
"marks": [
2
{
3
"type": "polys",
4
"from": { "data": "polys" },
5
6
... elided ...
7
8
}
9
}
10
]
Copied!
Polygon x and y vertex locations are transformed to the visualization area using the x and y scales.
1
"marks": [
2
{
3
... elided ...
4
5
"properties": {
6
"x": {
7
"scale": "x",
8
"field": "x"
9
},
10
"y": {
11
"scale": "y",
12
"field": "y"
13
},
14
... elided ...
15
}
16
}
17
]
Copied!
The x and y polygon vertex locations are implicitly encoded in the data table as described in Polys Type.
Polygon fill color color-codes the average contribution amount, avgContrib, linearly scaled by the polys_fillColor scale:
1
"marks": [
2
{
3
... elided ...
4
5
"properties": {
6
7
... elided ...
8
9
"fillColor": {
10
"scale": "polys_fillColor",
11
"field": "avgContrib"
12
},
13
14
... elided ...
15
16
}
17
}
18
]
Copied!
Finally, the marks property specifies the polygon border width and color, and line join constraints:
1
"marks": [
2
{
3
... elided ...
4
5
"properties": {
6
7
... elided ...
8
9
"strokeColor": "white",
10
"strokeWidth": 0,
11
"lineJoin": "miter",
12
"miterLimit": 10
13
}
14
}
15
]
Copied!

Source Code

Working with Polys Tutorial Directory Structure
1
index.html
2
/js
3
browser-connector.js
4
vegaspec.js
5
vegademo.js
Copied!

HTML

Working with Polys 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

Working with Polys 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!
Working with Polys Tutorial vegaspec.js
1
const exampleVega = {
2
"width": 1004,
3
"height": 336,
4
"data": [
5
{
6
"name": "polys",
7
"format": "polys",
8
"sql": "SELECT zipcodes.rowid,AVG(contributions_donotmodify.amount) AS avgContrib FROM contributions_donotmodify,zipcodes WHERE contributions_donotmodify.amount IS NOT NULL AND contributions_donotmodify.contributor_zipcode = zipcodes.ZCTA5CE10 GROUP BY zipcodes.rowid ORDER BY avgContrib DESC"
9
}
10
],
11
"scales": [
12
{
13
"name": "x",
14
"type": "linear",
15
"domain": [
16
-19646150.75527339,
17
19646150.755273417
18
],
19
"range": "width"
20
},
21
{
22
"name": "y",
23
"type": "linear",
24
"domain": [
25
-3071257.58106188,
26
10078357.267122284
27
],
28
"range": "height"
29
},
30
{
31
"name": "polys_fillColor",
32
"type": "linear",
33
"domain": [
34
0, 325, 650, 975,
35
1300, 1625, 1950, 2275, 2600
36
],
37
"range": [
38
"#115f9a", "#1984c5", "#22a7f0", "#48b5c4",
39
"#76c68f", "#a6d75b", "#c9e52f", "#d0ee11", "#d0f400"
40
],
41
"default": "green",
42
"nullValue": "#CACACA"
43
}
44
],
45
"marks&quoquot;: [
46
{
47
"type": "polys",
48
"from": {
49
"data": "polys"
50
},
51
"properties": {
52
"x": {
53
"scale": "x",
54
"field": "x"
55
},
56
"y": {
57
"scale": "y",
58
"field": "y"
59
},
60
"fillColor": {
61
"scale": "polys_fillColor",
62
"field": "avgContrib"
63
},
64
"strokeColor": "white",
65
"strokeWidth": 0,
66
"lineJoin": "miter",
67
"miterLimit": 10
68
}
69
}
70
]
71
};
Copied!
Last modified 1yr ago
Export as PDF
Copy link