【问题标题】:Highcharts not showing dataHighcharts不显示数据
【发布时间】:2020-01-03 20:10:16
【问题描述】:

我正在尝试显示一个图表,其中包含在角度组件之间传递的数据。打印图表的组件的 ngOnInit() 数据正常,但该图表没有显示任何内容。我使用 jquery 来控制到达的数据是否为空。请帮忙。非常感谢!

hello-component.component.ts

import { Component, OnInit, Input} from '@angular/core';

import * as $ from "jquery";
import * as Highcharts from 'highcharts';

@Component({
  selector: 'app-hello-component',
  templateUrl: './hello-component.component.html',
  styleUrls: ['./hello-component.component.css']
})
export class HelloComponentComponent implements OnInit {


  @Input() direct: number;

  constructor () {     
  };

  public options: any = {
    chart: {
      type: 'column',
      height: 700
    },
    title: {
      text: 'Sample Scatter Plot'
    },
    credits: {
      enabled: false
    },
    tooltip: {
      formatter: function() {
        return 'x: ' + Highcharts.dateFormat('%e %b %y %H:%M:%S', this.x) + 'y: ' + this.y.toFixed(2);
      }
    },
    xAxis: {
       categories: ['RG', 'WM']
    },
    series: [
      {
        name: 'Normal',
        
        data: (function () { $(function() { 
          var datos = [];
          for (let i = 0; i <=1; i += 1) {
            datos.push({
              x: i,
              y: this.direct + i
            });
          }
          return datos;
        })}())
        
      }    
    ]
  }  

  ngOnInit(){
    console.log("direct: " + this.direct);
    Highcharts.chart('container', this.options);       
  }
}

app.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
  title = 'passComponent';
  aces: number = 5;  
  
  ngOnInit() {
    //this.aces = 5;
    console.log("aces: " + this.aces);
  }
}

hello-component.component.html

<p>{{direct}} direct</p>
<div id="container"></div>

app.component.html

&lt;app-hello-component [direct]="aces"&gt;&lt;/app-hello-component&gt;

【问题讨论】:

    标签: angular


    【解决方案1】:
    I was told the solution. It's about the way you set the highcharts values with angular:
    
    onClickMe() {
    
        var indx = [0,1];
        var valor = [];
        indx.forEach(elm => { 
          console.log("elm: " + elm);
          valor[elm] = this.direct + elm;  
        });    
        this.options.series[0]['data'] = valor;
    
        console.log("direct 2: " + this.direct);
        Highcharts.chart('container', this.options); 
    
    
      }
    

    【讨论】:

      【解决方案2】:

      在构造函数中移动选项和 onInit 逻辑。我敢打赌,图表是在 onInit 出现在生命周期之前绘制的。

      【讨论】:

      • 在那种情况下,我会直接未定义,恐怕@input 不会进入构造函数...
      • 看起来在图表选项中您可以打开实时重绘或调用它 - 这将是我的下一个想法
      猜你喜欢
      • 1970-01-01
      • 2014-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      相关资源
      最近更新 更多