I was referring to pie chart examples of vega-lite.
I modified one of them to show percentage values as follows:
JavaScript
x
31
31
1
vegaEmbed("#pie-chart", {
2
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
3
"description": "A simple pie chart with embedded data.",
4
"data": {
5
"values": [
6
{"category": 1, "value": 4},
7
{"category": 2, "value": 6},
8
{"category": 3, "value": 10},
9
{"category": 4, "value": 3},
10
{"category": 5, "value": 7},
11
{"category": 6, "value": 8}
12
]
13
},
14
"transform": [{"calculate": "datum.value + ' %'", "as": "label"}],
15
"encoding": {
16
"theta": {"field": "value", "type": "quantitative", "stack": true},
17
"color": {"field": "category", "type": "nominal", "legend": null}
18
},
19
"layer": [
20
{
21
"mark": {"type": "arc"}
22
},
23
{
24
"mark": {"type": "text", "radius": 120},
25
"encoding": {
26
"text": {"field": "label", "type": "nominal"}
27
}
28
}
29
]
30
}
31
);
JavaScript
1
16
16
1
<!DOCTYPE html>
2
<html>
3
4
<head>
5
<title>Data visualizer</title>
6
<meta charset="utf-8" />
7
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
8
<script src="https://cdn.jsdelivr.net/npm/vega@5"></script>
9
<script src="https://cdn.jsdelivr.net/npm/vega-lite@5"></script>
10
<script src="https://cdn.jsdelivr.net/npm/vega-embed@6"></script>
11
</head>
12
13
<body>
14
<span id="pie-chart"></span>
15
</body>
16
</html>
This works perfectly and renders following:
However, I wanted those percentage labels in black color. So I referred text mark and tried following:
JavaScript
1
2
1
"mark": {"type": "text", "radius": 120, "color":"black"},
2
But this does not makes any changes to above output.
I also tried putting color
as encoding:
JavaScript
1
8
1
{
2
"mark": {"type": "text", "radius": 120},
3
"encoding": {
4
"text": {"field": "label", "type": "nominal"},
5
"color": {"value":"black"}
6
}
7
}
8
But it rendered following:
What is going wrong here?
Advertisement
Answer
Try this.
JavaScript
1
31
31
1
{
2
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
3
"description": "A simple pie chart with embedded data.",
4
"data": {
5
"values": [
6
{"category": 1, "value": 4},
7
{"category": 2, "value": 6},
8
{"category": 3, "value": 10},
9
{"category": 4, "value": 3},
10
{"category": 5, "value": 7},
11
{"category": 6, "value": 8}
12
]
13
},
14
"transform": [{"calculate": "datum.value + ' %'", "as": "label"}],
15
"encoding": {
16
"theta": {"field": "value", "type": "quantitative", "stack": true},
17
"color": {"field": "category", "type": "nominal", "legend": null}
18
},
19
"layer": [
20
{
21
"mark": {"type": "arc"}
22
},
23
{
24
"mark": {"type": "text", "radius": 120, "fill":"black"},
25
"encoding": {
26
"text": {"field": "label", "type": "nominal"}
27
}
28
}
29
]
30
}
31