Vega可视化从过滤的矩阵中移除空细胞

时间:2018-03-26 20:12:52

标签: data-visualization kibana vega vega-lite

我正在尝试制作一个vega可视化,它在矩阵中显示子图,并且能够过滤行和/或列,因此只显示一个子集。

当我应用滤镜时,只会按照我的预期绘制与滤镜匹配的标记,但矩阵中的所有单元格仍会显示。轴也正确响应过滤器。

如何在应用过滤器时更新单元格?

Unfiltered Matrix displays correctly

Filtered Matrix has too many cells

示例可视化
改编自https://vega.github.io/vega-lite/examples/trellis_barley.html

{
  "$schema": "https://vega.github.io/schema/vega/v3.0.json",
  "autosize": "pad",
  "padding": 5,
  "data": [
    {
      "name": "source_0",
      "values": [
        {
          "year": 1990,
          "level": 1,
          "region": "Region 1",
          "category": "Category 1"
        },
        {
          "year": 1990,
          "level": 1,
          "region": "Region 1",
          "category": "Category 2"
        },
        {
          "year": 1991,
          "level": 2,
          "region": "Region 1",
          "category": "Category 1"
        },
        {
          "year": 1991,
          "level": 1,
          "region": "Region 1",
          "category": "Category 2"
        },
        {
          "year": 1992,
          "level": 4,
          "region": "Region 1",
          "category": "Category 1"
        },
        {
          "year": 1992,
          "level": 3,
          "region": "Region 1",
          "category": "Category 2"
        },
        {
          "year": 1993,
          "level": 0,
          "region": "Region 1",
          "category": "Category 1"
        },
        {
          "year": 1993,
          "level": 2,
          "region": "Region 1",
          "category": "Category 2"
        },
        {
          "year": 1994,
          "level": 0,
          "region": "Region 1",
          "category": "Category 1"
        },
        {
          "year": 1994,
          "level": 3,
          "region": "Region 1",
          "category": "Category 2"
        },
        {
          "year": 1995,
          "level": 0,
          "region": "Region 1",
          "category": "Category 1"
        },
        {
          "year": 1995,
          "level": 0,
          "region": "Region 1",
          "category": "Category 2"
        },
        {
          "year": 1996,
          "level": 1,
          "region": "Region 1",
          "category": "Category 1"
        },
        {
          "year": 1996,
          "level": 4,
          "region": "Region 1",
          "category": "Category 2"
        },
        {
          "year": 1997,
          "level": 1,
          "region": "Region 1",
          "category": "Category 1"
        },
        {
          "year": 1997,
          "level": 2,
          "region": "Region 1",
          "category": "Category 2"
        },
        {
          "year": 1998,
          "level": 1,
          "region": "Region 1",
          "category": "Category 1"
        },
        {
          "year": 1998,
          "level": 0,
          "region": "Region 1",
          "category": "Category 2"
        },
        {
          "year": 1999,
          "level": 3,
          "region": "Region 1",
          "category": "Category 1"
        },
        {
          "year": 1999,
          "level": 1,
          "region": "Region 1",
          "category": "Category 2"
        },
        {
          "year": 2000,
          "level": 4,
          "region": "Region 1",
          "category": "Category 1"
        },
        {
          "year": 2000,
          "level": 1,
          "region": "Region 1",
          "category": "Category 2"
        },
        {
          "year": 2001,
          "level": 4,
          "region": "Region 1",
          "category": "Category 1"
        },
        {
          "year": 2001,
          "level": 1,
          "region": "Region 1",
          "category": "Category 2"
        },
        {
          "year": 2002,
          "level": 2,
          "region": "Region 1",
          "category": "Category 1"
        },
        {
          "year": 2002,
          "level": 0,
          "region": "Region 1",
          "category": "Category 2"
        },
        {
          "year": 1990,
          "level": 4,
          "region": "Region 2",
          "category": "Category 1"
        },
        {
          "year": 1990,
          "level": 2,
          "region": "Region 2",
          "category": "Category 2"
        },
        {
          "year": 1991,
          "level": 3,
          "region": "Region 2",
          "category": "Category 1"
        },
        {
          "year": 1991,
          "level": 2,
          "region": "Region 2",
          "category": "Category 2"
        },
        {
          "year": 1992,
          "level": 0,
          "region": "Region 2",
          "category": "Category 1"
        },
        {
          "year": 1992,
          "level": 2,
          "region": "Region 2",
          "category": "Category 2"
        },
        {
          "year": 1993,
          "level": 1,
          "region": "Region 2",
          "category": "Category 1"
        },
        {
          "year": 1993,
          "level": 4,
          "region": "Region 2",
          "category": "Category 2"
        },
        {
          "year": 1994,
          "level": 2,
          "region": "Region 2",
          "category": "Category 1"
        },
        {
          "year": 1994,
          "level": 3,
          "region": "Region 2",
          "category": "Category 2"
        },
        {
          "year": 1995,
          "level": 2,
          "region": "Region 2",
          "category": "Category 1"
        },
        {
          "year": 1995,
          "level": 0,
          "region": "Region 2",
          "category": "Category 2"
        },
        {
          "year": 1996,
          "level": 0,
          "region": "Region 2",
          "category": "Category 1"
        },
        {
          "year": 1996,
          "level": 4,
          "region": "Region 2",
          "category": "Category 2"
        },
        {
          "year": 1997,
          "level": 3,
          "region": "Region 2",
          "category": "Category 1"
        },
        {
          "year": 1997,
          "level": 2,
          "region": "Region 2",
          "category": "Category 2"
        },
        {
          "year": 1998,
          "level": 4,
          "region": "Region 2",
          "category": "Category 1"
        },
        {
          "year": 1998,
          "level": 0,
          "region": "Region 2",
          "category": "Category 2"
        },
        {
          "year": 1999,
          "level": 5,
          "region": "Region 2",
          "category": "Category 1"
        },
        {
          "year": 1999,
          "level": 0,
          "region": "Region 2",
          "category": "Category 2"
        },
        {
          "year": 2000,
          "level": 2,
          "region": "Region 2",
          "category": "Category 1"
        },
        {
          "year": 2000,
          "level": 3,
          "region": "Region 2",
          "category": "Category 2"
        },
        {
          "year": 2001,
          "level": 0,
          "region": "Region 2",
          "category": "Category 1"
        },
        {
          "year": 2001,
          "level": 2,
          "region": "Region 2",
          "category": "Category 2"
        },
        {
          "year": 2002,
          "level": 0,
          "region": "Region 2",
          "category": "Category 1"
        },
        {
          "year": 2002,
          "level": 1,
          "region": "Region 2",
          "category": "Category 2"
        },
        {
          "year": 1990,
          "level": 1,
          "region": "Region 3",
          "category": "Category 1"
        },
        {
          "year": 1990,
          "level": 1,
          "region": "Region 3",
          "category": "Category 2"
        },
        {
          "year": 1991,
          "level": 2,
          "region": "Region 3",
          "category": "Category 1"
        },
        {
          "year": 1991,
          "level": 1,
          "region": "Region 3",
          "category": "Category 2"
        },
        {
          "year": 1992,
          "level": 4,
          "region": "Region 3",
          "category": "Category 1"
        },
        {
          "year": 1992,
          "level": 3,
          "region": "Region 3",
          "category": "Category 2"
        },
        {
          "year": 1993,
          "level": 0,
          "region": "Region 3",
          "category": "Category 1"
        },
        {
          "year": 1993,
          "level": 2,
          "region": "Region 3",
          "category": "Category 2"
        },
        {
          "year": 1994,
          "level": 0,
          "region": "Region 3",
          "category": "Category 1"
        },
        {
          "year": 1994,
          "level": 3,
          "region": "Region 3",
          "category": "Category 2"
        },
        {
          "year": 1995,
          "level": 0,
          "region": "Region 3",
          "category": "Category 1"
        },
        {
          "year": 1995,
          "level": 0,
          "region": "Region 3",
          "category": "Category 2"
        },
        {
          "year": 1996,
          "level": 1,
          "region": "Region 3",
          "category": "Category 1"
        },
        {
          "year": 1996,
          "level": 4,
          "region": "Region 3",
          "category": "Category 2"
        },
        {
          "year": 1997,
          "level": 1,
          "region": "Region 3",
          "category": "Category 1"
        },
        {
          "year": 1997,
          "level": 2,
          "region": "Region 3",
          "category": "Category 2"
        },
        {
          "year": 1998,
          "level": 1,
          "region": "Region 3",
          "category": "Category 1"
        },
        {
          "year": 1998,
          "level": 0,
          "region": "Region 3",
          "category": "Category 2"
        },
        {
          "year": 1999,
          "level": 3,
          "region": "Region 3",
          "category": "Category 1"
        },
        {
          "year": 1999,
          "level": 1,
          "region": "Region 3",
          "category": "Category 2"
        },
        {
          "year": 2000,
          "level": 4,
          "region": "Region 3",
          "category": "Category 1"
        },
        {
          "year": 2000,
          "level": 1,
          "region": "Region 3",
          "category": "Category 2"
        },
        {
          "year": 2001,
          "level": 4,
          "region": "Region 3",
          "category": "Category 1"
        },
        {
          "year": 2001,
          "level": 1,
          "region": "Region 3",
          "category": "Category 2"
        },
        {
          "year": 2002,
          "level": 2,
          "region": "Region 3",
          "category": "Category 1"
        },
        {
          "year": 2002,
          "level": 0,
          "region": "Region 3",
          "category": "Category 2"
        }
      ],
      "transform": [
        {
          "type": "filter",
          "expr": "(!Category || test(regexp(Category, 'i'), datum.category))"
        },
        {
          "type": "filter",
          "expr": "(!Region || test(regexp(Region, 'i'), datum.region))"
        }
      ]
    },
    {
      "name": "data_0",
      "source": "source_0",
      "transform": [
        {
          "type": "formula",
          "expr": "toNumber(datum[\"year\"])",
          "as": "year"
        }
      ]
    },
    {
      "name": "trellis_barley_column_domain",
      "source": "data_0",
      "transform": [
        {
          "type": "aggregate",
          "groupby": [
            "category"
          ]
        }
      ]
    },
    {
      "name": "trellis_barley_row_domain",
      "source": "data_0",
      "transform": [
        {
          "type": "aggregate",
          "groupby": [
            "region"
          ]
        }
      ]
    },
    {
      "name": "data_2",
      "source": "data_0",
      "transform": [
        {
          "type": "filter",
          "expr": "datum[\"year\"] !== null && !isNaN(datum[\"year\"])"
        }
      ]
    }
  ],
  "signals": [
    {
      "name": "Region",
      "value": "",
      "bind": {
        "input": "text",
        "placeholder": "search",
        "autocomplete": "off"
      }
    },
    {
      "name": "Category",
      "value": "",
      "bind": {
        "input": "text",
        "placeholder": "search",
        "autocomplete": "off"
      }
    },
    {
      "name": "trellis_barley_child_width",
      "value": 200
    },
    {
      "name": "trellis_barley_y_step",
      "value": 40
    },
    {
      "name": "trellis_barley_child_height",
      "update": "bandspace(domain('trellis_barley_y').length, 1, 0.5) * trellis_barley_y_step"
    }
  ],
  "layout": {
    "padding": {
      "row": 10,
      "column": 10
    },
    "offset": 10,
    "columns": {
      "signal": "length(data('trellis_barley_column_domain'))"
    },
    "bounds": "full",
    "align": "all"
  },
  "marks": [
    {
      "name": "trellis_barley_row_title",
      "role": "row-title",
      "type": "group",
      "marks": [
        {
          "type": "text",
          "role": "row-title-text",
          "style": "guide-title",
          "encode": {
            "update": {
              "align": {
                "value": "center"
              },
              "text": {
                "value": "region"
              },
              "angle": {
                "value": 270
              }
            }
          }
        }
      ]
    },
    {
      "name": "trellis_barley_column_title",
      "role": "column-title",
      "type": "group",
      "marks": [
        {
          "type": "text",
          "role": "column-title-text",
          "style": "guide-title",
          "encode": {
            "update": {
              "align": {
                "value": "center"
              },
              "text": {
                "value": "category"
              }
            }
          }
        }
      ]
    },
    {
      "name": "trellis_barley_row_header",
      "type": "group",
      "role": "row-header",
      "from": {
        "data": "trellis_barley_row_domain"
      },
      "sort": {
        "field": "datum[\"region\"]",
        "order": "ascending"
      },
      "title": {
        "text": {
          "signal": "''+parent[\"region\"]"
        },
        "offset": 10,
        "orient": "left",
        "style": "guide-label"
      },
      "encode": {
        "update": {
          "height": {
            "signal": "trellis_barley_child_height"
          }
        }
      },
      "axes": [
        {
          "scale": "trellis_barley_y",
          "orient": "left",
          "title": "level",
          "labelOverlap": true,
          "zindex": 1
        }
      ]
    },
    {
      "name": "trellis_barley_column_header",
      "type": "group",
      "role": "column-header",
      "from": {
        "data": "trellis_barley_column_domain"
      },
      "sort": {
        "field": "datum[\"category\"]",
        "order": "ascending"
      },
      "title": {
        "text": {
          "signal": "''+parent[\"category\"]"
        },
        "offset": 10,
        "orient": "top",
        "style": "guide-label"
      },
      "encode": {
        "update": {
          "width": {
            "signal": "trellis_barley_child_width"
          }
        }
      }
    },
    {
      "name": "trellis_barley_column_footer",
      "type": "group",
      "role": "column-footer",
      "from": {
        "data": "trellis_barley_column_domain"
      },
      "sort": {
        "field": "datum[\"category\"]",
        "order": "ascending"
      },
      "encode": {
        "update": {
          "width": {
            "signal": "trellis_barley_child_width"
          }
        }
      },
      "axes": [
        {
          "scale": "trellis_barley_x",
          "orient": "bottom",
          "title": "year",
          "labelFlush": true,
          "labelOverlap": true,
          "tickCount": {
            "signal": "ceil(trellis_barley_child_width/40)"
          },
          "zindex": 1
        }
      ]
    },
    {
      "name": "trellis_barley_cell",
      "type": "group",
      "style": "cell",
      "from": {
        "facet": {
          "name": "trellis_barley_facet",
          "data": "data_0",
          "groupby": [
            "region",
            "category"
          ],
          "aggregate": {
            "cross": true
          }
        }
      },
      "sort": {
        "field": [
          "datum[\"region\"]",
          "datum[\"category\"]"
        ],
        "order": [
          "ascending",
          "ascending"
        ]
      },
      "data": [
        {
          "source": "trellis_barley_facet",
          "name": "data_0",
          "transform": [
            {
              "type": "filter",
              "expr": "datum[\"year\"] !== null && !isNaN(datum[\"year\"])"
            }
          ]
        }
      ],
      "encode": {
        "update": {
          "width": {
            "signal": "trellis_barley_child_width"
          },
          "height": {
            "signal": "trellis_barley_child_height"
          }
        }
      },
      "marks": [
        {
          "name": "trellis_barley_child_pathgroup",
          "type": "group",
          "from": {
            "facet": {
              "name": "faceted_path_trellis_barley_child_main",
              "data": "data_0",
              "groupby": [
                "region"
              ]
            }
          },
          "encode": {
            "update": {
              "width": {
                "field": {
                  "group": "width"
                }
              },
              "height": {
                "field": {
                  "group": "height"
                }
              }
            }
          },
          "marks": [
            {
              "name": "trellis_barley_child_marks",
              "type": "line",
              "style": [
                "line"
              ],
              "sort": {
                "field": "datum[\"year\"]",
                "order": "descending"
              },
              "from": {
                "data": "faceted_path_trellis_barley_child_main"
              },
              "encode": {
                "update": {
                  "stroke": {
                    "scale": "trellis_barley_color",
                    "field": "region"
                  },
                  "x": {
                    "scale": "trellis_barley_x",
                    "field": "year"
                  },
                  "y": {
                    "scale": "trellis_barley_y",
                    "field": "level"
                  }
                }
              }
            }
          ]
        }
      ],
      "axes": [
        {
          "scale": "trellis_barley_x",
          "orient": "bottom",
          "grid": true,
          "tickCount": {
            "signal": "ceil(trellis_barley_child_width/40)"
          },
          "gridScale": "trellis_barley_y",
          "domain": false,
          "labels": false,
          "maxExtent": 0,
          "minExtent": 0,
          "ticks": false,
          "zindex": 0
        }
      ]
    }
  ],
  "scales": [
    {
      "name": "trellis_barley_x",
      "type": "linear",
      "domain": {
        "data": "data_2",
        "field": "year"
      },
      "range": [
        0,
        {
          "signal": "trellis_barley_child_width"
        }
      ],
      "zero": false,
      "nice": true
    },
    {
      "name": "trellis_barley_y",
      "type": "point",
      "domain": {
        "data": "data_0",
        "field": "level",
        "sort": {
          "field": "year",
          "op": "median",
          "order": "descending"
        }
      },
      "range": {
        "step": {
          "signal": "trellis_barley_y_step"
        }
      },
      "padding": 0.5
    },
    {
      "name": "trellis_barley_color",
      "type": "ordinal",
      "domain": {
        "data": "data_2",
        "field": "region",
        "sort": true
      },
      "range": "category"
    }
  ],
  "legends": [
    {
      "stroke": "trellis_barley_color",
      "title": "region"
    }
  ],
  "config": {
    "axisY": {
      "minExtent": 30
    }
  }
}

0 个答案:

没有答案