【问题标题】:angular material tab content c3js chart not showing角度材料选项卡内容c3js图表未显示
【发布时间】:2018-02-16 15:44:38
【问题描述】:

我想在有角度的材料选项卡中显示图表。但是选项卡初始化似乎是异步的,因此 c3js 找不到 div #detailed-chart 并且我的选项卡仍然为空。

模板

<mat-card class="col-xs-12">
  <mat-card-title>
    Detailed Utilization
  </mat-card-title>

  <mat-tab-group>
    <mat-tab label="Chart">
      <div id="detailed-chart" style="height: 200px"></div>
    </mat-tab>
    <mat-tab label="Table">
    </mat-tab>
  </mat-tab-group>
</mat-card>

组件

export class UtilizationComponent implements OnInit {
  ngOnInit() {       
    this.detailedChart = c3.generate({
      bindto: '#detailed-chart',
      data: {
        x: 'date',
        columns: [
          ['date'],
          ['room'],
          ['desk'],
          ['office']
        ]
      },
      axis: {
        x: {
          type: 'timeseries',
          tick: {
            format: '%m.%Y'
          }
        },
        y: {
          tick: {
            format: d3.format('.1%')
          }
        }
      }
    });   
  }

}

我也试过手动找div,好像不存在。

const x = window.document.getElementById('detailed-chart');
console.log('foo', x);

输出

foo 为空

问题

在这里加载动态元素的 angluarish 方式是什么?我是否需要为此选项卡创建单独的组件?

【问题讨论】:

    标签: angular typescript tabs angular-material c3.js


    【解决方案1】:

    解决方案是使用另一个生命周期回调ngAfterViewInit。

    export class UtilizationComponent implements AfterViewInit {
      ngAfterViewInit() {       
        this.detailedChart = c3.generate({
          bindto: '#detailed-chart',
          data: {
            x: 'date',
            columns: [
              ['date'],
              ['room'],
              ['desk'],
              ['office']
            ]
          },
          axis: {
            x: {
              type: 'timeseries',
              tick: {
                format: '%m.%Y'
              }
            },
            y: {
              tick: {
                format: d3.format('.1%')
              }
            }
          }
        });   
      }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用回调AfterViewChecked 来显示图表。

      【讨论】:

        猜你喜欢
        • 2020-05-07
        • 2018-11-16
        • 2023-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多