Search code examples
powerbivisualizationpowerbi-desktopvega-litedeneb

Clarification on setting a dynamic domain in Vega-lite (Deneb)


In reference to this post

Setting a dynamic domain in Vega-Lite (Deneb)

I would like to understand expressions in the below lines of code

"params":   
[
{"name": "max", "expr": "data('data_0')[0]['max']"},
{"name": "min", "expr": "data('data_0)[0]['min']"}
]

I tried to implement the same code and tried to make changes but it doesn't work. The y-axis doesn't get plotted.

MaxRange and MinRange showing values as 'undefined'

Lines overlapped with Y -axis showing blank/null values.

{
  "data": {"name": "dataset"},
  "title": "All Points",
  "transform": [
    {
      "joinaggregate": [
        {
          "op": "max",
          "field": "dollar_price",
          "as": "_max"
        },
        {
          "op": "min",
          "field": "dollar_price",
          "as": "_min"
        }
      ]
    }
  ],
  "params": [
    {
      "name": "Max_Range",
      "expr": "data('dataset')[0]['_max']"
    },
    {
      "name": "Min_Range",
      "expr": "data('dataset')[0]['_min']"
    }
  ],
  "encoding": {
    "x": {
      "field": "date",
      "type": "temporal"
    },
    "y": {
      "field": "dollar_price",
      "type": "quantitative",
      "scale": {
        "domain": {
          "expr": "[Min_Range,Max_Range]"
        }
      }
    },
    "color": {
      "field": "country_name",
      "type": "nominal"
    }
  },
  "layer": [
    {"mark": "line"},
    {
      "mark": {
        "type": "point",
        "size": 20
      }
    },
    {
      "mark": {
        "type": "text",
        "align": "left",
        "baseline": "middle",
        "dy": -10
      },
      "transform": [
        {
          "calculate": "format(datum.dollar_price,'.1f')",
          "as": "New_Text"
        }
      ],
      "encoding": {
        "text": {
          "field": "New_Text",
          "type": "quantitative"
        }
      }
    }
  ]
}

Solution

  • VL does various things with the original dataset. Try this instead:

    "expr": "data('data_0')[0]['_max']"

    enter image description here

    {
      "data": {"name": "dataset"},
      "title": "All Points",
      "transform": [
        {
          "joinaggregate": [
            {
              "op": "max",
              "field": "dollar_price",
              "as": "_max"
            },
            {
              "op": "min",
              "field": "dollar_price",
              "as": "_min"
            }
          ]
        }
      ],
      "params": [
        {
          "name": "Max_Range",
          "expr": "data('data_0')[0]['_max']"
        },
        {
          "name": "Min_Range",
          "expr": "data('data_0')[0]['_min']"
        }
      ],
      "encoding": {
        "x": {
          "field": "date",
          "type": "temporal"
        },
        "y": {
          "field": "dollar_price",
          "type": "quantitative",
          "scale": {
            "domain": {
              "expr": "[Min_Range,Max_Range]"
            }
          }
        },
        "color": {
          "field": "country_name",
          "type": "nominal"
        }
      },
      "layer": [
        {"mark": "line"},
        {
          "mark": {
            "type": "point",
            "size": 20
          }
        },
        {
          "mark": {
            "type": "text",
            "align": "left",
            "baseline": "middle",
            "dy": -10
          },
          "transform": [
            {
              "calculate": "format(datum.dollar_price,'.1f')",
              "as": "New_Text"
            }
          ],
          "encoding": {
            "text": {
              "field": "New_Text",
              "type": "quantitative"
            }
          }
        }
      ]
    }