romenigld

romenigld

Installation of the uPlot with Phoenix 1.6.6 and Phoenix live view 0.17.5

I am doing a course from Poetcoding for doing an app with Cryptocurrency by WebSockets and showing them with Phoenix Live View.
But I am trying to do the course with the new version( v1.6.6 ) of Phoenix by myself.
And I am facing problems when I tried to install the uPlot Javascript.
I installed the uPlot with esbuild wich is a different way of the tutorial wich was used the webpack.
I will tell now the steps I tried to do for the installation of the uPlot:
1 - I go for the folder assets and run the npm install uplot:
npm i uplot --save

so I saw in my package.json this:

{
  "scripts": {
    "deploy": "cd .. && mix assets.deploy && rm -f _build/esbuild"
  },
  "dependencies": {
    "uplot": "^1.6.20"
  }
}

my package.lock.json:

{
  "name": "assets",
  "lockfileVersion": 2,
  "requires": true,
  "packages": {
    "": {
      "dependencies": {
        "uplot": "^1.6.20"
      }
    },
    "node_modules/uplot": {
      "version": "1.6.20",
      "resolved": "https://registry.npmjs.org/uplot/-/uplot-1.6.20.tgz",
      "integrity": "sha512-Jl4Z51Sns4xKLLQeBeiGdcgv4eW1UkKwukSTndIP3YcnlU4za9qGhejlX+XzRbvjaB32C0pxRsdq8m8Gwbq1Eg=="
    }
  },
  "dependencies": {
    "uplot": {
      "version": "1.6.20",
      "resolved": "https://registry.npmjs.org/uplot/-/uplot-1.6.20.tgz",
      "integrity": "sha512-Jl4Z51Sns4xKLLQeBeiGdcgv4eW1UkKwukSTndIP3YcnlU4za9qGhejlX+XzRbvjaB32C0pxRsdq8m8Gwbq1Eg=="
    }
  }
}

2 - I created the chart.js file and tried to do the logs for the mounted, beforeUpate, updated, etc.. and all was working very well. But when I tried to import the CSS and the Javascript of the uPlot my app begins to show a simple and white web interface like without the frontend, like don’t have CSS.

my chart.js file :

import _css from 'uplot/dist/uPlot.min.css'
import uPlot from "uplot"

let ChartHook = {
  mounted() {
    this.trades = [];
    this.plot = new uPlot(plotOptions(), [[], []], this.el);
  },
  updated() {
    let price = parseFloat(this.el.dataset.price),
      timestamp = parseInt(this.el.dataset.tradedAt)

    this.trades.push({
      timestamp: timestamp, price: price
    });

    if(this.trades.length > 20) {
      this.trades.splice(0, 1)
    }
    this.updateChart()
  },

  updateChart() {
    let x = this.trades.map(t => t.timestamp);
    let y = this.trades.map(t => t.price);
    this.plot.setData([x, y]);
  }
}

function plotOptions() {
  return {
    width: 200, height: 80, 
    class: "chart-container",
    cursor: {show: false}, 
    select: {show: false}, 
    legend: {show: false},
    scales: {},
    axes: [
      {show: false},
      {show: false}
    ],
    series: [
      {},
      {
        size: 0,
        width: 2,
        stroke: "white",
        fill: "rgb(45,85,150)",
      },
    ],
  };
}

export { ChartHook }

the chart component:

<div class="chart-component">
   <div phx-hook="Chart"
        id={ "product-chart-#{to_string(@product)}" }
        data-price={ @trade.price }
        data-traded-at={ DateTime.to_unix(@trade.traded_at, :millisecond) }
        phx-update="ignore"
    >
      <div class="chart-container"></div>
    </div>
 </div>

I am thinking it was the import of the CSS in the chart.js file because I saw something about the esbuild to not put the import of the app.css file in the app.js for something.
I would like to have a help for this. What I need to do for continue working on this project?
I need to do the steps for install the uPlot in a different way?
My app on github

Marked As Solved

ChristianAlexander

ChristianAlexander

Since esbuild is not responsible for the CSS in this project, the import has to take place from within an scss file, such as app.scss.

I was able to get this working in the project by adding --load-path=../assets/node_modules to the dart_sass config’s args property in config.exs, like this:

config :dart_sass,
  version: "1.49.11",
  default: [
    args: ~w(css/app.scss ../priv/static/assets/app.css --load-path=../assets/node_modules),
    cd: Path.expand("../assets", __DIR__)
  ]

Followed by an import of the css at the top of app.scss:

@use "uplot/dist/uPlot.min.css";
@import "./phoenix.scss";
@import "./poeticoins.scss";

I don’t know why it has to be an @use, but @import did not work.

In the end, the charts render like this:

Hopefully this helps!

Also Liked

romenigld

romenigld

This worked, thank you very much @ChristianAlexander!
I think this @import not worked because the file of the uPlot is a CSS file and before this problem I installed the DartSass and it was needed to change the extension .css for .scss of the @import phoenix.scss file to work.

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
_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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
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
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

Other popular topics Top

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 48475 226
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
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
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
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 31194 112
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 43806 214
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
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52774 488
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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

We're in Beta

About us Mission Statement