wyrdforge

wyrdforge

Need help with creating a legend on a layered line chart with vegalite

Hello,

I am currently working on a prototype for evaluation of pressurized air consumption data
in livebook with vegalite. I got the following layered chart from my processed raw data, which works fine, in general.

All metrics are per day averages ,so the x-axis represents the timeline and the y-axis the interpolated range of the daily usage in m3/min. The lines represent summed up usage of two air suspension support lines and the used pressurized air for the main production lines.

VegaLite.new(width: 700, height: 400, title: "Vergleich Durchfluss zu Verbrauch")
|> VegaLite.data_from_values(normalized)
|> VegaLite.layers([
  VegaLite.new()
  |> VegaLite.transform(calculate: "datum.Suspension_VL2 + datum.Suspension_VL3", as: "Summe Durchfluss")
  |> VegaLite.mark(:line, color: "#2C73D2")
  |> VegaLite.encode_field(:x, "Startzeit", type: :temporal, title: "Tagesdatum")
  |> VegaLite.encode_field(:y, "Summe Durchfluss", type: :quantitative, title: "Summe Durchfluss"),
  VegaLite.new(width: 400, height: 200, title: "Summe Druchfluss Ringleitungen 1 + 2")
  |> VegaLite.transform(calculate: "datum.Druckluft_BA1_1 - datum.Druckluft_BA1_2 - datum.Instrumentenluft + 6", as: "Summe Verbrauch")
  |> VegaLite.mark(:line, color: "orange")
  |> VegaLite.encode_field(:x, "Startzeit", type: :temporal)
  |> VegaLite.encode_field(:y, "Summe Verbrauch", type: :quantitative, title: "Summe Verbrauch")
  ])

By default, this code renders no legend to the chart.
I tried to get the lineś field name with setting the color field, but using

VegaLite.encode_field(:color "Summe Verbrauch")

renders a huge legend with about 500 points, one for each metric.

Randomluy following some examples from the documentation led to distorted, beautifully multicolored graphs or unexpected results for me, like breaking the line colors in general or rendering only the first entry correctly.

Maybe anyone ca point me in the right direction for creating a legend with a single title for each line layer and the corresponding color as text color or symbol?

Kind regards,
Marcel

Marked As Solved

jonatanklosko

jonatanklosko

Creator of Livebook

@al2o3cr good find, and the corresponding code could be:

Vl.new(...)
|> ...
|> Vl.encode_field(:color, "label")
|> Vl.layers([
  Vl.new()
  |> Vl.transform(calculate: "'Label 1'", as: "label")
  |> ...,
  Vl.new()
  |> Vl.transform(calculate: "'Label 2'", as: "label")
  |> ...
])

Also Liked

al2o3cr

al2o3cr

This demo achieves what I think you’re looking for by calculate-ing a static value into each dataset. The important bit:

      "transform": [
        {
          "calculate": "\"foo\"",
          "as": "label"
        }
      ],
      "encoding": {
        "x": {"field": "x", "type": "quantitative"},
        "y": {"field": "y1", "type": "quantitative"},
        "color": {"field": "label"}
      },

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement