在 Vega 图表中使用内联 CSV 数据

Using inline CSV data in Vega charts

我正在尝试将内联 csv 数据与 Vega 图表一起使用,使用 Vega 数据规范的值 属性。示例库中的 Vega documentation says that this possible, but doesn't give an example. I have tried to change the bar chart example 使用内联 CSV 数据而不是 JSON,但没有成功。

我用自己的代码替换了示例代码中的数据部分。原始片段如下所示:

"data": [
{
  "name": "table",
  "values": [
    {"category": "A", "amount": 28},
    {"category": "B", "amount": 55},
    {"category": "C", "amount": 43},
    {"category": "D", "amount": 91},
    {"category": "E", "amount": 81},
    {"category": "F", "amount": 53},
    {"category": "G", "amount": 19},
    {"category": "H", "amount": 87}
  ]
} ]

我用这个替换了它:

  "data": [
{
  "name": "table",
  "format": "csv",
  "values": {"category", "amount"
    "A", "28"
    "B", "55"
    "C", "43"
    "E", "91"
    "E", "81"
    "F", "53"
    "G", "19"
    "H", "87"}
} ]

我使用了 Vega online editor,但在 JSON 中只收到有关意外标记的错误消息。我还尝试了以下变体:

  "data": [
{
  "name": "table",
  "format": "csv",
  "values": "category, amount
    A, 28
    B, 55
    C, 43
    E, 91
    E, 81
    F, 53
    G, 19
    H, 87"
} ]

但这会导致相同的错误消息。这里的正确语法是什么?

正如您在文档中看到的那样,方式是这样的:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "A simple bar chart with embedded data.",
  "data": {"values": "a,b\nA,50\nB,30\nC,60", "format": {"type": "csv"}},
  "mark": "bar",
  "encoding": {
    "x": {"field": "a", "type": "nominal", "axis": {"labelAngle": 0}},
    "y": {"field": "b", "type": "quantitative"}
  }
}

一个例子here