简体   繁体   English

ChartJS更新后未呈现

[英]ChartJS not rendering after update

I'm trying to update the values of my chart after aplying or clearing filters with the following code 在使用以下代码申请或清除过滤器后,我尝试更新图表的值

 <div class="col-xs-8 card-container" style="padding: 0 0 0 0">
<mat-card  height="100%" style="padding: 16px 0px 16px 16px !important; height:100%">
    <mat-card-title style="text-align:center;">Gráfica</mat-card-title>
    <mat-card-content>
        <div style="display: block; width: 100%" id="canvasContainer" *ngIf="!emptyData()">
            <canvas  id="myChart"></canvas>   
        </div>
        <div style="display: block; width: 100%" *ngIf="emptyData()">
            <h3>Pendiente de evaluar.</h3>
        </div>
    </mat-card-content> 
</mat-card>

this is the html where I render the chart on the canvas with id myChart 这是html,我将图表绘制在ID为myChart的画布上

this is the code where I generate my first chart, it only executes once and it works 这是我生成第一个图表的代码,它只执行一次就可以了

  setup(): void{
const maxValue = this.getMaxValue();
var ctddx = document.getElementById('myChart');
var canvas = <HTMLCanvasElement> document.getElementById('myChart');
if(canvas !== null) {
this.ctx = canvas.getContext("2d");
      this.chart= new Chart(this.ctx, {
      type: 'bar',
      data: {
        labels: this.labels,
        datasets: [{
            data: this.values,
            borderWidth: 3,
            fillColor: this.colors,
            backgroundColor: this.colors,
            borderColor: this.colors
        }]
    },
      options: {
        responsive: true,
        legend: {
            display: false
        },
        hover: {
            mode: 'label'
        },
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true,
                    callback: function (value) { if (Number.isInteger(value)) { return value; } },
                    suggestedMax: maxValue,
                }
            }],
            xAxes: [{
              fontSize: 35,
              barThickness : 65
          }],
        },

        plugins: {
            datalabels: {
                // align: 'start',
                // anchor: 'start',
                clamp: true,
            }
        }
      }
    });
    this.initialized = true;
  }
 }

till this point the code works and render the first chart correctly. 直到这一点,代码才能正常工作并正确呈现第一个图表。 Now when I try to apply filters or clear filters it doesn't render anything this is the following code 现在,当我尝试应用过滤器或清除过滤器时,它不会呈现任何内容,这是以下代码

 modifyChart(labels,values){
  let oldchart = this.chart;
  this.removeData();
  this.addData(labels,values);
  console.log(oldchart===this.chart);
  }

   removeData() {
    this.chart.data.labels.pop();
    this.chart.data.datasets.forEach((dataset) => {
    dataset.data.pop();
   });
   this.chart.update();
}
 addData(label, data) {
  this.chart.data.labels.push(label);
  this.chart.data.datasets.forEach((dataset) => {
  dataset.data.push(data);
  });
 this.chart.update();
}

It doesn't work, it render nothing and I even tried to compare previously chart with new chart introducing the same values it had, and it returns true, so even being the same Chart, it isn't rendering 它不起作用,什么也没渲染,我什至试图将以前的图表与引入相同值的新图表进行比较,并返回true,因此即使是相同的Chart,也不会渲染

I think the problem has to be because update deletes de canvas and create it again, but after a while I can't find a workaround 我认为问题一定是因为update删除了de canvas并再次创建了它,但是过了一会儿我找不到解决方法

Ok, so I managed to fix this, I hope this answer can help someone who faces the same problem with re rendering Charts on the same canvas if update doesn't work. 好的,所以我设法解决了这个问题,希望这个答案可以帮助遇到相同问题的人在更新不起作用的情况下在同一画布上重新呈现图表。

first of all I had this lifecycle method 首先,我有这个生命周期方法

  ngAfterContentChecked(): void {
if (!this.initialized && !this.emptyData()) {
    this.setup();
  }
}

So I can render the first chart it can't be done on OnInit because you need the canvas to be ready, so with this I'm always waiting for canvas to be ready like my method setup I posted on the question 因此,我可以渲染第一个图表,该图表无法在OnInit上完成,因为您需要准备好画布,因此,我总是在等待画布准备就绪,就像我在问题中发布的方法设置一样

 setup(): void{
  const maxValue = this.getMaxValue();
  var canvas = <HTMLCanvasElement> document.getElementById('myChart');
  if(canvas !== null) {
        ...
  this.initialized = true;
     }
  }

the problem with Update I think that it also destroys the canvas and create a new one with the same Id, so I think it tries to render the chart before the canvas is ready, so I made a workaround like this Update的问题我认为它还会破坏画布并使用相同的ID创建一个新的画布,因此我认为它会在画布准备就绪之前尝试呈现图表,因此我采取了以下解决方法

after applying all filters needed and retrieving the new data 应用所需的所有过滤器并检索新数据之后

modifyChart() {
this.chart.destroy();
this.initialized = false;
}

with this code, my ngAfterContentChecked is going to call for setup till canvas is ready and change initialized to true again 使用此代码,我的ngAfterContentChecked将要求进行设置,直到画布准备就绪并再次将其初始化为true

I know this is probably not the best workaround, but at least it fixed my problem 我知道这可能不是最好的解决方法,但至少它解决了我的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM