angular 5 在同一组件中重置图表 js 栏

开发者

我有一个组件来显示我的产品的详细信息我在组件中使用了一个水平条形图,但是当我查看其他产品时,它会保留之前的字符条,新条将附加到它上面。我试图将图表条形值设为空,并且我删除了 artingArr 但它不起作用如何在产品数据更改时重置图表并重新绘制它?

我的图表是这样的:在 ngAfterinit() 中:

    for (let rate of this.product.product_ratings) {
      this.sum += rate.rating;
      this.ratingArr.push(rate.rating);
      let n = this.product.product_ratings.length;
      let nn = this.sum / n;
      this.product.peopleCount = n;
      this.product.between = Math.round(nn * 10) / 10;
    }


   new Chart(<HTMLCanvasElement>document.getElementById("user-rev-chart"), {
      type: 'horizontalBar',
      data: {
        labels: ["5 star", "4 star", "3 star", "2 star", "1 star"],
        datasets: [
          {
            fill: true,
            label: false,
            backgroundColor: ["#a1a7b3", "#a1a7b3", "#a1a7b3", "#a1a7b3", "#a1a7b3"],
            data: [this.fivestar, this.fourstar, this.threestar, this.twostar, this.onestar]
          }
        ]
      },
      options: {
        maintainAspectRatio: false,
        responsive: false,
        legend: {display: false},
        title: {
          display: false,
          text: ''
        },
        scales:
          {
            yAxes: [{
              // barPercentage: 0.4,
              barThickness: 20,
              barPercentage: .5,
              categoryPercentage: .2,
              isFixedWidth: true,
              //Number - Pixel width of the bar
              barWidth: 20,
              gridLines: {
                display: false
              },
              ticks: {
                min: 0,
                stepSize: 1,
                fixedStepSize: 1,
              }
            }],
            xAxes: [{
              display: false,
              gridLines: {
                display: false
              },
              ticks: {
                min: 0,
                stepSize: 1,
                fixedStepSize: 1,
              }
            }],
          }
      }
    });

ngInit():

  ngOnInit() {

    if (this.isActive() && this.auth.isAuthenticated()) {
      this.changeCaptcha();
    }
    this.all = null;
    this.cur = null;
    this.onestar = null;
    this.twostar = null;
    this.threestar = null;
    this.fourstar = null;
    this.fivestar = null;
    this.ratingArr = [];


  }
阿德里安·克劳迪乌·迪玛

您必须获得 2d 上下文。

模板 :

<div class="chart-container" fxFlex="80">
      <canvas id="currency"></canvas>
</div>

成分:

  private displayCurrencyGraph(currencyEntry: CurrencyEntry) {
    const currencyCanvas: any = document.getElementById('currency');
    const currencyCanvasContext = currencyCanvas.getContext('2d');
    ChartBuilder.buildChartLine(currencyCanvasContext,this.data );
  }

图表生成器:

static buildChartLine(canvasContext, data){
    new Chart(canvasContext, {
      type: 'line',
      data: {data} 
}

从您的代码中,您缺少getContext('2d')来自画布的电话。如果您添加它,它应该可以工作。

您必须在上下文上调用 destroy() 方法,然后分配新的数据集。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Angular 8:在同一组件中使用两个外部 angular 包 - moment.js

在angular 5组件中调用外部js文件的功能

如何在同一组件模板属性中调用Vue Js组件方法?

同一组件中的不同Highcharts(角度5)

Angular 2:同一组件中的多个HTML页面

如何在Angular 6中使用同一组件的多个实例?

Vue.js锚定到同一组件中的div

Vue.js:在页面中多次包含同一组件实例

在angular5中的登录页面之前隐藏导航栏

HTML5进度栏在使用Angular JS的Microsoft Edge上未显示价值

如何在组件vue js的数据中引用来自同一组件的方法

Angular 5组件需要一个参数

需要使用Angular中的唯一值多次使用同一组件两次

同一组件中js和CSS的html id和class不起作用

Angular 5组件过大

如何将同一组件视为同一父级中的两个不同组件 [Angular]

将js组件导入Angular 5

JS中的Angular2组件通信

检测同一组件内模型的更改-Angular

如何在Angular 2的同一组件中使用多个ng-content?

Angular 4创建通往同一组件的不同语言路径

.Net MVC 5中的Angular js结构

Chart.Js - 图表栏中的背景栏

图表Js可点击栏

点击取消按钮时删除/关闭 Angular 或 Ionic 5 中的搜索栏

Angular:在Angular Service中共享事件,并在页面上两次使用同一组件

无法使用 Node.js、Express 和 EJS 在 Bootstrap 5 导航栏中显示图像

Angular 5将@Input向下发送一组组件

Angular 6基于同一组件中的多个不同api调用来路由多个参数,这只是不好的设计吗?