0

在我的 Angular 5 项目中,我想添加一个指令,将导航窗格添加到它附加的任何图表中。为此,我需要在指令代码中动态地将窗格、类别轴、值轴和一些系列添加到图表中。我怎样才能做到这一点?我尝试将它添加到不同的钩子中,例如 ngAfterContentInit 但无济于事。这种事情的一般策略是什么?

编码:

import {
  Directive,
  Input,
  AfterContentInit,
  OnDestroy
} from '@angular/core';
import {
  ChartComponent,
  PaneComponent,
  CategoryAxisItemComponent,
  ValueAxisItemComponent,
  SeriesItemComponent,
  CategoryAxisLabelsComponent,
  CategoryAxis,
  ValueAxis
} from '@progress/kendo-angular-charts';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';
import { CollectionService } from '@progress/kendo-angular-charts/dist/es2015/common/collection.service';
import { Subscription } from 'rxjs/Subscription';

@Directive({
  selector: '[appChartNaviagation]'
})
export class ChartNaviagationDirective implements AfterContentInit {

  private navigationMin = new BehaviorSubject<Date>(undefined);

  private navigationMax = new BehaviorSubject<Date>(undefined);

  constructor(private chart: ChartComponent) {

  }

  ngAfterContentInit(): void {

    const navigationPane = new PaneComponent(this.chart.configurationService, new CollectionService());
    navigationPane.name = 'navigator';
    navigationPane.height = 100;
    this.chart.panes.push(navigationPane);

    const navigationCategoryAxis = new CategoryAxisItemComponent(this.chart.configurationService, new CollectionService());
    navigationCategoryAxis.name = 'navigatorCategories';
    navigationCategoryAxis.pane = 'navigator';
    this.chart.categoryAxis = [
      this.chart.categoryAxis as CategoryAxis || new CategoryAxisItemComponent(this.chart.configurationService, new CollectionService()),
      navigationCategoryAxis
    ];        
    const mainAxis = <CategoryAxisItemComponent>this.chart.categoryAxis[0];

    const navigationValueAxis = new ValueAxisItemComponent(this.chart.configurationService, new CollectionService());
    navigationCategoryAxis.name = 'navigationValues';
    navigationCategoryAxis.pane = 'navigator';
    const axisLabels = new CategoryAxisLabelsComponent(this.chart.configurationService);
    axisLabels.visible = false;
    navigationCategoryAxis.labels = axisLabels;

    this.chart.valueAxis = [
      this.chart.valueAxis as ValueAxis || new ValueAxisItemComponent(this.chart.configurationService, new CollectionService()),
      navigationValueAxis
    ];        

    if (this.navigationData) {
      const naviagationSeries = new SeriesItemComponent(this.chart.configurationService, undefined);
      naviagationSeries.type = 'area';
      naviagationSeries.line = { style: 'smooth' };
      naviagationSeries.data = this.navigationData;
      naviagationSeries.field = 'value';
      naviagationSeries.categoryField = 'time';
      naviagationSeries.axis = 'navigationValues';
      naviagationSeries.categoryAxis = 'navigatorCategories';
      naviagationSeries.aggregate = 'sumOrNull';

      this.chart.series.push(naviagationSeries);
    }
  }
}

我期望实现与此处描述的相同的行为。

4

0 回答 0