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

Vega多系列折线图:如何为单个系列设置虚线样式

为Vega-Lite多系列折线图指定单个类别的虚线样式

要给特定类别的折线设置虚线样式,你可以在编码中添加条件化的strokeDash属性,针对目标类别单独配置线条样式,其他类别保持默认实线。

修改后的完整配置如下:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "width": 200,
  "height": 200,
  "config": {
    "background": "transparent",
    "view": {"stroke": "transparent"},
    "aria": true
  },
  "data": {
    "name": "table",
    "values": [
      {"x": 0, "y": 0, "c": "Alpha"},
      {"x": 1, "y": 3, "c": "Alpha"},
      {"x": 2, "y": 1, "c": "Alpha"},
      {"x": 0, "y": 1, "c": "Beta"},
      {"x": 1, "y": 2, "c": "Beta"},
      {"x": 2, "y": 0, "c": "Beta"}
    ]
  },
  "mark": {"type": "line", "interpolate": "monotone", "point": false},
  "encoding": {
    "x": {
      "type": "quantitative",
      "field": "x"
    },
    "y": {
      "type": "quantitative",
      "field": "y"
    },
    "color": {
      "field": "c",
      "type": "nominal",
      "scale": {
        "domain": ["Alpha", "Beta"],
        "range": ["rgb(200, 200, 200)", "rgb(28, 51, 99)"]
      }
    },
    "strokeDash": {
      "condition": {
        "test": "datum.c === 'Alpha'",
        "value": [5, 3]
      },
      "value": []
    }
  }
}

关键说明

  • strokeDash字段通过condition实现条件判断:当数据的c字段等于"Alpha"时,使用[5,3]作为虚线参数(第一个数字是线段长度,第二个是间隔长度,可按需调整);
  • 未匹配条件的类别(如示例中的Beta)使用value: [],对应默认的实线样式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 18:29:49