如何避免带有图层的折线图中的缩放冲突?

时间:2020-05-07 21:47:43

标签: vega-lite

VEGA-lite并不完美,但是非常好,通常对于看起来像bug的东西,有一种解决方法...所以我想在这个“ bug”中我们有一种解决方法。

((答案后编辑:它不是真正的错误,是规范语言上的“语义错误”))

奇怪的行为,这是一个“语义错误”:我在简单的情况下使用selection: { "grid": {"type":"interval", "bind":"scales"} }通过简单的mark: 'line'进行缩放。当我添加layer时,它停止工作

    {
        title: "Número de registros por minuto (n_count normalizado)", 
        $schema: vglVers,
        data: { "url":"mySQLtable" },
        selection: { "grid": {"type":"interval", "bind":"scales"} }, // was working with simple mark
        //mark: 'line',
        width:340,
        encoding: {
          x: {"field": "instant", "type": "temporal"},
          y: {"field": "n_pmin", "type": "quantitative"},
          color: {"field": "symbol", "type": "nominal"}
        },
        layer: [
            {
              "mark": {"type": "line", "point": true},
              "transform": [{"filter": "datum.symbol == 'n_pmin'"}]
            },
            { "mark": {"type": "line"}, "transform": [{"filter": "datum.symbol != 'n_pmin'"}]  }
        ]
      }

解决方法:为@jakevdp commented here“必须将间隔选择添加到一层中” 。但是

  1. 如何进行“间隔选择”?

  2. 图表上的数据不是静态的,我需要一个随其变化的时间间隔,因此,设置时间间隔没有意义。

1 个答案:

答案 0 :(得分:1)

我所指的“区间选择”是图表中的区间选择定义:

selection: { "grid": {"type":"interval", "bind":"scales"} }

您不能在顶级图表中声明它;您必须在其中一层中声明它:

{
    title: "Número de registros por minuto (n_count normalizado)", 
    $schema: vglVers,
    data: { "url":"mySQLtable" },
    width:340,
    encoding: {
      x: {"field": "instant", "type": "temporal"},
      y: {"field": "n_pmin", "type": "quantitative"},
      color: {"field": "symbol", "type": "nominal"}
    },
    layer: [
        {
          "mark": {"type": "line", "point": true},
          "transform": [{"filter": "datum.symbol == 'n_pmin'"}],
          "selection": {"grid": {"type":"interval", "bind":"scales"}},
        },
        {
          "mark": {"type": "line"},
          "transform": [{"filter": "datum.symbol != 'n_pmin'"}]
        }
    ]
 }

您的问题不是bug,我的解决方案也不是解决方法:vega-lite架构指定必须在单位规范(即单个层)内声明选择内容。