admin管理员组文章数量:1330392
I have this bar chart that's made with Vega-lite (code and pic below).
But I want to customize the legend labels so that instead of videoGame it's Video Game and instead of tv its TV. Is there anyway to do this?
lineChart = vegalite ({
"$schema": ".json",
"width": 560,
"height": 200,
"data": {"values": chartData},
"mark": {"type": "bar"},
"encoding": {
"x": {"field": "year_reference", "type": "temporal", "axis": {"title": "Year", "grid": true}},
"y": {"field": "reference_count_total", "type": "quantitative", "axis": {"title": "References", "grid": true}},
"color": {
"field": "title_type",
"scale": {
"domain": [
"tv",
"movie",
"video",
"videoGame"
],
"range": [
"#9e9ac8",
"#74c476",
"#a6761d",
"#6baed6"
]
},
"legend": true,
"title": "Reference Type"
},
}
})
I have this bar chart that's made with Vega-lite (code and pic below).
But I want to customize the legend labels so that instead of videoGame it's Video Game and instead of tv its TV. Is there anyway to do this?
lineChart = vegalite ({
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"width": 560,
"height": 200,
"data": {"values": chartData},
"mark": {"type": "bar"},
"encoding": {
"x": {"field": "year_reference", "type": "temporal", "axis": {"title": "Year", "grid": true}},
"y": {"field": "reference_count_total", "type": "quantitative", "axis": {"title": "References", "grid": true}},
"color": {
"field": "title_type",
"scale": {
"domain": [
"tv",
"movie",
"video",
"videoGame"
],
"range": [
"#9e9ac8",
"#74c476",
"#a6761d",
"#6baed6"
]
},
"legend": true,
"title": "Reference Type"
},
}
})
Share Improve this question asked Apr 19, 2021 at 23:49 NewToJSNewToJS 2,1014 gold badges37 silver badges67 bronze badges1 Answer
Reset to default 8Just provide labelExpr
in legend, where you can conditionally give the labels depending on your fields data.
Refer the below code or the chart in editor
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"width": 560,
"height": 200,
"data": {
"values": [
{
"title_type": "tv",
"year_reference": "10-12-2012",
"reference_count_total": 10
},
{
"title_type": "movie",
"year_reference": "10-12-2012",
"reference_count_total": 10
},
{
"title_type": "video",
"year_reference": "10-12-2012",
"reference_count_total": 10
},
{
"title_type": "videoGame",
"year_reference": "10-12-2012",
"reference_count_total": 10
}
]
},
"mark": {"type": "bar"},
"encoding": {
"x": {
"field": "year_reference",
"type": "temporal",
"axis": {"title": "Year", "grid": true}
},
"y": {
"field": "reference_count_total",
"type": "quantitative",
"axis": {"title": "References", "grid": true}
},
"color": {
"field": "title_type",
"scale": {
"domain": ["tv", "movie", "video", "videoGame"],
"range": ["#9e9ac8", "#74c476", "#a6761d", "#6baed6"]
},
"legend": {
"labelExpr": "datum.label == 'tv' ? 'Tv' : datum.label == 'movie' ? 'Movie' :datum.label == 'video' ? 'Video' : 'Video Game'"
},
"title": "Reference Type"
}
}
}
本文标签: javascriptVegalite How do you customize legend labelsStack Overflow
版权声明:本文标题:javascript - Vega-lite: How do you customize legend labels? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742221674a2435486.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论