顺利连接Vega中的实际+预测线

问题描述 投票:0回答:1

我正在处理一个常见的业务场景,我想在图表上展示实际值和预测值。 然而,我在可视化中很难顺利连接这两条线。 您能否建议我可以使用哪些技术在 Vega/Vega-Lite 中实现这一目标?

非常感谢!

代码示例:

    {
      "$schema": "https://vega.github.io/schema/vega/v5.json",
      "width": 400,
      "height": 200,
      "padding": 5,
    
      "data": [
        {
          "name": "table",
          "values": [
            {"x": "A", "y": 28, "type":"Actual"},
            {"x": "B", "y": 55, "type":"Actual"},
            {"x": "C", "y": 43, "type":"Actual"},
            {"x": "D", "y": 91, "type":"Forecast"},
            {"x": "E", "y": 81, "type":"Forecast"},
            {"x": "F", "y": 53, "type":"Forecast"},
            {"x": "G", "y": 19, "type":"Forecast"},
            {"x": "H", "y": 87, "type":"Forecast"},
            {"x": "I", "y": 52, "type":"Forecast"},
            {"x": "J", "y": 48, "type":"Forecast"}
          ],
          "transform": [
           
            {
              "type": "formula",
              "expr": "datum.type==='Actual'?datum.y:null",
              "as": "actual"
            },
            {
              "type": "formula",
              "expr": "datum.type==='Forecast'?datum.y:null",
              "as": "forecast"
            }
          ]
        }
      ],
    
      "scales": [
        {
          "name": "x",
          "type": "band",
          "range": "width",
          
          "domain": {"data": "table", "field": "x"}
        },
        {
          "name": "y",
          "type": "linear",
          "range": "height",
          "nice": true,
          "zero": true,
          "domain": {"data": "table", "field": "y"}
        }
      ],
    
      "axes": [
        {"orient": "bottom", "scale": "x"},
        {"orient": "left", "scale": "y"}
      ],
    
      "marks": [
        {
          "type": "line",
          "from": {"data": "table"},
          "encode": {
            "enter": {
              "x": {"scale": "x", "field": "x"},
              "y": {"scale":"y","field": "actual"},
              "stroke": {"value": "black"},
              "strokeWidth": {"value": 2}
            }
          }
        },
        {
          "type": "line",
          "from": {"data": "table"},
          "encode": {
            "enter": {
              "x": {"scale": "x", "field": "x"},
              "y": {"scale":"y","field": "forecast"},
              "stroke": {"value": "gray"},
              "strokeWidth": {"value": 2},
              "strokeDash": {"value": [3,5]}
            }
          }
        }
 ]
}
powerbi powerbi-desktop vega-lite vega deneb
1个回答
0
投票

这是技术,但您必须复制最后一个实际值才能将线条连接起来。不过,您可以在任何地方执行此操作。

{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "width": 400,
  "height": 200,
  "padding": 5,
  "data": [
    {
      "name": "table",
      "values": [
        {"x": "A", "y": 28, "type": "Actual"},
        {"x": "B", "y": 55, "type": "Actual"},
        {"x": "C", "y": 43, "type": "Actual"},
        {"x": "C", "y": 43, "type": "Forecast"},
        {"x": "D", "y": 91, "type": "Forecast"},
        {"x": "E", "y": 81, "type": "Forecast"},
        {"x": "F", "y": 53, "type": "Forecast"},
        {"x": "G", "y": 19, "type": "Forecast"},
        {"x": "H", "y": 87, "type": "Forecast"},
        {"x": "I", "y": 52, "type": "Forecast"},
        {"x": "J", "y": 48, "type": "Forecast"}
      ]
    },
    {
      "name": "a",
      "source": "table",
      "transform": [{"type": "filter", "expr": "datum.type == 'Actual'"}]
    },
    {
      "name": "b",
      "source": "table",
      "transform": [{"type": "filter", "expr": "datum.type == 'Forecast'"}]
    }
  ],
  "scales": [
    {
      "name": "x",
      "type": "band",
      "range": "width",
      "domain": {"data": "table", "field": "x"}
    },
    {
      "name": "y",
      "type": "linear",
      "range": "height",
      "nice": true,
      "zero": true,
      "domain": {"data": "table", "field": "y"}
    }
  ],
  "axes": [
    {"orient": "bottom", "scale": "x"},
    {"orient": "left", "scale": "y"}
  ],
  "marks": [
    {
      "type": "line",
      "from": {"data": "a"},
      "encode": {
        "enter": {
          "x": {"scale": "x", "field": "x"},
          "y": {"scale": "y", "field": "y"},
          "stroke": {"value": "black"},
          "strokeWidth": {"value": 2}
        }
      }
    },
    {
      "type": "line",
      "from": {"data": "b"},
      "encode": {
        "enter": {
          "x": {"scale": "x", "field": "x"},
          "y": {"scale": "y", "field": "y"},
          "stroke": {"value": "gray"},
          "strokeWidth": {"value": 2},
          "strokeDash": {"value": [3, 5]}
        }
      }
    }
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.