|
49 | 49 | .legend.vertical .legend-item {white-space: nowrap;} |
50 | 50 | .legend-value {display: inline-block; line-height: 18px; vertical-align: top;} |
51 | 51 | .legend.horizontal ul.legend-content li.legend-item .legend-value, |
52 | | - .legend.horizontal ul.legend-content li.legend-item {display: inline-block; float: left; width: 30px; margin-bottom: 0; text-align: center; height: 30px;} |
| 52 | + .legend.horizontal ul.legend-content li.legend-item {display: inline-block; float: left; width: 30px; margin-bottom: 0; text-align: center; min-height: 30px;} |
53 | 53 |
|
54 | 54 | /* legend key styles */ |
55 | 55 | .legend-key {display: inline-block; height: 10px;} |
|
72 | 72 | .legend.vertical.contig li.legend-item {height: 15px;} |
73 | 73 | .legend.vertical.contig {padding-bottom: 6px;} |
74 | 74 |
|
| 75 | + /* vertical radius legend */ |
| 76 | + .legend.horizontal.legend-variable-radius ul.legend-content li.legend-item .legend-value, |
| 77 | + .legend.horizontal.legend-variable-radius ul.legend-content li.legend-item {width: 30px; min-height: 20px;} |
| 78 | + |
75 | 79 | </style> |
| 80 | + |
76 | 81 | {% block extra_css %}{% endblock extra_css %} |
| 82 | + |
77 | 83 | </head> |
78 | 84 | <body> |
79 | 85 |
|
|
84 | 90 | var legendHeader; |
85 | 91 |
|
86 | 92 | function calcColorLegend(myColorStops, title) { |
87 | | - |
88 | 93 | // create legend |
89 | | - var legend = document.createElement('div'); |
| 94 | + var legend = document.createElement('div'), |
| 95 | + legendContainer = document.getElementsByClassName('mapboxgl-ctrl-bottom-right')[0]; |
| 96 | + |
90 | 97 | if ('{{ legendKeyShape }}' === 'contiguous-bar') { |
91 | 98 | legend.className = 'legend {{ legendLayout }} contig'; |
92 | 99 | } |
93 | 100 | else { |
94 | 101 | legend.className = 'legend {{ legendLayout }}'; |
95 | 102 | } |
96 | | - |
97 | | - legend.id = 'legend'; |
| 103 | + legend.id = 'legend-0'; |
98 | 104 | document.body.appendChild(legend); |
99 | | - |
100 | 105 | // add legend header and content elements |
101 | 106 | var mytitle = document.createElement('div'), |
102 | 107 | legendContent = document.createElement('ul'); |
|
108 | 113 | legendHeader.appendChild(mytitle); |
109 | 114 | legend.appendChild(legendHeader); |
110 | 115 | legend.appendChild(legendContent); |
111 | | - |
112 | 116 | if ({{ legendGradient|safe }} === true) { |
113 | | - var gradientText = 'linear-gradient(to right, '; |
114 | | - var gradient = document.createElement('div'); |
| 117 | + var gradientText = 'linear-gradient(to right, ', |
| 118 | + gradient = document.createElement('div'); |
115 | 119 | gradient.className = 'gradient-bar'; |
116 | 120 | legend.appendChild(gradient); |
117 | 121 | } |
118 | | - |
119 | 122 | // calculate a legend entries on a Mapbox GL Style Spec property function stops array |
120 | 123 | for (p = 0; p < myColorStops.length; p++) { |
121 | | - if (!!document.getElementById('legend-points-value-' + p)) { |
122 | | - //update the legend if it already exists |
123 | | - document.getElementById('legend-points-value-' + p).textContent = myColorStops[p][0]; |
124 | | - document.getElementById('legend-points-id-' + p).style.backgroundColor = myColorStops[p][1]; |
| 124 | + if (!!document.getElementById('legend-color-points-value-' + p)) { |
| 125 | + // update the legend if it already exists |
| 126 | + document.getElementById('legend-color-points-value-' + p).textContent = myColorStops[p][0]; |
| 127 | + document.getElementById('legend-color-points-id-' + p).style.backgroundColor = myColorStops[p][1]; |
125 | 128 | } |
126 | 129 | else { |
127 | 130 | // create the legend if it doesn't yet exist |
128 | 131 | var item = document.createElement('li'); |
129 | 132 | item.className = 'legend-item'; |
130 | | - |
131 | 133 | var key = document.createElement('span'); |
132 | 134 | key.className = 'legend-key {{ legendKeyShape }}'; |
133 | | - key.id = 'legend-points-id-' + p; |
134 | | - key.style.backgroundColor = myColorStops[p][1]; |
135 | | - |
| 135 | + key.id = 'legend-color-points-id-' + p; |
| 136 | + key.style.backgroundColor = myColorStops[p][1]; |
136 | 137 | var value = document.createElement('span'); |
137 | 138 | value.className = 'legend-value'; |
138 | | - value.id = 'legend-points-value-' + p; |
139 | | - |
| 139 | + value.id = 'legend-color-points-value-' + p; |
140 | 140 | item.appendChild(key); |
141 | 141 | item.appendChild(value); |
142 | 142 | legendContent.appendChild(item); |
143 | 143 |
|
144 | | - data = document.getElementById('legend-points-value-' + p) |
145 | | - |
| 144 | + data = document.getElementById('legend-color-points-value-' + p) |
146 | 145 | // round number values in legend if precision defined |
147 | 146 | if ((typeof(myColorStops[p][0]) == 'number') && (typeof({{ legendNumericPrecision }}) == 'number')) { |
148 | 147 | data.textContent = myColorStops[p][0].toFixed({{ legendNumericPrecision }}); |
149 | 148 | } |
150 | 149 | else { |
151 | 150 | data.textContent = myColorStops[p][0]; |
152 | 151 | } |
153 | | - |
154 | 152 | // add color stop to gradient list |
155 | 153 | if ({{ legendGradient|safe }} === true) { |
156 | 154 | if (p < myColorStops.length - 1) { |
|
165 | 163 | } |
166 | 164 | } |
167 | 165 | } |
168 | | - |
169 | 166 | if ({{ legendGradient|safe }} === true) { |
170 | 167 | // convert to gradient scale appearance |
171 | 168 | gradient.style.background = gradientText; |
172 | | - |
173 | 169 | // hide legend keys generated above |
174 | 170 | var keys = document.getElementsByClassName('legend-key'); |
175 | 171 | for (var i=0; i < keys.length; i++) { |
176 | 172 | keys[i].style.visibility = 'hidden'; |
177 | 173 | } |
178 | | - |
179 | 174 | if ('{{ legendLayout }}' === 'vertical') { |
180 | 175 | gradient.style.height = (legendContent.offsetHeight - 6) + 'px'; |
181 | 176 | } |
182 | 177 | } |
183 | | - |
184 | 178 | // add class for styling bordered legend keys |
185 | 179 | if ({{ legendKeyBordersOn|safe }}) { |
186 | 180 | var keys = document.getElementsByClassName('legend-key'); |
|
196 | 190 | } |
197 | 191 | } |
198 | 192 | } |
| 193 | + // update right-margin for compact Mapbox attribution based on calculated legend width |
| 194 | + updateAttribMargin(legend); |
| 195 | + updateLegendMargin(legend); |
| 196 | +} |
| 197 | + |
| 198 | + |
| 199 | +function calcRadiusLegend(myRadiusStops, title, color) { |
| 200 | + |
| 201 | + // maximum legend item height |
| 202 | + var maxLegendItemHeight = 2 * myRadiusStops[myRadiusStops.length - 1][1]; |
| 203 | + |
| 204 | + // create legend |
| 205 | + var legend = document.createElement('div'); |
| 206 | + legend.className = 'legend {{ legendLayout }} legend-variable-radius'; |
| 207 | + |
| 208 | + legend.id = 'legend-1'; |
| 209 | + document.body.appendChild(legend); |
| 210 | + |
| 211 | + // add legend header and content elements |
| 212 | + var mytitle = document.createElement('div'), |
| 213 | + legendContent = document.createElement('ul'); |
| 214 | + legendHeader = document.createElement('div'); |
| 215 | + mytitle.textContent = title; |
| 216 | + mytitle.className = 'legend-title' |
| 217 | + legendHeader.className = 'legend-header' |
| 218 | + legendContent.className = 'legend-content' |
| 219 | + legendHeader.appendChild(mytitle); |
| 220 | + legend.appendChild(legendHeader); |
| 221 | + legend.appendChild(legendContent); |
| 222 | + |
| 223 | + // calculate a legend entries on a Mapbox GL Style Spec property function stops array |
| 224 | + for (p = 0; p < myRadiusStops.length; p++) { |
| 225 | + if (!!document.getElementById('legend-radius-points-value-' + p)) { |
| 226 | + //update the legend if it already exists |
| 227 | + document.getElementById('legend-radius-points-value-' + p).textContent = myRadiusStops[p][0]; |
| 228 | + document.getElementById('legend-radius-points-id-' + p).style.backgroundColor = color; |
| 229 | + } |
| 230 | + else { |
| 231 | + // create the legend if it doesn't yet exist |
| 232 | + var item = document.createElement('li'); |
| 233 | + item.className = 'legend-item'; |
| 234 | + item.height = '' + maxLegendItemHeight + 'px'; |
| 235 | + |
| 236 | + var key = document.createElement('span'); |
| 237 | + key.className = 'legend-key {{ legendKeyShape }}'; |
| 238 | + key.id = 'legend-radius-points-id-' + p; |
| 239 | + key.style.backgroundColor = color; |
| 240 | + |
| 241 | + key.style.width = '' + myRadiusStops[p][1] * 2 + 'px'; |
| 242 | + key.style.height = '' + myRadiusStops[p][1] * 2 + 'px'; |
| 243 | + |
| 244 | + keyVerticalMargin = (maxLegendItemHeight - myRadiusStops[p][1] * 2) * 0.5; |
| 245 | + key.style.marginTop = '' + keyVerticalMargin + 'px'; |
| 246 | + key.style.marginBottom = '' + keyVerticalMargin + 'px'; |
| 247 | + |
| 248 | + var value = document.createElement('span'); |
| 249 | + value.className = 'legend-value'; |
| 250 | + value.id = 'legend-radius-points-value-' + p; |
| 251 | + |
| 252 | + item.appendChild(key); |
| 253 | + item.appendChild(value); |
| 254 | + legendContent.appendChild(item); |
| 255 | + |
| 256 | + data = document.getElementById('legend-radius-points-value-' + p) |
| 257 | + |
| 258 | + // round number values in legend if precision defined |
| 259 | + if ((typeof(myRadiusStops[p][0]) == 'number') && (typeof({{ legendNumericPrecision }}) == 'number')) { |
| 260 | + data.textContent = myRadiusStops[p][0].toFixed({{ legendNumericPrecision }}); |
| 261 | + } |
| 262 | + else { |
| 263 | + data.textContent = myRadiusStops[p][0]; |
| 264 | + } |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + // add class for styling bordered legend keys |
| 269 | + if ({{ legendKeyBordersOn|safe }}) { |
| 270 | + var keys = document.getElementsByClassName('legend-key'); |
| 271 | + for (var i=0; i < keys.length; i++) { |
| 272 | + if (keys[i]) { |
| 273 | + keys[i].classList.add('bordered'); |
| 274 | + } |
| 275 | + } |
| 276 | + } |
199 | 277 |
|
200 | 278 | // update right-margin for compact Mapbox attribution based on calculated legend width |
| 279 | + updateAttribMargin(legend); |
| 280 | + updateLegendMargin(legend); |
| 281 | + |
| 282 | +} |
| 283 | + |
| 284 | + |
| 285 | +function updateAttribMargin(legend) { |
| 286 | + |
| 287 | + // default margin is based on calculated legend width |
201 | 288 | var attribMargin = legend.offsetWidth + 15; |
202 | | - document.getElementsByClassName('mapboxgl-ctrl-attrib')[0].style.marginRight = attribMargin.toString() + 'px'; |
| 289 | + |
| 290 | + // if horizontal legend layout (multiple legends are stacked vertically) |
| 291 | + if ('{{ legendLayout }}' === 'horizontal') { |
| 292 | + document.getElementsByClassName('mapboxgl-ctrl-attrib')[0].style.marginRight = (attribMargin).toString() + 'px'; |
| 293 | + } |
| 294 | + // vertical legend layout means multiple legends are side-by-side |
| 295 | + else if ('{{ legendLayout }}' === 'vertical') { |
| 296 | + var currentMargin = Number(document.getElementsByClassName('mapboxgl-ctrl-attrib')[0].style.marginRight.replace('px', '')); |
| 297 | + document.getElementsByClassName('mapboxgl-ctrl-attrib')[0].style.marginRight = (attribMargin + currentMargin).toString() + 'px'; |
| 298 | + } |
| 299 | +} |
| 300 | + |
203 | 301 |
|
| 302 | +function updateLegendMargin(legend) { |
| 303 | + |
| 304 | + var verticalLegends = document.getElementsByClassName('legend vertical'), |
| 305 | + horizontalLegends = document.getElementsByClassName('legend horizontal'); |
| 306 | + |
| 307 | + if (verticalLegends.length > 1) { |
| 308 | + for (i = 1; i < verticalLegends.length; i++) { |
| 309 | + verticalLegends[i].style.marginRight = (legend.offsetWidth - 5).toString() + 'px'; |
| 310 | + var legend = verticalLegends[i]; |
| 311 | + } |
| 312 | + } |
| 313 | + else if (horizontalLegends.length > 1) { |
| 314 | + for (i = 1; i < horizontalLegends.length; i++) { |
| 315 | + horizontalLegends[i].style.marginBottom = (legend.offsetHeight + 15).toString() + 'px'; |
| 316 | + var legend = horizontalLegends[i]; |
| 317 | + } |
| 318 | + } |
204 | 319 | } |
205 | 320 |
|
206 | 321 |
|
|
0 commit comments