Customize the position of the legend for a pie chart?

1,123 views
Skip to first unread message

wildnz

unread,
Sep 20, 2017, 7:42:20 PM9/20/17
to vega-js
How do you customize the position of a legend?  I've got it sitting in the top right corner, when I'd prefer it to be right-middle aligned:



My legends config is simply:

"legends": [
{
"title":"Type",
"stroke":"color",
"fill":"color",
"orient": "right"
}
]

I looked at https://vega.github.io/vega/docs/legends/ but could not figure it out.  
The documentation is very hard to follow if you do not have enough familiarity with vega.

I saw that the documentation says "Allows custom layout by setting the x and y properties within a legend encoding block." 
But I could not work out how or where to add x and y properties and there is no example of this given.

Thanks

Roy I

unread,
Sep 21, 2017, 10:36:05 AM9/21/17
to vega-js
Here is a modified Vega 3.0 "legend" based on this example:

The framed legend is custom positioned to the right of the chart.

Vega 3.0.2 spec
-------------------
...
"legends": [
{
 "fill": "scale_color",
 "title": "Web Browser",
 
 "orient": "none",
 
 "padding": {"value": 10},
 
 "encode": {
"symbols": {
 "enter": {
"fillOpacity": {"value": 0.5}
 }
},
"labels": {
 "update": {
"text": {"field": "value"}
 }
},
"legend": {
"update": {
"x": {"signal": "width", "offset": 20}, 
"y": {"signal": "height / 2", "offset": -50},
"stroke": {"value": "lightgrey"}
}
}
 }
}
...
Reply all
Reply to author
Forward
0 new messages