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

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New

Other popular topics Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31013 112
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement