I am developing app in Angular 8 and displaying 'scatter' plot using angular-plotly.js. For data in x,y I am getting data from rest api. And I am able to plot graph using the data from rest api. On frontend I have a two dropdown from where user can select option and send those options value to backend and in response I get new data back to frontend. Till this point everything works fine, but the issue I am facing is that my plot is not getting updated with new data. Frontend still showing the initial plot not the new plot with different data value.
My Html code:
Here is my two dropdown from where user select options and based on this options from backend through restapi I receive data points to plot my graph. And when user changes any option from dropdown and submit new choice to backend I again receive new data point and I need to remove my old plot and display new plot based on new dataset.
<form (ngSubmit)="segmentSelection()" #ff="ngForm">
<div id="userSelection" ngModelGroup="userData" #userData="ngModelGroup">
<mat-form-field>
<mat-label>Choose Segment Key</mat-label>
<mat-select id="selectme" ngModel name="segmentKey">
<mat-option *ngFor="let segment of segKeys" [value]="segment">
{{segment}}
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field style="margin-left: 30px;">
<mat-label>Choose Target Variable</mat-label>
<mat-select id="myselect" ngModel name="target_variable">
<mat-option *ngFor="let msg of mymessage" [value]="msg">
{{msg}}
</mat-option>
</mat-select>
</mat-form-field>
<button type="submit" class="btn btn-success btn-color" (click)="plotDiv()" style="margin-left: 20px;">Submit</button>
</div>
</form>
<plotly-plot [data]="graph.data" [layout]="graph.layout"></plotly-plot>
My component.ts code:
myhistoricPlot = []
myhistoricDate = []
segmentSelection(){
this.plotselection.segmentKey = this.segmentData.value.userData.segmentKey;
this.plotselection.target_variable = this.segmentData.value.userData.target_variable;
console.log(this.segmentData);
fetch("http://localhost:5000/data-decomposition", {
method: "POST",
headers: {
"Content-Type": "application/json"
},body: JSON.stringify({
segmentKey: this.segmentData.value.userData.segmentKey,
target_variable: this.segmentData.value.userData.target_variable
})
}).then(res => res.json()
// console.log(res);
// console.log('hello saheb')
// console.log(res.json())
).then(myjson => {
myjson['Data'].forEach(element =>{
this.myhistoricPlot.push(element)
})
myjson['Date'].forEach(element =>{
this.myhistoricDate.push(element)
})
// console.log(this.myhistoricPlot)})
// this.myhistoricDate = myjson['Date'];
// this.myhistoricPlot = myjson['Data'];
console.log(this.myhistoricDate);
console.log(this.myhistoricPlot);
console.log(myjson)
this.myhistoricPlot = [];
this.myhistoricDate = [];
})
}
public graph = {
data: [
{ x: this.myhistoricDate, y: this.myhistoricPlot, type: 'scatter', mode: 'lines+points', marker: {color: 'black'} },
],
layout: {width: 1200, height: 600, title: 'Historical Actuals Plot'}
};
****Note: myhistoricDate, myhistoricPlot are the two list with some numerical values, in which I am receiving data from restapi.
Thank you....your help on this issue would be highly appreciated