你如何为 TradingView 的图表库实现 save() 和 load()
How do you implement save() and load() for TradingView's Charting Library
在尝试实现 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 时,我无法在加载从后端返回的对象时让库刷新屏幕。
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;
}