Search code examples
vue.jsnuxt.jslightweight-charts

Best way to use lightweight-charts package in nuxt project?


I want to use lightweight-charts package in Nuxt project. I couldn't find any examples included in the Nuxt project. I did it with some methods but I don't know what is the best method. What is the correct way to run it on nuxt?

The use of the package on the site is as follows.

import { createChart } from 'lightweight-charts';

const chart = createChart(document.body, { width: 400, height: 300 });
const lineSeries = chart.addLineSeries();
lineSeries.setData([
    { time: '2019-04-11', value: 80.01 },
    { time: '2019-04-12', value: 96.63 },
    { time: '2019-04-13', value: 76.64 },
    { time: '2019-04-14', value: 81.89 },
    { time: '2019-04-15', value: 74.43 },
    { time: '2019-04-16', value: 80.01 },
    { time: '2019-04-17', value: 96.63 },
    { time: '2019-04-18', value: 76.64 },
    { time: '2019-04-19', value: 81.89 },
    { time: '2019-04-20', value: 74.43 },
]);

https://github.com/tradingview/lightweight-charts


Solution

  • There is no example on this one because this is not related to Nuxt anyhow, neither really to Vue but more of a generic JS vanilla question.

    Few points:

    • it's great to import your component only when it's needed aka in your component (and not globally with Nuxt plugins), kudos for this one!
    • usually, if you want to call an external function, you do this into mounted(), it depends on what you're doing there of course
    • meanwhile, you should probably use $refs rather than document.body since we're in a state-based framework here
    • lineSeries.setData could maybe be moved to data and injected afterwards