Last active
November 18, 2018 13:45
-
-
Save betafcc/3125c2287224b86b98e83ae2ebdd8fa0 to your computer and use it in GitHub Desktop.
Vega donut chart with centered rotated labels https://bl.ocks.org/betafcc/3125c2287224b86b98e83ae2ebdd8fa0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{"letter": "A", "number": 4}, | |
{"letter": "B", "number": 6}, | |
{"letter": "C", "number": 10}, | |
{"letter": "D", "number": 3}, | |
{"letter": "E", "number": 7}, | |
{"letter": "F", "number": 8} | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://cdn.jsdelivr.net/npm/vega@4"></script> | |
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script> | |
<script src="https://cdn.jsdelivr.net/npm/vega-embed@3"></script> | |
</head> | |
<body> | |
<div id="vis"></div> | |
<script> | |
vegaEmbed('#vis', 'spec.json', { | |
defaultStyle: true, | |
renderer: 'svg' | |
}) | |
.catch(console.warn); | |
</script> | |
</body> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"$schema": "https://vega.github.io/schema/vega/v4.json", | |
"autosize": "pad", | |
"width": 200, | |
"height": 200, | |
"data": [ | |
{ | |
"url": "data.json", | |
"name": "table", | |
"transform": [ | |
{ | |
"type": "pie", | |
"field": "number" | |
}, | |
{ | |
"type": "formula", | |
"expr": "(datum.startAngle + datum.endAngle) / 2", | |
"as": "midAngle" | |
} | |
] | |
} | |
], | |
"scales": [ | |
{ | |
"name": "color", | |
"type": "ordinal", | |
"domain": {"data": "table", "field": "letter"}, | |
"range": {"scheme": "category20"} | |
} | |
], | |
"marks": [ | |
{ | |
"from": {"data": "table"}, | |
"type": "arc", | |
"encode": { | |
"enter": { | |
"x": {"signal": "width / 2"}, | |
"y": {"signal": "height / 2"}, | |
"startAngle": {"field": "startAngle"}, | |
"endAngle": {"field": "endAngle"}, | |
"fill": {"field": "letter", "scale": "color"}, | |
"innerRadius": {"signal": "width / 3"}, | |
"outerRadius": {"signal": "width / 2"} | |
} | |
} | |
}, | |
{ | |
"from": {"data": "table"}, | |
"type": "text", | |
"encode": { | |
"enter": { | |
"align": {"value": "center"}, | |
"angle": {"field": "midAngle", "mult": "180 / Math.PI"}, | |
"baseline": {"value": "middle"}, | |
"radius": {"signal": "(width / 2 + width / 3) / 2"}, | |
"text": {"field": "letter"}, | |
"theta": {"field": "midAngle"}, | |
"x": {"signal": "width / 2"}, | |
"y": {"signal": "height / 2"} | |
} | |
} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment