简体   繁体   English

TradingView React 组件中的图表更新不起作用

[英]Chart update in TradingView React component doesn't work

Symbol of the chart is being selected in another component with an update of state which is passed back as a prop to this TradingView component.正在另一个组件中选择图表的符号,更新为 state,该更新作为 prop 传递回此 TradingView 组件。

I am trying to change the symbol in the chart with this:我试图用这个改变图表中的符号:

this.tvWidget.chart().setSymbol('BINANCE:' + this.props.selectedSymbol.name)

But where and how exactly should I place and use it?但是我应该在哪里以及如何放置和使用它呢? I am confused.我很迷惑。

I need to change the chart symbol, but in what method should it happen?我需要更改图表符号,但应该以什么方式更改? Thank you!谢谢!

import * as React from 'react';
import './index.css';
import {widget} from '../../charting_library/charting_library.min';
import {setSymbol} from "../../store/actions/symbols";
import {connect} from "react-redux";

class TradingView extends React.PureComponent {
    tvWidget = null;
    widgetOptions = {
        client_id: 'tradingview.com',
        user_id: 'public_user_id',
        datafeed: new window.Datafeeds.UDFCompatibleDatafeed('https://demo_feed.tradingview.com'),
        charts_storage_url: 'https://saveload.tradingview.com',
        symbol: "AAPL",
        symbol: this.props.selectedSymbol ? this.props.selectedSymbol.name : "BTCUSDT",
        snapshot_url: "https://www.tradingview.com/snapshot/",
        enabled_features: ['study_templates'],
        studies: ["RSI@tv-basicstudies", "StochasticRSI@tv-basicstudies", "MACD@tv-basicstudies"],
        watchlist: ["BINANCE:BTCUSDT"],
        disabled_features: ['use_localstorage_for_settings'],
        library_path: '/charting_library/',
        charts_storage_api_version: '1.1',
        container_id: 'tv_chart_container',
        debug: false,
        interval: 'D',
        theme: "Dark",
        allow_symbol_change: true,
        auto_save_delay: '5',
        range: "6m",
        hide_legend: true,
        locale: "en",
        timezone: "Europe/Berlin",
        autosize: true,
        enable_publishing: true,
    };
    componentDidMount() {
        this.tvWidget = new widget(this.widgetOptions);
        let tvWidget = this.tvWidget

        tvWidget.onChartReady(() => {
            tvWidget.headerReady().then(() => {
                let chart = tvWidget.chart();
                chart.onSymbolChanged().subscribe(null, onChartSymbolChanged);
            })
        })

        let setSymbol = this.props.setSymbol
        let symbols = this.props.symbols

        function onChartSymbolChanged() {
            let chart = tvWidget.chart();
            console.log("onChartSymbolChanged changing symbol to " + chart.symbol());
            setSymbol(symbol)         
        }
    }

    componentWillUnmount() {
        if (this.tvWidget !== null) {
            this.tvWidget.remove();
            this.tvWidget = null;
        }
    }

    render() {

        this.tvWidget && this.tvWidget.symbol ? this.tvWidget.chart().setSymbol('BINANCE:' + this.props.selectedSymbol.name) : null

        return (
            <div
                id="tv_chart_container"
                className={'TVChartContainer'}
            />
        );
    }
}

const mapStateToProps = state => {
    return {
        symbols: state.symbols.symbols,
        selectedSymbol: state.symbols.selectedSymbol
    };
};

const mapDispatchToProps = {setSymbol}
export default connect(mapStateToProps, mapDispatchToProps)(TradingView);

You can check the props by passing prevPops in componentDidUpdate(prevProps) {} and check the changes.您可以通过在componentDidUpdate(prevProps) {}中传递 prevPops 来检查道具并检查更改。

I made it work by removing the chart whenever the pair changes.我通过在货币对发生变化时删除图表来使其工作。 I don't know this the most optimized way or not.我不知道这是否是最优化的方式。

function TradingView(props) {
  const { pair } = props;
  const tv = useRef(null);

  useEffect(() => {
    if (tv.current) tv.current.remove();

    tv.current = new window.TradingView.widget({ symbol: pair });
  }, [pair]);

  return <div id="trading_view_chart" />;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM