canvasjs-chart 在 Angular 中的双向绑定

huangapple go评论122阅读模式
英文:

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 &#39;@angular/core&#39;;
import { NgModule } from &#39;@angular/core&#39;;
@Component({
  selector: &#39;app-counter-component&#39;,
  templateUrl: &#39;./counter.component.html&#39;
})
export class CounterComponent {
  public books = [{
    title: &quot;Life is good&quot;,
    author: &quot;benny&quot;,
    category: &quot;life&quot;
  }, {
    title: &#39;Canned in&#39;,
    author: &quot;francis&quot;,
    category: &quot;style&quot;
  }];
  chartOptions = {
    title: {
      text: &quot;Basic Column Chart in Angular&quot;
    },
    data: [{
      type: &quot;line&quot;,
      dataPoints: [
        { label: &quot;Apple&quot;, y: 121 },
        { label: &quot;Orange&quot;, y: 15 },
        { label: &quot;Banana&quot;, y: 25 },
        { label: &quot;Mango&quot;, y: 30 },
        { label: &quot;Grape&quot;, 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 :

&lt;button class=&quot;btn btn-primary&quot; (click)=&quot;changeChartData()&quot;&gt;change chart Data&lt;/button&gt;
&lt;canvasjs-chart [options]=&quot;chartOptions&quot;&gt;&lt;/canvasjs-chart&gt;

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中包含回调:
&lt;canvasjs-chart [options]=&quot;chartOptions&quot; (chartInstance)=&quot;getChartInstance($event)&quot;&gt;&lt;/canvasjs-chart&gt;  
英文:

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:
&lt;canvasjs-chart [options]=&quot;chartOptions&quot; (chartInstance)=&quot;getChartInstance($event)&quot;&gt;&lt;/canvasjs-chart&gt;  

答案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: &quot;Basic Column Chart in Angular&quot;
    },
    data: [{
      type: &quot;line&quot;,
      dataPoints: [
        { label: &quot;Apple&quot;, y: 20 },
        { label: &quot;Orange&quot;, y: 15 },
        { label: &quot;Banana&quot;, y: 25 },
        { label: &quot;Mango&quot;, y: 30 },
        { label: &quot;Grape&quot;, 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 ()

huangapple
  • 本文由 发表于 2023年5月21日 13:34:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定