Vega - rename layer/legend value names

Hello,

I'm trying to figure out how to rename my layer names from "_source.checks_total.policies_checks_result.passing" to just "Passing" and "_source.checks_total.policies_checks_result.failing" to just "Failing". I've searched but can't find any examples of this.

In the legend, it has the very long names which is not appealing.

Would anyone know how to do this by chance?

{
  $schema: https://vega.github.io/schema/vega-lite/v4.json
  title: Policy Checks
  "width": 400, "height": 100,
  "repeat": {
    "layer": ["_source.checks_total.policies_checks_result.passing", "_source.checks_total.policies_checks_result.failing"]
  },
  data: {
    url: {
      index: pan_bpa
      body: {
        "query": {
          "bool": {
            "filter": [
              {
                "bool": {
                  "should": [
                    {
                      "exists": {
                        "field": "vega.policies"
                      }
                    }
                  ],
                  "minimum_should_match": 1
                }
              }
            ]
          }
        },
        "size": 100,
        "sort": [
          {
            "@timestamp": {
            "order": "desc"
            }
          }
        ]
      }
    }
    format: {property: "hits.hits"}
  }
  "spec": {
    "mark": "line",
    "tooltip": true
    "encoding": {
      "x": {
        "field": "_source.@timestamp",
        "type": "ordinal",
        "timeUnit": "minutes",
        "title": "Timeframe"
        //"timeUnit": "day"
      },
      "y": {
        "aggregate": "policies.value",
        "field": {"repeat": "layer"},
        "type": "quantitative",
        "title": "Number of Checks"
      },
      "color": {
        "datum": {"repeat": "layer"},
        "type": "nominal"
      }
    }
  }
}

thanks!

Hi @rbergen,

What if you use scripted fields to rename your fields ("_source.checks_total.policies_checks_result.passing" to just "passing" etc)

To do that, follow the example from here: How to use scripted fields in vega/vega-lite visualizations?

Hope it helps!

Thank you @Marta_Bondyra!

I looked at the documentation and feel it is a bit above my head since I'm still pretty new to elastic. I have much to learn!

I'm not even sure where to start :joy:

No worries, I'll try to help you. Do you have to use Vega? I think your visualization is pretty straightforward and using Lens or Visualize might be much simpler for your case. Could you paste a screenshot of what you're trying to achieve?

You were right, in this particular case I don't think I have to use vega. I did for a different data set, because I couldn't use aggs for the data I needed. In this case, I think I can probably make it work for a multi-y axis line using aggs.

Not sure if you still need any guidance, but I think that Lens is perfect for your case, you just need to drag the fields to the proper places and change the type chart from default (bar) to line.

You can change chart type from bar to line by clicking on the setting above the chart.
You can change the colors and the labels in the legend by clicking on the buttons from the right side, for example 'Count of records' or 'Average of products.price' in this case.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.