Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
920 views
in Technique[技术] by (71.8m points)

angular - Callback and Testing Highcharts

I am currently using angular 9 with Highcharts.

Link to the code : https://stackblitz.com/edit/angular-ivy-wdejxk

Some instructions for running on application-side / test-side :

  1. Test - side : inside angular.json file on line number 18, change
        "main": "src/main.ts",
    
    to
        "main": "src/main-testing.ts",
    

and do a refresh of the browser.

  1. Application - side : Change exactly the opposite of previous.
       "main": "src/main-testing.ts",
    
    to
       "main": "src/main.ts",
    

Here are a few issues i am stuck upon :

  1. I have used chart callback to get the chart instance , but it is not working ( inside hello.component.ts , line numbers 38 to 40 ). How should i call it and when does actually the callback happens in Highcharts ?
  2. If suppose somehow i am able to assign the chart instance to chartCreated variable. Can i control the chart now, like line numbers 60 to 62 ( if i uncomment that ), will it work ? Basically i wanted to know the usefulness of updateFlag in Highcharts.
  3. Unable to addSeries when ngOnChanges is called inside hello.component.ts
  4. Inside the spec file hello.component.spec.ts i wanted to test the chart by putting a numeric data / adding a series on my own , like i did when onClick() is called. But jasmine shows error
       TypeError : Cannot read series of undefined
       TypeError : Cannot read property 'addSeries' of undefined
    
    How to resolve these ?

EDIT 1 : Implemented ngOnChanges and ngOnInit and removed most of the code from app.component.ts to hello.component.ts

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
    If you add the ngOnInit lifecycle hook, you will get values:

    1. export class AppComponent implements OnInit {.....


    2. ngOnInit(){
            this.chartCallback = (chart) => {
                this.chartCreated = chart;
                console.log('chart: ' + chart.chartHeight);         // shows 400
                console.log('this.chartCreated: ' + this.chartCreated.chartHeight); // shows 400
            }
          }

       addNewDataToChart(){
            this.updateFlag = false;
            this.chartCreated.addSeries({                     
            data: [3,4,2],                                         
            type: 'line'                                           
         });
            this.updateFlag = true;
       }

3. onClick(){
    if(this.clicked === false){
      this.chartCreated.series[0].data[0].update(4);
      this.clicked = true;
    } else {
      this.chartCreated.series[0].data[0].update(1);
      this.clicked = false;
    }
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...