Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 只有第一个添加值显示在ng2图表折线图中_Javascript_Angular_Typescript_Chart.js_Ng2 Charts - Fatal编程技术网

Javascript 只有第一个添加值显示在ng2图表折线图中

Javascript 只有第一个添加值显示在ng2图表折线图中,javascript,angular,typescript,chart.js,ng2-charts,Javascript,Angular,Typescript,Chart.js,Ng2 Charts,我有一个非常简单的折线图,当用户单击按钮时,我想在其中添加值。但无论出于何种原因,只会显示第一个添加的值(初始值之后),之后的所有值都是某种形式的剪切 我知道,但我已经在使用标签,它仍然不能正确地为我工作 我已经对我的问题做了一个版本,但这里基本上是我的代码: 模板: 加随机值 组成部分: 导出类ChartComponent实现OnInit{ 公共图表类型:字符串; 公共图表数据:编号[]; 公共图表标签:字符串[]; 公共图表选项:任意={ 回答:是的, MaintaintAspectR

我有一个非常简单的折线图,当用户单击按钮时,我想在其中添加值。但无论出于何种原因,只会显示第一个添加的值(初始值之后),之后的所有值都是某种形式的剪切

我知道,但我已经在使用标签,它仍然不能正确地为我工作

我已经对我的问题做了一个版本,但这里基本上是我的代码:

模板:


加随机值
组成部分:

导出类ChartComponent实现OnInit{
公共图表类型:字符串;
公共图表数据:编号[];
公共图表标签:字符串[];
公共图表选项:任意={
回答:是的,
MaintaintAspectRatio:false
};
ngOnInit():void{
this.chartType='line';
this.chartLabels=['1','2','3'];
this.chartData=[0,10,20];
}
addRandomValue():void{
this.chartData.push(Math.random()*10+10);
this.chartLabels.push(this.chartData.length+“”);
//用于刷新数据的奇怪解决方法,工作正常
//对于所有其他图表类型,我都试过了
this.chartData=this.chartData.slice();
this.chartLabels=this.chartLabels.slice();
}
}

这是某种bug还是有人知道如何解决这个问题?

这是一个众所周知的问题。如果您查看ng2图表的示例,您会注意到在条形图()的声明下面有一条注释


简而言之,您必须克隆数据,在克隆上完成CRUD,然后将数据重新分配给原始变量,以便识别更改

去掉标签的一部分,这样就可以了

这意味着您的addRandomValue()应该如下所示:

addRandomValue(): void {
    this.chartData.push(Math.random() * 10 + 10);
    this.chartLabels.push(this.chartData.length + '');

    // weird workaround for refreshing data, works fine
    // for all other chart types I tried it on
    this.chartData = this.chartData.slice();
}