Vega-Lite(Deneb)|瀑布图(开始和结束列问题)

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

我在尝试为瀑布图创建 BEGIN / END 列时遇到了死胡同。

我已成功创建 END 列,该列正确位于 X 轴(一月至十二月)的末尾。但是,我无法成功添加 BEGIN 列以使其显示在 x 轴上的 Jan 之前。它要么没有出现,要么位于 Jan 之后而不是之前。对 x 轴排序似乎也不起作用。

这可以直接在 Vega-Lite 中完成吗?

这里是 pbix 的链接:waterfall_pbix_file

这是我当前失败尝试的屏幕截图。 waterfall attempt

我希望我已经提供了足够的信息。提前感谢您的帮助!

{
  "data": {
    "name": "dataset"
  },
  "transform": [
    {
      "sort": [
        {
          "field": "MonthNo"
        }
      ],
      "window": [
        {
          "op": "lag",
          "field": "salesCY",
          "as": "salesCY_previous"
        },
        {
          "op": "lag",
          "field": "Month",
          "as": "month_previous"
        },
        {
          "op": "lead",
          "field": "salesCY",
          "as": "salesCY_following"
        },
        {
          "op": "lead",
          "field": "Month",
          "as": "month_following"
        }
      ]
    },
    {
      "calculate": "datum.month_previous == null ? 'BEGIN' : datum.month_previous",
      "as": "month_previous"
    },
    {
      "calculate": "datum.month_following == null ? 'END' : datum.month_following",
      "as": "month_following"
    },
    {
      "calculate": "datum.month_previous === 'BEGIN' ? -999 : datum.month_following === 'END' ? 999 : datum.MonthNo",
      "as": "sort_order"
    }
  ],
  "width": 500,
  "height": 200,
  "layer": [
    {
      "mark": {
        "type": "bar",
        "fill": {
          "expr": "datum.salesCY > datum.salesCY_previous ? '#0291DC' : '#F0535A' "
        },
        "width": {
          "band": 0.8
        }
      }
    },
    {
      "transform": [
        {
          "filter": "datum.month_previous == 'BEGIN'"
        }
      ],
      "mark": {
        "type": "bar",
        "fill": "lightgrey",
        "stroke": "black",
        "strokeWidth": 0.5,
        "width": {
          "band": 0.8
        }
      },
      "encoding": {
        "x": {
          "field": "month_previous",
          "type": "nominal",
          "axis": {
            "title": null,
            "labels": true,
            "labelFontSize": 15,
            "labelColor": "grey",
            "labelExpr": "datum.label[0]",
            "labelAngle": 0,
            "labelPadding": 15,
            "domain": false,
            "ticks": false
          },
          "sort": {
            "field": "sort_order",
            "order": "ascending"
          },
          "scale": {
            "zero": true
          }
        },
        "y2": {
          "datum": 0,
          "type": "quantitative",
          "axis": {
            "title": null,
            "labels": false,
            "domain": false,
            "ticks": false,
            "grid": false
          }
        },
        "y": {
          "field": "salesCY",
          "type": "quantitative",
          "axis": {
            "title": null,
            "labels": false,
            "domain": false,
            "ticks": false,
            "grid": false
          }
        }
      }
    },
    {
      "transform": [
        {
          "filter": "datum.month_following == 'END'"
        }
      ],
      "mark": {
        "type": "bar",
        "fill": "black",
        "width": {
          "band": 0.8
        }
      },
      "encoding": {
        "x": {
          "field": "month_following",
          "type": "nominal",
          "axis": {
            "title": null,
            "labels": true,
            "labelColor": "grey",
            "labelExpr": "datum.label[0]",
            "labelAngle": 0,
            "domain": false,
            "ticks": false
          },
          "sort": null,
          "scale": {
            "zero": true
          }
        },
        "y2": {
          "datum": 0,
          "type": "quantitative",
          "axis": {
            "title": null,
            "labels": false,
            "domain": false,
            "ticks": false,
            "grid": false
          }
        },
        "y": {
          "field": "salesCY",
          "type": "quantitative",
          "axis": {
            "title": null,
            "labels": false,
            "domain": false,
            "ticks": false,
            "grid": false
          }
        }
      }
    }
  ],
  "encoding": {
    "x": {
      "field": "Month",
      "type": "nominal",
      "axis": {
        "title": null,
        "labels": true,
        "labelColor": "grey",
        "labelExpr": "datum.label[0]",
        "labelAngle": 0,
        "domain": false,
        "ticks": false
      },
      "sort": {
        "field": "sort_order",
        "type": "ordinal"
      },
      "scale": {
        "zero": true
      }
    },
    "y2": {
      "field": "salesCY_previous",
      "type": "quantitative",
      "axis": {
        "title": null,
        "labels": false,
        "domain": false,
        "ticks": false,
        "grid": false
      }
    },
    "y": {
      "field": "salesCY",
      "type": "quantitative",
      "axis": {
        "title": null,
        "labels": false,
        "domain": false,
        "ticks": false,
        "grid": false
      }
    }
  }
}
powerbi visualization powerbi-desktop vega-lite deneb
1个回答
0
投票

试试这个:

{
  "data": {
    "name": "dataset"
  },
  "transform": [
    {
      "sort": [
        {
          "field": "MonthNo"
        }
      ],
      "window": [
        {
          "op": "lag",
          "field": "salesCY",
          "as": "salesCY_previous"
        },
        {
          "op": "lag",
          "field": "Month",
          "as": "month_previous"
        },
        {
          "op": "lead",
          "field": "salesCY",
          "as": "salesCY_following"
        },
        {
          "op": "lead",
          "field": "Month",
          "as": "month_following"
        }
      ]
    },
    {
      "calculate": "datum.month_previous == null ? 'BEGIN' : datum.month_previous",
      "as": "month_previous"
    },
    {
      "calculate": "datum.month_following == null ? 'END' : datum.month_following",
      "as": "month_following"
    },
    {
      "calculate": "datum.MonthNo",
      "as": "sort_order"
    }
  ],
  "width": 500,
  "height": 200,
  "layer": [
    {
      "mark": {
        "type": "bar",
        "fill": {
          "expr": "datum.salesCY > datum.salesCY_previous ? '#0291DC' : '#F0535A' "
        },
        "width": {
          "band": 0.8
        }
      }
    },
    {
      // CANT SEEM TO FIX THIS MARK TO APPEAR BEFORE THE JAN MONTH COLUMN
      "transform": [
        {
          "filter": "datum.month_previous == 'BEGIN'"
        },
        {
          "calculate": "-1",
          "as": "sort_order"
        }
      ],
      "mark": {
        "type": "bar",
        "fill": "lightgrey",
        "stroke": "black",
        "strokeWidth": 0.5,
        "width": {
          "band": 0.8
        }
      },
      "encoding": {
        "x": {
          "field": "month_previous",
          "type": "nominal",
          "axis": {
            "title": null,
            "labels": true,
            "labelFontSize": 15,
            "labelColor": "grey",
            "labelExpr": "datum.label[0]",
            "labelAngle": 0,
            "labelPadding": 15,
            "domain": false,
            "ticks": false
          },
          "sort": null,
          "scale": {
            "zero": true
          }
        },
        "y2": {
          "datum": 0,
          "type": "quantitative",
          "axis": {
            "title": null,
            "labels": false,
            "domain": false,
            "ticks": false,
            "grid": false
          }
        },
        "y": {
          "field": "salesCY",
          "type": "quantitative",
          "axis": {
            "title": null,
            "labels": false,
            "domain": false,
            "ticks": false,
            "grid": false
          }
        }
      }
    },
    {
      "transform": [
        {
          "filter": "datum.month_following == 'END'"
        },
        {
          "calculate": "1e6",
          "as": "sort_order"
        }
      ],
      "mark": {
        "type": "bar",
        "fill": "black",
        "width": {
          "band": 0.8
        }
      },
      "encoding": {
        "x": {
          "field": "month_following",
          "type": "nominal",
          "axis": {
            "title": null,
            "labels": true,
            "labelColor": "grey",
            "labelExpr": "datum.label[0]",
            "labelAngle": 0,
            "domain": false,
            "ticks": false
          },
          "sort": null,
          "scale": {
            "zero": true
          }
        },
        "y2": {
          "datum": 0,
          "type": "quantitative",
          "axis": {
            "title": null,
            "labels": false,
            "domain": false,
            "ticks": false,
            "grid": false
          }
        },
        "y": {
          "field": "salesCY",
          "type": "quantitative",
          "axis": {
            "title": null,
            "labels": false,
            "domain": false,
            "ticks": false,
            "grid": false
          }
        }
      }
    }
  ],
  "encoding": {
    "x": {
      "field": "Month",
      "type": "nominal",
      "axis": {
        "title": null,
        "labels": true,
        "labelColor": "grey",
        "labelExpr": "datum.label[0]",
        "labelAngle": 0,
        "domain": false,
        "ticks": false
      },
      "sort": {
        "field": "sort_order",
        "order": "ascending"
      },
      "scale": {
        "zero": true
      }
    },
    "y2": {
      "field": "salesCY_previous",
      "type": "quantitative",
      "axis": {
        "title": null,
        "labels": false,
        "domain": false,
        "ticks": false,
        "grid": false
      }
    },
    "y": {
      "field": "salesCY",
      "type": "quantitative",
      "axis": {
        "title": null,
        "labels": false,
        "domain": false,
        "ticks": false,
        "grid": false
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.