/*
 Map plugin v0.1 for Highcharts

 (c) 2011-2013 Torstein Hønsi

 License: www.highcharts.com/license
*/
(function(c){var r=c.Axis,o=c.each,w=c.extend,p=c.merge,s=c.pick,t=c.numberFormat,u=c.getOptions().plotOptions,v=c.Color,q=function(){};c.pathToArray=function(a){var b,a=a.replace(/([A-Za-z])/g," $1 "),a=a.replace(/^\s*/,"").replace(/\s*$/,""),a=a.split(/[ ,]+/);for(b=0;b<a.length;b++)/[a-zA-Z]/.test(a[b])||(a[b]=parseFloat(a[b]));return a};c.wrap(r.prototype,"init",function(a,b,d){b.options.chart.type==="map"&&w(this,{getSeriesExtremes:function(){var a=this.isXAxis,b=Number.MAX_VALUE,d=Number.MIN_VALUE;
o(this.series,function(g){b=Math.min(b,g[a?"minX":"minY"]);d=Math.max(d,g[a?"maxX":"maxY"])});this.dataMin=b;this.dataMax=d},setAxisTranslation:function(){var a=this.chart,b,d=a.plotWidth/a.plotHeight;b=this.isXAxis;a=a.xAxis[0];r.prototype.setAxisTranslation.call(this);if(!b&&a.transA!==void 0)this.transA=a.transA=Math.min(this.transA,a.transA),b=(a.max-a.min)/(this.max-this.min),b=b>d?this:a,d=(b.max-b.min)*b.transA,b.minPixelPadding=(b.len-d)/2}});return a.call(this,b,d)});u.map=p(u.scatter,{animation:!1,
minOpacity:0.2,nullColor:"#F8F8F8",borderColor:"silver",borderWidth:1,marker:null,stickyTracking:!1,tooltip:{followPointer:!0,headerFormat:'<span style="font-size:10px">{point.key}</span><br/>',pointFormat:"{series.name}: {point.y}<br/>"}});c.seriesTypes.map=c.extendClass(c.seriesTypes.scatter,{type:"map",pointAttrToOptions:{stroke:"borderColor","stroke-width":"borderWidth",fill:"color"},colorKey:"y",trackerGroups:["group","markerGroup"],getSymbol:q,getExtremesFromAll:!0,init:function(a){var b=this,
d=a.options.legend.valueDecimals,j=[],h,f,g,m,e,k,l;l=a.options.legend.layout==="horizontal";c.Series.prototype.init.apply(this,arguments);e=b.options.colorRange;if(b.options.valueRanges)o(b.options.valueRanges,function(a){f=a.from;g=a.to;h="";f===void 0?h="< ":g===void 0&&(h="> ");f!==void 0&&(h+=t(f,d));f!==void 0&&g!==void 0&&(h+=" - ");g!==void 0&&(h+=t(g,d));j.push(c.extend({chart:b.chart,name:h,options:{},drawLegendSymbol:c.seriesTypes.area.prototype.drawLegendSymbol,visible:!0,setState:function(){},
setVisible:function(){}},a))}),b.legendItems=j;else if(e)f=e.from,g=e.to,m=e.fromLabel,e=e.toLabel,k=l?[0,0,1,0]:[0,1,0,0],l||(l=m,m=e,e=l),k={linearGradient:{x1:k[0],y1:k[1],x2:k[2],y2:k[3]},stops:[[0,f],[1,g]]},j=[{chart:b.chart,options:{},fromLabel:m,toLabel:e,color:k,drawLegendSymbol:this.drawLegendSymbol,visible:!0,setState:function(){},setVisible:function(){}}],b.legendItems=j},drawLegendSymbol:function(a,b){var d=a.options.symbolPadding,j=s(a.options.padding,8),h,f,g=this.chart.renderer.fontMetrics(a.options.itemStyle.fontSize).h,
c=a.options.layout==="horizontal",e;e=s(a.options.rectangleLength,200);c?(h=-(d/2),f=0):(h=-e+a.baseline-d/2,f=j+g);b.fromText=this.chart.renderer.text(b.fromLabel,f,h).attr({zIndex:2}).add(b.legendGroup);f=b.fromText.getBBox();b.legendSymbol=this.chart.renderer.rect(c?f.x+f.width+d:f.x-g-d,f.y,c?e:g,c?g:e,2).attr({zIndex:1}).add(b.legendGroup);e=b.legendSymbol.getBBox();b.toText=this.chart.renderer.text(b.toLabel,e.x+e.width+d,c?h:e.y+e.height-d).attr({zIndex:2}).add(b.legendGroup);h=b.toText.getBBox();
c?(a.offsetWidth=f.width+e.width+h.width+d*2+j,a.itemY=g+j):(a.offsetWidth=Math.max(f.width,h.width)+d+e.width+j,a.itemY=e.height+j,a.itemX=d)},getBox:function(){var a=-Math.pow(2,31),b=Math.pow(2,31)-1,d=-Math.pow(2,31),j=Math.pow(2,31)-1;o(this.options.data,function(c){for(var c=c.path,f=c.length,g=!1;f--;)typeof c[f]==="number"&&(g?(a=Math.max(a,c[f]),b=Math.min(b,c[f])):(d=Math.max(d,c[f]),j=Math.min(j,c[f])),g=!g)});this.minY=j;this.maxY=d;this.minX=b;this.maxX=a},translatePath:function(a){var b=
!1,d=this.xAxis,c=this.yAxis,a=[].concat(a);for(i=a.length;i--;)typeof a[i]==="number"&&(a[i]=b?Math.round(d.translate(a[i])):Math.round(c.len-c.translate(a[i])),b=!b);return a},setData:function(){c.Series.prototype.setData.apply(this,arguments);this.getBox()},translate:function(){var a=this,b=Number.MAX_VALUE,d=Number.MIN_VALUE;a.generatePoints();o(a.data,function(c){c.shapeType="path";c.shapeArgs={d:a.translatePath(c.path)};if(typeof c.y==="number")if(c.y>d)d=c.y;else if(c.y<b)b=c.y});a.translateColors(b,
d)},translateColors:function(a,b){var d=this.options,c=d.valueRanges,h=d.colorRange,f=this.colorKey;o(this.data,function(d){var m=d[f],e=[],k,l,n;if(c)for(n=c.length;n--;){if(e=c[n],k=e.from,l=e.to,(k===void 0||m>=k)&&(l===void 0||m<=l)){d.options.color=e.color;break}}else if(h&&m!==void 0){k=v(h.from);l=v(h.to);m=(b-m)/(b-a);for(n=4;n--;)e[n]=Math.round(l.rgba[n]+(k.rgba[n]-l.rgba[n])*m);d.options.color="rgba("+e.join(",")+")"}})},drawGraph:q,drawDataLabels:q,drawPoints:function(){var a,b=this.colorKey;
o(this.data,function(a){a.plotY=1;if(a[b]===null)a[b]=0,a.isNull=!0});c.seriesTypes.column.prototype.drawPoints.apply(this);o(this.data,function(c){a=c.graphic.getBBox();c.tooltipPos=[a.x+a.width/2,a.y+a.height/2];c.plotX=c.tooltipPos[0];c.plotY=c.tooltipPos[1];c.isNull&&(c[b]=null)});c.Series.prototype.drawDataLabels.call(this)}});c.Map=function(a,b){var d={endOnTick:!1,gridLineWidth:0,labels:{enabled:!1},lineWidth:0,minPadding:0,maxPadding:0,startOnTick:!1,tickWidth:0,title:null};seriesOptions=
a.series;a.series=null;a=p({chart:{type:"map"},xAxis:d,yAxis:p(d,{reversed:!0})},a,{chart:{inverted:!1}});a.series=seriesOptions;return new c.Chart(a,b)}})(Highcharts);
