While our graphs have been things of beauty, the labels in the top left of our charts aren’t as neat as they could be.
For instance, in the previous example we get q:countriesAndTerritories: United_Kingdom > sum(cases) as a label.
We can fix this, but we’re going to need regular expressions.
At its simplest, we can append .label("LABEL_TEXT") to the expression in question to label that part of it.
For example, to create a label “UK Cases” to a graph we’ve created earlier, it’s not much more of a jump to write .es(index=covid, q="countriesAndTerritories: United_Kingdom”, metric=sum:cases).label("UK Cases").
We’re merely adding the .label() function to it.
Things get complicated if we want to create a label dynamically based on what the type of data we’re displaying.
To show the number of cases per continent, we can split out the data by the continentExp field and write .es(index=covid, split=continentExp:5, metric=sum:cases)
For Europe for example, this leaves us with q:* > continentExp:Europe > sum(cases) in the top left of our chart.
We want to capture the continent from this and set it to be the label for the Europe line.
To do this we can write .es(index=covid, split=continentExp:5, metric=sum:cases).label("$1”, “^.* > continentExp:(\S+) > .*").
This extracts everything that’s not a space character between continentExp: and > and dynamically sets it as the label for that section of the graph.
With a regular expression we can capture a match into the $1 variable by surrounding it with parentheses. ^ means the start of the line, \S corresponds to everything that’s not a space character, the + matches between one and unlimited times and .* is a wildcard greedy match for anything.
While we’re on the subject of labelling, if you wanted to give your graph a title, you can easily do this by adding .title(GRAPH_TITLE) to the end of your expression as well.
Current page: Refining labelsPrev Page Analysis...continued Next Page Making use of offsets
Are you a pro? Subscribe to our newsletter
Sign up to the TechRadar Pro newsletter to get all the top news, opinion, features and guidance your business needs to succeed!