在Vega-Lite中动态设置最大域值

huangapple go评论73阅读模式
英文:

Dynamically Set A Max Domain in Vega-Lite

问题

I'm sorry, but it seems like you've provided a code snippet and some explanations related to a specific programming issue. I can provide translations, but it's important to note that translating code snippets and technical discussions can be challenging and may not always yield accurate results. If you have specific parts of this content that you would like me to translate, please specify, and I'll do my best to provide translations for those parts.

英文:

I´m trying to adopt a Population Pyramid code from https://vega.github.io/vega-lite/examples/concat_population_pyramid.html

In this code the limits of the x axis for male / female are evaluated separatly. Thus max. values for the x axis in both plots differ, which sometimes give an wrong impression of the distribution.

I´m trying to set both axis to the same max value ig the max value of the male/female dataset dynamically, because in my context I`m filtering the dataset via slicers.

current situation
how it should look like

I´ve tried to use joinaggregate and use the evaluated parameter to scale :

"$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"},
 { "joinaggregate": 
       [{
          "op": "sum",
          "field": "people",
          "as": "MaxVal"
       }]
      }
],

and used it in

"encoding": {
  "y": {
    "field": "age", "title": null,
    "axis": null, "sort": "descending"
  },
  "x": {
    "aggregate": "sum", "field": "people",
    "title": "population",
    "axis": {"format": "s"},
    "scale": {"domain": [0, "MaxVal"]},

  },

but it did´n work. the "domain" or domainMax must be a calculated value, not a constant.

Code:

{
  "$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"},
	 { "joinaggregate": 
       [{
          "op": "sum",
          "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": {
        "aggregate": "sum", "field": "people",
        "title": "population",
        "axis": {"format": "s"},
		"scale": {"domain": [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": {
        "aggregate": "sum", "field": "people",
        "title": "population",
        "axis": {"format": "s"},
		"scale": {"domain": [0, "MaxVal"]}
      },
      "color": {
        "field": "gender",
        "legend": null
      }
    }
  }],
  "config": {
    "view": {"stroke": null},
    "axis": {"grid": false}
  }
}

答案1

得分: 1

设置您的比例尺如下:

"scale": {"domainMax":{"expr":"data('source_0')[0].MaxVal"}},

但是,您的 MaxVal 计算是所有数字的总和,可能不是您想要的,但这是一个不同的问题。

{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"description": "2000 年美国的人口金字塔。",
"data": {"url": "data/population.json"},
"transform": [
{"filter": "datum.year == 2000"},
{"calculate": "datum.sex == 2 ? 'Female' : 'Male'", "as": "gender"},
{"joinaggregate": [{"op": "sum", "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": {
"aggregate": "sum",
"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": {
"aggregate": "sum",
"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}}
}

英文:

Set your scale to the following:

"scale": {"domainMax":{"expr":"data('source_0')[0].MaxVal"}},

However, your MaxVal calculation is the sum of all figures so probably not what you want but that is a different question.

{
  "$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"},
    {"joinaggregate": [{"op": "sum", "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": {
          "aggregate": "sum",
          "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": {
          "aggregate": "sum",
          "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}}
}

huangapple
  • 本文由 发表于 2023年7月13日 15:49:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定