如何在显示新图表后“破坏”前一个图表(画布) Angular 4

时间:2017-09-27 18:48:07

标签: javascript angular typescript charts

我有一个代码,用于在选择下拉列表后更新Graphic的数据。但是,当我选择一个选项时,它看起来像新的Graphic和旧的Graphic同时停留在同一个画布上。

这是我的代码:

funcao(){

  this.graphicService.getDatas().subscribe(datas => {
        this.datas = datas; //Gets the data from the API and put on local datas variable

        if(this.data[0].dimension == "Tech")
        {
          this.counter=0;
        }
        else if(this.data[0].dimension == "Bus"){
          this.counter=1;
        }
              this.barChartData = { //Bar graphic data
                  labels: ["Entry", "Foundation"],
                  datasets: [{
                      label: this.datas[this.counter].subdimensions[0].name,
                      backgroundColor: 'rgba(37, 230, 131,0.7)'
                      data: [
                          this.datas[this.counter].subdimensions[0].entry,
                          this.datas[this.counter].subdimensions[0].foundation
                      ]
                  }]

              };

              this.canvas = document.getElementById('myChart');
              this.ctx = this.canvas.getContext('2d');

              let myChart = new Chart(this.ctx, { // Bar graphic configs
                     type: 'bar',
                     data: this.barChartData,
                     options: {
                         scales: {
                            yAxes: [{
                                ticks: {
                                    beginAtZero: true
                                }
                            }]
                          }
                     }
                 });
      });
}

HTML

<select [(ngModel)]="data[0].dimension" (change)="funcao()" class="form-control">
  <option *ngFor="let data of datas" [value]="data.dimension">{{ data.dimension }}</option>
</select>

  <canvas id="myChart"></canvas>

我想要做的是旧图形消失,只有新图形停留在屏幕上。

4 个答案:

答案 0 :(得分:8)

好像你正在使用ChartJS库。在这种情况下,您可以使用destroy()方法销毁任何以前的图表实例。

<强>ꜰɪʀꜱᴛ

在图表组件类中添加属性(图表实例将存储在其中)

public myChart: Chart

<强>ꜱᴇᴄᴏɴᴅ

检查并销毁图表实例(如果有),然后再创建新的

...
if (this.myChart) this.myChart.destroy(); //destroy prev chart instance
this.myChart = new Chart(this.ctx, {
         type: 'bar',
         data: this.barChartData,
         ...

答案 1 :(得分:1)

canvas api定义了 clearRect 方法。

您可以在创建新图表之前清除画布。

this.ctx = this.canvas.getContext('2d');
this.ctx.clearRect(0, 0, 100,100);

您阅读了文档here

答案 2 :(得分:1)

尝试destroy()方法。

保持对图表实例的可访问引用(在我的示例中为this.myChart):

this.myChart = new Chart(this.ctx, { // Bar graphic configs
  type: 'bar',
  data: this.barChartData,
  options: {
    scales: {
      yAxes: [{
        ticks: {
          beginAtZero: true
        }
      }]
    }
  }
});

然后,当你需要销毁它时,请致电:

// Destroys a specific chart instance
this.myChart.destroy();

这里是destroy()函数的代码(从CDN复制而来):

destroy : function(){
  this.clear();
  unbindEvents(this, this.events);
  var canvas = this.chart.canvas;

  // Reset canvas height/width attributes starts a fresh with the canvas context
  canvas.width = this.chart.width;
  canvas.height = this.chart.height;

  // < IE9 doesn't support removeProperty
  if (canvas.style.removeProperty) {
    canvas.style.removeProperty('width');
    canvas.style.removeProperty('height');
  } else {
    canvas.style.removeAttribute('width');
    canvas.style.removeAttribute('height');
  }

  delete Chart.instances[this.id];
}

clear()功能:

clear = helpers.clear = function(chart){
  chart.ctx.clearRect(0,0,chart.width,chart.height);
}

所以,destroy()函数负责ChartJS和canvas API,并且stated on the official site

  

必须在将画布重新用于新图表之前调用它

答案 3 :(得分:0)

我知道这已经解决,但是我遇到了同样的问题,并且该解决方案无法正常工作,可能是因为我使用的是最新版本。

如果有人正在阅读此线程并且与我处于相同情况,这就是我的工作方式:手动从ngOnDestroy()挂钩中的DOM中删除画布。

  ngOnDestroy(){
    ( < HTMLCanvasElement > document.getElementById('myChartCanvas')).remove();
  }