gpt4 book ai didi

vega-lite - 用 VEGA-LITE 绘制多个 "columns"并包含一个图例

转载 作者:行者123 更新时间:2023-12-03 08:03:21 28 4
gpt4 key购买 nike

我有以下最少的数据:

[
{"date": "2019-01-01", "foo": 10000, "bar": 10, "goo": 30},
{"date": "2019-01-02", "foo": 30000, "bar": 20, "goo": 20},
{"date": "2019-01-03", "foo": 40000, "bar": 20, "goo": 10},
{"date": "2019-01-04", "foo": 1000, "bar": 60, "goo": 20}
]

我使用 VEGA-LITE 绘制的:

<!DOCTYPE html>
<html>

<head>
<title>Embedding Vega-Lite</title>
<script src="https://cdn.jsdelivr.net/npm/vega@5.4.0"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-lite@3.3.0"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-embed@4.2.0"></script>
</head>

<body>
<div id="vis"></div>

<script type="text/javascript">
var yourVlSpec = {
"$schema": "https://vega.github.io/schema/vega-lite/v3.json",
"Title": "Insights stats",
"description": "Overview of insights stats",
"width": 1000,
"height": 450,
"data": {
"url": "./data.json"
},
"layer": [
{
"mark": "line",
"encoding": {
"x": {
"field": "date",
"type": "temporal",
"title": "Date"
},
"y": {
"field": "foo",
"type": "quantitative",
"title": "Some Foo"
}
}
},
{
"mark": {
"type": "line",
"stroke": "firebrick"
},
"encoding": {
"x": {
"field": "date",
"type": "temporal"
},
"y": {
"field": "bar",
"type": "quantitative",
"title": null,
"scale": { "domain": [0, 100] }
}
}
},
{
"mark": {
"type": "line",
"stroke": "red"
},
"encoding": {
"x": {
"field": "date",
"type": "temporal"
},
"y": {
"field": "goo",
"type": "quantitative",
"title": "Ratio (%)",
"scale": { "domain": [0, 100] }
}
}
}
],
"resolve": { "scale": { "y": "independent" } }
};
vegaEmbed('#vis', yourVlSpec);
</script>
</body>

</html>

我没有为每一行提供适当的图例。我错过了什么?

最佳答案

Vega-Lite 将为图表生成一个图例,如果有一个编码保证它,例如颜色、形状等。

在绘制多列的情况下,一个有用的模式是使用 Fold Transform为了通过编码而不是通过手动分层来指定您的颜色。结果看起来像这样(vega editor link):

{
"$schema": "https://vega.github.io/schema/vega-lite/v3.json",
"title": "Insights stats",
"description": "Overview of insights stats",
"width": 1000,
"height": 450,
"data": {
"values": [
{"date": "2019-01-01", "foo": 10, "bar": 10, "goo": 30},
{"date": "2019-01-02", "foo": 30, "bar": 20, "goo": 20},
{"date": "2019-01-03", "foo": 40, "bar": 20, "goo": 10},
{"date": "2019-01-04", "foo": 1, "bar": 60, "goo": 20}
]
},
"transform": [
{"fold": ["foo", "bar", "goo"]}
],
"mark": "line",
"encoding": {
"x": {"field": "date", "type": "temporal"},
"y": {"field": "value", "type": "quantitative"},
"color": {"field": "key", "type": "nominal"}
}
}

enter image description here

关于vega-lite - 用 VEGA-LITE 绘制多个 "columns"并包含一个图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56425430/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com