2017-02-27 67 views
0

我有這個小問題,我認爲這是關於異步消息。 當我嘗試在圖表上添加一個點時,它會添加數據的值而不是點。 你有想法嗎?角io高圖插座io

模塊==>

import { Component } from '@angular/core'; 
import * as io from 'socket.io-client'; 
@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'], 

}) 

export class AppComponent{ 
    url = 'http://localhost:5000'; 
    socket : any; 
    chart : any; 
    options: any; 
    v:any; 
    constructor() { 
    this.socket = io.connect(this.url); 
    this.options = { 
     title: { text : 'chart with dynamic data' }, 
     series: [{ 
      data: [2,3,5,8] 
     }] 
    } 
    this.getMessages(); 
} 

saveInstance(chartInstance) { 
    this.chart = chartInstance.context; 
} 
addPoint(n:any) { 
    this.chart.series[0].addPoint(n); 
} 
sendMessage(n:any){ 

    this.socket.emit('add-message',n);  
} 
getMessages() { 
    this.socket.on('message', (data) => { 
     console.log(data) 
     this.addPoint(data); 
    }) 
} 

}

HTML ==>

<chart [options]="options" (load)="saveInstance($event)"></chart> 
<input #v (keyup.enter)="sendMessage(v.value)"> 
+1

你的代碼是不完整的,它不能重現問題。如果將字符串/ undefined而不是數字添加爲點,則會發生此行爲。確保在使用addPoint()方法之前將字符串轉換爲數字/不發送空值 – morganfree

+0

完美! ParseInt()是這麼簡單的解決方案... – Nod

回答

1

parseInt函數()是正確的解決方案THX你morganfree