简体   繁体   English

如何在 vega lite 中的条形图上添加文本

[英]How to add text on bar chart in vega lite

I am trying to add text label to bar chart, but it not showing it.我正在尝试将文本标签添加到条形图,但它没有显示。 Here is a example i want to achieve这是我想要实现的一个例子图片

Here is code这是代码

 { "$schema": "https://vega.github.io/schema/vega-lite/v5.json", "background": "#EFF1EF", "width": 500, "height": 26, "autosize":"pad", "data": { "values": [ { "standarts": { "divisions": [ { "a": { "mean": 135, "part1": 10, "part2": 60, "part5": 130, "part7": 198, "part9": 255, "goal": { "value": 150 } } } ] } } ] }, "transform": [ {"calculate": "datum.standarts.divisions", "as": "D"}, {"flatten": ["D"]}, {"calculate": "datum.Da", "as": "x"} ], "encoding": { "x": {"field": "x", "type": "quantitative","scale":{"domain":[0,300]}, "stack": null}, "opacity": {"value": 1} }, "layer": [ { "mark":{"type":"bar", "color": "#b2b7b4", "cornerRadius": 40}, "encoding": {"x": {}} }, { "mark": {"type": "bar", "color": "#0ef9e5", "height": 25}, "encoding": {"x": {"field": "x.mean"}} }, { "mark":{"type": "text", "align":"center"}, "encoding": {"text": {"field": "x.mean"}} }, { "mark":{"type": "rule"}, "encoding": {"x": {"field":"x.goal.value", "title":["Target"],"axis":{ "titleFont": "Google Sans", "titleFontSize": 16, "titleFontWeight":700, "titleY": 8} }}} ], "config": { "axis": {"ticks": false, "labels": false, "domain": false}, "rule": { "strokeWidth": 4, "strokeDash": [ 0.2, 8 ], "strokeCap": "round", "align":"center" } } }

this is code And link to editor vega-lite editor这是代码并链接到编辑器vega-lite 编辑器

text part is still not working.文本部分仍然无法正常工作。

I used documentation on vega-lite website and example codes, but still no success.我使用了 vega-lite 网站上的文档和示例代码,但仍然没有成功。

Thanks for the help.谢谢您的帮助。

在此处输入图像描述

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "background": "#EFF1EF",
  "width": 500,
  "height": 26,
  "autosize": "pad",
  "data": {
    "values": [
      {
        "standarts": {
          "divisions": [
            {
              "a": {
                "mean": 135,
                "part1": 10,
                "part2": 60,
                "part5": 130,
                "part7": 198,
                "part9": 255,
                "goal": {"value": 150}
              }
            }
          ]
        }
      }
    ]
  },
  "transform": [
    {"calculate": "datum.standarts.divisions", "as": "D"},
    {"flatten": ["D"]},
    {"calculate": "datum.D.a", "as": "x"}
  ],
  "encoding": {
    "x": {
      "field": "x",
      "type": "quantitative",
      "scale": {"domain": [0, 300]},
      "stack": null
    },
    "opacity": {"value": 1}
  },
  "layer": [
    {
      "mark": {"type": "bar", "color": "#b2b7b4", "cornerRadius": 40},
      "encoding": {"x": {}}
    },
    {
      "mark": {"type": "bar", "color": "#0ef9e5", "height": 25},
      "encoding": {"x": {"field": "x.mean"}}
    },
    {
      "mark": {"type": "text", "align": "right", "dx": -10},
      "encoding": {"text": {"field": "x.mean"}, "x": {"field": "x.mean"}}
    },
    {
      "mark": {"type": "text", "align": "center", "dy": -30},
      "encoding": {
        "text": {"field": "x.goal.value"},
        "x": {"field": "x.goal.value"}
      }
    },
    {
      "mark": {"type": "rule"},
      "encoding": {
        "x": {
          "field": "x.goal.value",
          "title": ["Target"],
          "axis": {
            "titleFont": "Google Sans",
            "titleFontSize": 16,
            "titleFontWeight": 700,
            "titleY": 8
          }
        }
      }
    }
  ],
  "config": {
    "axis": {"ticks": false, "labels": false, "domain": false},
    "rule": {
      "strokeWidth": 4,
      "strokeDash": [0.2, 8],
      "strokeCap": "round",
      "align": "center"
    }
  }
}

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

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