Valeriy

Valeriy

How to use formatter function in ApexCharts with Phoenix heex

I use the Apexcharts JS library to draw graphs in my Phoenix application. The Apexchart integration is done according to this example.

I want to use custom tooltips as in the example from Apexcharts docs:

tooltip: {
  y: {
    formatter: function(value, { series, seriesIndex, dataPointIndex, w }) {
       return value
     }
  }
 }

But I don’t know how to call the js function inside heex. I tried it like this:

<.chart
   id="chart1"
   chartset={%{
     chart: %{
       type: "line",
       height: 450,
       toolbar: %{
         show: false
       }
      },
     series: [%{
       name: "CPU usage",
       data: @series
     }],
     xaxis: %{
       categories: @categories,
     },
     yaxis: %{
       title: %{
         text: "Average CPU usage, %",
       },
       min: 0,
       max: 100
     },
     tooltip: %{
       y: %{
         formatter: function(value, { series, seriesIndex, dataPointIndex, w }) {
           return value
         }
       }
     }
   }}
 />

and got the following error:

error: syntax error before: '{'
    │
 47 │ ...  formatter: function(value, { series, seriesIndex, dataPointIndex, w }) {
    │                                                                             ^
    │
    └─ lib/monitor_web/live/statistic_live/show_cpu.html.heex:47:100

Does anyone have any ideas? (it works without tooltip section)

First Post!

cmo

cmo

In HEEX templates and Phoenix Components you’re on the Elixir side. You can’t write JavaScript in Elixir*. Presumably, those attrs on chart are getting sent as JSON to your hook, which configures the chart. One thing you can do is have the formatter function defined in your hook and pass just the args that it needs from the back-end to the front-end.

e.g if you wanted to format axis labels in a specific timezone you can send the timezone to the hook (this is for a different charting lib). Thetimezone arg comes from the backend.

          // if timezone is provided, we assume the x-axis represents time and display it in said timezone
          if (timezone) {
            this.chart.setOption({
              xAxis: {
                axisLabel: {
                  formatter: (date) => datefns.formatInTimeZone(date, timezone, formatString || "HH:mm")
                }
              }
            })
          }

Maybe you could pass a function from the backend to the front-end as a string and eval it in the hook, but there may be dragons there.

* You could write a sigil that understands JS and you can write script tags in your templates.

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
New

Other popular topics Top

Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29703 241
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement