I was trying to add timeline graphs to my current application.
The application uses Surface UI 0.6.1 and LiveView 0.16.x.
Throughout the application I made use of JS Hooks every now and then, e.g. to draw or update markers on a Leaflet Map and all worked very well so far.
Now I am running into 2 problems:
- I cannot figure out how to add vega lite to my phoenix project. I followed the Third-party JS packages
section from GitHub - phoenixframework/esbuild: An installer for esbuild, as in runningnpm install
in the assets directory. However, I cannot see any files being added anywhere, but the compiler is also not complaining onimport vegaEmbed from 'vega-embed'
. Where are the packages located? - It seems like my Hooks do not work, but I do not see a difference to the Hooks I used before. Here is the code, the
console.log
statement is not showing in the console output in Chrome (always using “empty cache and hard reload” when reloading the page):
JS
import vegaEmbed from 'vega-embed'
const XyDiagram = {
mounted() {
this.handleEvent("draw", ({spec}) => {
console.log("XY Diagram Hook")
vegaEmbed(this.el, spec)
.then((result) => result.view)
.catch((error) => console.error(error))
})
},
}
export {XyDiagram}
EX
defmodule Components.XyDiagram do
use Surface.LiveComponent
require Logger
prop data, :list, required: true
prop height, :decimal, default: 200
prop value_key, :atom, required: true
@impl true
def update(assigns, socket) do
data =
Enum.map(assigns.data, & %{x: &1.timestamp, y: &1[assigns.value_key]})
spec =
VegaLite.new(title: "Title", width: :container, height: :container, padding: 5)
|> VegaLite.data_from_values(data)
|> VegaLite.mark(:line)
|> VegaLite.encode_field(:x, "x", type: :temporal)
|> VegaLite.encode_field(:y, "y", type: :quantitative)
|> VegaLite.to_spec()
|> IO.inspect() # print looks good here :)
{:ok, push_event(socket, "draw", %{"spec" => spec})}
end
@impl true
def render(assigns) do
~H"""
<div style={"width:100%; height: 300px"} id="graph" :hook="XyDiagram" phx-update="ignore" />
"""
end
end
I am a bit lost and do not know what else to try or search for.
Can you spot what I am doing wrong here?