Search code examples
kibanavega-litevega

Dynamic Chart Title From Data Values


I want to take the title of my vega-chart directly from the data which are base of the chart. For example the title of this chart should be 'charttitle':

{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"values": [
{"quater":"1", "sales":"100", "title":"charttitle"},
{"quater":"2", "sales":"200", "title":"charttitle"},
{"quater":"3", "sales":"300", "title":"charttitle"},
{"quater":"4", "sales":"400", "title":"charttitle"}
]
},

"mark": "bar",
"encoding": {
"x": {"field": "quater", "type":"ordinal"},
"y": {"field": "sales", "type": "quantitative"},
},
"title": ???
}

In pseudo code it could look like this:

"title": (select title from data limit 1); or in standard sql

"title": (select top 1 title from data)

but how can i achieve that in vega?


Solution

  • enter image description here

    {
      "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
      "data": {
        "values": [
          {"quater": "1", "sales": "100", "title": "charttitle"},
          {"quater": "2", "sales": "200", "title": "charttitle"},
          {"quater": "3", "sales": "300", "title": "charttitle"},
          {"quater": "4", "sales": "400", "title": "charttitle"}
        ]
      },
      "mark": "bar",
      "encoding": {
        "x": {"field": "quater", "type": "ordinal"},
        "y": {"field": "sales", "type": "quantitative"}
      },
      "title": {"text": {"expr": "data('source_0')[0].title"}}
    }