javascript 如何设置tradingview图表库图表自动更新?

rdrgkggo  于 2023-04-04  发布在  Java
关注(0)|答案(1)|浏览(312)

我正在使用Tradingview图表库设置一个显示图表的网站,并设法设置图表从数据馈送显示。然而,一旦图表加载,图表不会自动更新或刷新较新的数据,而无需重新加载网页。我如何设置图表自动更新(例如间隔1米,5米等)?这是我使用的代码:

function initOnReady() {
        var widget = window.tvWidget = new TradingView.widget({
            // debug: true, // uncomment this line to see Library errors and warnings in the 

            fullscreen: true,
            symbol: 'AAPL',
            interval: '1D',
            container_id: "tv_chart_container",

            //  BEWARE: no trailing slash is expected in feed URL
            datafeed: new Datafeeds.UDFCompatibleDatafeed("<data feed url>"),
            library_path: "charting_library/",
            locale: getParameterByName('lang') || "en",

            disabled_features: ["use_localstorage_for_settings"],

            enabled_features: ["study_templates"],
            charts_storage_url: 'https://saveload.tradingview.com',
            charts_storage_url: 'http://{$smarty.server.HTTP_HOST}',
            charts_storage_api_version: "1.1",
            client_id: 'tradingview.com',
            user_id: 'public_user_id',
        });

    };

提前感谢并感谢帮助。

deyfvvtc

deyfvvtc1#

创建名为datafeed的文件,如下所示:

export default {
  onReady: (callback) => {
    console.log("[onReady]: Method call");
    callback({});
  },
  searchSymbols: (userInput, exchange, symbolType, onResultReadyCallback) => {
    console.log("[searchSymbols]: Method call");
  },
  resolveSymbol: (
    symbolName,
    onSymbolResolvedCallback,
    onResolveErrorCallback
  ) => {
    console.log("[resolveSymbol]: Method call", symbolName);
  },
  getBars: async (
    symbolInfo,
    resolution,
    from,
    to,
    onHistoryCallback,
    onErrorCallback,
    firstDataRequest
  ) => {
   
  },
  subscribeBars: (
    symbolInfo,
    resolution,
    onRealtimeCallback,
    subscribeUID,
    onResetCacheNeededCallback
  ) => {
    console.log(
      "[subscribeBars]: Method call with subscribeUID:",
      subscribeUID
    );
  },
  unsubscribeBars: (subscriberUID) => {
    console.log(
      "[unsubscribeBars]: Method call with subscriberUID:",
      subscriberUID
    );
  },
};

并将其替换为数据源:

import DATAFEED from './datafeed';

function initOnReady() {
        var widget = window.tvWidget = new TradingView.widget({
            // debug: true, // uncomment this line to see Library errors and warnings in the 

            fullscreen: true,
            symbol: 'AAPL',
            interval: '1D',
            container_id: "tv_chart_container",

            //  BEWARE: no trailing slash is expected in feed URL

            datafeed: DATAFEED, // ---> replace here
            library_path: "charting_library/",
            locale: getParameterByName('lang') || "en",

            disabled_features: ["use_localstorage_for_settings"],

            enabled_features: ["study_templates"],
            charts_storage_url: 'https://saveload.tradingview.com',
            charts_storage_url: 'http://{$smarty.server.HTTP_HOST}',
            charts_storage_api_version: "1.1",
            client_id: 'tradingview.com',
            user_id: 'public_user_id',
        });

    };

注意:交易视图本身根据需要管理大多数操作。例如,如果你想拖动蜡烛图,交易视图计算视口并找出它需要显示多少蜡烛,然后在datafeeds.js中调用getBars方法。
参见示例:https://github.com/tradingview/charting-library-examples

相关问题