Search code examples
powerbivisualizationpowerbi-desktopvega-litedeneb

setting domainMax by determining the max of filtered datasets


I´m using the Deneb PowerBi visual, displaying a Population Pyramid for male / female

currently I´m defining the maxdomain of the axis by aggregating the whole (male+femal) numbers, which works, but isnt optimal:

current implementation

I´d like to set the max domain by determining the max from male or female, so the available display space is optimal used

{
  "data": {"name": "dataset"},
  "transform": 
    [
        {
          "calculate": "datum.Geschlecht == 'männlich' ? 'männlich' : 'weiblich'",
          "as": "gender"
        },
        {
          "joinaggregate": [{
      "op": "sum",
      "field": "KPI-Set Display",
      "as": "MaxVal"
    }]
        }
      
    ],

  
    
    
"spacing": 0,
  "hconcat": 
    [
  
      {
        
        "transform": 
            [
                    {
                        "filter": 
                          {
                            "field": "gender",
                            "equal": "weiblich"
                          }
                    }
                ],
        
              "title": "",
        "mark": 
            {
              "type": "bar",
              "tooltip": true
            },
              
              
            "encoding": 
                {
                        "y": 
                          {
                            "field": "Altersgruppe",
                            "axis": null,
                            "sort":
                              {
                                 "field":"Sortierung",
                                 "order":"descending"
                      }
                          },
                        "x": 
                          {
                            "aggregate": "sum",
                            "field": "KPI-Set Display",
                            "title": "weiblich & unbekannt",
                            "scale": {"domainMax":{"expr":"data('data_0')[0].MaxVal"}},
                            "axis": 
                              {
                                "format": "s"
                              },
                              "sort": "descending"
                          }
                }
      },
      
      
        {
            "width": 20,
            "view": 
                {
                    "stroke": null
                },
            "mark": 
                {
                    "type": "text",
                    "align": "center"
                },
            "encoding": 
                {
                    "y": 
                        {
                            "field": "Altersgruppe",
                            "type": "ordinal",
                            "axis": null,
                            "sort":
                              {
                                 "field":"Sortierung",
                                 "order":"descending"
                      }
                        },
                    "text": 
                        {
                            "field": "Altersgruppe",
                            "type": "nominal"
                        }
                }
        },        
        
      
           
      
      {
        "transform": 
            [
                    {
                        "filter": 
                          {
                            "field": "gender",
                            "equal": "männlich"
                          }
                    }
                ],
        
              "title": "",
        "mark": 
            {
              "type": "bar",
              "tooltip": true
            },       
            "encoding": 
                {
                        "y": 
                          {
                            "field": "Altersgruppe",
                            "axis": null,
                            "sort":
                              {
                                 "field":"Sortierung",
                                 "order":"descending"
                      }
                          },
                        "x": 
                          {
                            "aggregate": "sum",
                            "field": "KPI-Set Display",
                            "title": "männlich",
                            "scale": {"domainMax":{"expr":"data('data_0')[0].MaxVal"}},
                            "axis": 
                              {
                                "format": "s"
                              },
                              "sort": "ascending"
                          }
                }        
      }
  
  
    ]
  
  
  
}

Solution

  • Is this what you're after?

    enter image description here

    {
      "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
      "description": "A population pyramid for the US in 2000.",
      "data": {"url": "data/population.json"},
      "transform": [
        {"filter": "datum.year == 2000"},
        {"calculate": "datum.sex == 2 ? 'Female' : 'Male'", "as": "gender"},
        {
          "aggregate": [{"op": "sum", "field": "people", "as": "people"}],
          "groupby": ["age", "gender"]
        },
        {"joinaggregate": [{"op": "max", "field": "people", "as": "MaxVal"}]}
      ],
      "spacing": 0,
      "hconcat": [
        {
          "transform": [{"filter": {"field": "gender", "equal": "Female"}}],
          "title": "Female",
          "mark": "bar",
          "encoding": {
            "y": {"field": "age", "axis": null, "sort": "descending"},
            "x": {
              "type": "quantitative",
              "field": "people",
              "title": "population",
              "axis": {"format": "s"},
              "scale": {"domainMax": {"expr": "data('source_0')[0].MaxVal"}},
              "sort": "descending"
            },
            "color": {
              "field": "gender",
              "scale": {"range": ["#675193", "#ca8861"]},
              "legend": null
            }
          }
        },
        {
          "width": 20,
          "view": {"stroke": null},
          "mark": {"type": "text", "align": "center"},
          "encoding": {
            "y": {
              "field": "age",
              "type": "ordinal",
              "axis": null,
              "sort": "descending"
            },
            "text": {"field": "age", "type": "quantitative"}
          }
        },
        {
          "transform": [{"filter": {"field": "gender", "equal": "Male"}}],
          "title": "Male",
          "mark": "bar",
          "encoding": {
            "y": {
              "field": "age",
              "title": null,
              "axis": null,
              "sort": "descending"
            },
            "x": {
              "type": "quantitative",
              "field": "people",
              "title": "population",
              "axis": {"format": "s"},
              "scale": {"domainMax": {"expr": "data('source_0')[0].MaxVal"}}
            },
            "color": {"field": "gender", "legend": null}
          }
        }
      ],
      "config": {"view": {"stroke": null}, "axis": {"grid": false}}
    }