gpt4 book ai didi

tradingview-api - 你如何为 TradingView 的图表库实现 save() 和 load()

转载 作者:行者123 更新时间:2023-12-02 18:51:31 25 4
gpt4 key购买 nike

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

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

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

最佳答案

我可以确认 save() 和 load() 在我的项目上正常工作。对 Types 进行了一番操心才让它工作起来。我相信有人会看着这堆东西感到绝望 - 但它确实有效。

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

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

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

    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 header 中有一个加载按钮:

    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-api - 你如何为 TradingView 的图表库实现 save() 和 load(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66691491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com