简体   繁体   English

使用 Vega Lite 进行可视化:“emoji undefined”

[英]Visualisation with Vega Lite : “emoji undefined”

I'm trying to reproduce a visualisation in Vega Lite ( https://vega.github.io/vega-lite/examples/isotype_bar_chart_emoji.html ).我正在尝试在 Vega Lite 中重现可视化( https://vega.github.io/vega-lite/examples/isotype_bar_chart_emoji.ZFC35FDC70D5FC69D269883A.28 Instead of the animals emoji, I chose emoji to symbolise energy source.我没有选择动物表情符号,而是选择了表情符号来象征能源。 However my code does not work, looks like Vega does not recognise the emojis as it labels: "undefined".但是我的代码不起作用,看起来 Vega 无法识别表情符号,因为它标记为:“未定义”。 How can I help vega so it uses emojis as logos?我如何帮助 vega 使用表情符号作为徽标?

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "config": {"view": {"stroke": ""}},
  "width": 800,
  "height": 200,
  "data": {
    "values": [
{"continent": "Africa", "share": "coal"},
{"continent": "Africa", "share": "coal"},
{"continent": "Africa", "share": "gas"},
{"continent": "Europe", "share": "gas"},
{"continent": "Europe", "share": "gas"},
{"continent": "Europe", "share": "hydro"},
{"continent": "Europe", "share": "nuclear"},
{"continent": "Europe", "share": "renewables"},
{"continent": "Europe", "share": "renewables"},
{"continent": "Europe", "share": "renewables"},
{"continent": "Europe", "share": "renewables"},
{"continent": "Europe", "share": "renewables"}
    ]
  },
  "transform": [
    {
      "calculate": "{'coal': '🏭', 'nuclear': '⚛️', 'oil': '🛢️','renewables':'♻️','gas':'⛽'}",
      "as": "emoji"
    },
    {"window": [{"op": "rank", "as": "rank"}], "groupby": ["continent", "share"]}
  ],
  "mark": {"type": "text", "baseline": "middle"},
  "encoding": {
    "x": {"field": "rank", "type": "ordinal", "axis": null},
    "y": {"field": "share", "type": "nominal", "axis": null, "sort": null},
    "row": {"field": "continent", "header": {"title": ""}},
    "text": {"field": "emoji", "type": "nominal"},
    "size": {"value": 65}
  }
}

The transform calculate was not proper, you can use the datum to access the field from your data and depending on the condition provide the emoji value.转换计算不正确,您可以使用datum从数据中访问字段,并根据条件提供emoji值。 Refer the below code or the editor请参考以下代码或编辑器

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "config": {"view": {"stroke": ""}},
  "width": 800,
  "height": 200,
  "data": {
    "values": [
      {"continent": "Africa", "share": "coal"},
      {"continent": "Africa", "share": "coal"},
      {"continent": "Africa", "share": "gas"},
      {"continent": "Europe", "share": "gas"},
      {"continent": "Europe", "share": "gas"},
      {"continent": "Europe", "share": "hydro"},
      {"continent": "Europe", "share": "nuclear"},
      {"continent": "Europe", "share": "renewables"},
      {"continent": "Europe", "share": "renewables"},
      {"continent": "Europe", "share": "renewables"},
      {"continent": "Europe", "share": "renewables"},
      {"continent": "Europe", "share": "renewables"}
    ]
  },
  "transform": [
    {
      "calculate": "datum.share == 'coal' ?  '🏭' : datum.share == 'nuclear' ? '⚛️' : datum.share == 'oil' ? '🛢️' : datum.share == 'renewables' ?'♻️' : '⛽'",
      "as": "emoji"
    },
    {
      "window": [{"op": "rank", "as": "rank"}],
      "groupby": ["continent", "share"]
    }
  ],
  "mark": {"type": "text", "baseline": "middle"},
  "encoding": {
    "x": {"field": "rank", "type": "ordinal", "axis": null},
    "y": {"field": "share", "type": "nominal", "axis": null, "sort": null},
    "row": {"field": "continent", "header": {"title": ""}},
    "text": {"field": "emoji", "type": "nominal"},
    "size": {"value": 65}
  }
}

or simple adding this line to your current code will also resolve the issue:或者简单地将这一行添加到您当前的代码中也可以解决问题:

{
  "calculate": "{'coal': '🏭', 'nuclear': '⚛️', 'oil': '🛢️','renewables':'♻️','gas':'⛽', 'hydro': '🏭',}[datum.share]",
  "as": "emoji"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM