简体   繁体   English

如何在 Vega-Lite 中注释折线图?

[英]How to Annotate a Line in line Chart in Vega-Lite?

How to annotate a line in line chart in vega-lite For the below code https://vega.github.io/editor/#/如何在 vega-lite 中注释折线图中的线条对于以下代码https://vega.github.io/editor/#/

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "layer": [
    {
      "data": {"url": "data/stocks.csv"},
      "mark": "line",
      "encoding": {
        "x": {"field": "date", "type": "temporal"},
        "y": {"field": "price", "type": "quantitative"},
        "color": {"field": "symbol", "type": "nominal"}
      }
    },
    {
      "data": {"values": [{}]},
      "mark": {"type": "rule", "strokeDash": [2, 2], "size": 2},
      "encoding": {"x": {"datum": {"year": 2006}}}
    }
  ]
}

We get plot我们得到情节在此处输入图像描述

If I want to annotate the line at a specific position like (2004,400)如果我想在特定位置注释行,例如 (2004,400)

在此处输入图像描述

I tried this, it is working, but I don't want to pass hardcoded values like "a": 2004, "b": 400,我试过这个,它正在工作,但我不想传递像“a”这样的硬编码值:2004,“b”:400,

   {
      "data": {
        "values": [
          {"a": 2004, "b": 400}
       ]
     },
      "mark": {"type": "text", "fontSize" : 16, "fontWeight":"bold", "align" : "left"},
      "encoding": {
        "text": {"value": "Optimum"},
        "x": {"field": "a", "type": "quantitative", "title":""},
        "y": {"field": "b", "type": "quantitative", "title":""}     
      }
    },

How to pass specific values from the data like average value of date (say:2004) and average value of price (say:400)?如何从数据中传递特定值,例如日期平均值(例如:2004)和价格平均值(例如:400)?

or或者

just next to the line in the middle of y-axis就在 y 轴中间的线旁边

Transform and Aggregate Worked, I needed only for Y axis average position, so below code worked good.转换和聚合工作,我只需要 Y 轴平均位置,所以下面的代码效果很好。 For another axis we can use same transform.对于另一个轴,我们可以使用相同的变换。

    {
      "mark": {"type": "text", "fontSize" : 16, "fontWeight":"bold", "align" : "left"},
      "transform": [
        {
          "aggregate": [{
           "op": "mean",
           "field": "price",
           "as": "mean_y_axis"
          }],
          "groupby": ["date"]
        }
      ],      
      "encoding": {
        "text": {"value": "Optimum"},
        "x": {"field": "date",
              "type": "quantitative"},
        "y": {"field": "mean_y_axis",
              "type": "quantitative"}     
      }
    }

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

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