【问题标题】:Lightweight-charts doesn't show on html page轻量级图表不显示在 html 页面上
【发布时间】:2021-07-27 20:37:57
【问题描述】:

我正在尝试使用 Lightweight-charts-library 在我的网页上使用图表,但图表不会显示。 这是我的 HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type = "module" src="https://unpkg.com/lightweight-charts/dist/lightweight-charts.standalone.production.js"></script>
    <title>Socket Streams</title>
</head>

<body>
    <h1>Trades</h1>

    <div id ="chart"></div>
    <div id ="trades"></div>



    <script>
        var binanceSockets = new WebSocket("wss://stream.binance.com:9443/ws/dogebtc@trade")
        var tradeDiv = document.getElementById('trades');

        binanceSockets.onmessage = function (event){
            console.log(event.data);
            var object = JSON.parse(event.data);
            tradeDiv.append(object.p);
        }   
         
    </script>


    <script src="chart.js" type="module"></script>
    
</body>
</html>

我的目标是在“图表”div 上显示一个图表,所以我基本上从 Lightweight-charts 库中复制粘贴了这段代码,使其指向图表。

import { createChart } from 'lightweight-charts';

const chart = createChart(document.getElementById("chart"), { 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 },
]);

如果我从&lt;script src="chart.js"&gt;&lt;/script&gt;中删除type = module,我会得到Uncaught SyntaxError: Cannot use import statement outside a module。 javascript的文件名是charts.js

【问题讨论】:

    标签: javascript html charts lightweight-charts


    【解决方案1】:

    我假设你没有使用 webpack 或任何捆绑器。

    要考虑module,您需要以不同方式导入库,并按照chart.js 脚本中的import 'https://unpkg.com/lightweight-charts@latest/dist/lightweight-charts.standalone.production.js'; 进行操作,然后按照doc 中的建议使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多