简体   繁体   English

VEGA Lite,如何为串联图表创建单独的图例

[英]VEGA Lite, How to create separate Legends for concatenated charts

I am trying to create a visualization of multiple stacked bar graphs and I want to have each chart use its own legend, the problem is that the legend generated by Vegalite for each graph contains all fields, even the ones not present on that particular graph.我正在尝试创建多个堆叠条形图的可视化,并且我想让每个图表使用自己的图例,问题是 Vegalite 为每个图生成的图例包含所有字段,即使是那些不存在于该特定图表上的字段。

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "A dashboard with cross-highlighting.",
  "data": {"url": "data/movies.json"},
  "vconcat": [

    {
      "width": 400,
      "height": 150,
      "mark": "bar",
      "params": [{
        "name": "main",
        "select": {"type": "point", "encodings": ["x"]}
      }],
      "encoding": {
        "x": {"field": "Major Genre", "axis": {"labelAngle": -40}},
        "y": {"aggregate": "count"},
        "color": {
          "condition": {
            "param": "main",
            "field": "MPAA Rating"
          },
          "value": "lightgrey"
        }
      }
    },

    {
      "width": 400,
      "height": 150,  
      "transform": [{
        "filter": {"param": "main"}
      }],
      "mark": "bar",
      "params": [{
        "name": "i",
        "select": {"type": "point", "encodings": ["x"]}
      }],
      "encoding": {
        "x": {
          "field": "MPAA Rating"
        },
        "y": {"aggregate": "count"},
        "color": {
          "condition": {
            "param": "i",
            "field": "Source"
          },
          "value": "lightgrey"
          
        }
        
      }
    },

    {
      "width": 400,
      "height": 150,  
      "transform": [
        {"filter": {"param": "main"}},
        {"filter": {"param": "i"}}
        ],
      "mark": "bar",
      "encoding": {
        "x": {
          "field": "Source"
        },
        "y": {"aggregate": "count"},
        "color": {
          "field": "Source",
          "legend": {
            "values": {"expr":""}
          }
        }
      }
    }
  ],
  "resolve": {
    "legend": {
      "opacity": "independent",
      "color": "independent",
      "size": "independent"
      
    }
  }
}

The resulting graph can be seen here: You can use the online editor .生成的图表可以在这里看到:您可以使用在线编辑器

The desired behavior for this example would be to have the first graph's legend only display MPAA ratings and the seconds graph's legend only display source material.此示例的所需行为是让第一个图表的图例仅显示 MPAA 评级,第二个图表的图例仅显示源材料。 This is shown here: Image of desired legends此处显示:所需图例的图像

I have tried manually declaring which values to show in the legend using:我尝试使用以下方法手动声明要在图例中显示的值:

"legend": {
    "values": ['x', 'y', 'z']
}

But I really need a way to do it automatically但我真的需要一种自动完成的方法

I think your resolve just needs cleaning up.我认为你的决心只需要清理一下。

在此处输入图像描述

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "A dashboard with cross-highlighting.",
  "data": {"url": "data/movies.json"},

  "vconcat": [
    
    {
      "width": 400,
      "height": 150,
      "mark": "bar",
      "params": [
        {"name": "main", "select": {"type": "point", "encodings": ["x"]}}
      ],
      "encoding": {
        "x": {"field": "Major Genre", "axis": {"labelAngle": -40}},
        "y": {"aggregate": "count"},
        "color": {
          "condition": {"param": "main", "field": "MPAA Rating"},
          "value": "lightgrey"
        }
      }
    },
    {
      "width": 400,
      "height": 150,
      "transform": [{"filter": {"param": "main"}}],
      "mark": "bar",
      "params": [
        {"name": "i", "select": {"type": "point", "encodings": ["x"]}}
      ],
      "encoding": {
        "x": {"field": "MPAA Rating"},
        "y": {"aggregate": "count"},
        "color": {
          "condition": {"param": "i", "field": "Source"},
          "value": "lightgrey"
        }
      }
    },
    {
      "width": 400,
      "height": 150,
      "transform": [{"filter": {"param": "main"}}, {"filter": {"param": "i"}}],
      "mark": "bar",
      "encoding": {
        "x": {"field": "Source"},
        "y": {"aggregate": "count"},
        "color": {"field": "Source", "legend": false}
      }
    }
  ],
 "resolve": {"scale": {"color": "independent"}}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM