【问题标题】:How do you implement save() and load() for TradingView's Charting Library如何为 TradingView 的图表库实现 save() 和 load()
【发布时间】:2021-03-18 12:53:51
【问题描述】:

在尝试实现 TradingView 的保存和加载低级 API 时,在加载从后端返回的对象时,我无法让库刷新屏幕。

load() 只会导致屏幕闪烁,没有错误。图表不会更新。

如何从 Angular 前端的 TradingView 小部件保存和加载数据?

【问题讨论】:

    标签: tradingview-api


    【解决方案1】:

    我可以确认 save() 和 load() 在我的项目中正常工作。需要对 Types 进行一些大惊小怪才能使其正常工作。我相信有人会看着这堆东西而绝望——但它确实有效。

    所以对于其他想要测试的人:

    tvWidget.load(savedObject)
    tvWidget.save((saveObject) => this.chartService.saveCurrentChart(saveObject))
    

    我在 tvWidget 标题中创建了一个保存按钮:

        const saveButton = tvWidget.createButton();
        saveButton.setAttribute('title', 'Click to Save Chart');
        saveButton.classList.add('apply-common-tooltip');
        saveButton.addEventListener('click', () => tvWidget.save((saveObject) => this.chartService.saveCurrentChart(saveObject, tvWidget)))
        saveButton.innerHTML = 'Save Chart';
    

    在 tvWidget 标题中还有一个加载按钮:

        createDrawingButton(tvWidget: IChartingLibraryWidget) {
        const loadButton = tvWidget.createButton();
        loadButton.setAttribute('title', 'Click to Load Chart');
        loadButton.classList.add('apply-common-tooltip');
        loadButton.addEventListener('click', () => tvWidget.load(this.chartService.loadSavedChart()));
        loadButton.innerHTML = 'Load from Backend';
    

    这是我的 TypeScript 界面:

    export interface APIChartData {
        ltChartId?: number | undefined
            tvChartId?: string | undefined
        chartObject: Object
            lastUpdate?: number | undefined
    }
    

    这是我的保存功能:

    saveCurrentChart(tvChartObject: any) {
    
    let chartID = tvChartObject['charts'][0]['panes'][0]['sources'][0]['id'];
    let chartToSave: APIChartData = {
          ltChartId: this._currentChart?.ltChartId,
          tvChartId: chartID,
          chartObject: tvChartObject,
          lastUpdate: Math.floor(Date.now() / 1000),
        };
    
        this.postChart(chartToSave).subscribe((response) => {
    
          console.log("Response from Server")
          console.log(response.ltChartId)
          this._savedChartId =  response.ltChartId
        });
    
        console.log(`Saved Chart Object`);
      }
    
    private postChart(saveChart: APIChartData): Observable<ChartStoreResponse> {
        console.log('Trying to save...');
        const httpOptions = {
          headers: new HttpHeaders({
            'Content-Type': 'application/json',
            accept: 'application/json',
            Authorization: this._oAuthTokent,
          }),
        };
    

    这是我的加载函数:

    loadSavedChart(chartID:number = this._savedChartId): Object {
    
        console.log('Trying to load...');
        const httpOptions = {
          headers: new HttpHeaders({
            'Content-Type': 'application/json',
            accept: 'application/json',
            Authorization: this._oAuthTokent,
          }),
    
          params: new HttpParams().set('chart', chartID.toString()),
        };
    
        const httpParams = {};
    
        // My "Backend" in a docker image locally running FASTAPI
        const LoadURL = 'http://localhost/api/v1/tradingview';
    
        let chartObject: Object = {};
    
        let loadChart$ = this.http
          .get<APIChartData>(LoadURL, httpOptions)
          .pipe(tap(() => console.log('Loaded the goods.')));
    
        loadChart$.subscribe((object) => {
    
          this._chartFromDisk = object.chartObject;
          console.log(`Chart ${chartID} from Server Store:`)
          console.log(object.chartObject);
    
        });
    
        return this._chartFromDisk;
      }
    

    【讨论】:

    • 什么是tradingview版本?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多