@@ -68,6 +68,9 @@ export class LineChartService {
68
68
private _labelGroupHeight : number ;
69
69
private _legendGroupTop : number = this . _margin . top + this . _height + 50 ;
70
70
private _legendGroupLeft : number = this . _margin . left ;
71
+ private _legendGroupHeight ;
72
+ private _legendGroupColumnWidth ;
73
+ private _legendGroupColumns ;
71
74
private legendDataMap : Object = { } ;
72
75
private brush : BrushBehavior < { } > ;
73
76
private focusedLegendEntry : string ;
@@ -106,9 +109,9 @@ export class LineChartService {
106
109
let chart : D3Selection < D3BaseType , { } , D3ContainerElement , { } > = d3Select ( 'g#time-series-chart-drawing-area' ) ;
107
110
let xScale : D3ScaleTime < number , number > = this . getXScale ( data ) ;
108
111
let yScale : D3ScaleLinear < number , number > = this . getYScale ( data ) ;
109
- this . _labelGroupHeight = data . length * ChartCommons . LABEL_HEIGHT ;
112
+ this . calculateLegendDimensions ( ) ;
110
113
d3Select ( 'osm-time-series-line-chart' ) . transition ( ) . duration ( 500 ) . style ( 'visibility' , 'visible' ) ;
111
- d3Select ( 'svg#time-series-chart' ) . transition ( ) . duration ( 500 ) . attr ( 'height' , this . _height + this . _labelGroupHeight + this . _margin . top + this . _margin . bottom ) ;
114
+ d3Select ( 'svg#time-series-chart' ) . transition ( ) . duration ( 500 ) . attr ( 'height' , this . _height + this . _legendGroupHeight + this . _margin . top + this . _margin . bottom ) ;
112
115
d3Select ( '.x-axis' ) . transition ( ) . call ( this . updateXAxis , xScale ) ;
113
116
d3Select ( '.y-axis' ) . transition ( ) . call ( this . updateYAxis , yScale , this . _width , this . _margin ) ;
114
117
this . brush = d3BrushX ( ) . extent ( [ [ 0 , 0 ] , [ this . _width , this . _height ] ] ) ;
@@ -187,6 +190,7 @@ export class LineChartService {
187
190
. attr ( 'height' , 0 ) ;
188
191
189
192
svg . append ( 'g' )
193
+ . attr ( 'id' , 'time-series-chart-legend' )
190
194
. attr ( 'class' , 'legend-group' )
191
195
. attr ( 'transform' , `translate(${ this . _legendGroupLeft } , ${ this . _legendGroupTop } )` ) ;
192
196
@@ -245,6 +249,34 @@ export class LineChartService {
245
249
. nice ( ) ;
246
250
}
247
251
252
+ private calculateLegendDimensions ( ) : void {
253
+ let maximumLabelWidth : number = 1 ;
254
+ let labels = Object . keys ( this . legendDataMap ) ;
255
+
256
+ d3Select ( 'g#time-series-chart-legend' )
257
+ . append ( 'g' )
258
+ . attr ( 'id' , 'renderToCalculateMaxWidth' )
259
+ . selectAll ( '.renderToCalculateMaxWidth' )
260
+ . data ( labels )
261
+ . enter ( )
262
+ . append ( 'text' )
263
+ . attr ( 'class' , 'legend-text' )
264
+ . text ( datum => this . legendDataMap [ datum ] . text )
265
+ . each ( ( datum , index , groups ) => {
266
+ Array . from ( groups ) . forEach ( ( text ) => {
267
+ if ( text ) {
268
+ maximumLabelWidth = Math . max ( maximumLabelWidth , text . getBoundingClientRect ( ) . width )
269
+ }
270
+ } ) ;
271
+ } ) ;
272
+
273
+ d3Select ( 'g#renderToCalculateMaxWidth' ) . remove ( ) ;
274
+
275
+ this . _legendGroupColumnWidth = maximumLabelWidth + ChartCommons . COLOR_PREVIEW_SIZE + 30 ;
276
+ this . _legendGroupColumns = Math . floor ( this . _width / this . _legendGroupColumnWidth ) ;
277
+ this . _legendGroupHeight = Math . ceil ( labels . length / this . _legendGroupColumns ) * ChartCommons . LABEL_HEIGHT + 30 ;
278
+ }
279
+
248
280
private getMaxValue ( data : TimeSeries [ ] ) : number {
249
281
return d3Max ( data , ( dataItem : TimeSeries ) => {
250
282
return d3Max ( dataItem . values , ( point : TimeSeriesPoint ) => {
@@ -761,13 +793,8 @@ export class LineChartService {
761
793
}
762
794
763
795
private getPosition ( index : number ) : string {
764
- const columns = 3 ;
765
- const columnWidth = 550 ;
766
- const xMargin = 10 ;
767
- const yMargin = 10 ;
768
-
769
- const x = index % columns * columnWidth + xMargin ;
770
- const y = Math . floor ( index / columns ) * ChartCommons . LABEL_HEIGHT + yMargin ;
796
+ const x = index % this . _legendGroupColumns * this . _legendGroupColumnWidth ;
797
+ const y = Math . floor ( index / this . _legendGroupColumns ) * ChartCommons . LABEL_HEIGHT + 12 ;
771
798
772
799
return "translate(" + x + "," + y + ")" ;
773
800
}
0 commit comments