@@ -34,99 +34,107 @@ function getColor(d) {
34
34
'#fde0dd' ;
35
35
}
36
36
37
- var industryName = 'Manufacturing' ;
38
- industryVue . setIndustry ( industryName ) ;
39
-
40
- //Add feature styling to map
41
- function style ( feature ) {
42
- return {
43
- fillColor : getColor ( feature . properties [ industryName ] . locq ) ,
44
- weight : 1 ,
45
- opacity : 1 ,
46
- color : 'white' ,
47
- dashArray : '3' ,
48
- fillOpacity : 0.5
49
- } ;
50
- }
51
- L . geoJson ( statesData , { style : style } ) . addTo ( statesmap ) ;
52
-
53
- //Define a mouseover highlight listener
54
- function highlightFeature ( e ) {
55
- var layer = e . target ;
56
- //todo vm.data.selected = e.target
57
- layer . setStyle ( {
58
- weight : 3 ,
59
- color : '#3388ff' ,
60
- dashArray : '' ,
61
- fillOpacity : 0.9
62
- } ) ;
63
-
64
- if ( ! L . Browser . ie && ! L . Browser . opera && ! L . Browser . edge ) {
65
- layer . bringToFront ( ) ;
37
+ //Initialize map with default industry
38
+ styleMapforIndustry ( 'Manufacturing' ) ;
39
+
40
+ //Wrap up all map functions in one, starting here:
41
+ var info , legend ;
42
+ function styleMapforIndustry ( industry ) {
43
+ //Add feature styling to map
44
+ function style ( feature ) {
45
+ return {
46
+ fillColor : getColor ( feature . properties [ industry ] . locq ) ,
47
+ weight : 1 ,
48
+ opacity : 1 ,
49
+ color : 'white' ,
50
+ dashArray : '3' ,
51
+ fillOpacity : 0.5
52
+ } ;
66
53
}
54
+ L . geoJson ( statesData , { style : style } ) . addTo ( statesmap ) ;
67
55
68
- info . update ( layer . feature . properties ) ;
56
+ //Define a mouseover highlight listener
57
+ function highlightFeature ( e ) {
58
+ var layer = e . target ;
59
+ //todo vm.data.selected = e.target
60
+ layer . setStyle ( {
61
+ weight : 3 ,
62
+ color : '#3388ff' ,
63
+ dashArray : '' ,
64
+ fillOpacity : 0.9
65
+ } ) ;
69
66
70
- }
71
- //Define highlight reset
72
- function resetHighlight ( e ) {
73
- geojson . resetStyle ( e . target ) ;
74
- info . update ( )
75
- }
67
+ if ( ! L . Browser . ie && ! L . Browser . opera && ! L . Browser . edge ) {
68
+ layer . bringToFront ( ) ;
69
+ }
76
70
77
- //Add event listeners for interactivity
78
- function onEachFeature ( feature , layer ) {
79
- layer . on ( {
80
- mouseover : highlightFeature ,
81
- mouseout : resetHighlight ,
82
- click : zoomToFeature
83
- } ) ;
84
- }
71
+ info . update ( layer . feature . properties ) ;
85
72
86
- //Zoom to state
87
- function zoomToFeature ( e ) {
88
- statesmap . fitBounds ( e . target . getBounds ( ) ) ;
89
- }
73
+ }
74
+ //Define highlight reset
75
+ function resetHighlight ( e ) {
76
+ geojson . resetStyle ( e . target ) ;
77
+ info . update ( )
78
+ }
90
79
91
- geojson = L . geoJson ( statesData , {
92
- style : style ,
93
- onEachFeature : onEachFeature
94
- } ) . addTo ( statesmap ) ;
80
+ //Add event listeners for interactivity
81
+ function onEachFeature ( feature , layer ) {
82
+ layer . on ( {
83
+ mouseover : highlightFeature ,
84
+ mouseout : resetHighlight ,
85
+ click : zoomToFeature
86
+ } ) ;
87
+ }
95
88
96
- //Add info display panel
97
- var info = L . control ( ) ;
89
+ //Zoom to state
90
+ function zoomToFeature ( e ) {
91
+ statesmap . fitBounds ( e . target . getBounds ( ) ) ;
92
+ }
98
93
99
- info . onAdd = function ( map ) {
100
- this . _div = L . DomUtil . create ( 'div' , 'info' ) ; // create a div with a class "info"
101
- this . update ( ) ;
102
- return this . _div ;
103
- } ;
94
+ geojson = L . geoJson ( statesData , {
95
+ style : style ,
96
+ onEachFeature : onEachFeature
97
+ } ) . addTo ( statesmap ) ;
104
98
105
- info . update = function ( props ) {
106
- this . _div . innerHTML = '<h4>Automation Quotient By State</h4>' + ( props ?
107
- '<b>' + props . name + '</b><br />' + 'Quotient: ' + props [ industryName ] . locq . toFixed ( 4 )
108
- : 'Hover over a state' ) ;
109
- } ;
99
+ //Add info display panel
100
+ if ( info ) { info . remove ( ) } //clear if called before
110
101
111
- info . addTo ( statesmap ) ;
102
+ info = L . control ( ) ;
112
103
113
- //Add legend
114
- var legend = L . control ( { position : 'bottomright' } ) ;
104
+ info . onAdd = function ( map ) {
105
+ this . _div = L . DomUtil . create ( 'div' , 'info' ) ; // create a div with a class "info"
106
+ this . update ( ) ;
107
+ return this . _div ;
108
+ } ;
115
109
116
- legend . onAdd = function ( map ) {
110
+ info . update = function ( props ) {
111
+ this . _div . innerHTML = '<h4>Automation Quotient By State</h4>' + ( props ?
112
+ '<b>' + props . name + '</b><br />' + 'Quotient: ' + props [ industry ] . locq . toFixed ( 4 )
113
+ : 'Hover over a state' ) ;
114
+ } ;
117
115
118
- var div = L . DomUtil . create ( 'div' , 'info legend' ) ,
119
- grades = [ 0 , .25 , .5 , .75 , 1.0 , 1.25 , 1.5 , 1.75 ] ,
120
- labels = [ ] ;
116
+ info . addTo ( statesmap ) ;
121
117
122
- // loop through our density intervals and generate a label with a colored square for each interval
123
- for ( var i = 0 ; i < grades . length ; i ++ ) {
124
- div . innerHTML +=
125
- '<i style="background:' + getColor ( grades [ i ] + .25 ) + '"></i> ' +
126
- grades [ i ] + ( grades [ i + 1 ] ? '–' + grades [ i + 1 ] + '<br>' : '+' ) ;
127
- }
118
+ //Add legend
119
+ if ( legend ) { legend . remove ( ) }
120
+ legend = L . control ( { position : 'bottomright' } ) ;
121
+
122
+ legend . onAdd = function ( map ) {
128
123
129
- return div ;
130
- } ;
124
+ var div = L . DomUtil . create ( 'div' , 'info legend' ) ,
125
+ grades = [ 0 , .25 , .5 , .75 , 1.0 , 1.25 , 1.5 , 1.75 ] ,
126
+ labels = [ ] ;
131
127
132
- legend . addTo ( statesmap ) ;
128
+ // loop through our density intervals and generate a label with a colored square for each interval
129
+ for ( var i = 0 ; i < grades . length ; i ++ ) {
130
+ div . innerHTML +=
131
+ '<i style="background:' + getColor ( grades [ i ] + .25 ) + '"></i> ' +
132
+ grades [ i ] + ( grades [ i + 1 ] ? '–' + grades [ i + 1 ] + '<br>' : '+' ) ;
133
+ }
134
+
135
+ return div ;
136
+ } ;
137
+
138
+ legend . addTo ( statesmap ) ;
139
+
140
+ }
0 commit comments