From b6ac1c3e48747d573366d4a74f8f1fa5640bc611 Mon Sep 17 00:00:00 2001 From: Flo Ledermann Date: Mon, 23 Nov 2015 16:13:56 +0100 Subject: [PATCH] 0.2.7 --- mapmap.js | 29 +++++++++++++++-------------- mapmap.min.js | 4 ++-- package.json | 2 +- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/mapmap.js b/mapmap.js index faa5621..5bfddb4 100644 --- a/mapmap.js +++ b/mapmap.js @@ -1,4 +1,6 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.mapmap = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o. var dd = require('datadata'); -var version = '0.2.6'; +var version = '0.2.7'; // TODO: can we get rid of jQuery dependency through var extend = require("jquery-extend")? function _assert(test, message) { if (test) return; throw new Error("[mapmap] " + message);} @@ -800,6 +800,7 @@ mapmap.prototype.initEngine = function(element) { this.width = null; this.height = null; + // TODO: use options.width || options.defaultWidth etc. if (!this.width) { this.width = parseInt(mainEl.attr('width')) || 800; } @@ -955,6 +956,7 @@ var domain = [0,1]; var layer_counter = 0; +// TODO: think about caching loaded resources (#8) mapmap.prototype.geometry = function(spec, keyOrOptions) { // key is default option @@ -1080,8 +1082,7 @@ mapmap.prototype.geometry = function(spec, keyOrOptions) { // note this has to happen after merging into this._promise.geometry to make // sure layers are created first (e.g. for highlighting) this.promise_data(promise); - - + return this; }; @@ -1223,10 +1224,6 @@ mapmap.prototype.anchor = function(d) { }; mapmap.prototype.size = function() { - // TODO: - // our viewBox is set up for an extent of 800x400 units - // should we change this? - // bounds are re-calculate by initEvents on every resize return { width: this.width, @@ -1301,8 +1298,7 @@ mapmap.prototype.select = function(selection) { mapmap.prototype.highlight = function(selection) { var map = this; - - + if (selection === null) { map._elements.shadowEl.selectAll('path').remove(); map._elements.shadowCropEl.selectAll('path').remove(); @@ -1407,6 +1403,10 @@ mapmap.prototype.promise_data = function(promise) { // we not *always* fulfill with canonical data i.e. the // underlying selection, or keep canonical data and refresh // selection always? + // Also, we need to keep data that has no entities in the geometry + // e.g. for loading stats of aggregated entities. We could + // use a global array of GeoJSON features, as this allows + // either geometry or properties to be null -- fl 2015-11-21 var map = this; @@ -1432,6 +1432,7 @@ mapmap.prototype.then = function(callback) { return this; }; +// TODO: think about caching loaded resources (#8) mapmap.prototype.data = function(spec, keyOrOptions) { var options = dd.isDictionary(keyOrOptions) ? keyOrOptions : {map: keyOrOptions}; @@ -3224,5 +3225,5 @@ function keyOrCallback(val) { } module.exports = mapmap; -},{"datadata":1}]},{},[3])(3) +},{"datadata":2}]},{},[3])(3) }); \ No newline at end of file diff --git a/mapmap.min.js b/mapmap.min.js index bc5c950..c9a6354 100644 --- a/mapmap.min.js +++ b/mapmap.min.js @@ -1,2 +1,2 @@ -(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.mapmap=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o-1){doAdd=true;break}}for(j=0;j-1){doAdd=false;break}}if(doAdd&&prev[key]&&curr[key]&&!isNaN(prev[key])&&!isNaN(curr[key])){prev[key]=prev[key]+curr[key]}else{prev[key]=curr[key];if(doAdd){console.warn("datadata.emit.sum(): Cannot add keys "+key+"!")}}}return prev});emit(key,obj)}}};dd.map.geo={point:function(latProp,lonProp,keyProp){var id=0;return function(d,emit){var key=keyProp?d[keyProp]:id++;emit(key,dd.geo.Point(d[lonProp],d[latProp],d))}}};dd.emit.geo={segments:function(){return function(key,data,emit){var prev=null,cur=null;for(var i=0;i0){this.supports.hoverDomModification=false}else{this.supports.hoverDomModification=true}var map=this;this.current_scale=1;this.current_translate=[0,0];this.zoom=d3.behavior.zoom().translate([0,0]).scale(1).scaleExtent([1,8]).on("zoom",function(){map.current_scale=d3.event.scale;map.current_translate=d3.event.translate;mapEl.attr("transform","translate("+d3.event.translate+")scale("+d3.event.scale+")");if(!map.supports.nonScalingStroke){}});mapEl.call(this.zoom.event);var map=this;function constructEvent(event){var pos=[event.clientX,event.clientY];return{position:pos,location:map._projection.invert(pos),event:event}}mapEl.on("click",function(){map.dispatcher.click.call(map,constructEvent(d3.event))});mapEl.on("mousedown",function(){map.dispatcher.mousedown.call(map,constructEvent(d3.event))});mapEl.on("mouseup",function(){map.dispatcher.mousedown.call(map,constructEvent(d3.event))});mapEl.on("mousemove",function(){map.dispatcher.mousedown.call(map,constructEvent(d3.event))})};mapmap.prototype.initEvents=function(element){var map=this;function resize(){map.bounds=map.getBoundingClientRect();if(map.settings.keepAspectRatio){var width=element.getAttribute("width"),height=element.getAttribute("height");if(width&&height&&map.bounds.width){var ratio=width/height;element.style.height=map.bounds.width/ratio+"px"}}}window.onresize=resize;resize()};var domain=[0,1];var layer_counter=0;mapmap.prototype.geometry=function(spec,keyOrOptions){var options=dd.isString(keyOrOptions)?{key:keyOrOptions}:keyOrOptions;options=dd.merge({key:"id"},options);var map=this;if(dd.isFunction(spec)){this._promise.geometry.then(function(topo){var new_topo=spec(topo);if(typeof new_topo.length=="undefined"){new_topo=[new_topo]}new_topo.map(function(t){if(typeof t.geometry.length=="undefined"){t.geometry=[t.geometry]}if(typeof t.index=="undefined"){map.layers.push(t.name,t.geometry)}else{map.layers.insert(t.index,t.name,t.geometry)}});map.draw();if(options.ondraw)options.ondraw()});return this}if(dd.isArray(spec)){var new_topo=dd.mapreduce(spec,options.map,options.reduce);if(!options.layers){options.layers="layer-"+layer_counter++}map.layers.push(options.layers,new_topo.values());var promise=new Promise(function(resolve,reject){resolve(new_topo)});this.promise_data(promise);if(!map.selected_extent){map._extent(new_topo.values())}map.draw();if(options.ondraw)options.ondraw();return this}var promise=dd.load(spec);if(this._promise.geometry){var parent=this._promise.geometry;this._promise.geometry=new Promise(function(resolve,reject){parent.then(function(_){promise.then(function(data){resolve(data)})})})}else{this._promise.geometry=promise}this._promise.geometry.then(function(geom){if(geom.type&&geom.type=="Topology"){var keys=options.layers||Object.keys(geom.objects);keys.map(function(k){if(geom.objects[k]){var objs=topojson.feature(geom,geom.objects[k]).features;map.layers.push(k,objs);if(options.key){for(var i=0;i-1){left+=parseInt(cs.left.slice(0,-2))}if(bounds.left==left-scrollLeft){return bounds}var top=parentOffset.top,width=parseInt(cs.width.slice(0,-2)),height=parseInt(cs.height.slice(0,-2));return{left:left-scrollLeft,top:top-scrollTop,width:width,height:height,right:left+width-scrollLeft,bottom:top+height-scrollTop}};mapmap.prototype.select=function(selection){var map=this;function getName(sel){return typeof sel=="string"?sel:sel.selectionName||"function"}var oldSel=this.selected;if(this.selected){this._elements.main.classed("selected-"+getName(this.selected),false)}this.selected=selection;if(this.selected){this._elements.main.classed("selected-"+getName(this.selected),true)}this.promise_data().then(function(){if(oldSel){map.getRepresentations(oldSel).classed("selected",false)}if(selection){map.getRepresentations(selection).classed("selected",true)}});return this};mapmap.prototype.highlight=function(selection){var map=this;if(selection===null){map._elements.shadowEl.selectAll("path").remove();map._elements.shadowCropEl.selectAll("path").remove()}else{this.promise_data().then(function(data){var obj=map.getRepresentations(selection);map._elements.shadowEl.selectAll("path").remove();map._elements.shadowCropEl.selectAll("path").remove();obj.each(function(){map._elements.shadowEl.append("path").attr({d:this.attributes.d.value,fill:"rgba(0,0,0,0.5)"});map._elements.shadowCropEl.append("path").attr({d:this.attributes.d.value,fill:"#fff"})})})}return this};mapmap.prototype.getRepresentations=function(selection){if(typeof selection=="undefined"){selection=this.selected}if(selection){if(typeof selection=="function"){return this._elements.geometry.selectAll("path").filter(function(d,i){return selection(d.properties)})}if(selection.__data__){return d3.select(selection)}var obj=this.identify_func(this.layers,selection);if(!obj)return d3.select(null);if(obj.length){return d3.selectAll(obj.map(function(d){return d.__repr__}))}return d3.select(obj.__repr__)}return this._elements.geometry.selectAll("path")};mapmap.prototype.getData=function(key,selection){var map=this;return new Promise(function(resolve,reject){map._promise.data.then(function(data){data=dd.OrderedHash();map.getRepresentations(selection)[0].forEach(function(d){if(typeof d.__data__.properties[key]!="undefined"){data.push(d.__data__.properties[key],d.__data__.properties)}});resolve(data)})})};mapmap.prototype.getOverlayContext=function(){return this._elements.overlay};mapmap.prototype.project=function(point){return this._projection(point)};mapmap.prototype.promise_data=function(promise){var map=this;if(promise){if(this._promise.data){this._promise.data=new Promise(function(resolve,reject){map._promise.data.then(function(_){promise.then(function(data){resolve(data)})})})}else{this._promise.data=promise}}return this._promise.data};mapmap.prototype.then=function(callback){this.promise_data().then(callback);return this};mapmap.prototype.data=function(spec,keyOrOptions){var options=dd.isDictionary(keyOrOptions)?keyOrOptions:{map:keyOrOptions};options=dd.merge({geometryKey:"__key__"},options);var map=this;if(typeof spec=="function"){this.promise_data().then(function(data){map._elements.geometry.selectAll("path").each(function(geom){if(geom.properties){var val=spec(geom.properties);if(val){mapmap.extend(geom.properties,val)}}})})}else{this.promise_data(dd(spec,options.map,options.reduce,options)).then(function(data){if(data.length()==0){console.warn("Data for key '"+options.map+"' yielded no results!")}map._elements.geometry.selectAll("path").each(function(d){if(d.properties){var k=d.properties[options.geometryKey];if(k){mapmap.extend(d.properties,data.get(k))}else{}}})})}return this};var MetaDataSpec=function(key,fields){if(!(this instanceof MetaDataSpec))return new MetaDataSpec(key,fields);mapmap.extend(this,fields);this.key=key;return this};MetaDataSpec.prototype.specificity=function(){if(this.key instanceof RegExp)return this.key.toString()-2;return this.key.length-(this.key.match(/[\*\?]/g)||[]).length};MetaDataSpec.prototype.match=function(str){if(this.key instanceof RegExp)return str.search(this.key)==0;var rex=new RegExp("^"+this.key.replace("*",".*").replace("?","."));return str.search(rex)==0};var MetaData=function(fields,localeProvider){if(!(this instanceof MetaData))return new MetaData(fields,localeProvider);mapmap.extend(this,fields);if(!this.undefinedLabel)this.undefinedLabel=localeProvider.locale.undefinedLabel;this.format=function(val){if(!this._format){this._format=this.getFormatter()}if(val===undefined||val===null||this.numberFormat&&isNaN(val)){return this.undefinedValue}return this._format(val)};this.getFormatter=function(){if(this.scale=="ordinal"&&this.valueLabels){var scale=d3.scale.ordinal().domain(this.domain).range(this.valueLabels);return scale}if(this.numberFormat&&typeof this.numberFormat=="function"){return this.numberFormat}if(localeProvider.locale){return localeProvider.locale.numberFormat(this.numberFormat||".01f")}return d3.format(this.numberFormat||".01f")};this.getRangeFormatter=function(){var fmt=this.format.bind(this);return function(lower,upper,excludeLower,excludeUpper){if(localeProvider.locale&&localeProvider.locale.rangeLabel){return localeProvider.locale.rangeLabel(lower,upper,fmt,excludeLower,excludeUpper)}return defaultRangeLabel(lower,upper,fmt,excludeLower,excludeUpper)}};return this};mapmap.prototype.meta=function(metadata){var keys=Object.keys(metadata);for(var i=0;istats.max)stats.max=val;if(val>0)stats.anyPositive=true;if(val<0)stats.anyNegative=true}else if(val){stats.anyString=true}}}if(data.each&&typeof data.each=="function"){data.each(datumFunc)}else{for(var i=0;i-1?metadata.domain[i]:null}}return scale};mapmap.prototype.autoLinearScale=function(valueFunc){var stats=getStats(this._elements.geometry.selectAll("path"),properties_accessor(valueFunc));return d3.scale.linear().domain([0,stats.max])};mapmap.prototype.autoSqrtScale=function(valueFunc){var stats=getStats(this._elements.geometry.selectAll("path"),properties_accessor(valueFunc));return d3.scale.sqrt().domain([0,stats.max])};mapmap.prototype.symbolize=function(callback,selection,finalize){var map=this;selection=selection||this.selected;this.promise_data().then(function(data){map.getRepresentations(selection).each(function(geom){callback.call(map,d3.select(this),geom,geom.properties)});if(finalize)finalize.call(map)});return this};mapmap.prototype.choropleth=function(spec,metadata,selection){var colorScale=null,valueFunc=keyOrCallback(spec),map=this;function color(el,geom,data){if(spec===null){el.attr("fill",this.settings.pathAttributes.fill);return}if(!colorScale){if(typeof metadata=="string"){metadata=this.getMetadata(metadata)}if(!metadata){metadata=this.getMetadata(spec)}colorScale=this.autoColorScale(spec,metadata,selection);this.updateLegend(spec,"fill",metadata,colorScale,selection)}if(el.attr("fill")!="none"){el=el.transition()}el.attr("fill",function(geom){var val=valueFunc(geom.properties);if(val==null||metadata.scale!="ordinal"&&isNaN(val)){return metadata.undefinedColor||map.settings.pathAttributes.fill}return colorScale(val)||map.settings.pathAttributes.fill})}this.symbolize(color,selection,function(){this.dispatcher.choropleth.call(this,spec)});return this};mapmap.prototype.strokeColor=function(spec,metadata,selection){var colorScale=null,valueFunc=keyOrCallback(spec),map=this;function color(el,geom,data){if(spec===null){el.attr("stroke",this.settings.pathAttributes.stroke);return}if(!colorScale){if(typeof metadata=="string"){metadata=this.getMetadata(metadata)}if(!metadata){metadata=this.getMetadata(spec)}colorScale=this.autoColorScale(spec,metadata,selection);this.updateLegend(spec,"strokeColor",metadata,colorScale,selection)}if(el.attr("stroke")!="none"){el=el.transition()}el.attr("stroke",function(geom){var val=valueFunc(geom.properties);if(val==null||metadata.scale!="ordinal"&&isNaN(val)){return metadata.undefinedColor||map.settings.pathAttributes.stroke}return colorScale(val)||map.settings.pathAttributes.stroke})}this.symbolize(color,selection);return this};mapmap.prototype.proportional_circles=function(value,scale){var valueFunc=keyOrCallback(value);var pathGenerator=d3.geo.path().projection(this._projection);scale=scale||20;this.symbolize(function(el,geom,data){if(value===null){this._elements.overlay.select("circle").remove()}else if(geom.properties&&typeof valueFunc(geom.properties)!="undefined"){if(typeof scale!="function"){scale=this.autoSqrtScale(valueFunc).range([0,scale])}var centroid=pathGenerator.centroid(geom);this._elements.overlay.append("circle").attr(this.settings.overlayAttributes).attr({r:scale(valueFunc(geom.properties)),cx:centroid[0],cy:centroid[1]})}});return this};mapmap.symbolize={};mapmap.symbolize.addLabel=function(spec){var valueFunc=keyOrCallback(spec);var pathGenerator=d3.geo.path();return function(el,geom,data){if(pathGenerator.projection()!==this._projection){pathGenerator.projection(this._projection)}if(spec===null){this._elements.overlay.select("circle").remove();return}if(geom.properties&&typeof valueFunc(geom.properties)!="undefined"){var centroid=pathGenerator.centroid(geom);this._elements.overlay.append("text").text(valueFunc(geom.properties)).attr({stroke:"#ffffff",fill:"#000000","font-size":9,"paint-order":"stroke fill","alignment-baseline":"middle",dx:7,dy:1}).attr({x:centroid[0],y:centroid[1]})}}};function addOptionalElement(elementName){return function(value){var valueFunc=keyOrCallback(value);this.symbolize(function(el,d){if(value===null){el.select(elementName).remove();return}el.append(elementName).text(valueFunc(d.properties))});return this}}mapmap.prototype.title=addOptionalElement("title");mapmap.prototype.desc=addOptionalElement("desc");var center={x:.5,y:.5};mapmap.prototype.center=function(center_x,center_y){center.x=center_x;if(typeof center_y!="undefined"){center.y=center_y}return this};var hoverOutCallbacks=[];function callHoverOut(){for(var i=0;imapBounds.right-options.clipMargins.right)pt.x=mapBounds.right-options.clipMargins.right;if(pt.ymapBounds.bottom-options.clipMargins.bottom)pt.y=mapBounds.bottom-options.clipMargins.bottom}pt.x-=mapBounds.left;pt.y-=mapBounds.top;return pt};mapmap.prototype.getAnchorForMousePosition=function(event,repr,options){options=dd.merge({anchorOffset:[0,-20]},options);var offsetX=event.layerX||event.offsetX,offsetY=event.layerY||event.offsetY;return{x:offsetX+options.anchorOffset[0],y:offsetY+options.anchorOffset[1]}};mapmap.prototype.hover=function(overCB,outCB,options){options=dd.merge({moveToFront:true,clipToViewport:true,clipMargins:{top:40,left:40,bottom:0,right:40},selection:null,anchorPosition:this.getAnchorForRepr},options);var map=this;if(!this._oldPointerEvents){this._oldPointerEvents=[]}this.promise_data().then(function(){var obj=map.getRepresentations(options.selection);mouseover=function(d){if(options.moveToFront&&map.supports.hoverDomModification){this.__hoverinsertposition__=this.nextSibling;this.parentNode.appendChild(this)}var el=this,event=d3.event;window.setTimeout(function(){var anchor=options.anchorPosition.call(map,event,el,options); -overCB.call(map,d.properties,anchor,el)},10)};for(var i=0;i":"";post=i==0?"
":"
";if(typeof part=="function"){var str=part.call(map,d);if(str){html+=pre+str+post}continue}var meta=map.getMetadata(part);var prefix=meta.hoverLabel||meta.valueLabel||meta.label||"";if(prefix)prefix+=": ";var val=meta.format(d[part]);if(val=="NaN")val=d[part];if(val!==undefined&&val!==meta.undefinedValue){html+=pre+prefix+val+(meta.valueUnit?" "+meta.valueUnit:"")+post}else if(meta.undefinedLabel){html+=pre+prefix+meta.undefinedLabel+post}}}return html};return func};mapmap.prototype.hoverInfo=function(spec,options){options=dd.merge({selection:null,hoverClassName:"hoverInfo",hoverStyle:{position:"absolute",padding:"0.5em 0.7em","background-color":"rgba(255,255,255,0.85)"},hoverEnterStyle:{display:"block"},hoverLeaveStyle:{display:"none"}},options);var hoverEl=this._elements.parent.find("."+options.hoverClassName);if(!spec){return this.hover(null,null,options)}var htmlFunc=this.buildHTMLFunc(spec);if(hoverEl.length==0){hoverEl=$('
');this._elements.parent.append(hoverEl)}hoverEl.css(options.hoverStyle);if(!hoverEl.mapmap_eventHandlerInstalled){hoverEl.on("mouseenter",function(){hoverEl.css(options.hoverEnterStyle)}).on("mouseleave",function(){hoverEl.css(options.hoverLeaveStyle)});hoverEl.mapmap_eventHandlerInstalled=true}function show(d,point){hoverEl.css(options.hoverEnterStyle);var offsetEl=hoverEl.offsetParent(),offsetHeight=offsetEl.outerHeight(false),mainEl=this._elements.main.node(),bounds=this.getBoundingClientRect(),scrollTop=window.pageYOffset||document.documentElement.scrollTop||document.body.scrollTop||0,scrollLeft=window.pageXOffset||document.documentElement.scrollLeft||document.body.scrollLeft||0,top=bounds.top+scrollTop-offsetEl.offset().top,left=bounds.left+scrollLeft-offsetEl.offset().left;hoverEl.css({bottom:offsetHeight-top-point.y+"px",left:left+point.x+"px"}).html(htmlFunc(d))}function hide(){hoverEl.css(options.hoverLeaveStyle)}return this.hover(show,hide,options)};mapmap.prototype.clear=function(){this.choropleth(null);this.proportional_circles(null);this.title(null);this.desc(null);return this};mapmap.behavior={};mapmap.behavior.zoom=function(options){options=dd.merge({event:"click",cursor:"pointer",fitScale:.7,animationDuration:750,maxZoom:8,hierarchical:false,showRing:true,ringRadius:1.1,zoomstart:null,zoomend:null,center:[center.x,center.y],ringAttributes:{stroke:"#000","stroke-width":6,"stroke-opacity":.3,"pointer-events":"none",fill:"none"},closeButton:function(parent){parent.append("circle").attr({r:10,fill:"#fff",stroke:"#000","stroke-width":2.5,"stroke-opacity":.9,"fill-opacity":.9,cursor:"pointer"});parent.append("text").attr({"text-anchor":"middle",cursor:"pointer","font-weight":"bold","font-size":"18",y:6}).text("×")},activate:function(el){d3.select(el).classed("active",true)},deactivate:function(el){if(el)d3.select(el).classed("active",false)}},options);var ring=null,map=null,r,r0,zoomed=null;var z=function(selection){map=this;var size=this.size();r=Math.min(size.height,size.width)/2*options.ringRadius;r0=Math.sqrt(size.width*size.width+size.height*size.height)/1.5;if(options.cursor){selection.attr({cursor:options.cursor})}if(options.showRing&&!ring){ring=map._elements.fixed.selectAll("g.zoomRing").data([1]);var newring=ring.enter().append("g").attr("class","zoomRing").attr("transform","translate("+size.width*options.center[0]+","+size.height*options.center[1]+")");newring.append("circle").attr("class","main").attr("r",r0).attr(options.ringAttributes);var close=newring.append("g").attr("class","zoomOut").attr("transform","translate("+r0*.707+",-"+r0*.707+")");if(options.closeButton){options.closeButton(close)}}map.on("view",function(translate,scale){if(zoomed&&scale==1){zoomed=null;animateRing(null);map._elements.map.select(".background").on(options.event+".zoom",null);options.zoomstart&&options.zoomstart.call(map,null);options.zoomend&&options.zoomend.call(map,null)}});selection.on(options.event,function(d){callHoverOut();if(zoomed==this){reset()}else{options.deactivate(zoomed);var el=this;options.zoomstart&&options.zoomstart.call(map,el);map.zoomToSelection(this,{callback:function(){options.zoomend&&options.zoomend.call(map,el)},maxZoom:options.maxZoom,center:options.center});animateRing(this);options.activate(this);zoomed=this;map._elements.map.select(".background").on(options.event+".zoom",reset)}});if(zoomed){options.zoomstart&&options.zoomstart.call(map,zoomed);options.zoomend&&options.zoomend.call(map,zoomed)}};function zoomTo(selection){options.zoomstart&&options.zoomstart.call(map,selection);map.zoomToSelection(selection,{callback:function(){options.zoomend&&options.zoomend.call(map,selection)},maxZoom:options.maxZoom,center:options.center});animateRing(selection);zoomed=selection;map._elements.map.select(".background").on(options.event+".zoom",reset)}function animateRing(selection){if(ring){var new_r=selection?r:r0;ring.select("circle.main").transition().duration(options.animationDuration).attr({r:new_r});ring.select("g.zoomOut").transition().duration(options.animationDuration).attr("transform","translate("+new_r*.707+",-"+new_r*.707+")");ring.select("g.zoomOut").on("click",reset)}}function reset(){if(map){options.deactivate(zoomed);zoomed=null;map.resetZoom();animateRing(null);map._elements.map.select(".background").on(options.event+".zoom",null);if(options.zoomstart){options.zoomstart.call(map,null)}if(options.zoomend){options.zoomend.call(map,null)}}}z.reset=reset;z.active=function(){return zoomed};z.remove=function(){reset()};z.from=function(other){if(other&&other.active){zoomed=other.active()}return z};return z};mapmap.prototype.animateView=function(translate,scale,callback,duration){duration=duration||750;if(translate[0]==this.current_translate[0]&&translate[1]==this.current_translate[1]&&scale==this.current_scale){if(callback){window.setTimeout(callback,10)}return this}this.current_translate=translate;this.current_scale=scale;callHoverOut();var map=this;this._elements.map.transition().duration(duration).call(map.zoom.translate(translate).scale(scale).event).each("start",function(){map._elements.shadowGroup.attr("display","none")}).each("end",function(){map._elements.shadowGroup.attr("display","block");if(callback){callback()}}).each("interrupt",function(){map._elements.shadowGroup.attr("display","block");if(callback){callback()}});this.dispatcher.view.call(this,translate,scale);return this};mapmap.prototype.setView=function(translate,scale){translate=translate||this.current_translate;scale=scale||this.current_scale;this.current_translate=translate;this.current_scale=scale;this.zoom.translate(translate).scale(scale).event(this._elements.map);this.dispatcher.view.call(this,translate,scale);return this};mapmap.prototype.getView=function(){return{translate:this.current_translate,scale:this.current_scale}};mapmap.prototype.zoomToSelection=function(selection,options){options=dd.merge({fitScale:.7,animationDuration:750,maxZoom:8,center:[center.x,center.y]},options);var sel=this.getRepresentations(selection),bounds=[[Infinity,Infinity],[-Infinity,-Infinity]],pathGenerator=d3.geo.path().projection(this._projection);sel.each(function(el){var b=pathGenerator.bounds(el);bounds[0][0]=Math.min(bounds[0][0],b[0][0]);bounds[0][1]=Math.min(bounds[0][1],b[0][1]);bounds[1][0]=Math.max(bounds[1][0],b[1][0]);bounds[1][1]=Math.max(bounds[1][1],b[1][1])});var dx=bounds[1][0]-bounds[0][0],dy=bounds[1][1]-bounds[0][1],x=(bounds[0][0]+bounds[1][0])/2,y=(bounds[0][1]+bounds[1][1])/2,size=this.size(),scale=Math.min(options.maxZoom,options.fitScale/Math.max(dx/size.width,dy/size.height)),translate=[size.width*options.center[0]-scale*x,size.height*options.center[1]-scale*y];this.animateView(translate,scale,options.callback,options.animationDuration);return this};mapmap.prototype.zoomToBounds=function(bounds,callback,duration){var w=bounds[1][0]-bounds[0][0],h=bounds[1][1]-bounds[0][1],cx=(bounds[1][0]+bounds[0][0])/2,cy=(bounds[1][1]+bounds[0][1])/2,size=this.size(),scale=Math.min(2,.9/Math.max(w/size.width,h/size.height)),translate=[size.width*.5-scale*cx,size.height*.5-scale*cy];return this.animateView(translate,scale,callback,duration)};mapmap.prototype.zoomToCenter=function(center,scale,callback,duration){scale=scale||1;var size=this.size(),translate=[size.width*.5-scale*center[0],size.height*.5-scale*center[1]];return this.animateView(translate,scale,callback,duration)};mapmap.prototype.zoomToViewportPosition=function(center,scale,callback,duration){var point=this._elements.main.node().createSVGPoint();point.x=center[0];point.y=center[1];var ctm=this._elements.geometry.node().getScreenCTM().inverse();point=point.matrixTransform(ctm);point=[point.x,point.y];scale=scale||1;return this.zoomToCenter(point,scale,callback,duration)};mapmap.prototype.resetZoom=function(callback,duration){return this.animateView([0,0],1,callback,duration)};mapmap.prototype.applyBehavior=function(spec,selection){var map=this;this._promise.geometry.then(function(topo){var sel=map.getRepresentations(selection);sel.style("pointer-events","visiblePainted");if(typeof spec=="function"){spec.call(map,sel)}else{throw"Behavior "+spec+" not a function"}});return this};mapmap.prototype.applyMapBehavior=function(spec){spec.call(this,this._elements.map);return this};mapmap.prototype.applyBehaviour=function(spec,selection){console&&console.log&&console.log("Deprecation warning: applyBehaviour() is deprecated, use applyBehavior() (US spelling) instead!");return this.applyBehavior(spec,selection)};mapmap.prototype.applyMapBehaviour=function(spec,selection){console&&console.log&&console.log("Deprecation warning: applyMapBehaviour() is deprecated, use applyMapBehavior() (US spelling) instead!");return this.applyMapBehavior(spec,selection)};mapmap.prototype.on=function(eventName,handler){this.dispatcher.on(eventName,handler);return this};function defaultRangeLabel(lower,upper,format,excludeLower,excludeUpper){var f=format||function(lower){return lower};if(isNaN(lower)){if(isNaN(upper)){console.warn("rangeLabel: neither lower nor upper value specified!");return""}else{return(excludeUpper?"under ":"up to ")+f(upper)}}if(isNaN(upper)){return excludeLower?"more than "+f(lower):f(lower)+" and more"}return(excludeLower?"> ":"")+f(lower)+" to "+(excludeUpper?"<":"")+f(upper)}var d3_locales={en:{decimal:".",thousands:",",grouping:[3],currency:["$",""],dateTime:"%a %b %e %X %Y",date:"%m/%d/%Y",time:"%H:%M:%S",periods:["AM","PM"],days:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],shortDays:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],months:["January","February","March","April","May","June","July","August","September","October","November","December"],shortMonths:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],rangeLabel:defaultRangeLabel,undefinedLabel:"no data"},de:{decimal:",",thousands:".",grouping:[3],currency:["€",""],dateTime:"%a %b %e %X %Y",date:"%d.%m.%Y",time:"%H:%M:%S",periods:["AM","PM"],days:["Sonntag","Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag"],shortDays:["So","Mo","Di","Mi","Do","Fr","Sa"],months:["Jänner","Februar","März","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember"],shortMonths:["Jan.","Feb.","März","Apr.","Mai","Juni","Juli","Aug.","Sep.","Okt.","Nov.","Dez."],rangeLabel:function(lower,upper,format,excludeLower,excludeUpper){var f=format||function(lower){return lower};if(isNaN(lower)){if(isNaN(upper)){console.warn("rangeLabel: neither lower nor upper value specified!");return""}else{return(excludeUpper?"unter ":"bis ")+f(upper)}}if(isNaN(upper)){return excludeLower?"mehr als "+f(lower):f(lower)+" und mehr"}return(excludeLower?"> ":"")+f(lower)+" bis "+(excludeUpper?"<":"")+f(upper)},undefinedLabel:"keine Daten"}};var optionsListeners={locale:function(val,old_val){this.setLocale(val);return this}};mapmap.prototype.setLocale=function(lang){var locale;if(dd.isString(lang)&&d3_locales[lang]){locale=d3_locales[lang]}else{locale=lang}this.locale=d3.locale(locale);var keys=Object.keys(locale);for(var i=0;i0&&width<1)width=1;return width}});return function(attribute,reprAttribute,metadata,classes,undefinedClass){var legend=this._elements.parent.find("."+options.legendClassName);if(legend.length==0){legend=$('
');this._elements.parent.prepend(legend)}legend=d3.select(legend[0]);legend.style(options.legendStyle);var title=legend.selectAll("h3").data([valueOrCall(metadata.label,attribute)||(dd.isString(attribute)?attribute:"")]);title.enter().append("h3");title.html(function(d){return d});if(metadata.scale!="ordinal"){classes.reverse()}if(undefinedClass){classes.push(undefinedClass)}var cells=legend.selectAll("div.legendCell").data(classes);cells.exit().remove();var newcells=cells.enter().append("div").style(options.cellStyle);cells.attr("class","legendCell").each(function(d){if(d.class){d3.select(this).classed(d.class,true)}});if(reprAttribute=="fill"){if(classes[0].representation.substring(0,4)!="url("){newcells.append("span").attr("class","legendColor").style(options.colorBoxStyle).append("span").attr("class","fill").style(options.colorFillStyle);cells.select(".legendColor .fill").transition().style({"background-color":function(d){return d.representation},"border-color":function(d){return d.representation},color:function(d){return d.representation}})}else{newcells.append("svg").attr("class","legendColor").style(options.colorBoxStyle).append("rect").attr({width:100,height:100});cells.select(".legendColor rect").attr({fill:function(d){return d.representation}})}}else if(reprAttribute=="strokeColor"){newcells.append("span").attr("class","legendColor").style(options.colorBoxStyle).style("border","none").append("span").attr("class","fill").style(options.colorFillStyle);cells.select(".legendColor .fill").transition().style({"background-color":function(d){return d.representation},"border-color":function(d){return d.representation},color:function(d){return d.representation}})}newcells.append("span").attr("class","legendLabel").style(options.labelStyle);cells.attr("data-count",function(d){return d.count()});cells.select(".legendLabel").text(function(d){var formatter;if(d.valueRange){formatter=metadata.getRangeFormatter();return formatter(d.valueRange[0],d.valueRange[1],d.includeLower,d.includeUpper)}if(d.value){formatter=metadata.getFormatter();return formatter(d.value)}return metadata.undefinedLabel});if(options.histogram){newcells.append("span").attr("class","legendHistogramBar").style(options.histogramBarStyle);cells.select(".legendHistogramBar").transition().style("width",function(d){var width=options.histogramBarWidth(d.count());if(width.length&&width.indexOf("px")==width.lenght-2){return width}return Math.round(width)+"px"}).text(function(d){return" "+d.count()})}if(options.callback)options.callback()}};mapmap.legend.svg=function(range,labelFormat,histogram,options){var DEFAULTS={cellSpacing:5,layout:"vertical",histogram:false,histogramLength:80,containerAttributes:{transform:"translate(20,10)"},backgroundAttributes:{fill:"#fff","fill-opacity":.9,x:-10,y:-10,width:220},cellAttributes:{},colorAttributes:{width:40,height:18,stroke:"#000","stroke-width":"0.5px",fill:"#fff"},textAttributes:{"font-size":10,"pointer-events":"none",dy:12},histogramBarAttributes:{width:0,x:140,y:4,height:10,fill:"#000","fill-opacity":.2}};var layouts={horizontal:{cellAttributes:{transform:function(d,i){return"translate("+i*(options.colorAttributes.width+options.cellSpacing)+",0)"}},textAttributes:{y:function(){return options.colorAttributes.height+options.cellSpacing}}},vertical:{cellAttributes:{transform:function(d,i){return"translate(0,"+i*(options.colorAttributes.height+options.cellSpacing)+")"}},textAttributes:{x:function(){return options.colorAttributes.width+options.cellSpacing}}}};var layout=layouts[options.layout];if(options.layout=="vertical"){range.reverse()}this._elements.legend.attr(options.containerAttributes);var bg=this._elements.legend.selectAll("rect.background").data([1]);bg.enter().append("rect").attr("class","background").attr(options.backgroundAttributes);bg.transition().attr("height",histogram.length*(options.colorAttributes.height+options.cellSpacing)+(20-options.cellSpacing));var cells=this._elements.legend.selectAll("g.cell").data(range);cells.exit().remove();var newcells=cells.enter().append("g").attr("class","cell").attr(options.cellAttributes).attr(layout.cellAttributes);newcells.append("rect").attr("class","color").attr(options.colorAttributes).attr(layout.colorAttributes);if(options.histogram){newcells.append("rect").attr("class","bar").attr(options.histogramBarAttributes);cells.select(".bar").transition().attr("width",function(d,i){return histogram[histogram.length-i-1].y*options.histogramLength})}newcells.append("text").attr(options.textAttributes).attr(layout.textAttributes);cells.select(".color").transition().attr("fill",function(d){return d});cells.select("text").text(labelFormat)};mapmap.prototype.projection=function(projection){this._projection=projection;return this};mapmap.prototype.extent=function(selection,options){var map=this;this.selected_extent=selection||this.selected;this._promise.geometry.then(function(topo){var geom=map.getRepresentations(map.selected_extent);var all={type:"FeatureCollection",features:[]};geom.each(function(d){all.features.push(d)});map._extent(all,options)});return this};mapmap.prototype._extent=function(geom,options){options=dd.merge({fillFactor:.9},options);if(geom.type&&geom.type=="Topology"){var names=Object.keys(geom.objects);var all=[];for(var i=0;i-1){doAdd=true;break}}for(j=0;j-1){doAdd=false;break}}if(doAdd&&prev[key]&&curr[key]&&!isNaN(prev[key])&&!isNaN(curr[key])){prev[key]=prev[key]+curr[key]}else{prev[key]=curr[key];if(doAdd){console.warn("datadata.emit.sum(): Cannot add keys "+key+"!")}}}return prev});emit(key,obj)}}};dd.map.geo={point:function(latProp,lonProp,keyProp){var id=0;return function(d,emit){var key=keyProp?d[keyProp]:id++;emit(key,dd.geo.Point(d[lonProp],d[latProp],d))}}};dd.emit.geo={segments:function(){return function(key,data,emit){var prev=null,cur=null;for(var i=0;i0){this.supports.hoverDomModification=false}else{this.supports.hoverDomModification=true}var map=this;this.current_scale=1;this.current_translate=[0,0];this.zoom=d3.behavior.zoom().translate([0,0]).scale(1).scaleExtent([1,8]).on("zoom",function(){map.current_scale=d3.event.scale;map.current_translate=d3.event.translate;mapEl.attr("transform","translate("+d3.event.translate+")scale("+d3.event.scale+")");if(!map.supports.nonScalingStroke){}});mapEl.call(this.zoom.event);var map=this;function constructEvent(event){var pos=[event.clientX,event.clientY];return{position:pos,location:map._projection.invert(pos),event:event}}mapEl.on("click",function(){map.dispatcher.click.call(map,constructEvent(d3.event))});mapEl.on("mousedown",function(){map.dispatcher.mousedown.call(map,constructEvent(d3.event))});mapEl.on("mouseup",function(){map.dispatcher.mousedown.call(map,constructEvent(d3.event))});mapEl.on("mousemove",function(){map.dispatcher.mousedown.call(map,constructEvent(d3.event))})};mapmap.prototype.initEvents=function(element){var map=this;function resize(){map.bounds=map.getBoundingClientRect();if(map.settings.keepAspectRatio){var width=element.getAttribute("width"),height=element.getAttribute("height");if(width&&height&&map.bounds.width){var ratio=width/height;element.style.height=map.bounds.width/ratio+"px"}}}window.onresize=resize;resize()};var domain=[0,1];var layer_counter=0;mapmap.prototype.geometry=function(spec,keyOrOptions){var options=dd.isString(keyOrOptions)?{key:keyOrOptions}:keyOrOptions;options=dd.merge({key:"id"},options);var map=this;if(dd.isFunction(spec)){this._promise.geometry.then(function(topo){var new_topo=spec(topo);if(typeof new_topo.length=="undefined"){new_topo=[new_topo]}new_topo.map(function(t){if(typeof t.geometry.length=="undefined"){t.geometry=[t.geometry]}if(typeof t.index=="undefined"){map.layers.push(t.name,t.geometry)}else{map.layers.insert(t.index,t.name,t.geometry)}});map.draw();if(options.ondraw)options.ondraw()});return this}if(dd.isArray(spec)){var new_topo=dd.mapreduce(spec,options.map,options.reduce);if(!options.layers){options.layers="layer-"+layer_counter++}map.layers.push(options.layers,new_topo.values());var promise=new Promise(function(resolve,reject){resolve(new_topo)});this.promise_data(promise);if(!map.selected_extent){map._extent(new_topo.values())}map.draw();if(options.ondraw)options.ondraw();return this}var promise=dd.load(spec);if(this._promise.geometry){var parent=this._promise.geometry;this._promise.geometry=new Promise(function(resolve,reject){parent.then(function(_){promise.then(function(data){resolve(data)})})})}else{this._promise.geometry=promise}this._promise.geometry.then(function(geom){if(geom.type&&geom.type=="Topology"){var keys=options.layers||Object.keys(geom.objects);keys.map(function(k){if(geom.objects[k]){var objs=topojson.feature(geom,geom.objects[k]).features;map.layers.push(k,objs);if(options.key){for(var i=0;i-1){left+=parseInt(cs.left.slice(0,-2))}if(bounds.left==left-scrollLeft){return bounds}var top=parentOffset.top,width=parseInt(cs.width.slice(0,-2)),height=parseInt(cs.height.slice(0,-2));return{left:left-scrollLeft,top:top-scrollTop,width:width,height:height,right:left+width-scrollLeft,bottom:top+height-scrollTop}};mapmap.prototype.select=function(selection){var map=this;function getName(sel){return typeof sel=="string"?sel:sel.selectionName||"function"}var oldSel=this.selected;if(this.selected){this._elements.main.classed("selected-"+getName(this.selected),false)}this.selected=selection;if(this.selected){this._elements.main.classed("selected-"+getName(this.selected),true)}this.promise_data().then(function(){if(oldSel){map.getRepresentations(oldSel).classed("selected",false)}if(selection){map.getRepresentations(selection).classed("selected",true)}});return this};mapmap.prototype.highlight=function(selection){var map=this;if(selection===null){map._elements.shadowEl.selectAll("path").remove();map._elements.shadowCropEl.selectAll("path").remove()}else{this.promise_data().then(function(data){var obj=map.getRepresentations(selection);map._elements.shadowEl.selectAll("path").remove();map._elements.shadowCropEl.selectAll("path").remove();obj.each(function(){map._elements.shadowEl.append("path").attr({d:this.attributes.d.value,fill:"rgba(0,0,0,0.5)"});map._elements.shadowCropEl.append("path").attr({d:this.attributes.d.value,fill:"#fff"})})})}return this};mapmap.prototype.getRepresentations=function(selection){if(typeof selection=="undefined"){selection=this.selected}if(selection){if(typeof selection=="function"){return this._elements.geometry.selectAll("path").filter(function(d,i){return selection(d.properties)})}if(selection.__data__){return d3.select(selection)}var obj=this.identify_func(this.layers,selection);if(!obj)return d3.select(null);if(obj.length){return d3.selectAll(obj.map(function(d){return d.__repr__}))}return d3.select(obj.__repr__)}return this._elements.geometry.selectAll("path")};mapmap.prototype.getData=function(key,selection){var map=this;return new Promise(function(resolve,reject){map._promise.data.then(function(data){data=dd.OrderedHash();map.getRepresentations(selection)[0].forEach(function(d){if(typeof d.__data__.properties[key]!="undefined"){data.push(d.__data__.properties[key],d.__data__.properties)}});resolve(data)})})};mapmap.prototype.getOverlayContext=function(){return this._elements.overlay};mapmap.prototype.project=function(point){return this._projection(point)};mapmap.prototype.promise_data=function(promise){var map=this;if(promise){if(this._promise.data){this._promise.data=new Promise(function(resolve,reject){map._promise.data.then(function(_){promise.then(function(data){resolve(data)})})})}else{this._promise.data=promise}}return this._promise.data};mapmap.prototype.then=function(callback){this.promise_data().then(callback);return this};mapmap.prototype.data=function(spec,keyOrOptions){var options=dd.isDictionary(keyOrOptions)?keyOrOptions:{map:keyOrOptions};options=dd.merge({geometryKey:"__key__"},options);var map=this;if(typeof spec=="function"){this.promise_data().then(function(data){map._elements.geometry.selectAll("path").each(function(geom){if(geom.properties){var val=spec(geom.properties);if(val){mapmap.extend(geom.properties,val)}}})})}else{this.promise_data(dd(spec,options.map,options.reduce,options)).then(function(data){if(data.length()==0){console.warn("Data for key '"+options.map+"' yielded no results!")}map._elements.geometry.selectAll("path").each(function(d){if(d.properties){var k=d.properties[options.geometryKey];if(k){mapmap.extend(d.properties,data.get(k))}else{}}})})}return this};var MetaDataSpec=function(key,fields){if(!(this instanceof MetaDataSpec))return new MetaDataSpec(key,fields);mapmap.extend(this,fields);this.key=key;return this};MetaDataSpec.prototype.specificity=function(){if(this.key instanceof RegExp)return this.key.toString()-2;return this.key.length-(this.key.match(/[\*\?]/g)||[]).length};MetaDataSpec.prototype.match=function(str){if(this.key instanceof RegExp)return str.search(this.key)==0;var rex=new RegExp("^"+this.key.replace("*",".*").replace("?","."));return str.search(rex)==0};var MetaData=function(fields,localeProvider){if(!(this instanceof MetaData))return new MetaData(fields,localeProvider);mapmap.extend(this,fields);if(!this.undefinedLabel)this.undefinedLabel=localeProvider.locale.undefinedLabel;this.format=function(val){if(!this._format){this._format=this.getFormatter()}if(val===undefined||val===null||this.numberFormat&&isNaN(val)){return this.undefinedValue}return this._format(val)};this.getFormatter=function(){if(this.scale=="ordinal"&&this.valueLabels){var scale=d3.scale.ordinal().domain(this.domain).range(this.valueLabels);return scale}if(this.numberFormat&&typeof this.numberFormat=="function"){return this.numberFormat}if(localeProvider.locale){return localeProvider.locale.numberFormat(this.numberFormat||".01f")}return d3.format(this.numberFormat||".01f")};this.getRangeFormatter=function(){var fmt=this.format.bind(this);return function(lower,upper,excludeLower,excludeUpper){if(localeProvider.locale&&localeProvider.locale.rangeLabel){return localeProvider.locale.rangeLabel(lower,upper,fmt,excludeLower,excludeUpper)}return defaultRangeLabel(lower,upper,fmt,excludeLower,excludeUpper)}};return this};mapmap.prototype.meta=function(metadata){var keys=Object.keys(metadata);for(var i=0;istats.max)stats.max=val;if(val>0)stats.anyPositive=true;if(val<0)stats.anyNegative=true}else if(val){stats.anyString=true}}}if(data.each&&typeof data.each=="function"){data.each(datumFunc)}else{for(var i=0;i-1?metadata.domain[i]:null}}return scale};mapmap.prototype.autoLinearScale=function(valueFunc){var stats=getStats(this._elements.geometry.selectAll("path"),properties_accessor(valueFunc));return d3.scale.linear().domain([0,stats.max])};mapmap.prototype.autoSqrtScale=function(valueFunc){var stats=getStats(this._elements.geometry.selectAll("path"),properties_accessor(valueFunc));return d3.scale.sqrt().domain([0,stats.max])};mapmap.prototype.symbolize=function(callback,selection,finalize){var map=this;selection=selection||this.selected;this.promise_data().then(function(data){map.getRepresentations(selection).each(function(geom){callback.call(map,d3.select(this),geom,geom.properties)});if(finalize)finalize.call(map)});return this};mapmap.prototype.choropleth=function(spec,metadata,selection){var colorScale=null,valueFunc=keyOrCallback(spec),map=this;function color(el,geom,data){if(spec===null){el.attr("fill",this.settings.pathAttributes.fill);return}if(!colorScale){if(typeof metadata=="string"){metadata=this.getMetadata(metadata)}if(!metadata){metadata=this.getMetadata(spec)}colorScale=this.autoColorScale(spec,metadata,selection);this.updateLegend(spec,"fill",metadata,colorScale,selection)}if(el.attr("fill")!="none"){el=el.transition()}el.attr("fill",function(geom){var val=valueFunc(geom.properties);if(val==null||metadata.scale!="ordinal"&&isNaN(val)){return metadata.undefinedColor||map.settings.pathAttributes.fill}return colorScale(val)||map.settings.pathAttributes.fill})}this.symbolize(color,selection,function(){this.dispatcher.choropleth.call(this,spec)});return this};mapmap.prototype.strokeColor=function(spec,metadata,selection){var colorScale=null,valueFunc=keyOrCallback(spec),map=this;function color(el,geom,data){if(spec===null){el.attr("stroke",this.settings.pathAttributes.stroke);return}if(!colorScale){if(typeof metadata=="string"){metadata=this.getMetadata(metadata)}if(!metadata){metadata=this.getMetadata(spec)}colorScale=this.autoColorScale(spec,metadata,selection);this.updateLegend(spec,"strokeColor",metadata,colorScale,selection)}if(el.attr("stroke")!="none"){el=el.transition()}el.attr("stroke",function(geom){var val=valueFunc(geom.properties);if(val==null||metadata.scale!="ordinal"&&isNaN(val)){return metadata.undefinedColor||map.settings.pathAttributes.stroke}return colorScale(val)||map.settings.pathAttributes.stroke})}this.symbolize(color,selection);return this};mapmap.prototype.proportional_circles=function(value,scale){var valueFunc=keyOrCallback(value);var pathGenerator=d3.geo.path().projection(this._projection);scale=scale||20;this.symbolize(function(el,geom,data){if(value===null){this._elements.overlay.select("circle").remove()}else if(geom.properties&&typeof valueFunc(geom.properties)!="undefined"){if(typeof scale!="function"){scale=this.autoSqrtScale(valueFunc).range([0,scale])}var centroid=pathGenerator.centroid(geom);this._elements.overlay.append("circle").attr(this.settings.overlayAttributes).attr({r:scale(valueFunc(geom.properties)),cx:centroid[0],cy:centroid[1]})}});return this};mapmap.symbolize={};mapmap.symbolize.addLabel=function(spec){var valueFunc=keyOrCallback(spec);var pathGenerator=d3.geo.path();return function(el,geom,data){if(pathGenerator.projection()!==this._projection){pathGenerator.projection(this._projection)}if(spec===null){this._elements.overlay.select("circle").remove();return}if(geom.properties&&typeof valueFunc(geom.properties)!="undefined"){var centroid=pathGenerator.centroid(geom);this._elements.overlay.append("text").text(valueFunc(geom.properties)).attr({stroke:"#ffffff",fill:"#000000","font-size":9,"paint-order":"stroke fill","alignment-baseline":"middle",dx:7,dy:1}).attr({x:centroid[0],y:centroid[1]})}}};function addOptionalElement(elementName){return function(value){var valueFunc=keyOrCallback(value);this.symbolize(function(el,d){if(value===null){el.select(elementName).remove();return}el.append(elementName).text(valueFunc(d.properties))});return this}}mapmap.prototype.title=addOptionalElement("title");mapmap.prototype.desc=addOptionalElement("desc");var center={x:.5,y:.5};mapmap.prototype.center=function(center_x,center_y){center.x=center_x;if(typeof center_y!="undefined"){center.y=center_y}return this};var hoverOutCallbacks=[];function callHoverOut(){for(var i=0;imapBounds.right-options.clipMargins.right)pt.x=mapBounds.right-options.clipMargins.right;if(pt.ymapBounds.bottom-options.clipMargins.bottom)pt.y=mapBounds.bottom-options.clipMargins.bottom}pt.x-=mapBounds.left;pt.y-=mapBounds.top;return pt};mapmap.prototype.getAnchorForMousePosition=function(event,repr,options){options=dd.merge({anchorOffset:[0,-20]},options);var offsetX=event.layerX||event.offsetX,offsetY=event.layerY||event.offsetY;return{x:offsetX+options.anchorOffset[0],y:offsetY+options.anchorOffset[1]}};mapmap.prototype.hover=function(overCB,outCB,options){options=dd.merge({moveToFront:true,clipToViewport:true,clipMargins:{top:40,left:40,bottom:0,right:40},selection:null,anchorPosition:this.getAnchorForRepr},options);var map=this;if(!this._oldPointerEvents){this._oldPointerEvents=[]}this.promise_data().then(function(){var obj=map.getRepresentations(options.selection);mouseover=function(d){if(options.moveToFront&&map.supports.hoverDomModification){this.__hoverinsertposition__=this.nextSibling;this.parentNode.appendChild(this)}var el=this,event=d3.event;window.setTimeout(function(){var anchor=options.anchorPosition.call(map,event,el,options); +overCB.call(map,d.properties,anchor,el)},10)};for(var i=0;i":"";post=i==0?"
":"
";if(typeof part=="function"){var str=part.call(map,d);if(str){html+=pre+str+post}continue}var meta=map.getMetadata(part);var prefix=meta.hoverLabel||meta.valueLabel||meta.label||"";if(prefix)prefix+=": ";var val=meta.format(d[part]);if(val=="NaN")val=d[part];if(val!==undefined&&val!==meta.undefinedValue){html+=pre+prefix+val+(meta.valueUnit?" "+meta.valueUnit:"")+post}else if(meta.undefinedLabel){html+=pre+prefix+meta.undefinedLabel+post}}}return html};return func};mapmap.prototype.hoverInfo=function(spec,options){options=dd.merge({selection:null,hoverClassName:"hoverInfo",hoverStyle:{position:"absolute",padding:"0.5em 0.7em","background-color":"rgba(255,255,255,0.85)"},hoverEnterStyle:{display:"block"},hoverLeaveStyle:{display:"none"}},options);var hoverEl=this._elements.parent.find("."+options.hoverClassName);if(!spec){return this.hover(null,null,options)}var htmlFunc=this.buildHTMLFunc(spec);if(hoverEl.length==0){hoverEl=$('
');this._elements.parent.append(hoverEl)}hoverEl.css(options.hoverStyle);if(!hoverEl.mapmap_eventHandlerInstalled){hoverEl.on("mouseenter",function(){hoverEl.css(options.hoverEnterStyle)}).on("mouseleave",function(){hoverEl.css(options.hoverLeaveStyle)});hoverEl.mapmap_eventHandlerInstalled=true}function show(d,point){hoverEl.css(options.hoverEnterStyle);var offsetEl=hoverEl.offsetParent(),offsetHeight=offsetEl.outerHeight(false),mainEl=this._elements.main.node(),bounds=this.getBoundingClientRect(),scrollTop=window.pageYOffset||document.documentElement.scrollTop||document.body.scrollTop||0,scrollLeft=window.pageXOffset||document.documentElement.scrollLeft||document.body.scrollLeft||0,top=bounds.top+scrollTop-offsetEl.offset().top,left=bounds.left+scrollLeft-offsetEl.offset().left;hoverEl.css({bottom:offsetHeight-top-point.y+"px",left:left+point.x+"px"}).html(htmlFunc(d))}function hide(){hoverEl.css(options.hoverLeaveStyle)}return this.hover(show,hide,options)};mapmap.prototype.clear=function(){this.choropleth(null);this.proportional_circles(null);this.title(null);this.desc(null);return this};mapmap.behavior={};mapmap.behavior.zoom=function(options){options=dd.merge({event:"click",cursor:"pointer",fitScale:.7,animationDuration:750,maxZoom:8,hierarchical:false,showRing:true,ringRadius:1.1,zoomstart:null,zoomend:null,center:[center.x,center.y],ringAttributes:{stroke:"#000","stroke-width":6,"stroke-opacity":.3,"pointer-events":"none",fill:"none"},closeButton:function(parent){parent.append("circle").attr({r:10,fill:"#fff",stroke:"#000","stroke-width":2.5,"stroke-opacity":.9,"fill-opacity":.9,cursor:"pointer"});parent.append("text").attr({"text-anchor":"middle",cursor:"pointer","font-weight":"bold","font-size":"18",y:6}).text("×")},activate:function(el){d3.select(el).classed("active",true)},deactivate:function(el){if(el)d3.select(el).classed("active",false)}},options);var ring=null,map=null,r,r0,zoomed=null;var z=function(selection){map=this;var size=this.size();r=Math.min(size.height,size.width)/2*options.ringRadius;r0=Math.sqrt(size.width*size.width+size.height*size.height)/1.5;if(options.cursor){selection.attr({cursor:options.cursor})}if(options.showRing&&!ring){ring=map._elements.fixed.selectAll("g.zoomRing").data([1]);var newring=ring.enter().append("g").attr("class","zoomRing").attr("transform","translate("+size.width*options.center[0]+","+size.height*options.center[1]+")");newring.append("circle").attr("class","main").attr("r",r0).attr(options.ringAttributes);var close=newring.append("g").attr("class","zoomOut").attr("transform","translate("+r0*.707+",-"+r0*.707+")");if(options.closeButton){options.closeButton(close)}}map.on("view",function(translate,scale){if(zoomed&&scale==1){zoomed=null;animateRing(null);map._elements.map.select(".background").on(options.event+".zoom",null);options.zoomstart&&options.zoomstart.call(map,null);options.zoomend&&options.zoomend.call(map,null)}});selection.on(options.event,function(d){callHoverOut();if(zoomed==this){reset()}else{options.deactivate(zoomed);var el=this;options.zoomstart&&options.zoomstart.call(map,el);map.zoomToSelection(this,{callback:function(){options.zoomend&&options.zoomend.call(map,el)},maxZoom:options.maxZoom,center:options.center});animateRing(this);options.activate(this);zoomed=this;map._elements.map.select(".background").on(options.event+".zoom",reset)}});if(zoomed){options.zoomstart&&options.zoomstart.call(map,zoomed);options.zoomend&&options.zoomend.call(map,zoomed)}};function zoomTo(selection){options.zoomstart&&options.zoomstart.call(map,selection);map.zoomToSelection(selection,{callback:function(){options.zoomend&&options.zoomend.call(map,selection)},maxZoom:options.maxZoom,center:options.center});animateRing(selection);zoomed=selection;map._elements.map.select(".background").on(options.event+".zoom",reset)}function animateRing(selection){if(ring){var new_r=selection?r:r0;ring.select("circle.main").transition().duration(options.animationDuration).attr({r:new_r});ring.select("g.zoomOut").transition().duration(options.animationDuration).attr("transform","translate("+new_r*.707+",-"+new_r*.707+")");ring.select("g.zoomOut").on("click",reset)}}function reset(){if(map){options.deactivate(zoomed);zoomed=null;map.resetZoom();animateRing(null);map._elements.map.select(".background").on(options.event+".zoom",null);if(options.zoomstart){options.zoomstart.call(map,null)}if(options.zoomend){options.zoomend.call(map,null)}}}z.reset=reset;z.active=function(){return zoomed};z.remove=function(){reset()};z.from=function(other){if(other&&other.active){zoomed=other.active()}return z};return z};mapmap.prototype.animateView=function(translate,scale,callback,duration){duration=duration||750;if(translate[0]==this.current_translate[0]&&translate[1]==this.current_translate[1]&&scale==this.current_scale){if(callback){window.setTimeout(callback,10)}return this}this.current_translate=translate;this.current_scale=scale;callHoverOut();var map=this;this._elements.map.transition().duration(duration).call(map.zoom.translate(translate).scale(scale).event).each("start",function(){map._elements.shadowGroup.attr("display","none")}).each("end",function(){map._elements.shadowGroup.attr("display","block");if(callback){callback()}}).each("interrupt",function(){map._elements.shadowGroup.attr("display","block");if(callback){callback()}});this.dispatcher.view.call(this,translate,scale);return this};mapmap.prototype.setView=function(translate,scale){translate=translate||this.current_translate;scale=scale||this.current_scale;this.current_translate=translate;this.current_scale=scale;this.zoom.translate(translate).scale(scale).event(this._elements.map);this.dispatcher.view.call(this,translate,scale);return this};mapmap.prototype.getView=function(){return{translate:this.current_translate,scale:this.current_scale}};mapmap.prototype.zoomToSelection=function(selection,options){options=dd.merge({fitScale:.7,animationDuration:750,maxZoom:8,center:[center.x,center.y]},options);var sel=this.getRepresentations(selection),bounds=[[Infinity,Infinity],[-Infinity,-Infinity]],pathGenerator=d3.geo.path().projection(this._projection);sel.each(function(el){var b=pathGenerator.bounds(el);bounds[0][0]=Math.min(bounds[0][0],b[0][0]);bounds[0][1]=Math.min(bounds[0][1],b[0][1]);bounds[1][0]=Math.max(bounds[1][0],b[1][0]);bounds[1][1]=Math.max(bounds[1][1],b[1][1])});var dx=bounds[1][0]-bounds[0][0],dy=bounds[1][1]-bounds[0][1],x=(bounds[0][0]+bounds[1][0])/2,y=(bounds[0][1]+bounds[1][1])/2,size=this.size(),scale=Math.min(options.maxZoom,options.fitScale/Math.max(dx/size.width,dy/size.height)),translate=[size.width*options.center[0]-scale*x,size.height*options.center[1]-scale*y];this.animateView(translate,scale,options.callback,options.animationDuration);return this};mapmap.prototype.zoomToBounds=function(bounds,callback,duration){var w=bounds[1][0]-bounds[0][0],h=bounds[1][1]-bounds[0][1],cx=(bounds[1][0]+bounds[0][0])/2,cy=(bounds[1][1]+bounds[0][1])/2,size=this.size(),scale=Math.min(2,.9/Math.max(w/size.width,h/size.height)),translate=[size.width*.5-scale*cx,size.height*.5-scale*cy];return this.animateView(translate,scale,callback,duration)};mapmap.prototype.zoomToCenter=function(center,scale,callback,duration){scale=scale||1;var size=this.size(),translate=[size.width*.5-scale*center[0],size.height*.5-scale*center[1]];return this.animateView(translate,scale,callback,duration)};mapmap.prototype.zoomToViewportPosition=function(center,scale,callback,duration){var point=this._elements.main.node().createSVGPoint();point.x=center[0];point.y=center[1];var ctm=this._elements.geometry.node().getScreenCTM().inverse();point=point.matrixTransform(ctm);point=[point.x,point.y];scale=scale||1;return this.zoomToCenter(point,scale,callback,duration)};mapmap.prototype.resetZoom=function(callback,duration){return this.animateView([0,0],1,callback,duration)};mapmap.prototype.applyBehavior=function(spec,selection){var map=this;this._promise.geometry.then(function(topo){var sel=map.getRepresentations(selection);sel.style("pointer-events","visiblePainted");if(typeof spec=="function"){spec.call(map,sel)}else{throw"Behavior "+spec+" not a function"}});return this};mapmap.prototype.applyMapBehavior=function(spec){spec.call(this,this._elements.map);return this};mapmap.prototype.applyBehaviour=function(spec,selection){console&&console.log&&console.log("Deprecation warning: applyBehaviour() is deprecated, use applyBehavior() (US spelling) instead!");return this.applyBehavior(spec,selection)};mapmap.prototype.applyMapBehaviour=function(spec,selection){console&&console.log&&console.log("Deprecation warning: applyMapBehaviour() is deprecated, use applyMapBehavior() (US spelling) instead!");return this.applyMapBehavior(spec,selection)};mapmap.prototype.on=function(eventName,handler){this.dispatcher.on(eventName,handler);return this};function defaultRangeLabel(lower,upper,format,excludeLower,excludeUpper){var f=format||function(lower){return lower};if(isNaN(lower)){if(isNaN(upper)){console.warn("rangeLabel: neither lower nor upper value specified!");return""}else{return(excludeUpper?"under ":"up to ")+f(upper)}}if(isNaN(upper)){return excludeLower?"more than "+f(lower):f(lower)+" and more"}return(excludeLower?"> ":"")+f(lower)+" to "+(excludeUpper?"<":"")+f(upper)}var d3_locales={en:{decimal:".",thousands:",",grouping:[3],currency:["$",""],dateTime:"%a %b %e %X %Y",date:"%m/%d/%Y",time:"%H:%M:%S",periods:["AM","PM"],days:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],shortDays:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],months:["January","February","March","April","May","June","July","August","September","October","November","December"],shortMonths:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],rangeLabel:defaultRangeLabel,undefinedLabel:"no data"},de:{decimal:",",thousands:".",grouping:[3],currency:["€",""],dateTime:"%a %b %e %X %Y",date:"%d.%m.%Y",time:"%H:%M:%S",periods:["AM","PM"],days:["Sonntag","Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag"],shortDays:["So","Mo","Di","Mi","Do","Fr","Sa"],months:["Jänner","Februar","März","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember"],shortMonths:["Jan.","Feb.","März","Apr.","Mai","Juni","Juli","Aug.","Sep.","Okt.","Nov.","Dez."],rangeLabel:function(lower,upper,format,excludeLower,excludeUpper){var f=format||function(lower){return lower};if(isNaN(lower)){if(isNaN(upper)){console.warn("rangeLabel: neither lower nor upper value specified!");return""}else{return(excludeUpper?"unter ":"bis ")+f(upper)}}if(isNaN(upper)){return excludeLower?"mehr als "+f(lower):f(lower)+" und mehr"}return(excludeLower?"> ":"")+f(lower)+" bis "+(excludeUpper?"<":"")+f(upper)},undefinedLabel:"keine Daten"}};var optionsListeners={locale:function(val,old_val){this.setLocale(val);return this}};mapmap.prototype.setLocale=function(lang){var locale;if(dd.isString(lang)&&d3_locales[lang]){locale=d3_locales[lang]}else{locale=lang}this.locale=d3.locale(locale);var keys=Object.keys(locale);for(var i=0;i0&&width<1)width=1;return width}});return function(attribute,reprAttribute,metadata,classes,undefinedClass){var legend=this._elements.parent.find("."+options.legendClassName);if(legend.length==0){legend=$('
');this._elements.parent.prepend(legend)}legend=d3.select(legend[0]);legend.style(options.legendStyle);var title=legend.selectAll("h3").data([valueOrCall(metadata.label,attribute)||(dd.isString(attribute)?attribute:"")]);title.enter().append("h3");title.html(function(d){return d});if(metadata.scale!="ordinal"){classes.reverse()}if(undefinedClass){classes.push(undefinedClass)}var cells=legend.selectAll("div.legendCell").data(classes);cells.exit().remove();var newcells=cells.enter().append("div").style(options.cellStyle);cells.attr("class","legendCell").each(function(d){if(d.class){d3.select(this).classed(d.class,true)}});if(reprAttribute=="fill"){if(classes[0].representation.substring(0,4)!="url("){newcells.append("span").attr("class","legendColor").style(options.colorBoxStyle).append("span").attr("class","fill").style(options.colorFillStyle);cells.select(".legendColor .fill").transition().style({"background-color":function(d){return d.representation},"border-color":function(d){return d.representation},color:function(d){return d.representation}})}else{newcells.append("svg").attr("class","legendColor").style(options.colorBoxStyle).append("rect").attr({width:100,height:100});cells.select(".legendColor rect").attr({fill:function(d){return d.representation}})}}else if(reprAttribute=="strokeColor"){newcells.append("span").attr("class","legendColor").style(options.colorBoxStyle).style("border","none").append("span").attr("class","fill").style(options.colorFillStyle);cells.select(".legendColor .fill").transition().style({"background-color":function(d){return d.representation},"border-color":function(d){return d.representation},color:function(d){return d.representation}})}newcells.append("span").attr("class","legendLabel").style(options.labelStyle);cells.attr("data-count",function(d){return d.count()});cells.select(".legendLabel").text(function(d){var formatter;if(d.valueRange){formatter=metadata.getRangeFormatter();return formatter(d.valueRange[0],d.valueRange[1],d.includeLower,d.includeUpper)}if(d.value){formatter=metadata.getFormatter();return formatter(d.value)}return metadata.undefinedLabel});if(options.histogram){newcells.append("span").attr("class","legendHistogramBar").style(options.histogramBarStyle);cells.select(".legendHistogramBar").transition().style("width",function(d){var width=options.histogramBarWidth(d.count());if(width.length&&width.indexOf("px")==width.lenght-2){return width}return Math.round(width)+"px"}).text(function(d){return" "+d.count()})}if(options.callback)options.callback()}};mapmap.legend.svg=function(range,labelFormat,histogram,options){var DEFAULTS={cellSpacing:5,layout:"vertical",histogram:false,histogramLength:80,containerAttributes:{transform:"translate(20,10)"},backgroundAttributes:{fill:"#fff","fill-opacity":.9,x:-10,y:-10,width:220},cellAttributes:{},colorAttributes:{width:40,height:18,stroke:"#000","stroke-width":"0.5px",fill:"#fff"},textAttributes:{"font-size":10,"pointer-events":"none",dy:12},histogramBarAttributes:{width:0,x:140,y:4,height:10,fill:"#000","fill-opacity":.2}};var layouts={horizontal:{cellAttributes:{transform:function(d,i){return"translate("+i*(options.colorAttributes.width+options.cellSpacing)+",0)"}},textAttributes:{y:function(){return options.colorAttributes.height+options.cellSpacing}}},vertical:{cellAttributes:{transform:function(d,i){return"translate(0,"+i*(options.colorAttributes.height+options.cellSpacing)+")"}},textAttributes:{x:function(){return options.colorAttributes.width+options.cellSpacing}}}};var layout=layouts[options.layout];if(options.layout=="vertical"){range.reverse()}this._elements.legend.attr(options.containerAttributes);var bg=this._elements.legend.selectAll("rect.background").data([1]);bg.enter().append("rect").attr("class","background").attr(options.backgroundAttributes);bg.transition().attr("height",histogram.length*(options.colorAttributes.height+options.cellSpacing)+(20-options.cellSpacing));var cells=this._elements.legend.selectAll("g.cell").data(range);cells.exit().remove();var newcells=cells.enter().append("g").attr("class","cell").attr(options.cellAttributes).attr(layout.cellAttributes);newcells.append("rect").attr("class","color").attr(options.colorAttributes).attr(layout.colorAttributes);if(options.histogram){newcells.append("rect").attr("class","bar").attr(options.histogramBarAttributes);cells.select(".bar").transition().attr("width",function(d,i){return histogram[histogram.length-i-1].y*options.histogramLength})}newcells.append("text").attr(options.textAttributes).attr(layout.textAttributes);cells.select(".color").transition().attr("fill",function(d){return d});cells.select("text").text(labelFormat)};mapmap.prototype.projection=function(projection){this._projection=projection;return this};mapmap.prototype.extent=function(selection,options){var map=this;this.selected_extent=selection||this.selected;this._promise.geometry.then(function(topo){var geom=map.getRepresentations(map.selected_extent);var all={type:"FeatureCollection",features:[]};geom.each(function(d){all.features.push(d)});map._extent(all,options)});return this};mapmap.prototype._extent=function(geom,options){options=dd.merge({fillFactor:.9},options);if(geom.type&&geom.type=="Topology"){var names=Object.keys(geom.objects);var all=[];for(var i=0;i