|
10 | 10 | setProjection: setProjection,
|
11 | 11 | projection: 'equirectangular',
|
12 | 12 | dataType: 'json',
|
| 13 | + data: {}, |
13 | 14 | done: function() {},
|
14 | 15 | fills: {
|
15 | 16 | defaultFill: '#ABDDA4'
|
|
35 | 36 | borderWidth: 2,
|
36 | 37 | borderColor: '#FFFFFF',
|
37 | 38 | popupOnHover: true,
|
38 |
| - radius: 80, |
| 39 | + radius: null, |
39 | 40 | popupTemplate: function(geography, data) {
|
40 | 41 | return '<div class="hoverinfo"><strong>' + data.name + '</strong></div>';
|
41 | 42 | },
|
|
60 | 61 | Getter for value. If not declared on datumValue, look up the chain into optionsValue
|
61 | 62 | */
|
62 | 63 | function val( datumValue, optionsValue, context ) {
|
63 |
| - |
| 64 | + if ( typeof context === 'undefined' ) { |
| 65 | + context = optionsValue; |
| 66 | + optionsValues = undefined; |
| 67 | + } |
64 | 68 | var value = typeof datumValue !== 'undefined' ? datumValue : optionsValue;
|
65 | 69 |
|
| 70 | + if (typeof value === 'undefined') { |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
66 | 74 | if ( typeof value === 'function' ) {
|
67 |
| - console.log(value, context); |
68 |
| - console.log( 'GetValueFn', value.apply(null, [context]) ); |
69 |
| - return value.apply(null, [context]); |
| 75 | + var fnContext = [context]; |
| 76 | + if ( context.geography ) { |
| 77 | + fnContext = [context.geography, context.data]; |
| 78 | + } |
| 79 | + return value.apply(null, fnContext); |
70 | 80 | }
|
71 | 81 | else {
|
72 |
| - console.log( 'GetValueLiteral', value); |
73 | 82 | return value;
|
74 | 83 | }
|
75 | 84 | }
|
|
174 | 183 | return JSON.stringify( colorCodeData[d.id]);
|
175 | 184 | })
|
176 | 185 | .style('fill', function(d) {
|
| 186 | + //if fillKey - use that |
| 187 | + //otherwise check 'fill' |
| 188 | + //otherwise check 'defaultFill' |
177 | 189 | var fillColor;
|
178 | 190 |
|
179 |
| - if ( colorCodeData[d.id] ) { |
180 |
| - fillColor = fillData[ colorCodeData[d.id].fillKey ]; |
| 191 | + var datum = colorCodeData[d.id]; |
| 192 | + if ( datum && datum.fillKey ) { |
| 193 | + fillColor = fillData[ val(datum.fillKey, {data: colorCodeData[d.id], geography: d}) ]; |
| 194 | + } |
| 195 | + else { |
| 196 | + fillColor = val(datum && datum.fillColor, fillData.defaultFill, {data: colorCodeData[d.id], geography: d}); |
181 | 197 | }
|
182 | 198 |
|
183 |
| - return fillColor || fillData.defaultFill; |
| 199 | + return fillColor; |
184 | 200 | })
|
185 | 201 | .style('stroke-width', geoConfig.borderWidth)
|
186 | 202 | .style('stroke', geoConfig.borderColor);
|
|
196 | 212 | svg.selectAll('.datamaps-subunit')
|
197 | 213 | .on('mouseover', function(d) {
|
198 | 214 | var $this = d3.select(this);
|
199 |
| - |
| 215 | + var datum = self.options.data[d.id] || {}; |
200 | 216 | if ( options.highlightOnHover ) {
|
201 | 217 | var previousAttributes = {
|
202 | 218 | 'fill': $this.style('fill'),
|
|
206 | 222 | };
|
207 | 223 |
|
208 | 224 | $this
|
209 |
| - .style('fill', options.highlightFillColor) |
210 |
| - .style('stroke', options.highlightBorderColor) |
211 |
| - .style('stroke-width', options.highlightBorderWidth) |
212 |
| - .style('fill-opacity', options.highlightFillOpacity) |
| 225 | + .style('fill', val(datum.highlightFillColor, options.highlightFillColor, datum)) |
| 226 | + .style('stroke', val(datum.highlightBorderColor, options.highlightBorderColor, datum)) |
| 227 | + .style('stroke-width', val(datum.highlightBorderWidth, options.highlightBorderWidth, datum)) |
| 228 | + .style('fill-opacity', val(datum.highlightFillOpacity, options.highlightFillOpacity, datum)) |
213 | 229 | .attr('data-previousAttributes', JSON.stringify(previousAttributes));
|
214 | 230 |
|
215 | 231 | //as per discussion on https://github.com/markmarkoh/datamaps/issues/19
|
|
311 | 327 | var path = d3.geo.path()
|
312 | 328 | .projection(self.projection);
|
313 | 329 |
|
314 |
| - // TODO: Move this to inside `if` clause when setting attr `d` |
315 |
| - var arc = d3.geo.greatArc() |
316 |
| - .source(function(d) { return [d.origin.longitude, d.origin.latitude]; }) |
317 |
| - .target(function(d) { return [d.destination.longitude, d.destination.latitude]; }); |
318 |
| - |
319 | 330 | arcs
|
320 | 331 | .enter()
|
321 | 332 | .append('svg:path')
|
|
329 | 340 | return val(datum.strokeWidth, options.strokeWidth, datum);
|
330 | 341 | })
|
331 | 342 | .attr('d', function(datum) {
|
332 |
| - var originXY = self.latLngToXY(datum.origin.latitude, datum.origin.longitude); |
333 |
| - var destXY = self.latLngToXY(datum.destination.latitude, datum.destination.longitude); |
| 343 | + var originXY = self.latLngToXY(val(datum.origin.latitude, datum), val(datum.origin.longitude, datum)) |
| 344 | + var destXY = self.latLngToXY(val(datum.destination.latitude, datum), val(datum.destination.longitude, datum)); |
334 | 345 | var midXY = [ (originXY[0] + destXY[0]) / 2, (originXY[1] + destXY[1]) / 2];
|
335 | 346 | if (options.greatArc) {
|
336 |
| - return path(arc(datum)) |
| 347 | + // TODO: Move this to inside `if` clause when setting attr `d` |
| 348 | + var greatArc = d3.geo.greatArc() |
| 349 | + .source(function(d) { return [val(d.origin.longitude, d), val(d.origin.latitude, d)]; }) |
| 350 | + .target(function(d) { return [val(d.destination.longitude, d), val(d.destination.latitude, d)]; }); |
| 351 | + |
| 352 | + return path(greatArc(datum)) |
337 | 353 | }
|
338 | 354 | var sharpness = val(datum.arcSharpness, options.arcSharpness, datum);
|
339 | 355 | return "M" + originXY[0] + ',' + originXY[1] + "S" + (midXY[0] + (50 * sharpness)) + "," + (midXY[1] - (75 * sharpness)) + "," + destXY[0] + "," + destXY[1];
|
|
0 commit comments