Created
June 25, 2021 19:06
-
-
Save sayon-bitquery/0aaa2fa0f8314b4867c235787c5eb65a to your computer and use it in GitHub Desktop.
tradingview-2:12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react'; | |
import './index.css'; | |
import { widget } from '../../charting_library'; | |
import Datafeed from './datafeed'; | |
function getLanguageFromURL() { | |
const regex = new RegExp('[\\?&]lang=([^&#]*)'); | |
const results = regex.exec(window.location.search); | |
return results === null ? null : decodeURIComponent(results[1].replace(/\+/g, ' ')); | |
} | |
export class TVChartContainer extends React.PureComponent { | |
static defaultProps = { | |
symbol: "0xac51066d7bec65dc4589368da368b212745d63e8", | |
interval: '15', | |
containerId: 'tv_chart_container', | |
// datafeedUrl: 'https://demo_feed.tradingview.com', | |
libraryPath: '../../charting_library/', | |
chartsStorageUrl: 'https://saveload.tradingview.com', | |
chartsStorageApiVersion: '1.1', | |
clientId: 'tradingview.com', | |
userId: 'public_user_id', | |
fullscreen: false, | |
autosize: true | |
}; | |
tvWidget = null; | |
componentDidMount() { | |
const widgetOptions = { | |
symbol: this.props.symbol, | |
// BEWARE: no trailing slash is expected in feed URL | |
datafeed: Datafeed, | |
// datafeed: new window.Datafeeds.UDFCompatibleDatafeed(this.props.datafeedUrl), | |
interval: this.props.interval, | |
container_id: this.props.containerId, | |
library_path: this.props.libraryPath, | |
theme: 'Dark', | |
locale: getLanguageFromURL() || 'en', | |
disabled_features: ['use_localstorage_for_settings'], | |
enabled_features: ['study_templates'], | |
charts_storage_url: this.props.chartsStorageUrl, | |
charts_storage_api_version: this.props.chartsStorageApiVersion, | |
client_id: this.props.clientId, | |
user_id: this.props.userId, | |
fullscreen: this.props.fullscreen, | |
autosize: this.props.autosize, | |
studies_overrides: this.props.studiesOverrides, | |
}; | |
const tvWidget = new widget(widgetOptions); | |
this.tvWidget = tvWidget; | |
tvWidget.onChartReady(() => { | |
tvWidget.headerReady().then(() => { | |
const button = tvWidget.createButton(); | |
button.setAttribute('title', 'Click to show a notification popup'); | |
button.classList.add('apply-common-tooltip'); | |
button.addEventListener('click', () => tvWidget.showNoticeDialog({ | |
title: 'Notification', | |
body: 'TradingView Charting Library API works correctly', | |
callback: () => { | |
console.log('Noticed!'); | |
}, | |
})); | |
button.innerHTML = 'Check API'; | |
}); | |
}); | |
} | |
componentWillUnmount() { | |
if (this.tvWidget !== null) { | |
this.tvWidget.remove(); | |
this.tvWidget = null; | |
} | |
} | |
render() { | |
return ( | |
<div | |
id={ this.props.containerId } | |
className={ 'TVChartContainer' } | |
/> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment