You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vega-lite中ArgMax与Color属性问题:无法同时编码两类标记

Vega-Lite 折线图分层可视化技术瓶颈

我在使用Vega-Lite时遇到技术问题:希望通过filter和color属性,同时分层实现以下效果:

  • 折线端点的圆形标记
  • 端点文本标签
  • 折线的最高点/最低点标记
    但始终无法完成正确编码。

已开展三次尝试:

  • 首次尝试完全失败;
  • 第二次尝试取得进展,但仍存在编码逻辑问题;
  • 第三次尝试实现了部分效果,但必须依赖额外DAX辅助度量来识别高低点,理想状态是无需额外DAX度量即可达成目标。

配套PBIX文件可用于测试验证。

以下为最新Vega-Lite代码:

{
  "data": {"name": "dataset"},
  "encoding": {
    "x": {
      "field": "DayOfYear",
      "type": "temporal"
    },
    "y": {
      "field": "_temp",
      "type": "quantitative",
      "scale": {"zero": false}
    },
    "color": {
      "field": "Year",
      "type": "nominal"
    }
  },
  "layer": [
    {
      "transform": [
        {
          "filter": {
            "field": "Year",
            "oneOf": [
              "2023",
              "2022",
              "2021",
              "1979-2000 mean"
            ]
          }
        }
      ],
      "layer": [
        {
          "mark": {"type": "line"},
          "encoding": {
            "x": {
              "field": "DayOfYear",
              "type": "temporal"
            },
            "y": {
              "field": "_temp",
              "type": "quantitative",
              "scale": {"zero": false}
            },
            "color": {
              "field": "Year",
              "type": "nominal"
            }
          }
        },
        {
          "description": "COLOR_LINES_BLUE_RED",
          "layer": [
            {
              "mark": {"type": "line"},
              "encoding": {
                "x": {
                  "field": "DayOfYear",
                  "type": "temporal"
                },
                "y": {
                  "field": "_temp",
                  "type": "quantitative",
                  "scale": {
                    "zero": false
                  }
                },
                "color": {
                  "field": "Year",
                  "type": "nominal",
                  "scale": {
                    "domain": [
                      "2023",
                      "2022",
                      "2021",
                      "1979-2000 mean"
                    ],
                    "range": [
                      "crimson",
                      "#000FFF50",
                      "#000FFF80",
                      "orange"
                    ]
                  }
                }
              }
            }
          ]
        },
        {
          "transform": [
            {
              "filter": "datum['_temp'] == datum['_highpoints'] || datum['_temp'] == datum['_lowpoints']"
            }
          ],
          "mark": {
            "type": "point",
            "filled": true,
            "stroke": "black",
            "strokeWidth": 0.8,
            "shape": {
              "expr": "datum['_temp'] == datum['_highpoints'] ? 'triangle-down' : 'triangle-up'"
            },
            "size": 250,
            "opacity": 1,
            "yOffset": {
              "expr": "datum['_temp'] == datum['_highpoints'] ? -10 : 10"
            }
          },
          "encoding": {
            "x": {
              "field": "DayOfYear",
              "type": "temporal"
            },
            "y": {
              "field": "_temp",
              "type": "quantitative",
              "scale": {"zero": false}
            },
            "color": {
              "field": "Year",
              "type": "nominal"
            }
          }
        },
        {
          "transform": [
            {
              "filter": "datum['_temp'] == datum['_highpoints'] || datum['_temp'] == datum['_lowpoints']"
            }
          ],
          "mark": {
            "type": "text",
            "filled": true,
            "stroke": "whitesmoke",
            "strokeWidth": 8,
            "size": 15,
            "fontWeight": "1000",
            "opacity": 1,
            "yOffset": {
              "expr": "datum['_temp'] == datum['_highpoints'] ? -30 : 30"
            }
          },
          "encoding": {
            "x": {
              "field": "DayOfYear",
              "type": "temporal"
            },
            "y": {
              "field": "_temp",
              "type": "quantitative",
              "scale": {"zero": false}
            },
            "color": {
              "field": "Year",
              "type": "nominal"
            },
            "text": {
              "field": "_temp",
              "format": ",.3~s"
            }
          }
        },
        {
          "transform": [
            {
              "filter": "datum['_temp'] == datum['_highpoints'] || datum['_temp'] == datum['_lowpoints']"
            }
          ],
          "mark": {
            "type": "text",
            "filled": true,
            "size": 15,
            "fontWeight": "1000",
            "opacity": 1,
            "yOffset": {
              "expr": "datum['_temp'] == datum['_highpoints'] ? -30 : 30"
            }
          },
          "encoding": {
            "text": {
              "field": "_temp",
              "format": ",.3~s"
            }
          }
        }
      ]
    }
  ]
}

内容的提问来源于stack exchange,提问作者PBI DataVizzle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 04:45:20