Snake-Sanders

Snake-Sanders

Latency re-rendering a linear chart, with Chart.js and LiveView

Environment

  • Chart.js v3.9.1
  • :phoenix, “~> 1.6.11”
  • :phoenix_live_view, “~> 0.17.5”
  • Elixir 1.13.4

The picture above shows what I am working on. It is a line chart I have drawn with Chart.js the latest greatest.

First I describe the issue and then below I will describe what I am doing.

The Issue

When I click on the button to add a new dot, the diagram shifts to the left (*t1) and after a second (*t2) the new dot is shown. The delay is so big that if I schedule an automatic update of 1 second period, the diagram will start shifting but no dot will be added to the chart, ending up in an empty chart after all the dots are dropped on the left hand side.

Detailed description

You might be familiar with this example if you have done the “Phoenix LiveView” course from Pragmatic Studio.
Here it goes, when I click on the refresh button, Phoenix Live View will send an event to the client to add a new point to the chart.

push_event(socket, "new-point", point)

On the client, there is a JavaScript Hook that handles the event and adds a dot to the chart.

On the mount function a handler is hooked for adding new points.

        this.handleEvent("new-point", ({label, value}) => {
            this.chart.addPoint(label, value)
        })

The function that adds the point is below:

  addPoint(label, value) {
    const labels = this.chart.data.labels;
    const data = this.chart.data.datasets[0].data;

    labels.push(label);
    data.push(value);

    if (data.length > 12) {
      data.shift();
      labels.shift();
    }

    this.chart.update();
  }

I measured the addPoint function and it is executed in less than 8 milliseconds, I see the chart be shifted to the left and after approximately 1 second the new dot appears in the chart. I commented out the rest of the hooks I added for the other pages to make sure they did not interfere with the drawing, but I don’t know what is causing this delay.

In addition, the canvas is wrapped in a div so phoenix ignores rendering changes phx-update="ignore".

Where else this delay can come from?

Thank you!

Marked As Solved

Nicd

Nicd

Did you try preventing animation of the Chart to see if that has any effect? I.e. this.chart.update("none");

I use Chart.js with LV (example: assets/js/frontend/profile/graphs/last-weeks.component.js · ea44484f605c905060e1973a10d8ef53daa43d90 · CodeStats / code-stats · GitLab) and I’ve had no such delay issue (though I transfer my data in a different way).

Last Post!

Snake-Sanders

Snake-Sanders

This solved the problem. Thank you so much

Where Next?

Popular in Questions Top

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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement