1
1
import * as d3 from 'd3' ;
2
2
3
3
export class ExposureStatsTile {
4
- constructor ( container , data ) {
5
- let container_div ;
4
+ constructor ( container , data ) {
5
+ let container_div ;
6
6
7
7
if ( typeof container === 'string' ) {
8
8
container_div = document . querySelector ( container ) ;
@@ -13,7 +13,7 @@ export class ExposureStatsTile {
13
13
container_div . innerHTML = '' ;
14
14
15
15
d3 . select ( container_div ) . attr ( 'chart_type' , 'ExposureStatsTile' ) ;
16
-
16
+
17
17
container_div . classList . add ( 'ExposureStatsTile' ) ;
18
18
container_div . classList . add ( 'd3chart' ) ;
19
19
container_div . classList . add ( 'chart_container' ) ;
@@ -28,11 +28,11 @@ export class ExposureStatsTile {
28
28
const marginBottom = 5 ;
29
29
const marginLeft = 5 ;
30
30
31
- // Declare text positions
32
- let asset_posiiton_x = ( width - marginLeft ) / 2 ,
33
- text_position_y = height - marginBottom - 35 ,
34
- perc_position_y = ( height - marginBottom ) / 2 - 10 ,
35
- sector_position_x = width - marginLeft ;
31
+ // Declare text positions
32
+ let asset_posiiton_x = ( width - marginLeft ) / 2 ,
33
+ text_position_y = height - marginBottom - 35 ,
34
+ perc_position_y = ( height - marginBottom ) / 2 - 10 ,
35
+ sector_position_x = width - marginLeft ;
36
36
37
37
// Create the svg container
38
38
const svg = this . container
@@ -43,88 +43,86 @@ export class ExposureStatsTile {
43
43
. attr ( 'preserveAspectRatio' , 'xMinYMin meet' )
44
44
. attr ( 'style' , 'max-width: 100%; height: auto;' ) ;
45
45
46
- // Filter data
47
- let sector = document . querySelector ( '#sector_selector' ) . value ,
46
+ // Filter data
47
+ let sector = document . querySelector ( '#sector_selector' ) . value ,
48
48
asset_class = document . querySelector ( '#asset_class_selector' ) . value ;
49
-
50
- let subdata = data
51
- . filter ( ( d ) => d . asset_type == asset_class )
52
- . filter ( ( d ) => d . sector == sector ) ;
53
-
54
- // Annotate with stats info - asset class
55
- svg
56
- . append ( 'g' )
57
- . attr ( 'transform' , 'translate(' + [ asset_posiiton_x , marginTop ] + ')' )
58
- . append ( 'text' )
59
- . text ( asset_class )
60
- . style ( 'dominant-baseline' , 'middle' )
61
- . style ( 'font-weight' , 'bold' )
62
- . style ( 'text-anchor' , 'end' ) ;
63
-
64
- svg
65
- . append ( 'g' )
66
- . attr ( 'transform' , 'translate(' + [ asset_posiiton_x , perc_position_y ] + ')' )
67
- . append ( 'text' )
68
- . text ( prcnt_format ( subdata [ 0 ] . percentage_value_invested ) )
69
- . style ( 'dominant-baseline' , 'middle' )
70
- . style ( 'font-weight' , 'bold' )
71
- . style ( 'text-anchor' , 'end' )
72
- . style ( 'font-size' , '2.5em' ) ;
73
-
74
- let label_total = [ 'of the' , 'total portfolio' ] ;
75
- svg
76
- . append ( 'g' )
77
- . attr ( 'transform' , 'translate(' + [ asset_posiiton_x , text_position_y ] + ')' )
78
- . selectAll ( 'text' )
79
- . data ( label_total )
80
- . enter ( )
81
- . append ( 'text' )
82
- . attr ( 'transform' , ( d , i ) => 'translate(0, ' + ( i * 15 ) + ')' )
83
- . text ( d => d )
84
- . style ( 'dominant-baseline' , 'middle' )
85
- . style ( 'text-anchor' , 'end' ) ;
86
-
87
- // Annotate with stats info - sector
88
- svg
89
- . append ( 'g' )
90
- . attr ( 'transform' , 'translate(' + [ sector_position_x , marginTop ] + ')' )
91
- . append ( 'text' )
92
- . text ( sector )
93
- . style ( 'dominant-baseline' , 'middle' )
94
- . style ( 'font-weight' , 'bold' )
95
- . style ( 'text-anchor' , 'end' ) ;
96
-
97
- svg
98
- . append ( 'g' )
99
- . attr ( 'transform' , 'translate(' + [ sector_position_x , perc_position_y ] + ')' )
100
- . append ( 'text' )
101
- . text ( prcnt_format ( subdata [ 0 ] . perc_asset_val_sector ) )
102
- . style ( 'dominant-baseline' , 'middle' )
103
- . style ( 'font-weight' , 'bold' )
104
- . style ( 'text-anchor' , 'end' )
105
- . style ( 'font-size' , '2.5em' ) ;
106
-
107
- let label_sector = [ 'of the' , asset_class , 'portion of portfolio' ] ;
108
- svg
109
- . append ( 'g' )
110
- . attr ( 'transform' , 'translate(' + [ sector_position_x , text_position_y ] + ')' )
111
- . selectAll ( 'text' )
112
- . data ( label_sector )
113
- . enter ( )
114
- . append ( 'text' )
115
- . attr ( 'transform' , ( d , i ) => 'translate(0, ' + ( i * 15 ) + ')' )
116
- . text ( d => d )
117
- . style ( 'dominant-baseline' , 'middle' )
118
- . style ( 'text-anchor' , 'end' ) ;
119
-
120
- function prcnt_format ( num ) {
49
+
50
+ let subdata = data . filter ( ( d ) => d . asset_type == asset_class ) . filter ( ( d ) => d . sector == sector ) ;
51
+
52
+ // Annotate with stats info - asset class
53
+ svg
54
+ . append ( 'g' )
55
+ . attr ( 'transform' , 'translate(' + [ asset_posiiton_x , marginTop ] + ')' )
56
+ . append ( 'text' )
57
+ . text ( asset_class )
58
+ . style ( 'dominant-baseline' , 'middle' )
59
+ . style ( 'font-weight' , 'bold' )
60
+ . style ( 'text-anchor' , 'end' ) ;
61
+
62
+ svg
63
+ . append ( 'g' )
64
+ . attr ( 'transform' , 'translate(' + [ asset_posiiton_x , perc_position_y ] + ')' )
65
+ . append ( 'text' )
66
+ . text ( prcnt_format ( subdata [ 0 ] . percentage_value_invested ) )
67
+ . style ( 'dominant-baseline' , 'middle' )
68
+ . style ( 'font-weight' , 'bold' )
69
+ . style ( 'text-anchor' , 'end' )
70
+ . style ( 'font-size' , '2.5em' ) ;
71
+
72
+ let label_total = [ 'of the' , 'total portfolio' ] ;
73
+ svg
74
+ . append ( 'g' )
75
+ . attr ( 'transform' , 'translate(' + [ asset_posiiton_x , text_position_y ] + ')' )
76
+ . selectAll ( 'text' )
77
+ . data ( label_total )
78
+ . enter ( )
79
+ . append ( 'text' )
80
+ . attr ( 'transform' , ( d , i ) => 'translate(0, ' + i * 15 + ')' )
81
+ . text ( ( d ) => d )
82
+ . style ( 'dominant-baseline' , 'middle' )
83
+ . style ( 'text-anchor' , 'end' ) ;
84
+
85
+ // Annotate with stats info - sector
86
+ svg
87
+ . append ( 'g' )
88
+ . attr ( 'transform' , 'translate(' + [ sector_position_x , marginTop ] + ')' )
89
+ . append ( 'text' )
90
+ . text ( sector )
91
+ . style ( 'dominant-baseline' , 'middle' )
92
+ . style ( 'font-weight' , 'bold' )
93
+ . style ( 'text-anchor' , 'end' ) ;
94
+
95
+ svg
96
+ . append ( 'g' )
97
+ . attr ( 'transform' , 'translate(' + [ sector_position_x , perc_position_y ] + ')' )
98
+ . append ( 'text' )
99
+ . text ( prcnt_format ( subdata [ 0 ] . perc_asset_val_sector ) )
100
+ . style ( 'dominant-baseline' , 'middle' )
101
+ . style ( 'font-weight' , 'bold' )
102
+ . style ( 'text-anchor' , 'end' )
103
+ . style ( 'font-size' , '2.5em' ) ;
104
+
105
+ let label_sector = [ 'of the' , asset_class , 'portion of portfolio' ] ;
106
+ svg
107
+ . append ( 'g' )
108
+ . attr ( 'transform' , 'translate(' + [ sector_position_x , text_position_y ] + ')' )
109
+ . selectAll ( 'text' )
110
+ . data ( label_sector )
111
+ . enter ( )
112
+ . append ( 'text' )
113
+ . attr ( 'transform' , ( d , i ) => 'translate(0, ' + i * 15 + ')' )
114
+ . text ( ( d ) => d )
115
+ . style ( 'dominant-baseline' , 'middle' )
116
+ . style ( 'text-anchor' , 'end' ) ;
117
+
118
+ function prcnt_format ( num ) {
121
119
if ( num == 0 ) {
122
120
return '0%' ;
123
121
} else if ( num < 0.001 ) {
124
- return '< 0.1%' ;
125
- } else {
122
+ return '< 0.1%' ;
123
+ } else {
126
124
return d3 . format ( '.2p' ) ( num ) ;
127
125
}
128
126
}
129
- }
130
- }
127
+ }
128
+ }
0 commit comments