英文:
canvasjs-chart in angular 2way binding
问题
I am using canvasjs-chart in my angular to draw chart.(I am new in Angular)
here is my component ts code:
import { Component } from '@angular/core';
import { NgModule } from '@angular/core';
@Component({
selector: 'app-counter-component',
templateUrl: './counter.component.html'
})
export class CounterComponent {
public books = [{
title: "Life is good",
author: "benny",
category: "life"
}, {
title: 'Canned in',
author: "francis",
category: "style"
}];
chartOptions = {
title: {
text: "Basic Column Chart in Angular"
},
data: [{
type: "line",
dataPoints: [
{ label: "Apple", y: 121 },
{ label: "Orange", y: 15 },
{ label: "Banana", y: 25 },
{ label: "Mango", y: 30 },
{ label: "Grape", y: 28 }
]
}]
};
public changeChartData() {
this.chartOptions.data[0].dataPoints[0].y = 20;
}
}
As you can see I have a books object which it passed to html :
<button class="btn btn-primary" (click)="changeChartData()">change chart Data
<canvasjs-chart [options]="chartOptions">
The data is shown correctly but my problem is when I click on the change chart Data
to change the value of my books object I can't see the changes in the chart. I think the problem is the chart is not binding to the books object.
英文:
I am using canvasjs-chart in my angular to draw chart.(I am new in Angular)
here is my component ts code :
import { Component } from '@angular/core';
import { NgModule } from '@angular/core';
@Component({
selector: 'app-counter-component',
templateUrl: './counter.component.html'
})
export class CounterComponent {
public books = [{
title: "Life is good",
author: "benny",
category: "life"
}, {
title: 'Canned in',
author: "francis",
category: "style"
}];
chartOptions = {
title: {
text: "Basic Column Chart in Angular"
},
data: [{
type: "line",
dataPoints: [
{ label: "Apple", y: 121 },
{ label: "Orange", y: 15 },
{ label: "Banana", y: 25 },
{ label: "Mango", y: 30 },
{ label: "Grape", y: 28 }
]
}]
};
public changeChartData() {
this.chartOptions.data[0].dataPoints[0].y =20;
}
As you can see I have a books object which it passed to html :
<button class="btn btn-primary" (click)="changeChartData()">change chart Data</button>
<canvasjs-chart [options]="chartOptions"></canvasjs-chart>
The data is shown correctly but my problem is when I click on the change chart Data
to change the value of my books object I can't see the changes in the chart .I think the problem is the chart is not binding to books object
答案1
得分: 1
从文档中我看到,在每次选项/数据更新后,您需要调用chart.render()
:
public changeChartData() {
this.chartOptions.data[0].dataPoints[0].y =20;
this.chart.render();
}
在Angular中获取图表对象的方法在canvas-chart angular integration tutorial和一个工作示例中有描述:
- 在您的组件类中声明一个
chart: any
属性 - 添加一个初始化
chart
属性的方法
getChartInstance(chart: object) {
this.chart = chart;
}
- 在HTML中包含回调:
<canvasjs-chart [options]="chartOptions" (chartInstance)="getChartInstance($event)"></canvasjs-chart>
英文:
From the docs I see you need to call chart.render()
after each option/data update:
public changeChartData() {
this.chartOptions.data[0].dataPoints[0].y =20;
this.chart.render();
}
Getting the chart object in Angular is described in the canvas-chart angular integration tutorial and in a working example:
- declare a
chart: any
property in your component class - add a method to initialize the
chart
property
getChartInstance(chart: object) {
this.chart = chart;
}
- include the callback in the html:
<canvasjs-chart [options]="chartOptions" (chartInstance)="getChartInstance($event)"></canvasjs-chart>
答案2
得分: 0
你的代码部分已翻译如下:
public changeChartData() {
this.chartOptions = {
title: {
text: "Angular中的基本柱形图"
},
data: [{
type: "line",
dataPoints: [
{ label: "苹果", y: 20 },
{ label: "橙子", y: 15 },
{ label: "香蕉", y: 25 },
{ label: "芒果", y: 30 },
{ label: "葡萄", y: 28 }
]
}]
};
}
请注意,这是你提供的代码的翻译。如果你有任何其他需要,随时告诉我。
英文:
You are dependant on how canvasjs-chart is set up. But its normal for libraries to utilize changeDetection.onPush strategy in their components. That means it will only update when the inputs changes and output triggers, now if it runs with onpush strategy however, mutations on objects would not be counted as a input change. So what you could do instead you need to set the changes as a new object.
public changeChartData() {
this.chartOptions ={
title: {
text: "Basic Column Chart in Angular"
},
data: [{
type: "line",
dataPoints: [
{ label: "Apple", y: 20 },
{ label: "Orange", y: 15 },
{ label: "Banana", y: 25 },
{ label: "Mango", y: 30 },
{ label: "Grape", y: 28 }
]
}]
};
}
FYI: the bounding you are doing is one way binding. Two-way binding in angular binding is bindings that looks like [(value)]
where the two-way binding is incased in @Input binding []
and @Output binding ()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论