@@ -24,14 +24,15 @@ import {
2424 OperationMetadata ,
2525 FieldBasedIndexPatternColumn ,
2626 SumIndexPatternColumn ,
27+ TermsIndexPatternColumn ,
2728} from '../../../../../../lens/public' ;
2829import {
2930 buildPhraseFilter ,
3031 buildPhrasesFilter ,
3132 IndexPattern ,
3233} from '../../../../../../../../src/plugins/data/common' ;
33- import { FieldLabels } from './constants' ;
34- import { DataSeries , UrlFilter , URLReportDefinition } from '../types' ;
34+ import { FieldLabels , FILTER_RECORDS , USE_BREAK_DOWN_COLUMN } from './constants' ;
35+ import { ColumnFilter , DataSeries , UrlFilter , URLReportDefinition } from '../types' ;
3536
3637function getLayerReferenceName ( layerId : string ) {
3738 return `indexpattern-datasource-layer-${ layerId } ` ;
@@ -53,6 +54,7 @@ export const parseCustomFieldName = (
5354) => {
5455 let fieldName = sourceField ;
5556 let columnType ;
57+ let columnFilters ;
5658
5759 const rdf = reportViewConfig . reportDefinitions ?? [ ] ;
5860
@@ -61,17 +63,21 @@ export const parseCustomFieldName = (
6163 if ( customField ) {
6264 if ( selectedDefinitions [ fieldName ] ) {
6365 fieldName = selectedDefinitions [ fieldName ] [ 0 ] ;
64- if ( customField ?. options )
65- columnType = customField ?. options ?. find ( ( { field } ) => field === fieldName ) ?. columnType ;
66- } else if ( customField . defaultValue ) {
67- fieldName = customField . defaultValue ;
68- } else if ( customField . options ?. [ 0 ] . field ) {
69- fieldName = customField . options ?. [ 0 ] . field ;
66+ if ( customField ?. options ) {
67+ const currField = customField ?. options ?. find (
68+ ( { field, id } ) => field === fieldName || id === fieldName
69+ ) ;
70+ columnType = currField ?. columnType ;
71+ columnFilters = currField ?. columnFilters ;
72+ }
73+ } else if ( customField . options ?. [ 0 ] . field || customField . options ?. [ 0 ] . id ) {
74+ fieldName = customField . options ?. [ 0 ] . field || customField . options ?. [ 0 ] . id ;
7075 columnType = customField . options ?. [ 0 ] . columnType ;
76+ columnFilters = customField . options ?. [ 0 ] . columnFilters ;
7177 }
7278 }
7379
74- return { fieldName, columnType } ;
80+ return { fieldName, columnType, columnFilters } ;
7581} ;
7682
7783export class LensAttributes {
@@ -82,19 +88,22 @@ export class LensAttributes {
8288 seriesType : SeriesType ;
8389 reportViewConfig : DataSeries ;
8490 reportDefinitions : URLReportDefinition ;
91+ breakdownSource ?: string ;
8592
8693 constructor (
8794 indexPattern : IndexPattern ,
8895 reportViewConfig : DataSeries ,
8996 seriesType ?: SeriesType ,
9097 filters ?: UrlFilter [ ] ,
9198 operationType ?: OperationType ,
92- reportDefinitions ?: URLReportDefinition
99+ reportDefinitions ?: URLReportDefinition ,
100+ breakdownSource ?: string
93101 ) {
94102 this . indexPattern = indexPattern ;
95103 this . layers = { } ;
96104 this . filters = filters ?? [ ] ;
97105 this . reportDefinitions = reportDefinitions ?? { } ;
106+ this . breakdownSource = breakdownSource ;
98107
99108 if ( operationType ) {
100109 reportViewConfig . yAxisColumns . forEach ( ( yAxisColumn ) => {
@@ -109,24 +118,33 @@ export class LensAttributes {
109118 this . visualization = this . getXyState ( ) ;
110119 }
111120
112- addBreakdown ( sourceField : string ) {
121+ getBreakdownColumn ( sourceField : string ) : TermsIndexPatternColumn {
113122 const fieldMeta = this . indexPattern . getFieldByName ( sourceField ) ;
114123
115- this . layers . layer1 . columns [ 'break-down-column' ] = {
124+ return {
116125 sourceField,
117126 label : `Top values of ${ FieldLabels [ sourceField ] } ` ,
118127 dataType : fieldMeta ?. type as DataType ,
119128 operationType : 'terms' ,
120129 scale : 'ordinal' ,
121130 isBucketed : true ,
122131 params : {
123- size : 3 ,
132+ size : 10 ,
124133 orderBy : { type : 'column' , columnId : 'y-axis-column' } ,
125134 orderDirection : 'desc' ,
126135 otherBucket : true ,
127136 missingBucket : false ,
128137 } ,
129138 } ;
139+ }
140+
141+ addBreakdown ( sourceField : string ) {
142+ const { xAxisColumn } = this . reportViewConfig ;
143+ if ( xAxisColumn ?. sourceField === USE_BREAK_DOWN_COLUMN ) {
144+ // do nothing since this will be used a x axis source
145+ return ;
146+ }
147+ this . layers . layer1 . columns [ 'break-down-column' ] = this . getBreakdownColumn ( sourceField ) ;
130148
131149 this . layers . layer1 . columnOrder = [
132150 'x-axis-column' ,
@@ -229,15 +247,27 @@ export class LensAttributes {
229247 getXAxis ( ) {
230248 const { xAxisColumn } = this . reportViewConfig ;
231249
250+ if ( xAxisColumn ?. sourceField === USE_BREAK_DOWN_COLUMN ) {
251+ return this . getBreakdownColumn ( this . breakdownSource || this . reportViewConfig . breakdowns [ 0 ] ) ;
252+ }
253+
232254 return this . getColumnBasedOnType ( xAxisColumn . sourceField ! , undefined , xAxisColumn . label ) ;
233255 }
234256
235- getColumnBasedOnType ( sourceField : string , operationType ?: OperationType , label ?: string ) {
236- const { fieldMeta, columnType, fieldName } = this . getFieldMeta ( sourceField ) ;
257+ getColumnBasedOnType (
258+ sourceField : string ,
259+ operationType ?: OperationType ,
260+ label ?: string ,
261+ colIndex ?: number
262+ ) {
263+ const { fieldMeta, columnType, fieldName, columnFilters } = this . getFieldMeta ( sourceField ) ;
237264 const { type : fieldType } = fieldMeta ?? { } ;
238265
239- if ( fieldName === 'Records' ) {
240- return this . getRecordsColumn ( ) ;
266+ if ( fieldName === 'Records' || columnType === FILTER_RECORDS ) {
267+ return this . getRecordsColumn (
268+ label ,
269+ colIndex !== undefined ? columnFilters ?. [ colIndex ] : undefined
270+ ) ;
241271 }
242272
243273 if ( fieldType === 'date' ) {
@@ -256,11 +286,11 @@ export class LensAttributes {
256286 }
257287
258288 getFieldMeta ( sourceField : string ) {
259- const { fieldName, columnType } = this . getCustomFieldName ( sourceField ) ;
289+ const { fieldName, columnType, columnFilters } = this . getCustomFieldName ( sourceField ) ;
260290
261291 const fieldMeta = this . indexPattern . getFieldByName ( fieldName ) ;
262292
263- return { fieldMeta, fieldName, columnType } ;
293+ return { fieldMeta, fieldName, columnType, columnFilters } ;
264294 }
265295
266296 getMainYAxis ( ) {
@@ -270,7 +300,7 @@ export class LensAttributes {
270300 return this . getRecordsColumn ( label ) ;
271301 }
272302
273- return this . getColumnBasedOnType ( sourceField ! , operationType , label ) ;
303+ return this . getColumnBasedOnType ( sourceField ! , operationType , label , 0 ) ;
274304 }
275305
276306 getChildYAxises ( ) {
@@ -286,20 +316,22 @@ export class LensAttributes {
286316 lensColumns [ `y-axis-column-${ i } ` ] = this . getColumnBasedOnType (
287317 sourceField ! ,
288318 operationType ,
289- label
319+ label ,
320+ i
290321 ) ;
291322 }
292323 return lensColumns ;
293324 }
294325
295- getRecordsColumn ( label ?: string ) : CountIndexPatternColumn {
326+ getRecordsColumn ( label ?: string , columnFilter ?: ColumnFilter ) : CountIndexPatternColumn {
296327 return {
297328 dataType : 'number' ,
298329 isBucketed : false ,
299330 label : label || 'Count of records' ,
300331 operationType : 'count' ,
301332 scale : 'ratio' ,
302333 sourceField : 'Records' ,
334+ filter : columnFilter ,
303335 } as CountIndexPatternColumn ;
304336 }
305337
@@ -331,7 +363,9 @@ export class LensAttributes {
331363 layerId : 'layer1' ,
332364 seriesType : this . seriesType ?? 'line' ,
333365 palette : this . reportViewConfig . palette ,
334- yConfig : [ { forAccessor : 'y-axis-column' , color : 'green' } ] ,
366+ yConfig : this . reportViewConfig . yConfig || [
367+ { forAccessor : 'y-axis-column' , color : 'green' } ,
368+ ] ,
335369 xAccessor : 'x-axis-column' ,
336370 } ,
337371 ] ,
0 commit comments