chartogram
Advanced tools
Comparing version 0.1.14 to 0.1.15
@@ -1,2 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).chartogram=e()}(this,function(){"use strict";function t(t){return function(t){if(Array.isArray(t)){for(var e=0,n=new Array(t.length);e<t.length;e++)n[e]=t[e];return n}}(t)||function(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}function e(t){for(;t.firstChild;)t.removeChild(t.firstChild)}function n(t,e){for(t=Math.floor(t);;){if(t<e)return e;if(t%e==0)return t;t--}}function i(t,e){var n,i=0,a=function(){n=void 0,i=Date.now(),t()};return function(){var r=Date.now(),o=e-(r-i);o<=0?(n&&(clearTimeout(n),n=void 0),i=r,t()):n||(n=setTimeout(a,o))}}function a(t,e,n){function i(t){n(t.changedTouches[0].clientX,t.changedTouches[0].clientY)}function a(t){n(t.clientX,t.clientY)}function r(){window.removeEventListener("pointermove",a),window.removeEventListener("touchmove",i),window.removeEventListener("pointerup",r),window.removeEventListener("pointercancel",r),window.removeEventListener("touchend",r),window.removeEventListener("touchcancel",r)}function o(t){if(t.touches.length>1)return r();e(t.changedTouches[0].clientX,t.changedTouches[0].clientY),window.addEventListener("touchmove",i),window.addEventListener("touchend",r),window.addEventListener("touchcancel",r)}function s(t){e(t.clientX,t.clientY),window.addEventListener("pointermove",a),window.addEventListener("pointerup",r),window.addEventListener("pointercancel",r)}return t.addEventListener("touchstart",o),t.addEventListener("pointerdown",s),function(){r(),t.removeEventListener(s),t.removeEventListener(o)}}function r(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}function o(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}var s=function(){function t(e){var n=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),o(this,"setUpListener",function(){!function(t,e,n,i){var a;function r(t,e){n(t,e,a)}function o(){a=t.getBoundingClientRect(),e()}function s(e){if(e.touches.length>1)return l();o(),t.addEventListener("touchend",l),t.addEventListener("touchmove",h),t.addEventListener("touchend",l),t.addEventListener("touchcancel",l),h(e)}function h(t){var e=t.changedTouches[0].clientX,n=t.changedTouches[0].clientY;e<a.left||e>a.left+a.width||n<a.top||n>a.top+a.height?l():r(e,n)}function c(t){r(t.clientX,t.clientY)}function l(){t.removeEventListener("pointermove",c),t.removeEventListener("pointerleave",l),t.removeEventListener("pointercancel",l),t.removeEventListener("touchmove",h),t.removeEventListener("touchend",l),t.removeEventListener("touchcancel",l),i()}function u(){o(),t.addEventListener("pointermove",c),t.addEventListener("pointerleave",l),t.addEventListener("pointercancel",l)}t.addEventListener("touchstart",s),t.addEventListener("pointerenter",u)}(n.props.canvas,function(){},n.onTrack,function(){return n.unmount()})}),o(this,"onTrack",function(t,e,i){var a,r=n.props,o=r.minX,s=r.maxX,h=r.xPoints,c=o+(t-i.left)/i.width*(s-o),l=h.findIndex(function(t){return t>=c}),u=l-1;if((l<0||l>=h.length)&&(l=-1),(u<0||u>=h.length)&&(u=-1),l<0){if(u<0)return n.unmount();a=u}else if(u<0)a=l;else{var p=h[u],d=h[l];a=c-p>d-c?l:u}var m=h[a];m!==n.tooltipForX&&(n.tooltipForX=m,n.tooltip||n.mount(),n.updateTooltip(m,a,i.width))}),o(this,"unmountLine",function(){n.props.canvas.removeChild(n.line),n.line=void 0}),o(this,"mountPoints",function(){var t=n.props.pointsContainer;n.points=n.renderPoints();var e=n.points,i=Array.isArray(e),a=0;for(e=i?e:e[Symbol.iterator]();;){var r;if(i){if(a>=e.length)break;r=e[a++]}else{if((a=e.next()).done)break;r=a.value}var o=r;t.appendChild(o)}}),o(this,"unmountPoints",function(){var t=n.props.pointsContainer,e=n.points,i=Array.isArray(e),a=0;for(e=i?e:e[Symbol.iterator]();;){var r;if(i){if(a>=e.length)break;r=e[a++]}else{if((a=e.next()).done)break;r=a.value}var o=r;t.removeChild(o)}n.points=void 0}),o(this,"updateLinePosition",function(t){n.isLineRendered()||n.mountLine();var e=n.props,i=e.canvasWidth,a=e.aspectRatio,r=e.fixSvgCoordinate,o=e.mapX;n.line.setAttributeNS(null,"x1",r(o(t))),n.line.setAttributeNS(null,"x2",r(o(t))),n.line.setAttributeNS(null,"y1",0),n.line.setAttributeNS(null,"y2",r(i/a))}),o(this,"updatePointPositions",function(t,e){for(var i=n.props,a=i.maxY,r=i.y,o=0,s=0;o<r.length;){if(r[o].isShown){var h=n.points[s],c=r[o].points[t]/a;h.style.left="".concat(100*e,"%"),h.style.bottom="".concat(100*c,"%"),s++}o++}}),this.props=e}var e,n,i;return e=t,(n=[{key:"componentDidMount",value:function(){this.unlisten=this.setUpListener()}},{key:"componentWillUnmount",value:function(){this.unlisten(),this.unmount()}},{key:"componentDidUpdate",value:function(){}},{key:"mount",value:function(){var t=this.props.container;this.tooltip=this.renderTooltip(),t.appendChild(this.tooltip),this.mountPoints(),this.isLineRendered()||this.mountLine()}},{key:"unmount",value:function(){if(this.tooltip){var t=this.props.container;this.tooltipForX=void 0,t.removeChild(this.tooltip),this.tooltip=void 0,this.unmountPoints(),this.isLineRendered()&&this.unmountLine()}}},{key:"mountLine",value:function(){var t=this.props.canvas;this.line=this.renderLine(),t.insertBefore(this.line,t.querySelector("polyline"))}},{key:"renderTooltip",value:function(){var t=this.props.y,e=document.createElement("div");e.classList.add("chartogram__tooltip");var n=document.createElement("h1");n.classList.add("chartogram__tooltip-header"),e.appendChild(n);var i=document.createElement("ul");i.classList.add("chartogram__tooltip-values"),e.appendChild(i);var a=t,r=Array.isArray(a),o=0;for(a=r?a:a[Symbol.iterator]();;){var s;if(r){if(o>=a.length)break;s=a[o++]}else{if((o=a.next()).done)break;s=o.value}var h=s,c=h.isShown,l=h.color;if(c){var u=document.createElement("li");u.style.color=l,i.appendChild(u);var p=document.createElement("div");u.appendChild(p);var d=document.createElement("div");u.appendChild(d)}}return e}},{key:"renderLine",value:function(){var t=document.createElementNS("http://www.w3.org/2000/svg","line");return t.setAttribute("class","chartogram__tooltip-line"),t}},{key:"isLineRendered",value:function(){var t=this.props.canvas;return this.line&&this.line.parentNode===t}},{key:"renderPoints",value:function(){var t=[],e=this.props,n=e.pointsContainer,i=e.y,a=Array.isArray(i),r=0;for(i=a?i:i[Symbol.iterator]();;){var o;if(a){if(r>=i.length)break;o=i[r++]}else{if((r=i.next()).done)break;o=r.value}var s=o;if(s.isShown){var h=document.createElement("div");h.classList.add("chartogram__tooltip-point"),h.style.color=s.color,t.push(h),n.appendChild(h)}}return t}},{key:"updateTooltip",value:function(t,e,n){var i=this.props,a=i.minX,r=(t-a)/(i.maxX-a);this.updateTooltipXValue(t),this.updateTooltipYValues(e),this.updateTooltipPosition(r,n),this.updatePointPositions(e,r),this.updateLinePosition(t)}},{key:"updateTooltipPosition",value:function(t,e){var n=this.props,i=n.tooltipShift,a=n.maxOverflow,r=Math.round(t*e);r-=void 0===i?40:i,this.tooltip.style.left=0,this.tooltip.style.marginRight=0;var o=this.tooltip.offsetWidth,s=void 0===a?5:a,h=0;r<0?(h=-1*Math.min(-1*r,s),r=0):r>e-o&&(h=Math.min(r-(e-o),s),r=e-o),this.tooltip.style.left=r+h+"px",this.tooltip.style.marginRight=-h+"px"}},{key:"updateTooltipXValue",value:function(t){var e=this.props.formatX;this.tooltip.childNodes[0].textContent=e(t,{long:!0})}},{key:"updateTooltipYValues",value:function(t){var e=this.props.y,n=this.tooltip.childNodes[1],i=0,a=e,r=Array.isArray(a),o=0;for(a=r?a:a[Symbol.iterator]();;){var s;if(r){if(o>=a.length)break;s=a[o++]}else{if((o=a.next()).done)break;s=o.value}var h=s,c=h.isShown,l=h.points,u=h.name;c&&(n.childNodes[i].childNodes[0].textContent=l[t],n.childNodes[i].childNodes[1].textContent=u,i++)}}}])&&r(e.prototype,n),i&&r(e,i),t}();function h(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}function c(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}var l=function(){function t(e){var n=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),c(this,"onResize",function(t){n.setState({aspectRatio:n.getCanvasAspectRatio(),canvasWidthPx:n.canvas.getBoundingClientRect().width})}),c(this,"onResizeThrottled",i(this.onResize,33)),c(this,"mapX",function(t){var e=n.props,i=e.canvasWidth,a=e.minX;return(t-a)/(e.maxX-a)*i}),c(this,"mapY",function(t){var e=n.props,i=e.canvasWidth,a=e.minY;return(t-a)/(e.maxY-a)*i/n.state.aspectRatio}),this.props=e}var n,a,r;return n=t,(a=[{key:"componentDidMount",value:function(t){this.canvasWrapper=t.querySelector(".chartogram__canvas-wrapper"),this.canvas=t.querySelector(".chartogram__canvas"),this.xAxis=t.querySelector(".chartogram__x"),this.yAxis=t.querySelector(".chartogram__y"),this.tooltipContainer=t.querySelector(".chartogram__plan"),this.state={aspectRatio:this.getCanvasAspectRatio(),canvasWidthPx:this.canvas.getBoundingClientRect().width},this.updateAspectRatio(),this.mountGridLines(),this.mountXAxis(),this.mountYAxis(),this.mountGraphs(),this.tooltip=new s(this.getTooltipProps()),this.tooltip.componentDidMount(t),this.render(),window.addEventListener("resize",this.onResizeThrottled)}},{key:"componentWillUnmount",value:function(){window.removeEventListener("resize",this.onResizeThrottled)}},{key:"setState",value:function(t){var e=this.state;this.state=function(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{},i=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(i=i.concat(Object.getOwnPropertySymbols(n).filter(function(t){return Object.getOwnPropertyDescriptor(n,t).enumerable}))),i.forEach(function(e){c(t,e,n[e])})}return t}({},this.state,t),this.componentDidUpdate(this.props,e)}},{key:"componentDidUpdate",value:function(t,n){n&&(this.state.aspectRatio!==n.aspectRatio&&this.updateAspectRatio(this.state.aspectRatio),this.state.canvasWidthPx!==n.canvasWidthPx&&(e(this.xAxis),this.mountXAxis())),this.render(),this.tooltip.props=this.getTooltipProps(),this.tooltip.componentDidUpdate()}},{key:"getTooltipProps",value:function(){return{canvas:this.canvas,container:this.tooltipContainer,pointsContainer:this.canvasWrapper,formatX:this.props.formatX,formatY:this.props.formatY,canvasWidth:this.props.canvasWidth,aspectRatio:this.state.aspectRatio,mapX:this.mapX,fixSvgCoordinate:this.props.fixSvgCoordinate,minX:this.props.minX,maxX:this.props.maxX,maxY:this.props.maxY,xPoints:this.props.xPoints,y:this.props.y}}},{key:"updateAspectRatio",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.state.aspectRatio,e=this.props,n=e.canvasWidth,i=e.fixSvgCoordinate;this.canvas.setAttribute("viewBox","0 0 ".concat(n," ").concat(i(n/t)))}},{key:"getCanvasAspectRatio",value:function(){var t=this.canvas.getBoundingClientRect();return t.width/t.height}},{key:"render",value:function(){var t=this,e=this.props,n=e.yAxisTickMarksCount,i=e.y,a=e.minX,r=e.maxX,o=e.minY,s=e.maxY,h=e.maxYGaugeMark,c=e.graphOpacity,l=o,u=h,p=(s-o)/(u-l);(function(t,e,n){for(var i=new Array(n),a=0;a<n;)i[a]=t+a*(e-t)/(n-1),a++;return i})(l,u,n).forEach(function(e,n){return t.updateGridLine(n,e)});for(var d=0;d<i.length;){var m=i[d],f=m.isShown,v=m.graphPoints,g=m.color,y=c[d];f||y>0?this.graphs[d]?this.updateGraph(d,v,y):this.mountGraph(d,v,g,y):this.graphs[d]&&this.unmountGraph(d),d++}this.updateAxes(a,r,l,u,p)}},{key:"renderGraph",value:function(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:1,i=document.createElementNS("http://www.w3.org/2000/svg","polyline");return i.setAttribute("stroke",e),i.classList.add("chartogram__graph"),this.updateGraph(i,t,n),i}},{key:"renderGridLine",value:function(t){var e=this.props,n=e.fixSvgCoordinate,i=e.minX,a=e.maxX,r=(e.minY,e.maxY),o=document.createElementNS("http://www.w3.org/2000/svg","line");return o.classList.add("chartogram__grid-line"),o.setAttribute("x1",n(this.mapX(i))),o.setAttribute("x2",n(this.mapX(a))),o.setAttribute("y1",n(this.mapY(r-t))),o.setAttribute("y2",n(this.mapY(r-t))),o}},{key:"mountGridLines",value:function(){var t=this.props.yAxisTickMarksCount;this.gridLines=[];for(var e=0;e<t;){var n=this.renderGridLine(0);this.gridLines.push(n),this.canvas.appendChild(n),e++}}},{key:"mountXAxis",value:function(){for(var t=this.getXGaugeTickMarksCount(),e=0;e<t;)this.xAxis.appendChild(document.createElement("div")),e++}},{key:"getXGaugeTickMarksCount",value:function(){var t=this.props.xAxisTickMarkWidth,e=this.state.canvasWidthPx/t;return e=e>2?Math.pow(e-2,.75):0,2+Math.floor(e)}},{key:"mountYAxis",value:function(){for(var t=this.props.yAxisTickMarksCount,e=0;e<t;)this.yAxis.appendChild(document.createElement("div")),e++}},{key:"mountGraphs",value:function(){var t=this;this.graphs=[],this.props.y.forEach(function(e,n){var i=e.graphPoints,a=e.color;t.mountGraph(n,i,a)})}},{key:"mountGraph",value:function(t,e,n,i){var a=this.renderGraph(e,n,i);this.graphs[t]=a,this.canvas.appendChild(a)}},{key:"unmountGraph",value:function(t){this.canvas.removeChild(this.graphs[t]),this.graphs[t]=void 0}},{key:"updateGraph",value:function(t,e,n){var i=this,a=this.props,r=a.createPolylinePoints,o=a.xGraphPoints,s=a.maxY;"number"==typeof t&&(t=this.graphs[t]),t.setAttribute("points",r(o.map(this.mapX),e.map(function(t){return i.mapY(s-t)})).join(" ")),1!==n&&(t.style.opacity=n)}},{key:"updateGridLine",value:function(t,e){var n=this.props,i=n.fixSvgCoordinate,a=n.maxY,r=this.gridLines[t];r.setAttribute("y1",i(this.mapY(a-e))),r.setAttribute("y2",i(this.mapY(a-e)))}},{key:"updateAxes",value:function(t,e,n,i,a){var r=this.props,o=r.formatX,s=r.formatY;this.updateAxis(this.xAxis,t,e,o),this.updateAxis(this.yAxis,n,i,s),this.yAxis.style.height="".concat(100/a,"%")}},{key:"updateAxis",value:function(t,e,n,i){for(var a=t.childNodes.length,r=0;r<a;){var o=t.childNodes[r],s=e+r*(n-e)/(a-1);i&&(s=i(s)),o.textContent=s,r++}}}])&&h(n.prototype,a),r&&h(n,r),t}();function u(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var n=[],i=!0,a=!1,r=void 0;try{for(var o,s=t[Symbol.iterator]();!(i=(o=s.next()).done)&&(n.push(o.value),!e||n.length!==e);i=!0);}catch(t){a=!0,r=t}finally{try{i||null==s.return||s.return()}finally{if(a)throw r}}return n}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}function p(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}function d(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}l.INITIAL_MARKUP='\n\t<div class="chartogram__plan-with-axes">\n\t\t<div class="chartogram__plan">\n\t\t\t<div class="chartogram__top-border"></div>\n\t\t\t<div class="chartogram__canvas-wrapper">\n\t\t\t\t<svg class="chartogram__canvas"></svg>\n\t\t\t\t<div class="chartogram__x"></div>\n\t\t\t\t<div class="chartogram__y-wrapper">\n\t\t\t\t\t<div class="chartogram__y"></div>\n\t\t\t\t</div>\n\t\t\t</div>\n\t\t</div>\n\t</div>\n';var m=function(){function e(t){var n=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e),d(this,"onResize",function(t){n.setState({aspectRatio:n.getCanvasAspectRatio()})}),d(this,"onResizeThrottled",i(this.onResize,33)),d(this,"setState",function(t){t.aspectRatio!==n.state.aspectRatio&&n.updateAspectRatio(t.aspectRatio),n.state=function(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{},i=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(i=i.concat(Object.getOwnPropertySymbols(n).filter(function(t){return Object.getOwnPropertyDescriptor(n,t).enumerable}))),i.forEach(function(e){d(t,e,n[e])})}return t}({},n.state,t),n.render()}),d(this,"mapX",function(t){var e=n.props,i=e.canvasWidth,a=e.data,r=a.minX;return(t-r)/(a.maxX-r)*i}),d(this,"mapY",function(t){var e=n.props,i=e.canvasWidth,a=e.minY;return(t-a)/(e.maxY-a)*i/n.state.aspectRatio}),d(this,"setUpTimelineWindowHandle",function(t){var e,i,r,o,s="left"===t?n.timelineWindowLeftHandle:n.timelineWindowRightHandle,h=parseFloat(getComputedStyle(n.timelineWindow).borderLeftWidth);return a(s,function(a){e=n.timeline.getBoundingClientRect();var s=n.timelineWindow.getBoundingClientRect();"left"===t?(i=e.left,r=s.left+s.width-2*h,o=a-s.left):(i=s.left+2*h,r=e.left+e.width,o=a-(s.left+s.width))},function(a){a-=o;var s=((a=Math.max(Math.min(a,r),i))-e.left)/e.width;"left"===t?n.updateBounds(s,n.props.toRatio):n.updateBounds(n.props.fromRatio,s)})}),d(this,"setUpTimelineWindow",function(){var t,e,i,r,o;return a(n.timelineWindowDrag,function(a){t=n.timeline.getBoundingClientRect(),e=n.timelineWindow.getBoundingClientRect(),o=a-e.left,i=t.left,r=t.left+(t.width-e.width)},function(a){a-=o;var s=((a=Math.max(Math.min(a,r),i))-t.left)/t.width;n.updateBounds(s,s+e.width/t.width)})}),d(this,"setTimelineWindowLeft",function(t){n.timelineOverlayLeft.style.right="".concat(100*(1-t),"%"),n.timelineWindow.style.left="".concat(100*t,"%")}),d(this,"setTimelineWindowRight",function(t){n.timelineOverlayRight.style.left="".concat(100*t,"%"),n.timelineWindow.style.right="".concat(100*(1-t),"%")}),this.props=t}var n,r,o;return n=e,(r=[{key:"componentDidUpdate",value:function(){this.state={aspectRatio:this.getCanvasAspectRatio()},this.onChangeBounds(this.props.fromRatio,this.props.toRatio),this.render()}},{key:"componentDidMount",value:function(t){this.timeline=t.querySelector(".chartogram__timeline"),this.timelineOverlayLeft=t.querySelector(".chartogram__timeline-overlay-left"),this.timelineWindowLeftHandle=t.querySelector(".chartogram__timeline-window__left-handle"),this.timelineWindow=t.querySelector(".chartogram__timeline-window"),this.timelineWindowDrag=t.querySelector(".chartogram__timeline-window__drag"),this.timelineWindowRightHandle=t.querySelector(".chartogram__timeline-window__right-handle"),this.timelineOverlayRight=t.querySelector(".chartogram__timeline-overlay-right"),this.timelineCanvas=t.querySelector(".chartogram__timeline-canvas"),this.state={aspectRatio:this.getCanvasAspectRatio()},this.updateAspectRatio(),this.setUpTimelineWindowHandle("left"),this.setUpTimelineWindowHandle("right"),this.setUpTimelineWindow(),window.addEventListener("resize",this.onResizeThrottled),this.onChangeBounds(this.props.fromRatio,this.props.toRatio),this.mountGraphs(),this.render()}},{key:"componentWillUnmount",value:function(){window.removeEventListener("resize",this.onResizeThrottled)}},{key:"updateAspectRatio",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.state.aspectRatio,e=this.props,n=e.canvasWidth,i=e.fixSvgCoordinate;this.timelineCanvas.setAttribute("viewBox","0 0 ".concat(n," ").concat(i(n/t)))}},{key:"getCanvasAspectRatio",value:function(){var t=this.timelineCanvas.getBoundingClientRect();return t.width/t.height}},{key:"render",value:function(){for(var e=this,n=this.props,i=n.y,a=n.data,r=n.graphOpacity,o=0,s=function(){var n=a.y[o],s=n.id,h=n.points,c=n.color,l=i.find(function(t){return t.id===s}).isShown,p=r[o];if(l||p>0){var d=u(function e(n,i,a){var r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:Math.max.apply(Math,t(i)),o=arguments.length>4&&void 0!==arguments[4]?arguments[4]:.025,s=arguments.length>5&&void 0!==arguments[5]?arguments[5]:0,h=arguments.length>6&&void 0!==arguments[6]?arguments[6]:new Array(n.length),c=arguments.length>7&&void 0!==arguments[7]?arguments[7]:new Array(n.length),l=arguments.length>8&&void 0!==arguments[8]?arguments[8]:0;if(s+2>n.length-1){for(;s<i.length;)h[l]=n[s],c[l]=i[s],l++,s++;return h=h.slice(0,l),c=c.slice(0,l),h.length<=a?[h,c]:(n.length/h.length<1.1&&(o=Math.min(o+.025,1)),e(h,c,a,r,o))}var u=(i[s+2]+i[s])/2;return Math.abs(u-i[s+1])/r<o?(h[l]=n[s],h[l+1]=n[s+2],c[l]=i[s],c[l+1]=i[s+2],e(n,i,a,r,o,s+2,h,c,l+1)):(h[l]=n[s],h[l+1]=n[s+1],h[l+2]=n[s+2],c[l]=i[s],c[l+1]=i[s+1],c[l+2]=i[s+2],e(n,i,a,r,o,s+2,h,c,l+2))}(a.x.points,h,80),2),m=d[0],f=d[1];e.graphs[o]?e.updateGraph(o,m,f,p):e.mountGraph(o,m,f,c,p)}else e.graphs[o]&&e.unmountGraph(o);o++};o<a.y.length;)s()}},{key:"renderGraph",value:function(t,e,n){var i=arguments.length>3&&void 0!==arguments[3]?arguments[3]:1,a=document.createElementNS("http://www.w3.org/2000/svg","polyline");return a.setAttribute("stroke",n),this.updateGraph(a,t,e,i),a.classList.add("chartogram__graph"),a}},{key:"mountGraphs",value:function(){var t=this;this.graphs=[];var e=this.props.data;e.y.forEach(function(n,i){var a=n.points,r=n.color;t.mountGraph(i,e.x.points,a,r)})}},{key:"mountGraph",value:function(t,e,n,i,a){var r=this.renderGraph(e,n,i,a);this.graphs[t]=r,this.timelineCanvas.appendChild(r)}},{key:"unmountGraph",value:function(t){this.timelineCanvas.removeChild(this.graphs[t]),this.graphs[t]=void 0}},{key:"updateGraph",value:function(t,e,n,i){var a=this,r=this.props,o=r.maxY,s=r.createPolylinePoints;"number"==typeof t&&(t=this.graphs[t]),t.setAttribute("points",s(e.map(this.mapX),n.map(function(t){return a.mapY(o-t)})).join(" ")),1!==i&&(t.style.opacity=i)}},{key:"onChangeBounds",value:function(t,e){this.setTimelineWindowLeft(t),this.setTimelineWindowRight(e)}},{key:"updateBounds",value:function(t,e){this.props.onChangeBounds(t,e),this.props.fromRatio=t,this.props.toRatio=e,this.onChangeBounds(t,e)}}])&&p(n.prototype,r),o&&p(n,o),e}();function f(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}function v(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}m.INITIAL_MARKUP='\n\t<div class="chartogram__timeline">\n\t\t<div class="chartogram__timeline-canvas-padding">\n\t\t\t<svg class="chartogram__timeline-canvas" preserveAspectRatio="none"></svg>\n\t\t</div>\n\t\t<div class="chartogram__timeline-overlay-left"></div>\n\t\t<div class="chartogram__timeline-overlay-right"></div>\n\t\t<div class="chartogram__timeline-window">\n\t\t\t<button type="button" class="chartogram__reset-button chartogram__timeline-window__drag"></button>\n\t\t\t<button type="button" class="chartogram__reset-button chartogram__timeline-window__left-handle"></button>\n\t\t\t<button type="button" class="chartogram__reset-button chartogram__timeline-window__right-handle"></button>\n\t\t</div>\n\t</div>\n';var g=function(){function t(e){var n=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),v(this,"renderToggler",function(t){var e=t.id,i=t.name,a=t.color,r=document.createElement("button");r.setAttribute("type","button"),r.classList.add("chartogram__chart-toggler"),r.classList.add("chartogram__chart-toggler--on"),r.classList.add("chartogram__reset-button");var o="http://www.w3.org/2000/svg",s=document.createElementNS(o,"svg");s.setAttribute("viewBox","0 0 19 19"),s.setAttribute("class","chartogram__chart-toggler-check");var h=document.createElementNS(o,"circle");h.setAttribute("cx","9.5"),h.setAttribute("cy","9.5"),h.setAttribute("r","9.5"),h.setAttribute("fill",a),s.appendChild(h);var c=document.createElementNS(o,"circle");c.setAttribute("cx","9.5"),c.setAttribute("cy","9.5"),c.setAttribute("r","8"),c.setAttribute("class","chartogram__chart-toggler-check-circle"),s.appendChild(c);var l=document.createElementNS(o,"path");return l.setAttribute("d","M13.64 4.94l-6.2 6.34-1.69-1.9c-.73-.63-1.89.1-1.36 1.06l2 3.38c.3.43 1.04.85 1.78 0 .32-.42 6.31-7.93 6.31-7.93.74-.84-.2-1.58-.84-.95z"),l.setAttribute("fill","white"),l.setAttribute("class","chartogram__chart-toggler-check-mark"),s.appendChild(l),r.appendChild(s),r.appendChild(document.createTextNode(i)),r.addEventListener("click",function(){return n.onToggle(e,r)}),r}),v(this,"onToggle",function(t,e){(0,n.props.onToggle)(t)&&e.classList.toggle("chartogram__chart-toggler--on")}),this.props=e}var e,n,i;return e=t,(n=[{key:"componentDidMount",value:function(t){this.togglers=t.querySelector(".chartogram__chart-togglers"),this.mount()}},{key:"mount",value:function(){var t=this.props.data.y,e=Array.isArray(t),n=0;for(t=e?t:t[Symbol.iterator]();;){var i;if(e){if(n>=t.length)break;i=t[n++]}else{if((n=t.next()).done)break;i=n.value}var a=i;this.togglers.appendChild(this.renderToggler(a))}}}])&&f(e.prototype,n),i&&f(e,i),t}();function y(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{},i=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(i=i.concat(Object.getOwnPropertySymbols(n).filter(function(t){return Object.getOwnPropertyDescriptor(n,t).enumerable}))),i.forEach(function(e){b(t,e,n[e])})}return t}function w(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}function b(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}g.INITIAL_MARKUP='\n\t<div class="chartogram__chart-togglers"></div>\n';var x=function(){function t(e,n,i){var a=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),b(this,"transitions",[]),b(this,"tick",function(){var t,e=y({},a.getState()),n=a.transitions,i=Array.isArray(n),r=0;for(n=i?n:n[Symbol.iterator]();;){var o;if(i){if(r>=n.length)break;o=n[r++]}else{if((r=n.next()).done)break;o=r.value}var s=o;s.tick(e)&&(t=t||[]).push(s)}a.setState(e),t&&(a.transitions=a.transitions.filter(function(e){return t.indexOf(e)<0})),a.transitions.length>0?a.timer=requestAnimationFrame(a.tick):a.timer=void 0}),this.getState=e,this.setState=n,this.props=i}var e,n,i;return e=t,(n=[{key:"cleanUp",value:function(){this.timer&&(cancelAnimationFrame(this.timer),this.timer=void 0)}},{key:"batch",value:function(){this.pendingTransitions=[]}},{key:"run",value:function(){var t=this,e=this.transitions.filter(function(e){var n=t.pendingTransitions,i=Array.isArray(n),a=0;for(n=i?n:n[Symbol.iterator]();;){var r;if(i){if(a>=n.length)break;r=n[a++]}else{if((a=n.next()).done)break;r=a.value}var o=r;if(e.owner===o.owner&&e.property===o.property)return!0}});if(e.length>0){var n=y({},this.getState()),i=e,a=Array.isArray(i),r=0;for(i=a?i:i[Symbol.iterator]();;){var o;if(a){if(r>=i.length)break;o=i[r++]}else{if((r=i.next()).done)break;o=r.value}o.tick(n)}this.setState(n),this.transitions=this.transitions.filter(function(t){return e.indexOf(t)<0})}var s=this.getTransitionDuration(this.pendingTransitions);this.transitions=this.transitions.concat(this.pendingTransitions.map(function(e){return y({},e,{startedAt:Date.now(),duration:s,easing:t.props.transitionEasing,fromValue:e.getter?e.getter(t.getState()):t.getState()[e.property],tick:function(t){var e,n=Date.now()-this.startedAt,i=Math.min(n/this.duration,1);return i=k[this.easing](i),e=this.getNewValue?this.getNewValue(this.fromValue,this.toValue,i):this.fromValue+(this.toValue-this.fromValue)*i,this.setter?this.setter(t,e):t[this.property]=e,1===i}})})),this.pendingTransitions=void 0,this.timer||(this.timer=requestAnimationFrame(this.tick))}},{key:"add",value:function(t,e,n,i){var a,r;"string"!=typeof e&&(a=e,r=n,n=i,e=void 0),this.pendingTransitions.push({owner:t,property:e,getter:a,setter:r,toValue:n})}},{key:"getTransitionDuration",value:function(t){var e=this.props.transitionDuration,n=e,i=t.find(function(t){return"charts"===t.owner&&"minY"===t.property}),a=t.find(function(t){return"charts"===t.owner&&"maxY"===t.property});if(i&&a){var r=this.getState().minY,o=this.getState().maxY,s=i.toValue,h=a.toValue,c=o-r,l=Math.abs(h-o)/c,u=Math.abs(s-r)/c,p=Math.max(u,l);n=e*Math.max(.2,2*Math.min(p,.5))}return n}}])&&w(e.prototype,n),i&&w(e,i),t}(),k={linear:function(t){return t},easeInOutSin:function(t){return(1+Math.sin(Math.PI*t-Math.PI/2))/2},easeInOutQuad:function(t){return t<.5?2*t*t:(4-2*t)*t-1},easeInOutCubic:function(t){return t<.5?4*t*t*t:(t-1)*(2*t-2)*(2*t-2)+1},easeOutCubic:function(t){return--t*t*t+1},easeOutQuad:function(t){return t*(2-t)}};function A(t){return function(t){if(Array.isArray(t)){for(var e=0,n=new Array(t.length);e<t.length;e++)n[e]=t[e];return n}}(t)||function(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}function _(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{},i=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(i=i.concat(Object.getOwnPropertySymbols(n).filter(function(t){return Object.getOwnPropertyDescriptor(n,t).enumerable}))),i.forEach(function(e){C(t,e,n[e])})}return t}function S(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}function C(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}var T=function(){function t(e,n){var i=this,a=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"Title",r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{};!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),C(this,"setState",function(t){var e=i.state;i.state=_({},i.state,t),i.componentDidUpdate(i.props,e)}),C(this,"onToggle",function(t){var e=i.state.y.findIndex(function(e){return e.id===t}),n=i.state.y[e];if(n.isShown&&1===i.state.y.filter(function(t){return t.isShown}).length)return;n.isShown=!n.isShown;var a=i.calculateMinMaxY(i.state.y),r=a.minY,o=a.maxY,s=a.minYGlobal,h=a.maxYGlobal,c=a.maxYGaugeMark;return i.transition.batch(),i.transition.add("charts","minY",r),i.transition.add("charts","maxY",o),i.transition.add("charts","maxYGaugeMark",c),i.transition.add("timeline","minYGlobal",s),i.transition.add("timeline","maxYGlobal",h),i.transition.add("graph#".concat(t,"/opacity"),function(t){return t.graphOpacity[e]},function(t,n){t.graphOpacity=t.graphOpacity.slice(),t.graphOpacity[e]=n},n.isShown?1:0),i.transition.run(),!0}),C(this,"onChangeBounds",function(t,e){var n=i.createState(t,e),a=n.minY,r=n.maxY,o=n.maxYGaugeMark;delete n.minY,delete n.maxY,delete n.maxYGaugeMark,i.setState(n),i.transition.batch(),i.transition.add("charts","minY",a),i.transition.add("charts","maxY",r),i.transition.add("charts","maxYGaugeMark",o),i.transition.run()}),C(this,"createPolylinePoints",function(t,e){return n=t.map(i.fixSvgCoordinate),a=e.map(i.fixSvgCoordinate),n.map(function(t,e){return"".concat(t,",").concat(a[e])});var n,a}),C(this,"fixSvgCoordinate",function(t){var e=i.props.precisionFactor;return Math.round(t*e)/e}),C(this,"formatX",function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=i.props,a=n.locale,r=n.formatX;if(r)return r(t,e);i.dateTimeFormatShort||(i.dateTimeFormatShort=new Intl.DateTimeFormat(a,{month:"short",day:"numeric"}),i.dateTimeFormatLong=new Intl.DateTimeFormat(a,{weekday:"short",month:"short",day:"numeric"}),i.dateTimeFormatLongWithYear=new Intl.DateTimeFormat(a,{weekday:"short",year:"numeric",month:"short",day:"numeric"}));var o=new Date(t);return e.long?o.getFullYear()===(new Date).getFullYear()?i.dateTimeFormatLong.format(o):i.dateTimeFormatLongWithYear.format(o):i.dateTimeFormatShort.format(o)}),C(this,"formatY",function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=i.props.formatY;return n?n(t,e):Math.round(t)}),this.props=_({title:a,transitionDuration:250,transitionEasing:"easeOutQuad",xAxisTickMarkWidth:60,yAxisTickMarksCount:6,timelineWindowSize:40,canvasWidth:512,precisionFactor:Math.pow(10,r.precision||3)},r),this.rootNode=e,this.data=_({},n,{minX:Math.min.apply(Math,A(n.x.points)),maxX:Math.max.apply(Math,A(n.x.points)),y:n.y.map(function(t){return _({},t,{min:Math.min.apply(Math,A(t.points)),max:Math.max.apply(Math,A(t.points))})})}),this.transition=new x(function(){return i.state},this.setState,this.props)}var i,a,r;return i=t,(a=[{key:"componentDidMount",value:function(){this.rootNode.classList.add("chartogram"),this.rootNode.innerHTML='\n\t\t\t<header class="chartogram__header">\n\t\t\t\t<h1 class="chartogram__title">'.concat(this.props.title,"</h1>\n\t\t\t</header>\n\t\t\t").concat(l.INITIAL_MARKUP,"\n\t\t\t").concat(m.INITIAL_MARKUP,"\n\t\t\t").concat(g.INITIAL_MARKUP,"\n\t\t"),this.state=this.getInitialState(),this.charts=new l(this.getChartsProps()),this.charts.componentDidMount(this.rootNode),this.timeline=new m(this.getTimelineProps()),this.timeline.componentDidMount(this.rootNode),this.togglers=new g(this.getTogglersProps()),this.togglers.componentDidMount(this.rootNode)}},{key:"componentWillUnmount",value:function(){this.charts.componentWillUnmount(),this.timeline.componentWillUnmount(),this.transition.cleanUp(),this.rootNode.classList.remove("chartogram"),e(this.rootNode)}},{key:"componentDidUpdate",value:function(t,e){this.charts.props=this.getChartsProps(),this.charts.componentDidUpdate(),this.shouldUpdateTimeline(e)&&(this.timeline.props=this.getTimelineProps(),this.timeline.componentDidUpdate())}},{key:"shouldUpdateTimeline",value:function(t){return this.state.graphOpacity!==t.graphOpacity||this.state.minYGlobal!==t.minYGlobal||this.state.maxYGlobal!==t.maxYGlobal}},{key:"getChartsProps",value:function(){return _({},this.props,this.state,{createPolylinePoints:this.createPolylinePoints,fixSvgCoordinate:this.fixSvgCoordinate,formatX:this.formatX,formatY:this.formatY})}},{key:"getTimelineProps",value:function(){return{data:this.data,canvasWidth:this.props.canvasWidth,fixSvgCoordinate:this.fixSvgCoordinate,createPolylinePoints:this.createPolylinePoints,fromRatio:this.state.fromRatio,toRatio:this.state.toRatio,minY:this.state.minYGlobal,maxY:this.state.maxYGlobal,y:this.state.y,graphOpacity:this.state.graphOpacity,onChangeBounds:this.onChangeBounds}}},{key:"getTogglersProps",value:function(){return{data:this.data,onToggle:this.onToggle}}},{key:"getInitialState",value:function(){var t,e=this.props.timelineWindowSize,n=this.data,i=n.minX,a=n.maxX;t=this.data.x.points.length>e?this.data.x.points.length-e:0;var r=(this.data.x.points[t]-i)/(a-i);return _({},this.createState(r,1),{graphOpacity:this.data.y.map(function(t){return 1})})}},{key:"createState",value:function(t,e){var n,i,a=this,r=this.data.x,o=this.data.minX+t*(this.data.maxX-this.data.minX),s=this.data.minX+e*(this.data.maxX-this.data.minX);n=o===this.data.minX?0:r.points.findIndex(function(t){return t>o})-1,i=s===this.data.maxX?r.points.length-1:r.points.findIndex(function(t){return t>s});var h=r.points.slice(n,i+1),c=h.slice();h.length>=2&&(r.points[n]!==o&&(c[0]=o),r.points[i]!==s&&(c[c.length-1]=s));var l=this.data.y.map(function(t,e){var c=a.data.y[e].points.slice(n,i+1),l=c.slice();if(h.length>=2){if(r.points[n]!==o){var u=a.data.y[e].points[n],p=u+(a.data.y[e].points[n+1]-u)*((o-a.data.x.points[n])/(a.data.x.points[n+1]-a.data.x.points[n]));l[0]=p}if(r.points[i]!==s){var d=a.data.y[e].points[i],m=a.data.y[e].points[i-1],f=m+(d-m)*((s-a.data.x.points[i-1])/(a.data.x.points[i]-a.data.x.points[i-1]));l[l.length-1]=f}}return _({},a.data.y[e],a.state?a.state.y[e]:{isShown:!0},{points:c,graphPoints:l,min:0,max:Math.max.apply(Math,A(l))})});return _({minX:o,maxX:s,fromIndex:n,toIndex:i,fromRatio:t,toRatio:e,xPoints:h,xGraphPoints:c},this.calculateMinMaxY(l),{y:l})}},{key:"calculateMinMaxY",value:function(t){var e=1/0,i=-1/0,a=t,r=Array.isArray(a),o=0;for(a=r?a:a[Symbol.iterator]();;){var s;if(r){if(o>=a.length)break;s=a[o++]}else{if((o=a.next()).done)break;s=o.value}var h=s;h.isShown&&(e=Math.min(e,h.min),i=Math.max(i,h.max))}var c=1/0,l=-1/0,u=function(){if(d){if(m>=p.length)return"break";f=p[m++]}else{if((m=p.next()).done)return"break";f=m.value}var e=f;t.find(function(t){return t.id===e.id}).isShown&&(c=Math.min(c,e.min),l=Math.max(l,e.max))},p=this.data.y,d=Array.isArray(p),m=0;for(p=d?p:p[Symbol.iterator]();;){var f;if("break"===u())break}return{minY:e,maxY:i,minYGlobal:c=0,maxYGlobal:l,maxYGaugeMark:n(i,10)}}}])&&S(i.prototype,a),r&&S(i,r),t}();return function(t,e,n,i){var a=new T(t,e,n,i);return a.componentDidMount(),function(){a.componentWillUnmount()}}}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).chartogram=e()}(this,function(){"use strict";function t(t){return function(t){if(Array.isArray(t)){for(var e=0,n=new Array(t.length);e<t.length;e++)n[e]=t[e];return n}}(t)||function(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}function e(t){for(;t.firstChild;)t.removeChild(t.firstChild)}function n(t,e,n){var a=n&&Math.pow(10,n);a&&(t*=a);var r=Math.floor(t/e);return r=function(t){var e,n=(r=t,1+(0|Math.log10((r^r>>31)-(r>>31)))),a=1;var r;n>2&&(a=Math.pow(10,n-2),t=Math.floor(t/Math.pow(10,n-2)));for(var o=i,s=0;s<o.length;s++){var h=o[s];if(!(t>h.threshold))break;e=h}if(e){var c=t%e.adjustment;c&&(t-=c)}return t*a}(r),a&&(r/=a),r*e}var i=[{threshold:5,adjustment:2},{threshold:40,adjustment:10}];function a(t,e){var n,i=0,a=function(){n=void 0,i=Date.now(),t()};return function(){var r=Date.now(),o=e-(r-i);o<=0?(n&&(clearTimeout(n),n=void 0),i=r,t()):n||(n=setTimeout(a,o))}}function r(t,e,n){function i(t){n(t.changedTouches[0].clientX,t.changedTouches[0].clientY)}function a(t){n(t.clientX,t.clientY)}function r(){window.removeEventListener("pointermove",a),window.removeEventListener("touchmove",i),window.removeEventListener("pointerup",r),window.removeEventListener("pointercancel",r),window.removeEventListener("touchend",r),window.removeEventListener("touchcancel",r)}function o(t){if(t.touches.length>1)return r();e(t.changedTouches[0].clientX,t.changedTouches[0].clientY),window.addEventListener("touchmove",i),window.addEventListener("touchend",r),window.addEventListener("touchcancel",r)}function s(t){e(t.clientX,t.clientY),window.addEventListener("pointermove",a),window.addEventListener("pointerup",r),window.addEventListener("pointercancel",r)}return t.addEventListener("touchstart",o),t.addEventListener("pointerdown",s),function(){r(),t.removeEventListener(s),t.removeEventListener(o)}}function o(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}function s(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}var h=function(){function t(e){var n=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),s(this,"setUpListener",function(){!function(t,e,n,i){var a;function r(t,e){n(t,e,a)}function o(){a=t.getBoundingClientRect(),e()}function s(e){if(e.touches.length>1)return l();o(),t.addEventListener("touchend",l),t.addEventListener("touchmove",h),t.addEventListener("touchend",l),t.addEventListener("touchcancel",l),h(e)}function h(t){var e=t.changedTouches[0].clientX,n=t.changedTouches[0].clientY;e<a.left||e>a.left+a.width||n<a.top||n>a.top+a.height?l():r(e,n)}function c(t){r(t.clientX,t.clientY)}function l(){t.removeEventListener("pointermove",c),t.removeEventListener("pointerleave",l),t.removeEventListener("pointercancel",l),t.removeEventListener("touchmove",h),t.removeEventListener("touchend",l),t.removeEventListener("touchcancel",l),i()}function u(){o(),t.addEventListener("pointermove",c),t.addEventListener("pointerleave",l),t.addEventListener("pointercancel",l)}t.addEventListener("touchstart",s),t.addEventListener("pointerenter",u)}(n.props.canvas,function(){},n.onTrack,function(){return n.unmount()})}),s(this,"onTrack",function(t,e,i){var a,r=n.props,o=r.minX,s=r.maxX,h=r.xPoints,c=o+(t-i.left)/i.width*(s-o),l=h.findIndex(function(t){return t>=c}),u=l-1;if((l<0||l>=h.length)&&(l=-1),(u<0||u>=h.length)&&(u=-1),l<0){if(u<0)return n.unmount();a=u}else if(u<0)a=l;else{var p=h[u],d=h[l];a=c-p>d-c?l:u}var m=h[a];m!==n.tooltipForX&&(n.tooltipForX=m,n.tooltip||n.mount(),n.updateTooltip(m,a,i.width))}),s(this,"unmountLine",function(){n.props.canvas.removeChild(n.line),n.line=void 0}),s(this,"mountPoints",function(){var t=n.props.pointsContainer;n.points=n.renderPoints();var e=n.points,i=Array.isArray(e),a=0;for(e=i?e:e[Symbol.iterator]();;){var r;if(i){if(a>=e.length)break;r=e[a++]}else{if((a=e.next()).done)break;r=a.value}var o=r;t.appendChild(o)}}),s(this,"unmountPoints",function(){var t=n.props.pointsContainer,e=n.points,i=Array.isArray(e),a=0;for(e=i?e:e[Symbol.iterator]();;){var r;if(i){if(a>=e.length)break;r=e[a++]}else{if((a=e.next()).done)break;r=a.value}var o=r;t.removeChild(o)}n.points=void 0}),s(this,"updateLinePosition",function(t){n.isLineRendered()||n.mountLine();var e=n.props,i=e.canvasWidth,a=e.aspectRatio,r=e.fixSvgCoordinate,o=e.mapX;n.line.setAttributeNS(null,"x1",r(o(t))),n.line.setAttributeNS(null,"x2",r(o(t))),n.line.setAttributeNS(null,"y1",0),n.line.setAttributeNS(null,"y2",r(i/a))}),s(this,"updatePointPositions",function(t,e){for(var i=n.props,a=i.maxY,r=i.y,o=0,s=0;o<r.length;){if(r[o].isShown){var h=n.points[s],c=r[o].points[t]/a;h.style.left="".concat(100*e,"%"),h.style.bottom="".concat(100*c,"%"),s++}o++}}),this.props=e}var e,n,i;return e=t,(n=[{key:"componentDidMount",value:function(){this.unlisten=this.setUpListener()}},{key:"componentWillUnmount",value:function(){this.unlisten(),this.unmount()}},{key:"componentDidUpdate",value:function(){}},{key:"mount",value:function(){var t=this.props.container;this.tooltip=this.renderTooltip(),t.appendChild(this.tooltip),this.mountPoints(),this.isLineRendered()||this.mountLine()}},{key:"unmount",value:function(){if(this.tooltip){var t=this.props.container;this.tooltipForX=void 0,t.removeChild(this.tooltip),this.tooltip=void 0,this.unmountPoints(),this.isLineRendered()&&this.unmountLine()}}},{key:"mountLine",value:function(){var t=this.props.canvas;this.line=this.renderLine(),t.insertBefore(this.line,t.querySelector("polyline"))}},{key:"renderTooltip",value:function(){var t=this.props.y,e=document.createElement("div");e.classList.add("chartogram__tooltip");var n=document.createElement("h1");n.classList.add("chartogram__tooltip-header"),e.appendChild(n);var i=document.createElement("ul");i.classList.add("chartogram__tooltip-values"),e.appendChild(i);var a=t,r=Array.isArray(a),o=0;for(a=r?a:a[Symbol.iterator]();;){var s;if(r){if(o>=a.length)break;s=a[o++]}else{if((o=a.next()).done)break;s=o.value}var h=s,c=h.isShown,l=h.color;if(c){var u=document.createElement("li");u.style.color=l,i.appendChild(u);var p=document.createElement("div");u.appendChild(p);var d=document.createElement("div");u.appendChild(d)}}return e}},{key:"renderLine",value:function(){var t=document.createElementNS("http://www.w3.org/2000/svg","line");return t.setAttribute("class","chartogram__tooltip-line"),t}},{key:"isLineRendered",value:function(){var t=this.props.canvas;return this.line&&this.line.parentNode===t}},{key:"renderPoints",value:function(){var t=[],e=this.props,n=e.pointsContainer,i=e.y,a=Array.isArray(i),r=0;for(i=a?i:i[Symbol.iterator]();;){var o;if(a){if(r>=i.length)break;o=i[r++]}else{if((r=i.next()).done)break;o=r.value}var s=o;if(s.isShown){var h=document.createElement("div");h.classList.add("chartogram__tooltip-point"),h.style.color=s.color,t.push(h),n.appendChild(h)}}return t}},{key:"updateTooltip",value:function(t,e,n){var i=this.props,a=i.minX,r=(t-a)/(i.maxX-a);this.updateTooltipXValue(t),this.updateTooltipYValues(e),this.updateTooltipPosition(r,n),this.updatePointPositions(e,r),this.updateLinePosition(t)}},{key:"updateTooltipPosition",value:function(t,e){var n=this.props,i=n.tooltipShift,a=n.maxOverflow,r=Math.round(t*e);r-=void 0===i?40:i,this.tooltip.style.left=0,this.tooltip.parentNode.style.marginRight=0;var o=this.tooltip.getBoundingClientRect().width,s=void 0===a?5:a,h=0;r<0?(h=-1*Math.min(-1*r,s),r=0):r>e-o&&(h=Math.min(r-(e-o),s),r=e-o),this.tooltip.style.left=r+h+"px",this.tooltip.style.marginRight=-h+"px"}},{key:"updateTooltipXValue",value:function(t){var e=this.props.formatX;this.tooltip.childNodes[0].textContent=e(t,{long:!0})}},{key:"updateTooltipYValues",value:function(t){var e=this.props.y,n=this.tooltip.childNodes[1],i=0,a=e,r=Array.isArray(a),o=0;for(a=r?a:a[Symbol.iterator]();;){var s;if(r){if(o>=a.length)break;s=a[o++]}else{if((o=a.next()).done)break;s=o.value}var h=s,c=h.isShown,l=h.points,u=h.name;c&&(n.childNodes[i].childNodes[0].textContent=l[t],n.childNodes[i].childNodes[1].textContent=u,i++)}}}])&&o(e.prototype,n),i&&o(e,i),t}();function c(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}function l(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}var u=function(){function t(e){var n=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),l(this,"onResize",function(t){n.setState({aspectRatio:n.getCanvasAspectRatio(),canvasWidthPx:n.canvas.getBoundingClientRect().width})}),l(this,"onResizeThrottled",a(this.onResize,33)),l(this,"mapX",function(t){var e=n.props,i=e.canvasWidth,a=e.minX;return(t-a)/(e.maxX-a)*i}),l(this,"mapY",function(t){var e=n.props,i=e.canvasWidth,a=e.minY;return(t-a)/(e.maxY-a)*i/n.state.aspectRatio}),this.props=e}var n,i,r;return n=t,(i=[{key:"componentDidMount",value:function(t){this.canvasWrapper=t.querySelector(".chartogram__canvas-wrapper"),this.canvas=t.querySelector(".chartogram__canvas"),this.xAxis=t.querySelector(".chartogram__x"),this.yAxis=t.querySelector(".chartogram__y"),this.tooltipContainer=t.querySelector(".chartogram__plan"),this.state={aspectRatio:this.getCanvasAspectRatio(),canvasWidthPx:this.canvas.getBoundingClientRect().width},this.updateAspectRatio(),this.mountGridLines(),this.mountXAxis(),this.mountYAxis(),this.mountGraphs(),this.tooltip=new h(this.getTooltipProps()),this.tooltip.componentDidMount(t),this.render(),window.addEventListener("resize",this.onResizeThrottled)}},{key:"componentWillUnmount",value:function(){window.removeEventListener("resize",this.onResizeThrottled)}},{key:"setState",value:function(t){var e=this.state;this.state=function(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{},i=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(i=i.concat(Object.getOwnPropertySymbols(n).filter(function(t){return Object.getOwnPropertyDescriptor(n,t).enumerable}))),i.forEach(function(e){l(t,e,n[e])})}return t}({},this.state,t),this.componentDidUpdate(this.props,e)}},{key:"componentDidUpdate",value:function(t,n){n&&(this.state.aspectRatio!==n.aspectRatio&&this.updateAspectRatio(this.state.aspectRatio),this.state.canvasWidthPx!==n.canvasWidthPx&&(e(this.xAxis),this.mountXAxis())),this.render(),this.tooltip.props=this.getTooltipProps(),this.tooltip.componentDidUpdate()}},{key:"getTooltipProps",value:function(){return{canvas:this.canvas,container:this.tooltipContainer,pointsContainer:this.canvasWrapper,formatX:this.props.formatX,formatY:this.props.formatY,canvasWidth:this.props.canvasWidth,aspectRatio:this.state.aspectRatio,mapX:this.mapX,fixSvgCoordinate:this.props.fixSvgCoordinate,minX:this.props.minX,maxX:this.props.maxX,maxY:this.props.maxY,xPoints:this.props.xPoints,y:this.props.y}}},{key:"updateAspectRatio",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.state.aspectRatio,e=this.props,n=e.canvasWidth,i=e.fixSvgCoordinate;this.canvas.setAttribute("viewBox","0 0 ".concat(n," ").concat(i(n/t)))}},{key:"getCanvasAspectRatio",value:function(){var t=this.canvas.getBoundingClientRect();return t.width/t.height}},{key:"render",value:function(){var t=this,e=this.props,n=e.yAxisTickMarksCount,i=e.y,a=e.minX,r=e.maxX,o=e.minY,s=e.maxY,h=e.maxYGaugeMark,c=e.graphOpacity,l=o,u=h,p=(s-o)/(u-l);(function(t,e,n){for(var i=new Array(n),a=0;a<n;)i[a]=t+a*(e-t)/(n-1),a++;return i})(l,u,n).forEach(function(e,n){return t.updateGridLine(n,e)});for(var d=0;d<i.length;){var m=i[d],v=m.isShown,f=m.graphPoints,g=m.color,y=c[d];v||y>0?this.graphs[d]?this.updateGraph(d,f,y):this.mountGraph(d,f,g,y):this.graphs[d]&&this.unmountGraph(d),d++}this.updateAxes(a,r,l,u,p)}},{key:"renderGraph",value:function(t,e){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:1,i=document.createElementNS("http://www.w3.org/2000/svg","polyline");return i.setAttribute("stroke",e),i.classList.add("chartogram__graph"),this.updateGraph(i,t,n),i}},{key:"renderGridLine",value:function(t){var e=this.props,n=e.fixSvgCoordinate,i=e.minX,a=e.maxX,r=(e.minY,e.maxY),o=document.createElementNS("http://www.w3.org/2000/svg","line");return o.classList.add("chartogram__grid-line"),o.setAttribute("x1",n(this.mapX(i))),o.setAttribute("x2",n(this.mapX(a))),o.setAttribute("y1",n(this.mapY(r-t))),o.setAttribute("y2",n(this.mapY(r-t))),o}},{key:"mountGridLines",value:function(){var t=this.props.yAxisTickMarksCount;this.gridLines=[];for(var e=0;e<t;){var n=this.renderGridLine(0);this.gridLines.push(n),this.canvas.appendChild(n),e++}}},{key:"mountXAxis",value:function(){for(var t=this.getXGaugeTickMarksCount(),e=0;e<t;)this.xAxis.appendChild(document.createElement("div")),e++}},{key:"getXGaugeTickMarksCount",value:function(){var t=this.props.xAxisTickMarkWidth,e=this.state.canvasWidthPx/t;return e=e>2?Math.pow(e-2,.75):0,2+Math.floor(e)}},{key:"mountYAxis",value:function(){for(var t=this.props.yAxisTickMarksCount,e=0;e<t;)this.yAxis.appendChild(document.createElement("div")),e++}},{key:"mountGraphs",value:function(){var t=this;this.graphs=[],this.props.y.forEach(function(e,n){var i=e.graphPoints,a=e.color;t.mountGraph(n,i,a)})}},{key:"mountGraph",value:function(t,e,n,i){var a=this.renderGraph(e,n,i);this.graphs[t]=a,this.canvas.appendChild(a)}},{key:"unmountGraph",value:function(t){this.canvas.removeChild(this.graphs[t]),this.graphs[t]=void 0}},{key:"updateGraph",value:function(t,e,n){var i=this,a=this.props,r=a.createPolylinePoints,o=a.xGraphPoints,s=a.maxY;"number"==typeof t&&(t=this.graphs[t]),t.setAttribute("points",r(o.map(this.mapX),e.map(function(t){return i.mapY(s-t)})).join(" ")),1!==n&&(t.style.opacity=n)}},{key:"updateGridLine",value:function(t,e){var n=this.props,i=n.fixSvgCoordinate,a=n.maxY,r=this.gridLines[t];r.setAttribute("y1",i(this.mapY(a-e))),r.setAttribute("y2",i(this.mapY(a-e)))}},{key:"updateAxes",value:function(t,e,n,i,a){var r=this.props,o=r.formatX,s=r.formatY;this.updateAxis(this.xAxis,t,e,o),this.updateAxis(this.yAxis,n,i,s),this.yAxis.style.height="".concat(100/a,"%")}},{key:"updateAxis",value:function(t,e,n,i){for(var a=t.childNodes.length,r=0;r<a;){var o=t.childNodes[r],s=e+r*(n-e)/(a-1);i&&(s=i(s)),o.textContent=s,r++}}}])&&c(n.prototype,i),r&&c(n,r),t}();function p(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var n=[],i=!0,a=!1,r=void 0;try{for(var o,s=t[Symbol.iterator]();!(i=(o=s.next()).done)&&(n.push(o.value),!e||n.length!==e);i=!0);}catch(t){a=!0,r=t}finally{try{i||null==s.return||s.return()}finally{if(a)throw r}}return n}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}function d(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}function m(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}u.INITIAL_MARKUP='\n\t<div class="chartogram__plan-with-axes">\n\t\t<div class="chartogram__plan">\n\t\t\t<div class="chartogram__top-border"></div>\n\t\t\t<div class="chartogram__canvas-wrapper">\n\t\t\t\t<svg class="chartogram__canvas"></svg>\n\t\t\t\t<div class="chartogram__x"></div>\n\t\t\t\t<div class="chartogram__y-wrapper">\n\t\t\t\t\t<div class="chartogram__y"></div>\n\t\t\t\t</div>\n\t\t\t</div>\n\t\t</div>\n\t</div>\n';var v=function(){function e(t){var n=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e),m(this,"onResize",function(t){n.setState({aspectRatio:n.getCanvasAspectRatio()})}),m(this,"onResizeThrottled",a(this.onResize,33)),m(this,"setState",function(t){t.aspectRatio!==n.state.aspectRatio&&n.updateAspectRatio(t.aspectRatio),n.state=function(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{},i=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(i=i.concat(Object.getOwnPropertySymbols(n).filter(function(t){return Object.getOwnPropertyDescriptor(n,t).enumerable}))),i.forEach(function(e){m(t,e,n[e])})}return t}({},n.state,t),n.render()}),m(this,"mapX",function(t){var e=n.props,i=e.canvasWidth,a=e.data,r=a.minX;return(t-r)/(a.maxX-r)*i}),m(this,"mapY",function(t){var e=n.props,i=e.canvasWidth,a=e.minY;return(t-a)/(e.maxY-a)*i/n.state.aspectRatio}),m(this,"setUpTimelineWindowHandle",function(t){var e,i,a,o,s="left"===t?n.timelineWindowLeftHandle:n.timelineWindowRightHandle,h=parseFloat(getComputedStyle(n.timelineWindow).borderLeftWidth);return r(s,function(r){e=n.timeline.getBoundingClientRect();var s=n.timelineWindow.getBoundingClientRect();"left"===t?(i=e.left,a=s.left+s.width-2*h,o=r-s.left):(i=s.left+2*h,a=e.left+e.width,o=r-(s.left+s.width))},function(r){r-=o;var s=((r=Math.max(Math.min(r,a),i))-e.left)/e.width;"left"===t?n.updateBounds(s,n.props.toRatio):n.updateBounds(n.props.fromRatio,s)})}),m(this,"setUpTimelineWindow",function(){var t,e,i,a,o;return r(n.timelineWindowDrag,function(r){t=n.timeline.getBoundingClientRect(),e=n.timelineWindow.getBoundingClientRect(),o=r-e.left,i=t.left,a=t.left+(t.width-e.width)},function(r){r-=o;var s=((r=Math.max(Math.min(r,a),i))-t.left)/t.width;n.updateBounds(s,s+e.width/t.width)})}),m(this,"setTimelineWindowLeft",function(t){n.timelineOverlayLeft.style.right="".concat(100*(1-t),"%"),n.timelineWindow.style.left="".concat(100*t,"%")}),m(this,"setTimelineWindowRight",function(t){n.timelineOverlayRight.style.left="".concat(100*t,"%"),n.timelineWindow.style.right="".concat(100*(1-t),"%")}),this.props=t}var n,i,o;return n=e,(i=[{key:"componentDidUpdate",value:function(){this.state={aspectRatio:this.getCanvasAspectRatio()},this.onChangeBounds(this.props.fromRatio,this.props.toRatio),this.render()}},{key:"componentDidMount",value:function(t){this.timeline=t.querySelector(".chartogram__timeline"),this.timelineOverlayLeft=t.querySelector(".chartogram__timeline-overlay-left"),this.timelineWindowLeftHandle=t.querySelector(".chartogram__timeline-window__left-handle"),this.timelineWindow=t.querySelector(".chartogram__timeline-window"),this.timelineWindowDrag=t.querySelector(".chartogram__timeline-window__drag"),this.timelineWindowRightHandle=t.querySelector(".chartogram__timeline-window__right-handle"),this.timelineOverlayRight=t.querySelector(".chartogram__timeline-overlay-right"),this.timelineCanvas=t.querySelector(".chartogram__timeline-canvas"),this.state={aspectRatio:this.getCanvasAspectRatio()},this.updateAspectRatio(),this.setUpTimelineWindowHandle("left"),this.setUpTimelineWindowHandle("right"),this.setUpTimelineWindow(),window.addEventListener("resize",this.onResizeThrottled),this.onChangeBounds(this.props.fromRatio,this.props.toRatio),this.mountGraphs(),this.render()}},{key:"componentWillUnmount",value:function(){window.removeEventListener("resize",this.onResizeThrottled)}},{key:"updateAspectRatio",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:this.state.aspectRatio,e=this.props,n=e.canvasWidth,i=e.fixSvgCoordinate;this.timelineCanvas.setAttribute("viewBox","0 0 ".concat(n," ").concat(i(n/t)))}},{key:"getCanvasAspectRatio",value:function(){var t=this.timelineCanvas.getBoundingClientRect();return t.width/t.height}},{key:"render",value:function(){for(var e=this,n=this.props,i=n.y,a=n.data,r=n.graphOpacity,o=0,s=function(){var n=a.y[o],s=n.id,h=n.points,c=n.color,l=i.find(function(t){return t.id===s}).isShown,u=r[o];if(l||u>0){var d=p(function e(n,i,a){var r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:Math.max.apply(Math,t(i)),o=arguments.length>4&&void 0!==arguments[4]?arguments[4]:.025,s=arguments.length>5&&void 0!==arguments[5]?arguments[5]:0,h=arguments.length>6&&void 0!==arguments[6]?arguments[6]:new Array(n.length),c=arguments.length>7&&void 0!==arguments[7]?arguments[7]:new Array(n.length),l=arguments.length>8&&void 0!==arguments[8]?arguments[8]:0;if(s+2>n.length-1){for(;s<i.length;)h[l]=n[s],c[l]=i[s],l++,s++;return h=h.slice(0,l),c=c.slice(0,l),h.length<=a?[h,c]:(n.length/h.length<1.1&&(o=Math.min(o+.025,1)),e(h,c,a,r,o))}var u=(i[s+2]+i[s])/2;return Math.abs(u-i[s+1])/r<o?(h[l]=n[s],h[l+1]=n[s+2],c[l]=i[s],c[l+1]=i[s+2],e(n,i,a,r,o,s+2,h,c,l+1)):(h[l]=n[s],h[l+1]=n[s+1],h[l+2]=n[s+2],c[l]=i[s],c[l+1]=i[s+1],c[l+2]=i[s+2],e(n,i,a,r,o,s+2,h,c,l+2))}(a.x.points,h,80),2),m=d[0],v=d[1];e.graphs[o]?e.updateGraph(o,m,v,u):e.mountGraph(o,m,v,c,u)}else e.graphs[o]&&e.unmountGraph(o);o++};o<a.y.length;)s()}},{key:"renderGraph",value:function(t,e,n){var i=arguments.length>3&&void 0!==arguments[3]?arguments[3]:1,a=document.createElementNS("http://www.w3.org/2000/svg","polyline");return a.setAttribute("stroke",n),this.updateGraph(a,t,e,i),a.classList.add("chartogram__graph"),a}},{key:"mountGraphs",value:function(){var t=this;this.graphs=[];var e=this.props.data;e.y.forEach(function(n,i){var a=n.points,r=n.color;t.mountGraph(i,e.x.points,a,r)})}},{key:"mountGraph",value:function(t,e,n,i,a){var r=this.renderGraph(e,n,i,a);this.graphs[t]=r,this.timelineCanvas.appendChild(r)}},{key:"unmountGraph",value:function(t){this.timelineCanvas.removeChild(this.graphs[t]),this.graphs[t]=void 0}},{key:"updateGraph",value:function(t,e,n,i){var a=this,r=this.props,o=r.maxY,s=r.createPolylinePoints;"number"==typeof t&&(t=this.graphs[t]),t.setAttribute("points",s(e.map(this.mapX),n.map(function(t){return a.mapY(o-t)})).join(" ")),1!==i&&(t.style.opacity=i)}},{key:"onChangeBounds",value:function(t,e){this.setTimelineWindowLeft(t),this.setTimelineWindowRight(e)}},{key:"updateBounds",value:function(t,e){this.props.onChangeBounds(t,e),this.props.fromRatio=t,this.props.toRatio=e,this.onChangeBounds(t,e)}}])&&d(n.prototype,i),o&&d(n,o),e}();function f(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}function g(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}v.INITIAL_MARKUP='\n\t<div class="chartogram__timeline">\n\t\t<div class="chartogram__timeline-canvas-padding">\n\t\t\t<svg class="chartogram__timeline-canvas" preserveAspectRatio="none"></svg>\n\t\t</div>\n\t\t<div class="chartogram__timeline-overlay-left"></div>\n\t\t<div class="chartogram__timeline-overlay-right"></div>\n\t\t<div class="chartogram__timeline-window">\n\t\t\t<button type="button" class="chartogram__reset-button chartogram__timeline-window__drag"></button>\n\t\t\t<button type="button" class="chartogram__reset-button chartogram__timeline-window__left-handle"></button>\n\t\t\t<button type="button" class="chartogram__reset-button chartogram__timeline-window__right-handle"></button>\n\t\t</div>\n\t</div>\n';var y=function(){function t(e){var n=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),g(this,"renderToggler",function(t){var e=t.id,i=t.name,a=t.color,r=document.createElement("button");r.setAttribute("type","button"),r.classList.add("chartogram__chart-toggler"),r.classList.add("chartogram__chart-toggler--on"),r.classList.add("chartogram__reset-button");var o="http://www.w3.org/2000/svg",s=document.createElementNS(o,"svg");s.setAttribute("viewBox","0 0 19 19"),s.setAttribute("class","chartogram__chart-toggler-check");var h=document.createElementNS(o,"circle");h.setAttribute("cx","9.5"),h.setAttribute("cy","9.5"),h.setAttribute("r","9.5"),h.setAttribute("fill",a),s.appendChild(h);var c=document.createElementNS(o,"circle");c.setAttribute("cx","9.5"),c.setAttribute("cy","9.5"),c.setAttribute("r","8"),c.setAttribute("class","chartogram__chart-toggler-check-circle"),s.appendChild(c);var l=document.createElementNS(o,"path");return l.setAttribute("d","M13.64 4.94l-6.2 6.34-1.69-1.9c-.73-.63-1.89.1-1.36 1.06l2 3.38c.3.43 1.04.85 1.78 0 .32-.42 6.31-7.93 6.31-7.93.74-.84-.2-1.58-.84-.95z"),l.setAttribute("fill","white"),l.setAttribute("class","chartogram__chart-toggler-check-mark"),s.appendChild(l),r.appendChild(s),r.appendChild(document.createTextNode(i)),r.addEventListener("click",function(){return n.onToggle(e,r)}),r}),g(this,"onToggle",function(t,e){(0,n.props.onToggle)(t)&&e.classList.toggle("chartogram__chart-toggler--on")}),this.props=e}var e,n,i;return e=t,(n=[{key:"componentDidMount",value:function(t){this.togglers=t.querySelector(".chartogram__chart-togglers"),this.mount()}},{key:"mount",value:function(){var t=this.props.data.y,e=Array.isArray(t),n=0;for(t=e?t:t[Symbol.iterator]();;){var i;if(e){if(n>=t.length)break;i=t[n++]}else{if((n=t.next()).done)break;i=n.value}var a=i;this.togglers.appendChild(this.renderToggler(a))}}}])&&f(e.prototype,n),i&&f(e,i),t}();function w(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{},i=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(i=i.concat(Object.getOwnPropertySymbols(n).filter(function(t){return Object.getOwnPropertyDescriptor(n,t).enumerable}))),i.forEach(function(e){x(t,e,n[e])})}return t}function b(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}function x(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}y.INITIAL_MARKUP='\n\t<div class="chartogram__chart-togglers"></div>\n';var k=function(){function t(e,n,i){var a=this;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),x(this,"transitions",[]),x(this,"tick",function(){var t,e=w({},a.getState()),n=a.transitions,i=Array.isArray(n),r=0;for(n=i?n:n[Symbol.iterator]();;){var o;if(i){if(r>=n.length)break;o=n[r++]}else{if((r=n.next()).done)break;o=r.value}var s=o;s.tick(e)&&(t=t||[]).push(s)}a.setState(e),t&&(a.transitions=a.transitions.filter(function(e){return t.indexOf(e)<0})),a.transitions.length>0?a.timer=requestAnimationFrame(a.tick):a.timer=void 0}),this.getState=e,this.setState=n,this.props=i}var e,n,i;return e=t,(n=[{key:"cleanUp",value:function(){this.timer&&(cancelAnimationFrame(this.timer),this.timer=void 0)}},{key:"batch",value:function(){this.pendingTransitions=[]}},{key:"run",value:function(){var t=this,e=this.transitions.filter(function(e){var n=t.pendingTransitions,i=Array.isArray(n),a=0;for(n=i?n:n[Symbol.iterator]();;){var r;if(i){if(a>=n.length)break;r=n[a++]}else{if((a=n.next()).done)break;r=a.value}var o=r;if(e.owner===o.owner&&e.property===o.property)return!0}});if(e.length>0){var n=w({},this.getState()),i=e,a=Array.isArray(i),r=0;for(i=a?i:i[Symbol.iterator]();;){var o;if(a){if(r>=i.length)break;o=i[r++]}else{if((r=i.next()).done)break;o=r.value}o.tick(n)}this.setState(n),this.transitions=this.transitions.filter(function(t){return e.indexOf(t)<0})}var s=this.getTransitionDuration(this.pendingTransitions);this.transitions=this.transitions.concat(this.pendingTransitions.map(function(e){return w({},e,{startedAt:Date.now(),duration:s,easing:t.props.transitionEasing,fromValue:e.getter?e.getter(t.getState()):t.getState()[e.property],tick:function(t){var e,n=Date.now()-this.startedAt,i=Math.min(n/this.duration,1);return i=A[this.easing](i),e=this.getNewValue?this.getNewValue(this.fromValue,this.toValue,i):this.fromValue+(this.toValue-this.fromValue)*i,this.setter?this.setter(t,e):t[this.property]=e,1===i}})})),this.pendingTransitions=void 0,this.timer||(this.timer=requestAnimationFrame(this.tick))}},{key:"add",value:function(t,e,n,i){var a,r;"string"!=typeof e&&(a=e,r=n,n=i,e=void 0),this.pendingTransitions.push({owner:t,property:e,getter:a,setter:r,toValue:n})}},{key:"getTransitionDuration",value:function(t){var e=this.props.transitionDuration,n=e,i=t.find(function(t){return"charts"===t.owner&&"minY"===t.property}),a=t.find(function(t){return"charts"===t.owner&&"maxY"===t.property});if(i&&a){var r=this.getState().minY,o=this.getState().maxY,s=i.toValue,h=a.toValue,c=o-r,l=Math.abs(h-o)/c,u=Math.abs(s-r)/c,p=Math.max(u,l);n=e*Math.max(.2,2*Math.min(p,.5))}return n}}])&&b(e.prototype,n),i&&b(e,i),t}(),A={linear:function(t){return t},easeInOutSin:function(t){return(1+Math.sin(Math.PI*t-Math.PI/2))/2},easeInOutQuad:function(t){return t<.5?2*t*t:(4-2*t)*t-1},easeInOutCubic:function(t){return t<.5?4*t*t*t:(t-1)*(2*t-2)*(2*t-2)+1},easeOutCubic:function(t){return--t*t*t+1},easeOutQuad:function(t){return t*(2-t)}};function _(t){return function(t){if(Array.isArray(t)){for(var e=0,n=new Array(t.length);e<t.length;e++)n[e]=t[e];return n}}(t)||function(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}function S(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{},i=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(i=i.concat(Object.getOwnPropertySymbols(n).filter(function(t){return Object.getOwnPropertyDescriptor(n,t).enumerable}))),i.forEach(function(e){T(t,e,n[e])})}return t}function C(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}function T(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}var L=function(){function t(e,n){var i=this,a=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"Title",r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{};!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),T(this,"setState",function(t){var e=i.state;i.state=S({},i.state,t),i.componentDidUpdate(i.props,e)}),T(this,"onToggle",function(t){var e=i.state.y.findIndex(function(e){return e.id===t}),n=i.state.y[e];if(n.isShown&&1===i.state.y.filter(function(t){return t.isShown}).length)return;n.isShown=!n.isShown;var a=i.calculateMinMaxY(i.state.y),r=a.minY,o=a.maxY,s=a.minYGlobal,h=a.maxYGlobal,c=a.maxYGaugeMark;return i.transition.batch(),i.transition.add("charts","minY",r),i.transition.add("charts","maxY",o),i.transition.add("charts","maxYGaugeMark",c),i.transition.add("timeline","minYGlobal",s),i.transition.add("timeline","maxYGlobal",h),i.transition.add("graph#".concat(t,"/opacity"),function(t){return t.graphOpacity[e]},function(t,n){t.graphOpacity=t.graphOpacity.slice(),t.graphOpacity[e]=n},n.isShown?1:0),i.transition.run(),!0}),T(this,"onChangeBounds",function(t,e){var n=i.createState(t,e),a=n.minY,r=n.maxY,o=n.maxYGaugeMark;delete n.minY,delete n.maxY,delete n.maxYGaugeMark,i.setState(n),i.transition.batch(),i.transition.add("charts","minY",a),i.transition.add("charts","maxY",r),i.transition.add("charts","maxYGaugeMark",o),i.transition.run()}),T(this,"createPolylinePoints",function(t,e){return n=t.map(i.fixSvgCoordinate),a=e.map(i.fixSvgCoordinate),n.map(function(t,e){return"".concat(t,",").concat(a[e])});var n,a}),T(this,"fixSvgCoordinate",function(t){var e=i.props.precisionFactor;return Math.round(t*e)/e}),T(this,"formatX",function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=i.props,a=n.locale,r=n.formatX;if(r)return r(t,e);i.dateTimeFormatShort||(i.dateTimeFormatShort=new Intl.DateTimeFormat(a,{month:"short",day:"numeric"}),i.dateTimeFormatLong=new Intl.DateTimeFormat(a,{weekday:"short",month:"short",day:"numeric"}),i.dateTimeFormatLongWithYear=new Intl.DateTimeFormat(a,{weekday:"short",year:"numeric",month:"short",day:"numeric"}));var o=new Date(t);return e.long?i.dateTimeFormatLong.format(o):i.dateTimeFormatShort.format(o)}),T(this,"formatY",function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=i.props,a=n.locale,r=n.formatY,o=n.yAxisPrecision;if(r)return r(t,e);i.numberFormat||(i.numberFormat=new Intl.NumberFormat(a,{minimumFractionDigits:o}));var s=o&&Math.pow(10,o);return s&&(t/=s),t=Math.round(t),s&&(t*=s),i.numberFormat.format(t)}),this.props=S({title:a,transitionDuration:250,transitionEasing:"easeOutQuad",xAxisTickMarkWidth:60,yAxisTickMarksCount:6,yAxisPrecision:0,timelineWindowSize:40,canvasWidth:512,precisionFactor:Math.pow(10,r.precision||3)},r),this.rootNode=e,this.data=S({},n,{minX:Math.min.apply(Math,_(n.x.points)),maxX:Math.max.apply(Math,_(n.x.points)),y:n.y.map(function(t){return S({},t,{min:Math.min.apply(Math,_(t.points)),max:Math.max.apply(Math,_(t.points))})})}),this.transition=new k(function(){return i.state},this.setState,this.props)}var i,a,r;return i=t,(a=[{key:"componentDidMount",value:function(){this.rootNode.classList.add("chartogram"),this.rootNode.innerHTML='\n\t\t\t<header class="chartogram__header">\n\t\t\t\t<h1 class="chartogram__title">'.concat(this.props.title,"</h1>\n\t\t\t</header>\n\t\t\t").concat(u.INITIAL_MARKUP,"\n\t\t\t").concat(v.INITIAL_MARKUP,"\n\t\t\t").concat(y.INITIAL_MARKUP,"\n\t\t"),this.state=this.getInitialState(),this.charts=new u(this.getChartsProps()),this.charts.componentDidMount(this.rootNode),this.timeline=new v(this.getTimelineProps()),this.timeline.componentDidMount(this.rootNode),this.togglers=new y(this.getTogglersProps()),this.togglers.componentDidMount(this.rootNode)}},{key:"componentWillUnmount",value:function(){this.charts.componentWillUnmount(),this.timeline.componentWillUnmount(),this.transition.cleanUp(),this.rootNode.classList.remove("chartogram"),e(this.rootNode)}},{key:"componentDidUpdate",value:function(t,e){this.charts.props=this.getChartsProps(),this.charts.componentDidUpdate(),this.shouldUpdateTimeline(e)&&(this.timeline.props=this.getTimelineProps(),this.timeline.componentDidUpdate())}},{key:"shouldUpdateTimeline",value:function(t){return this.state.graphOpacity!==t.graphOpacity||this.state.minYGlobal!==t.minYGlobal||this.state.maxYGlobal!==t.maxYGlobal}},{key:"getChartsProps",value:function(){return S({},this.props,this.state,{createPolylinePoints:this.createPolylinePoints,fixSvgCoordinate:this.fixSvgCoordinate,formatX:this.formatX,formatY:this.formatY})}},{key:"getTimelineProps",value:function(){return{data:this.data,canvasWidth:this.props.canvasWidth,fixSvgCoordinate:this.fixSvgCoordinate,createPolylinePoints:this.createPolylinePoints,fromRatio:this.state.fromRatio,toRatio:this.state.toRatio,minY:this.state.minYGlobal,maxY:this.state.maxYGlobal,y:this.state.y,graphOpacity:this.state.graphOpacity,onChangeBounds:this.onChangeBounds}}},{key:"getTogglersProps",value:function(){return{data:this.data,onToggle:this.onToggle}}},{key:"getInitialState",value:function(){var t,e=this.props.timelineWindowSize,n=this.data,i=n.minX,a=n.maxX;t=this.data.x.points.length>e?this.data.x.points.length-e:0;var r=(this.data.x.points[t]-i)/(a-i);return S({},this.createState(r,1),{graphOpacity:this.data.y.map(function(t){return 1})})}},{key:"createState",value:function(t,e){var n,i,a=this,r=this.data.x,o=this.data.minX+t*(this.data.maxX-this.data.minX),s=this.data.minX+e*(this.data.maxX-this.data.minX);n=o===this.data.minX?0:r.points.findIndex(function(t){return t>o})-1,i=s===this.data.maxX?r.points.length-1:r.points.findIndex(function(t){return t>s});var h=r.points.slice(n,i+1),c=h.slice();h.length>=2&&(r.points[n]!==o&&(c[0]=o),r.points[i]!==s&&(c[c.length-1]=s));var l=this.data.y.map(function(t,e){var c=a.data.y[e].points.slice(n,i+1),l=c.slice();if(h.length>=2){if(r.points[n]!==o){var u=a.data.y[e].points[n],p=u+(a.data.y[e].points[n+1]-u)*((o-a.data.x.points[n])/(a.data.x.points[n+1]-a.data.x.points[n]));l[0]=p}if(r.points[i]!==s){var d=a.data.y[e].points[i],m=a.data.y[e].points[i-1],v=m+(d-m)*((s-a.data.x.points[i-1])/(a.data.x.points[i]-a.data.x.points[i-1]));l[l.length-1]=v}}return S({},a.data.y[e],a.state?a.state.y[e]:{isShown:!0},{points:c,graphPoints:l,min:0,max:Math.max.apply(Math,_(l))})});return S({minX:o,maxX:s,fromIndex:n,toIndex:i,fromRatio:t,toRatio:e,xPoints:h,xGraphPoints:c},this.calculateMinMaxY(l),{y:l})}},{key:"calculateMinMaxY",value:function(t){var e=this.props,i=e.yAxisTickMarksCount,a=e.yAxisPrecision,r=1/0,o=-1/0,s=t,h=Array.isArray(s),c=0;for(s=h?s:s[Symbol.iterator]();;){var l;if(h){if(c>=s.length)break;l=s[c++]}else{if((c=s.next()).done)break;l=c.value}var u=l;u.isShown&&(r=Math.min(r,u.min),o=Math.max(o,u.max))}var p=1/0,d=-1/0,m=function(){if(f){if(g>=v.length)return"break";y=v[g++]}else{if((g=v.next()).done)return"break";y=g.value}var e=y;t.find(function(t){return t.id===e.id}).isShown&&(p=Math.min(p,e.min),d=Math.max(d,e.max))},v=this.data.y,f=Array.isArray(v),g=0;for(v=f?v:v[Symbol.iterator]();;){var y;if("break"===m())break}return{minY:r,maxY:o,minYGlobal:p=0,maxYGlobal:d,maxYGaugeMark:n(.985*o,i-1,a)}}}])&&C(i.prototype,a),r&&C(i,r),t}();return function(t,e,n,i){var a=new L(t,e,n,i);return a.componentDidMount(),function(){a.componentWillUnmount()}}}); | ||
//# sourceMappingURL=chartogram.js.map |
@@ -172,9 +172,7 @@ "use strict"; | ||
if (options.long) { | ||
var isSameYear = date.getFullYear() === new Date().getFullYear(); | ||
if (isSameYear) { | ||
return _this.dateTimeFormatLong.format(date); | ||
} else { | ||
return _this.dateTimeFormatLongWithYear.format(date); | ||
} | ||
// const isSameYear = date.getFullYear() === new Date().getFullYear() | ||
// if (isSameYear) { | ||
return _this.dateTimeFormatLong.format(date); // } else { | ||
// return this.dateTimeFormatLongWithYear.format(date) | ||
// } | ||
} else { | ||
@@ -187,3 +185,6 @@ return _this.dateTimeFormatShort.format(date); | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var formatY = _this.props.formatY; | ||
var _this$props2 = _this.props, | ||
locale = _this$props2.locale, | ||
formatY = _this$props2.formatY, | ||
yAxisPrecision = _this$props2.yAxisPrecision; | ||
@@ -194,3 +195,21 @@ if (formatY) { | ||
return Math.round(value); | ||
if (!_this.numberFormat) { | ||
_this.numberFormat = new Intl.NumberFormat(locale, { | ||
minimumFractionDigits: yAxisPrecision | ||
}); | ||
} | ||
var precisionFactor = yAxisPrecision && Math.pow(10, yAxisPrecision); | ||
if (precisionFactor) { | ||
value /= precisionFactor; | ||
} | ||
value = Math.round(value); | ||
if (precisionFactor) { | ||
value *= precisionFactor; | ||
} | ||
return _this.numberFormat.format(value); | ||
}); | ||
@@ -204,2 +223,3 @@ | ||
yAxisTickMarksCount: 6, | ||
yAxisPrecision: 0, | ||
timelineWindowSize: 40, | ||
@@ -414,3 +434,6 @@ canvasWidth: 512, | ||
value: function calculateMinMaxY(y) { | ||
// Calculate visible min/max Y for the graphs being shown. | ||
var _this$props3 = this.props, | ||
yAxisTickMarksCount = _this$props3.yAxisTickMarksCount, | ||
yAxisPrecision = _this$props3.yAxisPrecision; // Calculate visible min/max Y for the graphs being shown. | ||
var minY = Infinity; | ||
@@ -479,3 +502,3 @@ var maxY = -Infinity; | ||
maxYGlobal: maxYGlobal, | ||
maxYGaugeMark: (0, _utility.getLowerSiblingDivisibleBy)(maxY, 10) | ||
maxYGaugeMark: (0, _utility.roundNumber)(maxY * 0.985, yAxisTickMarksCount - 1, yAxisPrecision) | ||
}; | ||
@@ -482,0 +505,0 @@ } |
@@ -351,5 +351,7 @@ "use strict"; | ||
this.tooltip.style.marginRight = 0; // Measure tooltip width with the updated content. | ||
this.tooltip.parentNode.style.marginRight = 0; // Measure tooltip width with the updated content. | ||
var tooltipWidth = this.tooltip.offsetWidth; | ||
var tooltipWidth = this.tooltip.getBoundingClientRect().width; // `.offsetWidth` returns a slightly incorrect (rounded) value. | ||
// const tooltipWidth = this.tooltip.offsetWidth | ||
var maxSideOverflow = maxOverflow === undefined ? 5 : maxOverflow; | ||
@@ -356,0 +358,0 @@ var overflow = 0; |
@@ -8,3 +8,3 @@ "use strict"; | ||
exports.commaJoin = commaJoin; | ||
exports.getLowerSiblingDivisibleBy = getLowerSiblingDivisibleBy; | ||
exports.roundNumber = roundNumber; | ||
exports.divideInterval = divideInterval; | ||
@@ -36,16 +36,58 @@ exports.throttle = throttle; | ||
function getLowerSiblingDivisibleBy(n, divider) { | ||
n = Math.floor(n); | ||
function roundNumber(n, count, precision) { | ||
var precisionFactor = precision && Math.pow(10, precision); | ||
while (true) { | ||
if (n < divider) { | ||
return divider; | ||
if (precisionFactor) { | ||
n *= precisionFactor; | ||
} | ||
var delta = Math.floor(n / count); | ||
delta = round(delta); | ||
if (precisionFactor) { | ||
delta /= precisionFactor; | ||
} | ||
return delta * count; | ||
} | ||
var ROUND_POINTS = [{ | ||
threshold: 5, | ||
adjustment: 2 | ||
}, { | ||
threshold: 40, | ||
adjustment: 10 | ||
}]; | ||
function round(n) { | ||
var digitCount = getDigitCount(n); | ||
var factor = 1; | ||
if (digitCount > 2) { | ||
factor = Math.pow(10, digitCount - 2); | ||
n = Math.floor(n / Math.pow(10, digitCount - 2)); | ||
} | ||
var roundPoint; | ||
var _arr = ROUND_POINTS; | ||
for (var _i2 = 0; _i2 < _arr.length; _i2++) { | ||
var point = _arr[_i2]; | ||
if (n > point.threshold) { | ||
roundPoint = point; | ||
} else { | ||
break; | ||
} | ||
} | ||
if (n % divider === 0) { | ||
return n; | ||
if (roundPoint) { | ||
var remainder = n % roundPoint.adjustment; | ||
if (remainder) { | ||
n -= remainder; | ||
} | ||
} | ||
n--; | ||
} | ||
return n * factor; | ||
} | ||
@@ -266,3 +308,8 @@ | ||
}; | ||
} // https://stackoverflow.com/questions/14879691/get-number-of-digits-with-javascript/28203456#28203456 | ||
function getDigitCount(x) { | ||
return (Math.log10((x ^ x >> 31) - (x >> 31)) | 0) + 1; // return Math.max(Math.floor(Math.log10(Math.abs(x))), 0) + 1 | ||
} | ||
//# sourceMappingURL=utility.js.map |
@@ -19,3 +19,3 @@ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } | ||
import { clearElement, commaJoin, getLowerSiblingDivisibleBy } from './utility'; | ||
import { clearElement, commaJoin, roundNumber } from './utility'; | ||
import Charts from './Charts'; | ||
@@ -160,9 +160,7 @@ import Timeline from './Timeline'; | ||
if (options.long) { | ||
var isSameYear = date.getFullYear() === new Date().getFullYear(); | ||
if (isSameYear) { | ||
return _this.dateTimeFormatLong.format(date); | ||
} else { | ||
return _this.dateTimeFormatLongWithYear.format(date); | ||
} | ||
// const isSameYear = date.getFullYear() === new Date().getFullYear() | ||
// if (isSameYear) { | ||
return _this.dateTimeFormatLong.format(date); // } else { | ||
// return this.dateTimeFormatLongWithYear.format(date) | ||
// } | ||
} else { | ||
@@ -175,3 +173,6 @@ return _this.dateTimeFormatShort.format(date); | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var formatY = _this.props.formatY; | ||
var _this$props2 = _this.props, | ||
locale = _this$props2.locale, | ||
formatY = _this$props2.formatY, | ||
yAxisPrecision = _this$props2.yAxisPrecision; | ||
@@ -182,3 +183,21 @@ if (formatY) { | ||
return Math.round(value); | ||
if (!_this.numberFormat) { | ||
_this.numberFormat = new Intl.NumberFormat(locale, { | ||
minimumFractionDigits: yAxisPrecision | ||
}); | ||
} | ||
var precisionFactor = yAxisPrecision && Math.pow(10, yAxisPrecision); | ||
if (precisionFactor) { | ||
value /= precisionFactor; | ||
} | ||
value = Math.round(value); | ||
if (precisionFactor) { | ||
value *= precisionFactor; | ||
} | ||
return _this.numberFormat.format(value); | ||
}); | ||
@@ -192,2 +211,3 @@ | ||
yAxisTickMarksCount: 6, | ||
yAxisPrecision: 0, | ||
timelineWindowSize: 40, | ||
@@ -402,3 +422,6 @@ canvasWidth: 512, | ||
value: function calculateMinMaxY(y) { | ||
// Calculate visible min/max Y for the graphs being shown. | ||
var _this$props3 = this.props, | ||
yAxisTickMarksCount = _this$props3.yAxisTickMarksCount, | ||
yAxisPrecision = _this$props3.yAxisPrecision; // Calculate visible min/max Y for the graphs being shown. | ||
var minY = Infinity; | ||
@@ -467,3 +490,3 @@ var maxY = -Infinity; | ||
maxYGlobal: maxYGlobal, | ||
maxYGaugeMark: getLowerSiblingDivisibleBy(maxY, 10) | ||
maxYGaugeMark: roundNumber(maxY * 0.985, yAxisTickMarksCount - 1, yAxisPrecision) | ||
}; | ||
@@ -470,0 +493,0 @@ } |
@@ -343,5 +343,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
this.tooltip.style.marginRight = 0; // Measure tooltip width with the updated content. | ||
this.tooltip.parentNode.style.marginRight = 0; // Measure tooltip width with the updated content. | ||
var tooltipWidth = this.tooltip.offsetWidth; | ||
var tooltipWidth = this.tooltip.getBoundingClientRect().width; // `.offsetWidth` returns a slightly incorrect (rounded) value. | ||
// const tooltipWidth = this.tooltip.offsetWidth | ||
var maxSideOverflow = maxOverflow === undefined ? 5 : maxOverflow; | ||
@@ -348,0 +350,0 @@ var overflow = 0; |
@@ -19,17 +19,59 @@ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } | ||
} | ||
export function getLowerSiblingDivisibleBy(n, divider) { | ||
n = Math.floor(n); | ||
export function roundNumber(n, count, precision) { | ||
var precisionFactor = precision && Math.pow(10, precision); | ||
while (true) { | ||
if (n < divider) { | ||
return divider; | ||
if (precisionFactor) { | ||
n *= precisionFactor; | ||
} | ||
var delta = Math.floor(n / count); | ||
delta = round(delta); | ||
if (precisionFactor) { | ||
delta /= precisionFactor; | ||
} | ||
return delta * count; | ||
} | ||
var ROUND_POINTS = [{ | ||
threshold: 5, | ||
adjustment: 2 | ||
}, { | ||
threshold: 40, | ||
adjustment: 10 | ||
}]; | ||
function round(n) { | ||
var digitCount = getDigitCount(n); | ||
var factor = 1; | ||
if (digitCount > 2) { | ||
factor = Math.pow(10, digitCount - 2); | ||
n = Math.floor(n / Math.pow(10, digitCount - 2)); | ||
} | ||
var roundPoint; | ||
var _arr = ROUND_POINTS; | ||
for (var _i2 = 0; _i2 < _arr.length; _i2++) { | ||
var point = _arr[_i2]; | ||
if (n > point.threshold) { | ||
roundPoint = point; | ||
} else { | ||
break; | ||
} | ||
} | ||
if (n % divider === 0) { | ||
return n; | ||
if (roundPoint) { | ||
var remainder = n % roundPoint.adjustment; | ||
if (remainder) { | ||
n -= remainder; | ||
} | ||
} | ||
n--; | ||
} | ||
return n * factor; | ||
} | ||
export function divideInterval(min, max, GAUGE_TICK_MARKS_COUNT) { | ||
@@ -244,3 +286,7 @@ var points = new Array(GAUGE_TICK_MARKS_COUNT); | ||
}; | ||
} // https://stackoverflow.com/questions/14879691/get-number-of-digits-with-javascript/28203456#28203456 | ||
function getDigitCount(x) { | ||
return (Math.log10((x ^ x >> 31) - (x >> 31)) | 0) + 1; // return Math.max(Math.floor(Math.log10(Math.abs(x))), 0) + 1 | ||
} | ||
//# sourceMappingURL=utility.js.map |
{ | ||
"name": "chartogram", | ||
"version": "0.1.14", | ||
"version": "0.1.15", | ||
"description": "Charts in JS with no dependencies", | ||
@@ -5,0 +5,0 @@ "main": "index.commonjs.js", |
@@ -162,2 +162,3 @@ # chartogram | ||
* `yAxisTickMarksCount: number` — Y axis tick marks count. | ||
* `yAxisPrecision: number` — Y axis tick mark labels rounding precision (if using fractional Y points). Is `0` by default. | ||
* `xAxisTickMarkWidth: number` — (in pixels) Is used to calculate the count of X axis tick marks based on canvas width (in pixels). | ||
@@ -164,0 +165,0 @@ * `canvasWidth: number` — SVG `viewBox` width (not pixels). |
import { | ||
clearElement, | ||
commaJoin, | ||
getLowerSiblingDivisibleBy | ||
roundNumber | ||
} from './utility' | ||
@@ -20,2 +20,3 @@ | ||
yAxisTickMarksCount: 6, | ||
yAxisPrecision: 0, | ||
timelineWindowSize: 40, | ||
@@ -228,2 +229,3 @@ canvasWidth: 512, | ||
calculateMinMaxY(y) { | ||
const { yAxisTickMarksCount, yAxisPrecision } = this.props | ||
// Calculate visible min/max Y for the graphs being shown. | ||
@@ -255,3 +257,3 @@ let minY = Infinity | ||
maxYGlobal, | ||
maxYGaugeMark: getLowerSiblingDivisibleBy(maxY, 10) | ||
maxYGaugeMark: roundNumber(maxY * 0.985, yAxisTickMarksCount - 1, yAxisPrecision) | ||
} | ||
@@ -350,8 +352,8 @@ } | ||
if (options.long) { | ||
const isSameYear = date.getFullYear() === new Date().getFullYear() | ||
if (isSameYear) { | ||
// const isSameYear = date.getFullYear() === new Date().getFullYear() | ||
// if (isSameYear) { | ||
return this.dateTimeFormatLong.format(date) | ||
} else { | ||
return this.dateTimeFormatLongWithYear.format(date) | ||
} | ||
// } else { | ||
// return this.dateTimeFormatLongWithYear.format(date) | ||
// } | ||
} else { | ||
@@ -363,8 +365,21 @@ return this.dateTimeFormatShort.format(date) | ||
formatY = (value, options = {}) => { | ||
const { formatY } = this.props | ||
const { locale, formatY, yAxisPrecision } = this.props | ||
if (formatY) { | ||
return formatY(value, options) | ||
} | ||
return Math.round(value) | ||
if (!this.numberFormat) { | ||
this.numberFormat = new Intl.NumberFormat(locale, { | ||
minimumFractionDigits: yAxisPrecision | ||
}) | ||
} | ||
const precisionFactor = yAxisPrecision && Math.pow(10, yAxisPrecision) | ||
if (precisionFactor) { | ||
value /= precisionFactor | ||
} | ||
value = Math.round(value) | ||
if (precisionFactor) { | ||
value *= precisionFactor | ||
} | ||
return this.numberFormat.format(value) | ||
} | ||
} |
@@ -200,5 +200,7 @@ import { setUpTouchMove } from './utility' | ||
// Reset side margin. | ||
this.tooltip.style.marginRight = 0 | ||
this.tooltip.parentNode.style.marginRight = 0 | ||
// Measure tooltip width with the updated content. | ||
const tooltipWidth = this.tooltip.offsetWidth | ||
const tooltipWidth = this.tooltip.getBoundingClientRect().width | ||
// `.offsetWidth` returns a slightly incorrect (rounded) value. | ||
// const tooltipWidth = this.tooltip.offsetWidth | ||
const maxSideOverflow = maxOverflow === undefined ? 5 : maxOverflow | ||
@@ -205,0 +207,0 @@ let overflow = 0 |
@@ -11,13 +11,48 @@ export function clearElement(element) { | ||
export function getLowerSiblingDivisibleBy(n, divider) { | ||
n = Math.floor(n) | ||
while (true) { | ||
if (n < divider) { | ||
return divider | ||
export function roundNumber(n, count, precision) { | ||
const precisionFactor = precision && Math.pow(10, precision) | ||
if (precisionFactor) { | ||
n *= precisionFactor | ||
} | ||
let delta = Math.floor(n / count) | ||
delta = round(delta) | ||
if (precisionFactor) { | ||
delta /= precisionFactor | ||
} | ||
return delta * count | ||
} | ||
const ROUND_POINTS = [ | ||
{ | ||
threshold: 5, | ||
adjustment: 2 | ||
}, | ||
{ | ||
threshold: 40, | ||
adjustment: 10 | ||
} | ||
] | ||
function round(n) { | ||
const digitCount = getDigitCount(n) | ||
let factor = 1 | ||
if (digitCount > 2) { | ||
factor = Math.pow(10, digitCount - 2) | ||
n = Math.floor(n / Math.pow(10, digitCount - 2)) | ||
} | ||
let roundPoint | ||
for (const point of ROUND_POINTS) { | ||
if (n > point.threshold) { | ||
roundPoint = point | ||
} else { | ||
break | ||
} | ||
if (n % divider === 0) { | ||
return n | ||
} | ||
if (roundPoint) { | ||
const remainder = n % roundPoint.adjustment | ||
if (remainder) { | ||
n -= remainder | ||
} | ||
n-- | ||
} | ||
return n * factor | ||
} | ||
@@ -207,2 +242,8 @@ | ||
} | ||
} | ||
// https://stackoverflow.com/questions/14879691/get-number-of-digits-with-javascript/28203456#28203456 | ||
function getDigitCount(x) { | ||
return (Math.log10((x ^ (x >> 31)) - (x >> 31)) | 0) + 1 | ||
// return Math.max(Math.floor(Math.log10(Math.abs(x))), 0) + 1 | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1246799
5850
205