11/* eslint-disable max-len */
2- import { AfterViewInit , ChangeDetectorRef , Component , CUSTOM_ELEMENTS_SCHEMA , ElementRef , OnInit , Pipe , PipeTransform , QueryList , ViewChild , ViewChildren } from '@angular/core' ;
3- import { IgxChartIntegrationDirective , IgxContextMenuDirective , OPTIONS_TYPE , CHART_TYPE } from 'igniteui-angular-extras' ;
2+ import { AfterViewInit , ChangeDetectorRef , ViewContainerRef , Component , CUSTOM_ELEMENTS_SCHEMA , ElementRef , OnInit , Pipe , PipeTransform , QueryList , ViewChild , ViewChildren , TemplateRef } from '@angular/core' ;
3+ import { IgxChartIntegrationDirective , IgxConditionalFormattingDirective , IgxContextMenuDirective , OPTIONS_TYPE , CHART_TYPE , IDeterminedChartTypesArgs } from 'igniteui-angular-extras' ;
44import { IgcDockManagerLayout , IgcDockManagerPaneType , IgcSplitPane , IgcSplitPaneOrientation } from 'igniteui-dockmanager' ;
55import { FinancialData } from '../../data/financialData' ;
66import { FloatingPanesService } from '../../services/floating-panes.service' ;
77import { DockSlotComponent } from './dock-slot/dock-slot.component' ;
8- import { IgxGridComponent , IgxColumnComponent , IgxCellTemplateDirective , IgxDividerDirective , IgxBadgeComponent } from 'igniteui-angular' ;
8+ import { IgxGridComponent , IgxColumnComponent , IgxCellTemplateDirective , IgxDividerDirective , IgxBadgeComponent , IColumnSelectionEventArgs } from 'igniteui-angular' ;
99import { NgClass , DecimalPipe , TitleCasePipe , CurrencyPipe } from '@angular/common' ;
10+ import { debounceTime } from 'rxjs/operators' ;
1011
1112@Pipe ( {
1213 name : 'filterType'
@@ -53,10 +54,12 @@ export class HastDuplicateLayouts implements PipeTransform {
5354 templateUrl : './data-analysis-dock-manager.component.html' ,
5455 styleUrls : [ './data-analysis-dock-manager.component.scss' ] ,
5556 providers : [ FloatingPanesService ] ,
56- imports : [ IgxGridComponent , IgxChartIntegrationDirective , IgxContextMenuDirective , IgxBadgeComponent , IgxColumnComponent , IgxCellTemplateDirective , NgClass , IgxDividerDirective , DockSlotComponent , DecimalPipe , TitleCasePipe , CurrencyPipe , FilterTypePipe , HastDuplicateLayouts ] ,
57+ imports : [ IgxGridComponent , IgxConditionalFormattingDirective , IgxChartIntegrationDirective , IgxContextMenuDirective , IgxBadgeComponent , IgxColumnComponent , IgxCellTemplateDirective , NgClass , IgxDividerDirective , DockSlotComponent , DecimalPipe , TitleCasePipe , CurrencyPipe , FilterTypePipe , HastDuplicateLayouts ] ,
5758 schemas : [ CUSTOM_ELEMENTS_SCHEMA ]
5859} )
59- export class DataAnalysisDockManagerComponent implements OnInit , AfterViewInit {
60+ export class DataAnalysisDockManagerComponent implements OnInit , AfterViewInit {
61+ @ViewChild ( 'grid' , { read : IgxGridComponent , static : true } )
62+ public grid : IgxGridComponent ;
6063
6164 @ViewChild ( 'dock' , { read : ElementRef } )
6265 public dockManager : ElementRef < HTMLIgcDockmanagerElement > ;
@@ -67,6 +70,10 @@ export class DataAnalysisDockManagerComponent implements OnInit, AfterViewInit {
6770 @ViewChildren ( DockSlotComponent )
6871 public dockSlots : QueryList < DockSlotComponent > ;
6972
73+ @ViewChild ( 'template' , { read : TemplateRef } )
74+ public emptyChartTemplate : TemplateRef < any > ;
75+
76+ public availableCharts : CHART_TYPE [ ] = [ ] ;
7077 public allCharts : CHART_TYPE [ ] = [ ] ;
7178 public data ;
7279 public chartData = [ ] ;
@@ -81,7 +88,7 @@ export class DataAnalysisDockManagerComponent implements OnInit, AfterViewInit {
8188 }
8289
8390 public ngAfterViewInit ( ) {
84- this . allCharts = this . chartIntegration . getAvailableCharts ( ) ;
91+ this . allCharts = this . chartIntegration . getAllChartTypes ( ) ;
8592 this . cdr . detectChanges ( ) ;
8693 const pieChartOptions = {
8794 labelsPosition : 4 ,
@@ -96,6 +103,30 @@ export class DataAnalysisDockManagerComponent implements OnInit, AfterViewInit {
96103 chart . indexOf ( 'Bar' ) === - 1 ||
97104 chart !== CHART_TYPE . PIE )
98105 . forEach ( chart => this . chartIntegration . setChartComponentOptions ( chart , OPTIONS_TYPE . X_AXIS , { labelAngle : 30 } ) ) ;
106+
107+ this . chartIntegration . onChartTypesDetermined . subscribe ( ( args : IDeterminedChartTypesArgs ) => {
108+ if ( args . chartsAvailability . size === 0 || args . chartsForCreation . length === 0 ) {
109+ this . chartIntegration . disableCharts ( this . allCharts ) ;
110+ } else {
111+ args . chartsAvailability . forEach ( ( isAvailable , chart ) => {
112+ if ( args . chartsForCreation . indexOf ( chart ) === - 1 ) {
113+ this . chartIntegration . disableCharts ( [ chart ] ) ;
114+ } else {
115+ this . chartIntegration . enableCharts ( [ chart ] ) ;
116+ }
117+ } ) ;
118+ }
119+ this . availableCharts = this . chartIntegration . getAvailableCharts ( ) ;
120+ } ) ;
121+ this . cdr . detectChanges ( ) ;
122+
123+ this . grid . rangeSelected . subscribe ( range => {
124+ this . createChartCommonLogic ( ) ;
125+ } ) ;
126+
127+ this . grid . columnSelectionChanging . pipe ( debounceTime ( 100 ) ) . subscribe ( ( args : IColumnSelectionEventArgs ) => {
128+ this . createChartCommonLogic ( ) ;
129+ } ) ;
99130 }
100131
101132 // eslint-disable-next-line @typescript-eslint/member-ordering
@@ -163,12 +194,47 @@ export class DataAnalysisDockManagerComponent implements OnInit, AfterViewInit {
163194 this . paneService . appendPane ( splitPane ) ;
164195 const chartHost = this . getChartHostFromSlot ( type ) ;
165196 chartHost . viewContainerRef . clear ( ) ;
166- this . chartIntegration . chartFactory
167197 const chart = this . chartIntegration . chartFactory ( type , chartHost . viewContainerRef ) ;
168198
169199 this . dockManager . nativeElement . layout . floatingPanes . push ( splitPane ) ;
170200 this . docLayout = { ...this . dockManager . nativeElement . layout } ;
171201 this . selectedCharts [ type ] = chart ;
172202 this . cdr . detectChanges ( ) ;
173203 }
174- }
204+
205+ public createChartCommonLogic ( ) {
206+ if ( Object . keys ( this . selectedCharts ) . length !== 0 ) {
207+ setTimeout ( ( ) => {
208+ Object . keys ( this . selectedCharts ) . forEach ( ( c : CHART_TYPE ) => {
209+ const chartHost = this . getChartHostFromSlot ( c ) ;
210+ if ( this . availableCharts . indexOf ( c ) !== - 1 ) {
211+ if ( c !== CHART_TYPE . PIE && typeof this . selectedCharts [ c ] === 'object' ) {
212+ this . selectedCharts [ c ] = this . chartIntegration . chartFactory ( c , null , this . selectedCharts [ c ] ) ;
213+ } else {
214+ chartHost . viewContainerRef . clear ( ) ;
215+ this . selectedCharts [ c ] = this . chartIntegration . chartFactory ( c , chartHost . viewContainerRef ) ;
216+ }
217+ } else {
218+ this . clearViewContainer ( chartHost . viewContainerRef ) ;
219+ const embeddedView = chartHost . viewContainerRef . createEmbeddedView ( this . emptyChartTemplate ) ;
220+ embeddedView . detectChanges ( ) ;
221+ this . selectedCharts [ c ] = 'Empty' ;
222+ }
223+ } ) ;
224+ } ) ;
225+ }
226+ }
227+
228+ private clearViewContainer ( viewContainerRef : ViewContainerRef ) {
229+ for ( let i = viewContainerRef . length - 1 ; i >= 0 ; i -- ) {
230+ const viewRef = viewContainerRef . get ( i ) ;
231+ if ( viewRef ) {
232+ const componentInstance = ( viewRef as any ) . context ;
233+ if ( componentInstance && ( componentInstance as any ) . destroy ) {
234+ ( componentInstance as any ) . destroy ( ) ;
235+ }
236+ }
237+ }
238+ viewContainerRef . clear ( ) ;
239+ }
240+ }
0 commit comments