【问题标题】:Create a discontinued line chart using Vega lib使用 Vega lib 创建已停止使用的折线图
【发布时间】:2019-03-15 15:34:37
【问题描述】:

我希望在 Vega 中创建这种图表:

我在这里仔细阅读了有关标记的文档: https://vega.github.io/vega/docs/marks/line/

我阅读了 Type-Specific Mark Propertiesdefined 属性,这似乎是我所需要的。但我不知道如何使用这个属性。

我的标记是这样定义的:

'marks': [
      {
        'name': 'expected_sales',
        'description': 'The sales line',
        'type': 'line',
        'defined': 'false', // this I added based on the documentation
        'from': {
          'data': 'SalesData'
        },
        'zindex': 100,
        'encode': { ... }
      }
 ]

但这显然行不通。该线路仍在继续。不得不提的是,我得到的数据点没有null 值,而是0.0

【问题讨论】:

    标签: charts data-visualization linechart vega


    【解决方案1】:

    考虑到销售额在某个时候可能为 0 美元,最好区分 0 值和 null 值。

    也就是说,因为空值在数据集中被定义为0.0,所以defined 属性对于所有其他点都必须为真,除非值为0.0

    在下面的示例中,"defined": {"signal": "datum.v !== 0.0"} 用于有条件地将 "defined" 属性分配为 false,如果值 "datum.v" 为 0.0

    {
      "$schema": "https://vega.github.io/schema/vega/v5.json",
      "width": 400,
      "height": 200,
      "padding": 5,
    
      "data": [
        {
          "name": "table",
          "values": [
            {"u": 1, "v": 28}, {"u": 2, "v": 12.0},
            {"u": 3, "v": 0.0}, {"u": 4, "v": 10},
            {"u": 5, "v": 36}, {"u": 6, "v": 44}
          ]
        }
      ],
    
      "scales": [
        {
          "name": "xscale",
          "type": "linear",
          "range": "width",
          "zero": false,
          "domain": {"data": "table", "field": "u"}
        },
        {
          "name": "yscale",
          "type": "linear",
          "range": "height",
          "nice": true,
          "zero": false,
          "domain": {"data": "table", "field": "v"}
        }
      ],
      "axes": [
        {"scale": "xscale", "orient": "bottom", "grid": true},
        {"scale": "yscale", "orient": "left"}
    
      ],
    
      "marks": [
        {
          "type": "line",
          "from": {"data": "table"},
          "encode": {
            "enter": {
              "stroke": {"value": "#652c90"}
            },
            "update": {
              "x": {"scale": "xscale", "field": "u"},
              "y": {"scale": "yscale", "field": "v"},
              "defined": {"signal": "datum.v !== 0.0"},
              "interpolate": {"value": "linear"},
              "strokeWidth": {"value": 4},
              "strokeDash": {"value": [1,0]},
              "strokeCap": {"value": "square"},
              "opacity": {"value": 1}
            },
            "hover": {
              "opacity": {"value": 0.5}
            }
          }
        }
      ]
    }

    【讨论】:

    • 那么,对于 null 值,我们只需将 0.0 替换为 null?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 2016-05-14
    相关资源
    最近更新 更多