forked from mode/alamode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalamode.min.js
More file actions
1 lines (1 loc) · 51.5 KB
/
Copy pathalamode.min.js
File metadata and controls
1 lines (1 loc) · 51.5 KB
1
var version="0.23",alamode={reportError:function(t){$("<h1 class='mode-error'>").text(t).prependTo(document.body)},getColumnsFromQuery:function(e){var t=datasets.filter(function(t){if(t)return t.queryName==e})[0];return t?t.columns:(alamode.reportError("No such query: '"+e+"'"),[])},getDataFromQuery:function(e){var t=datasets.filter(function(t){if(t)return t.queryName==e})[0];return t?t.content:(alamode.reportError("No such query: '"+e+"'"),[])},makeId:function(t){for(var e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",a="",n=0;n<t;n++)a+=e.charAt(Math.floor(Math.random()*e.length));return a},addContainerElement:function(t,e){return e=e||!1,id=alamode.makeId(10),"body"==t?$("<div id='"+id+"'></div>").addClass(id).addClass("mode-graphic-container").appendTo(".mode-content"):0===$(t).length?alamode.reportError("No such element: '"+t+"'"):(e&&$(t).empty(),$(t).addClass("mode-graphic-container"),$(t).addClass(id)),"."+id},addLinksToTables:function(t){var r="#"+t.table_id,e=t.link_columns,a=t.link_urls,n=t.query_name,s=t.open_in_new_tab||!1,o=[],d={};e.forEach(function(t,e){o.push({column:t,link_string:a[e]})});var c=alamode.getDataFromQuery(n);alamode.getColumnsFromQuery(n);function l(e){var t,a=$(r+" table"),n=$(r+" .js-header-table"),n=n?$(r+" .js-col-header"):$(n).find("th"),a=a.find("tr");n.each(function(){text=$(this).find(".axel-table-header-label").text(),t=$(this).attr("data-axel-column"),d[text]=t-1}),a.each(function(t){var l,i;0<t&&t<=c.length&&(l=$(this).find("td"),i=l.first().attr("data-axel-rowkey"),e.forEach(function(t){var e=d[t.column],a=l.eq(e).text();for(url=t.link_string;-1!=url.indexOf("{{");){var n=url.length,r=url.indexOf("{{"),o=url.substring(r+2,n).indexOf("}}"),n=url.substring(r+2,r+o+2),o=url.substring(r,r+o+4),n=(d[n],c[i][n]);url=url.replace(o,n)}a="<a target='"+(s?"_blank":"_top")+"' href='"+encodeURI(url)+"'>"+a+"</a>";l.eq(e).html(a)}))})}setTimeout(function(){l(o)},1e3),$(r).mousemove(function(){l(o)})},customChartColors:function(t){var e=t.charts,n=t.colors,l=t.opacity,s=t.line_dashes;function m(t,e){var t=$("#"+t),a=(0<t.find(".nv-bar").length?t.find(".nv-group"):0<t.find(".nv-line").length||0<t.find(".nv-areaWrap").length?t.find(".nv-noninteractive"):t.find(".nv-pie .nv-slice")).length,n=t.find(".nv-series .nv-legend-symbol"),r={},o={},l={},s=0;if(0==n.length&&a<=1)o[0]=s,r[l[s]=0]=e[0];else if(0==n.length&&1<a)for(i=0;i<a;i++)o[i]=i,r=e;else for(i=0;i<n.length;i++)r[i]=e[i%Object.keys(e).length];return n.each(function(t){1==$(this).css("fill-opacity")?(o[t]=s,l[s]=t,s+=1):o[t]=-1}),{chart:t,legend:n,colors:r,m:o,r:l,seriesCount:a}}"all"==e&&(e=[],$("mode-chart").each(function(){e.push(this.id)})),setInterval(function(){e.forEach(function(t){!function(t,e){var a,n=(e=m(t,e)).chart,r=e.colors,o=e.m;for(a in r)n.find(".nv-linesWrap .nv-groups .nv-series-"+o[a]).css({fill:r[a],stroke:r[a]}),n.find(".nv-barsWrap .nv-groups .nv-series-"+o[a]+" rect").css({fill:r[a],stroke:r[a]}),n.find(".nv-linePlusBar .nv-barsWrap .nv-bars rect").each(function(t){$(this).css({fill:r[0],stroke:r[0]})}),n.find(".nv-linePlusBar .nv-linesWrap .nv-groups .nv-series-0").css({fill:r[1],stroke:r[1]}),n.find(".nv-scatterWrap .nv-groups .nv-series-"+o[a]).css({fill:r[a],stroke:r[a]}),n.find(".nv-areaWrap .nv-area-"+o[a]).css({fill:r[a],stroke:r[a]}),n.find(".nv-pie .nv-slice").each(function(t){$(this).css({fill:r[t],stroke:r[t]})});for(a in l)n.find(".nv-linesWrap .nv-groups .nv-series-"+o[a]).css({opacity:l[a]}),n.find(".nv-barsWrap .nv-groups .nv-series-"+o[a]+" rect").css({opacity:l[a]}),0==a&&(n.find(".nv-linePlusBar .nv-barsWrap .nv-bars rect").each(function(t){$(this).css({opacity:l[a]})}),n.find(".nv-linePlusBar .nv-linesWrap .nv-groups .nv-series-0").css({opacity:l[1]})),n.find(".nv-scatterWrap .nv-groups .nv-series-"+o[a]).css({opacity:l[a]}),n.find(".nv-areaWrap .nv-area-"+o[a]).css({opacity:l[a]}),n.find(".nv-pie .nv-slice").each(function(t){$(this).css({opacity:l[t]})});for(a in s)n.find(".nv-linesWrap .nv-groups .nv-series-"+o[a]).css({"stroke-dasharray":s[a]});n.find(".nv-legendWrap .nv-series .nv-legend-symbol").each(function(t){$(this).css({fill:r[t],stroke:r[t]})})}(t,n)})},500),e.forEach(function(t){var e,a,u;e=t,a=n,u=$("#"+e),$(u).mousemove(function(){var t=m(e,a),n=t.legend,r=t.colors,o=t.r,l=t.m,i=t.seriesCount,s=isArea=u.find(".nv-area").length,d=u.find(".nv-bar").length,c=u.find(".nv-line").length;$("html").find(".nvtooltip table .legend-color-guide").each(function(t){var e,a;0==n.length&&0==d?$(this).find("div").css({"background-color":r[l[t]]}):0<c&&0<d?(e=u.find(".nv-linePlusBar .nv-linesWrap .nv-groups .nv-series-0").css("fill"),a=u.find(".nv-linePlusBar .nv-barsWrap .nv-bars rect").css("fill"),""==$(this).closest(".nvtooltip").find(".key")[0].innerText?$(this).find("div").css("background-color",a):$(this).find("div").css("background-color",e)):0<d?1==i&&$(this).find("div").css({"background-color":r[o[i-t-1]]}):0<s?$(this).find("div").css({"background-color":r[s-t-1]}):$(this).find("div").css({"background-color":r[t]})});t=u.find(".nv-pie .nv-slice.hover").css("fill");$("html").find(".nvtooltip table .legend-color-guide div").css("background-color",t)}),$(u).mouseleave(function(){$("html").find(".nvtooltip table .legend-color-guide").remove()})})},addTotalsRow:function(t){var n,r,e,a=t.query_name,o="#"+(t.table_id||""),l=alamode.getColumnsFromQuery(a),i=alamode.getDataFromQuery(a),a=t.columns_with_totals,s=t.fmt||d3.format(","),a=(e=a,numberColumns=_.map(_.filter(l,function(t){return-1!=["number","integer","float"].indexOf(t.type)}),"name"),"all"==e?numberColumns:_.intersection(e,numberColumns)),d=(n=a,r=[],l.forEach(function(t,e){var a;a=-1==n.indexOf(t.name)?{idx:e,name:"",total:""}:(a=_.map(i,t.name),a=d3.sum(a),{idx:e,name:t.name,total:a}),r.push(a)}),r);setTimeout(function(){container="#"==o?(table=$(".main-table"),$(".js-table-content-container")):(table=$(o+" .main-table"),$(o+" .js-table-content-container"));var e,t=table.find("tr:last"),a=(e="<tr><td>TTL</td>",d.forEach(function(t){""!=t.total?e=e+"<td class='cell-type-number'>"+s(t.total)+"</td>":e+="<td></td>"}),e+"</tr>"),n=+container.css("height").match(/\d+/)[0];t.after(a),container.css("height",26+n)},1e3)},addImagesToTables:function(t){var n="#"+t.table,r=t.column,o=t.image_height||100;function e(){var t=$(n+" table"),e=$(n+" .js-header-table"),e=e?$(n+" .js-col-header"):$(e).find("th"),t=t.find("tr"),a=0;e.each(function(){text=$(this).find(".axel-table-header-label").text(),text==r&&(a=+$(this).attr("data-axel-column"))}),t.each(function(){$(this).find("td").each(function(t){t==a-1&&(t=$(this).text(),0==$(this).find("img").length&&($(this).css("text-align","center"),$(this).html("<img style='height: "+o+"px;' src='"+t+"'>")))})})}setTimeout(function(){e()},1e3),$(n).keyup(function(){setTimeout(function(){e()},500)}),$(n).mousemove(function(){e()})},resizeChartHeight:function(t){var e=t.chart,t=t.height;"python"==e.slice(0,6)?($("#"+e+" .mode-python").css("height",t),$("#"+e+" .mode-python").css("max-height",t),$("#"+e+" img").css("max-height",t)):($("#"+e+" .chart").css("height",t),$("#"+e+" .chart-svg").css("height",t)),window.dispatchEvent(new Event("resize"))},retentionHeatmap:function(t){var a,n,r,e=t.query_name,i=t.cohort_column,s=t.pivot_column,d=t.value_column,o=t.color_gradient||["#d73027","#f46d43","#fdae61","#fee08b","#ffffbf","#d9ef8b","#a6d96a","#66bd63","#1a9850"],l=t.gradient_by||"all",c=t.gradient_column||d,u=t.total_column,m=t.html_element||"body",h=t.title||e,p=t.pivot_label||"",f=t.value_is_percent,g=t.precision||0,v=alamode.getDataFromQuery(e),y=alamode.getColumnsFromQuery(e),e=_.uniq(_.map(v,i)),x=_.sortBy(_.uniq(_.map(v,s))),m=alamode.addContainerElement(m);"cohort_column"===l?(a={},e.forEach(function(e){var t=v.filter(function(t){return t[i]===e});a[e]=d3.scale.quantize().domain(d3.extent(t,function(t){return t[c]})).range(o)})):"pivot_column"===l?(n={},x.forEach(function(e){var t=v.filter(function(t){return t[s]===e});n[e]=d3.scale.quantize().domain(d3.extent(t,function(t){return t[c]})).range(o)})):r=d3.scale.quantize().domain(d3.extent(v,function(t){return t[c]})).range(o),d3.select(m).append("div").attr("class","mode-graphic-title").text(h),d3.select(m).append("div").attr("class","mode-retention-heatmap-label").text(p),headers=(u?[i,u]:[i]).concat(x);m=d3.select(m).append("table").attr("class","mode-retention-heatmap-table");function b(e){return y.filter(function(t){return t.name==e})[0].type}m.selectAll(".mode-retention-heatmap-table-header").data([0]).enter().append("tr").attr("class","mode-retention-heatmap-table-header").selectAll("mode-retention-heatmap-table-header-cell").data(headers).enter().append("td").attr("class",function(t){return isNaN(t)?"mode-retention-heatmap-table-header-cell heatmap-string":"mode-retention-heatmap-table-header-cell heatmap-number"}).text(function(t){return t}),m.selectAll(".mode-retention-heatmap-table-row").data(e).enter().append("tr").attr("class","mode-retention-heatmap-table-row").selectAll(".mode-retention-heatmap-table-cell").data(function(t){return function(r,o){var l=[{column:i,value:o}];{var t;u&&(t=_.filter(r,function(t){return t[i]==o})[0],t={column:u,value:t[u]},l=l.concat(t))}return x.forEach(function(e){var t="",a="",n=_.filter(r,function(t){return t[i]==o&&t[s]==e});0<n.length&&(t=d3.mean(_.map(n,d)),a=d3.mean(_.map(n,c))),l=l.concat({column:d,value:t,cohort:o,pivot:e,gradientValue:a})}),l}(v,t)}).enter().append("td").style("background",function(t){if((e=t).column==d&&""!==e.value)return function(t){{if("cohort_column"===l)return a[t.cohort](t.gradientValue);if("pivot_column"===l)return n[t.pivot](t.gradientValue)}return r(t.gradientValue)}(t);var e}).attr("class",function(t){t=b(t.column);return"float"==t||"integer"==t||"number"==t?"heatmap-number":"heatmap-string"}).text(function(t){return a=b((e=t).column),n=d3.format(","),r=d3.format("."+g+"%"),t=d3.time.format("%b %d, %Y"),""==e.value?e.value:"datetime"==a||"timestamp"==a||"date"==a?"function"==typeof moment?moment(e.value).utc().format("ll"):t(new Date(e.value)):e.column==u?n(e.value):e.column==d&&f?r(e.value):e.column==d?n(e.value):e.value;var e,a,n,r})},simpleHeatmap:function(t){var e=t.query_name,o=t.x_column,l=t.y_column,i=t.value_column,a=t.max_value||Number.MAX_VALUE,s=0===t.min_value?0:t.min_value||Number.MIN_VALUE,n=t.color_gradient||["#d73027","#f46d43","#fdae61","#fee08b","#ffffbf","#d9ef8b","#a6d96a","#66bd63","#1a9850"],r=t.html_element||"body",d=t.title||e,c=(t.x_label,t.y_label||""),u=t.value_is_percent,m=t.precision||0,h=alamode.getDataFromQuery(e),p=alamode.getColumnsFromQuery(e),e=_.uniq(_.map(h,o)),f=_.uniq(_.map(h,l)),r=alamode.addContainerElement(r),g=d3.scale.quantize().domain(d3.extent(h,function(t){return Math.max(s,Math.min(a,t[i]))})).range(n);d3.select(r).append("div").attr("class","mode-graphic-title").text(d),d3.select(r).append("div").attr("class","mode-simple-heatmap-label").text(c),headers=[o].concat(f);r=d3.select(r).append("table").attr("class","mode-simple-heatmap-table");function v(e){return p.filter(function(t){return t.name==e})[0].type}r.selectAll(".mode-simple-heatmap-table-header").data([0]).enter().append("tr").attr("class","mode-simple-heatmap-table-header").selectAll("mode-simple-heatmap-table-header-cell").data(headers).enter().append("td").attr("class",function(t){return isNaN(t)?"mode-simple-heatmap-table-header-cell heatmap-string":"mode-simple-heatmap-table-header-cell heatmap-number"}).text(function(t){return t}),r.selectAll(".mode-simple-heatmap-table-row").data(e).enter().append("tr").attr("class","mode-simple-heatmap-table-row").selectAll(".mode-simple-heatmap-table-cell").data(function(t){return a=h,r=[{column:o,value:n=t}],f.forEach(function(e){var t=_.filter(a,function(t){return t[o]==n&&t[l]==e});entry=0<t.length?d3.mean(_.map(t,i)):s,r=r.concat({column:i,value:entry})}),r;var a,n,r}).enter().append("td").style("background",function(t){if(""!==(e=t).value&&(e.column!=o&&e.column==i))return g(Math.max(s,Math.min(a,t.value)));var e}).attr("class",function(t){t=v(t.column);return"float"==t||"integer"==t||"number"==t?"heatmap-number":"heatmap-string"}).text(function(t){return a=v((e=t).column),n=d3.format(","),r=d3.format("."+m+"%"),t=d3.time.format("%b %d, %Y"),""==e.value?e.value:"datetime"==a||"timestamp"==a||"date"==a?"function"==typeof moment?moment(e.value).utc().format("ll"):t(new Date(e.value)):e.column==i&&u?r(e.value):e.column==i?n(e.value):e.value;var e,a,n,r})},googleMap:function(t){var e=alamode.makeId(10),l=t.lat_column,i=t.lng_column,a=t.query_name,n=t.google_maps_api_key,r=t.title||a,s=t.label_column,o=t.html_element||"body",d=t.center_lat||39.5,c=t.center_lng||-98.35,u=t.starting_zoom||4,m=t.map_type||"terrain",t=t.height||600,h=alamode.getDataFromQuery(a),o=alamode.addContainerElement(o);d3.select(o).append("div").attr("class","mode-graphic-title").text(r),d3.select(o).append("div").attr("class","mode-google-map").attr("id",e).style("height",t+"px"),jQuery.getScript("https://maps.googleapis.com/maps/api/js?key="+n,function(){var t,o;t={zoom:u,center:new google.maps.LatLng(d,c),mapTypeId:m},o=new google.maps.Map(document.getElementById(e),t),h.forEach(function(t){var e=t[l],a=t[i];label=s?t[s]:"";var n=new google.maps.Marker({position:{lat:e,lng:a},map:o,title:label}),r=new google.maps.InfoWindow({content:label});n.addListener("click",function(){r.open(o,n)})})})},leafletMap:function(t){var e=alamode.makeId(10),a=t.lat_column,n=t.lng_column,r=t.query_name,o=t.title||r,l=t.height||400,i=t.html_element||"body",s=t.center_lat||39.5,d=t.center_lng||-98.35,c=t.starting_zoom||4,u=t.dot_size||.4,m=t.dot_opacity||.8,t=t.apply_filter||!1,r=alamode.getDataFromQuery(r),h=[];r.forEach(function(t){"number"==typeof t[a]&&"number"==typeof t[n]&&h.push(t)});t=alamode.addContainerElement(i,t);d3.select(t).style("height",l+"px").append("div").attr("class","mode-graphic-title").text(o);o=l-$(t+".mode-graphic-title").height(),l=$(t).width();d3.select(t).append("div").attr("class","mode-leaflet-map").attr("id",e).style("height",o+"px").style("width",l+"px");o=L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",{maxZoom:18}),l={max:8,data:h},c={lat:s,lng:d,zoom:c},m=new HeatmapOverlay({radius:u,maxOpacity:m,scaleRadius:!0,useLocalExtrema:!0,latField:a,lngField:n}),new L.Map(e,{center:new L.LatLng(c.lat,c.lng),zoom:Math.floor(c.zoom),layers:[o,m]});m.setData(l)},wordCloud:function(t){var e=t.query_name,a=t.word_column,n=t.word_count_column,r=t.html_element||"body",o=t.title||e,l=t.height||"400",i=t.width||"800",s=t.colors||["black"],e=alamode.getDataFromQuery(e),d=alamode.addContainerElement(r);d3.select(d).append("div").attr("class","mode-graphic-title").text(o);var c=d3.scale.linear().domain(d3.extent(e,function(t){return t[n]})).range([12,60]),u=d3.layout.cloud().size([i,l]).words(e.map(function(t){return{text:t[a],size:c(t[n])}})).padding(2).rotate(function(){return 360*(~~(6*Math.random())-3)}).font("Impact").fontSize(function(t){return t.size}).on("end",function(t){d3.select(d).append("div").attr("class","mode-wordcloud").append("svg").attr("width",u.size()[0]).attr("height",u.size()[1]).append("g").attr("transform","translate("+u.size()[0]/2+","+u.size()[1]/2+")").selectAll("text").data(t).enter().append("text").style("font-size",function(t){return t.size+"px"}).style("font-family","Impact").style("fill",function(t,e){return s[e%s.length]}).attr("text-anchor","middle").attr("transform",function(t){return"translate("+[t.x,t.y]+")rotate("+t.rotate+")"}).text(function(t){return t.text})});u.start()},funnel:function(t){var e=alamode.makeId(10),a=t.query_name,n=t.stage_column,r=t.value_column,o=t.html_element||"body",l=t.title||a,i=t.height||"300",t=t.width||"500",a=alamode.getDataFromQuery(a),o=alamode.addContainerElement(o);d3.select(o).append("div").attr("class","mode-graphic-title").text(l),d3.select(o).append("div").attr("class","mode-funnel").attr("id",e).style("width",t+"px").style("height",i-20+"px");var s=[];a.forEach(function(t){s.push([t[n],t[r]])});new D3Funnel("#"+e).draw(s,{label:{format:"{l}: {f}"},block:{dynamicHeight:!0},chart:{bottomPinch:1},animation:100}),d3.select("#"+e).style("height",i+"px")},horizontalBarChart:function(n){var t=n.query_name,e=(n.bar_column,n.series_columns),r=n.colors||["#EE8D24","#43A5DA","#6AB328","#BB60F8","#E14459","#EAD022","#06D0AD","#DB38B7"];stacked=n.stacked||!1,leftpad=n.left_pad||175,htmlElement=n.html_element||"body",title=n.title||t,height=n.chart_height||395,width=n.width||"500";var o=alamode.getDataFromQuery(t),a=alamode.addContainerElement(htmlElement);d3.select(a).append("div").attr("class","mode-graphic-title").text(title),d3.select(a).append("div").attr("class","mode-horizontal-bar-chart").style("height",height-50+"px").append("svg");var l=[];e.forEach(function(e,t){var t={key:e,color:r[t%r.length]},a=[];o.forEach(function(t){a.push({label:t[n.bar_column],value:t[e]})}),t.values=a,l.push(t)}),nv.addGraph(function(){var t=nv.models.multiBarHorizontalChart().x(function(t){return t.label}).y(function(t){return t.value}).margin({top:30,right:20,bottom:50,left:leftpad}).showValues(!0).showControls(!1).stacked(stacked);return t.yAxis.tickFormat(d3.format(",.2f")),d3.select(a+" svg").datum(l).call(t),nv.utils.windowResize(t.update),t})},chartAnnotations:function(t){var u="#"+t.chart_id,n=t.x_axis_column,e=t.query_name,m=t.orientations,h=t.comment_values,r=t.group_by,a=t.comments,o=alamode.getDataFromQuery(e),p=[],l={};function i(){a.forEach(function(t,e){var a,n,r=p[e],o=m[e],l=h[e],i=d3.tip().attr("class","d3-tip").style("z-index",100).offset([-10,0]).html(function(t){return t}),s=$(u).find("g.nvd3.nv-wrap").attr("transform"),d=s.indexOf("("),c=s.indexOf(")"),e=s.indexOf(","),d=+s.slice(d+1,e),e=+s.slice(e+1,c);-1!=r&&"v"==o?(a=(c=$(u).find(".nv-point.nv-point-"+r).attr("transform")).indexOf("("),n=c.indexOf(")"),r=c.indexOf(","),a=+c.slice(a+1,r),r=+c.slice(r+1,n),$(u).find("g.nvd3.nv-wrap").first().find("rect").first().attr("height"),$(u).find("g.nvd3.nv-wrap").first().find("rect").first().attr("width"),(n=d3.select(u+" .nvd3svg")).call(i),n.append("rect").attr("x",a+d).attr("y",e-5).attr("width",1).attr("class","flag").attr("height",5+r).attr("fill","#ff8f53"),n.append("circle").data([t]).attr("cx",a+d).attr("cy",e-5).attr("class","flag").attr("r",5).attr("fill","#ff8f53").on("mouseover",i.show).on("mouseout",i.hide)):"h"!=o&&"h-left"!=o&&"h-right"!=o||(y="h"==o?"":"1",$(u).find("g.nv-y"+y+".nv-axis").find(".tick").each(function(t){lineLength="h-right"==o?+$(u).find("g.nv-y1.nv-axis").find(".tick").first().find("line").attr("x2"):+$(this).find("line").attr("x2"),tickTrans=$(this).attr("transform"),tickClosePos=tickTrans.indexOf(")"),tickCommaPos=tickTrans.indexOf(","),0==t?(yTrans1=+tickTrans.slice(tickCommaPos+1,tickClosePos),yVal1=+$(this).find("text").text().replace(",","")):1==t&&(yTrans2=+tickTrans.slice(tickCommaPos+1,tickClosePos),yVal2=+$(this).find("text").text().replace(",",""))}),a=(yTrans2-yTrans1)/(yVal2-yVal1),a=yTrans2-yVal2*a+l*a,(n=d3.select(u+" .nvd3svg")).call(i),n.append("rect").attr("x",d).attr("y",a+e).attr("width",lineLength+10).attr("height",1).attr("class","flag").attr("fill","#ff8f53"),n.append("circle").data([t]).attr("cx",lineLength+d+10).attr("cy",a+e).attr("class","flag").attr("r",5).attr("fill","#ff8f53").on("mouseover",i.show).on("mouseout",i.hide))})}r&&(l=_.groupBy(o,function(t){return t[r]})),a.forEach(function(t,e){var a=_.filter(o,function(t){return t[n]==h[e]});pointNumber=0!=a.length?(r?l[a[0][r]]:o).indexOf(a[0]):-1,p.push(pointNumber)}),setTimeout(function(){d3.select(u).selectAll(".flag").remove(),i()},1e3),$(window).resize(function(){d3.select(u).selectAll(".flag").remove(),d(function(){i()},500,"")});var s,d=(s={},function(t,e,a){s[a=a||"Don't call this twice without a uniqueId"]&&clearTimeout(s[a]),s[a]=setTimeout(t,e)})},bulletChart:function(l){var i=alamode.makeId(10),t=l.query_name,e=l.html_element||"body",a=l.title||t,s=l.chart_width||"800",d=l.bar_column||"",c=l.marker_column||"",u=l.left_pad||150,m=l.color,t=alamode.getDataFromQuery(t),e=alamode.addContainerElement(e);d3.select(e).append("div").attr("class","mode-graphic-title").text(a),d3.select(e).append("div").attr("class","mode-bullet-chart").style("width",s).attr("id",i),t.forEach(function(t){var e=t[l.title_column]||"",a=t[l.subtitle_column]||"",n=t[l.marker_column]||"",r=t[l.bar_column]||"";scale=l.scale_columns&&[t[l.scale_columns[0]],t[l.scale_columns[1]],t[l.scale_columns[2]]];var o={title:e,subtitle:a,ranges:scale,measures:[r],measureLabels:[d],markers:[n],markerLabels:[c],color:m};nv.addGraph(function(){var t=nv.models.bulletChart().height(50).width(s).margin({left:u,right:15,top:10,bottom:10});d3.select("#"+i).append("svg").style("width",s+"px").style("height","70px").style("display","inline").datum(o).transition().duration(500).call(t);return t})})},sunburstChart:function(t){var o=alamode.makeId(10),e=t.query_name,n=t.event_columns,r=t.event_counts,a=t.title||e,l=t.color_range||["#e41a1c","#377eb8","#4daf4a","#984ea3","#ff7f00","#ffff33","#a65628","#f781bf","#999999"],t=t.html_element||"body",s=alamode.getDataFromQuery(e),d=850,c=Math.min(d,600)/2,u={w:(d-30)/n.length,h:20,s:3,t:10},m=[];n.forEach(function(t){m=m.concat(_.uniq(_.map(s,t)))});var e=_.uniq(m),h={};e.forEach(function(t,e){null!=t&&(h[t]=l[e%l.length])}),h.end="#666";var p=0,t=alamode.addContainerElement(t);d3.select(t).append("div").attr("class","mode-graphic-title").text(a),d3.select(t).append("div").attr("class","mode-sunburst-sequence").attr("id","sequence-"+o),d3.select(t).append("div").attr("class","mode-sunburst").attr("id",o),d3.select(t).append("div").attr("class","mode-sunburst-legend-container").attr("id","legend-container-"+o),vis=d3.select("#"+o).append("svg:svg").attr("width",d).attr("height",600).append("svg:g").attr("transform","translate("+d/2+",300)"),vis.append("text").attr("x",0).attr("y",-30).attr("text-anchor","middle").attr("class","mode-sunburst-explanation mode-sunburst-percentage").attr("id","percentage-"+o).style("visibility","hidden").text(""),vis.append("text").attr("x",0).attr("y",-10).attr("text-anchor","middle").attr("class","mode-sunburst-explanation").style("visibility","hidden").text("of total sequences."),vis.append("text").attr("x",0).attr("y",20).attr("text-anchor","middle").attr("class","mode-sunburst-explanation mode-sunburst-cond-percentage").attr("id","cond-percentage-"+o).style("visibility","hidden").text(""),vis.append("text").attr("x",0).attr("y",40).attr("text-anchor","middle").attr("class","mode-sunburst-explanation").style("visibility","hidden").text("from previous location.");var f=d3.layout.partition().size([2*Math.PI,c*c]).value(function(t){return t.size}),g=d3.svg.arc().startAngle(function(t){return t.x}).endAngle(function(t){return t.x+t.dx}).innerRadius(function(t){return Math.sqrt(t.y)}).outerRadius(function(t){return Math.sqrt(t.y+t.dy)}),v=[];function y(t){var e=(100*t.value/p).toPrecision(3),a=e+"%";e<.1&&(a="< 0.1%");var n=b(t),r=t.parent.value,e=(100*t.value/r).toPrecision(3),r=e+"%";e<1&&(a="< 1%"),d3.select("#cond-percentage-"+o).text(r),d3.select("#percentage-"+o).text(a),d3.selectAll(".mode-sunburst-explanation").style("visibility","");n=b(t);a=n,t=d3.select("#trail-"+o).selectAll("g").data(a,function(t){return t.name+t.depth}),(a=t.enter().append("svg:g")).append("svg:polygon").attr("points",w).style("fill",function(t){return h[t.name]}),a.append("svg:text").attr("x",(u.w+u.t)/2).attr("y",u.h/2).attr("dy","0.35em").attr("text-anchor","middle").text(function(t){return t.name}),t.attr("transform",function(t,e){return 5<e&&e<10?"translate("+(e-=5)*(u.w+u.s)+", 20)":10<e?"translate("+(e-=11)*(u.w+u.s)+", 40)":"translate("+e*(u.w+u.s)+", 0)"}),t.exit().remove(),d3.select("#trail-"+o).style("visibility",""),d3.selectAll("path").style("opacity",.3),vis.selectAll("path").filter(function(t){return 0<=n.indexOf(t)}).style("opacity",1)}function x(t){d3.select("#trail-"+o).style("visibility","hidden"),d3.selectAll("path").on("mouseover",null),4==d3.version.split(".")[0]?d3.selectAll("path").transition().duration(300).style("opacity",1).on("end",function(){d3.select(this).on("mouseover",y)}):d3.selectAll("path").transition().duration(300).style("opacity",1).each("end",function(){d3.select(this).on("mouseover",y)}),d3.selectAll(".mode-sunburst-explanation").style("visibility","hidden")}function b(t){for(var e=[],a=t;a.parent;)e.unshift(a),a=a.parent;return e}function w(t,e){var a=[];return a.push("0,0"),a.push(u.w+",0"),a.push(u.w+u.t+","+u.h/2),a.push(u.w+","+u.h),a.push("0,"+u.h),0<e&&a.push(u.t+","+u.h/2),a.join(" ")}s.forEach(function(t){var e="";for(i=0;i<n.length;i++){if(prefix=0!=i?"-~-":"",null==t[n[i]]){e=e+prefix+"end";break}e=e+prefix+t[n[i]]}var a={0:e,1:t[r]};v.push(a)}),function(t){d3.select("#sequence-"+o).append("svg:svg").attr("width",d).attr("height",60).attr("id","trail-"+o).append("svg:text").attr("id","endlabel").style("fill","#000"),function(){var e=195,a=30,n=3;d3.entries(h).forEach(function(t){divContainer=d3.select("#legend-container-"+o).append("div").attr("class","mode-sunburst-legend").attr("id","legend-"+o),svg=divContainer.append("svg:svg").attr("width",e).attr("height",a),svg.append("svg:rect").attr("rx",n).attr("ry",n).attr("width",e).attr("height",a).style("fill",function(){return t.value}),svg.append("svg:text").attr("x",e/2).attr("y",a/2).attr("dy","0.35em").attr("text-anchor","middle").text(function(){return t.key})})}(),vis.append("svg:circle").attr("r",c).style("opacity",0);var e=f.nodes(t).filter(function(t){return.005<t.dx}),e=vis.data([t]).selectAll("path").data(e).enter().append("svg:path").attr("display",function(t){return t.depth?null:"none"}).attr("d",g).attr("fill-rule","evenodd").style("fill",function(t){return h[t.name]}).style("opacity",1).on("mouseover",y);vis.on("mouseleave",x),p=e.node().__data__.value}(function(t){for(var e,a={name:"root",children:[]},n=0;n<t.length;n++){var r=t[n][0],o=+t[n][1];if(!isNaN(o))for(var l=r.split("-~-"),i=a,s=0;s<l.length;s++){var d=i.children,c=l[s];if(s+1<l.length){for(var u=!1,m=0;m<d.length;m++)if(d[m].name==c){e=d[m],u=!0;break}u||(e={name:c,children:[]},d.push(e)),i=e}else e={name:c,size:o},d.push(e)}}return a}(v))},zipcodeChoropleth:function(t){var a=alamode.makeId(10),e=t.query_name,n=t.zipcode_column,r=t.value_column,o=t.width||950,l=t.height||o/1.9,i=t.title||e,s=t.color_range,d=t.color_gradient||["#FFF8CC","#FFF5B2","#FFF299","#E5D87F","#CCBF66","#B2A54C","#998C33","#7F7219","#665900"],c=t.html_element||"body",t=alamode.getDataFromQuery(e),u=d3.map(),e=d3.geoAlbersUsa().scale(o).translate([o/2,l/2]),m=d3.geoPath().projection(e),c=alamode.addContainerElement(c);d3.select(c).append("div").attr("class","mode-graphic-title").text(i),svg=d3.select(c).append("div").attr("class","mode-zipcode-chorolpleth").append("svg").attr("id","mode-zipcode-chorolpleth-"+a).attr("width",o).attr("height",l),t.forEach(function(t){u.set(t[n],+t[r])}),colorDomain=s||d3.extent(t,function(t){return t[r]});var h=d3.scale.quantize().domain(colorDomain).range(d);queue().defer(d3.json,"https://s3-us-west-2.amazonaws.com/mode-alamode/zips_us_topo.json").await(function(t,e){d3.select("#mode-zipcode-chorolpleth-"+a).append("g").attr("class","zipcodes").selectAll(".mode-zipcode-chorolpleth-zipcodes"+a).data(topojson.feature(e,e.objects.zip_codes_for_the_usa).features).enter().append("path").attr("class","mode-zipcode-chorolpleth-zipcodes-"+a).attr("fill",function(t){return h(u.get(t.properties.zip))}).attr("d",m)})},countyChoropleth:function(t){var a=alamode.makeId(10),e=t.query_name,n=t.county_id_column,r=t.value_column,o=t.width||950,l=t.height||o/1.9,i=t.title||e,s=t.color_range,d=t.color_gradient||["#f7fbff","#deebf7","#c6dbef","#9ecae1","#6baed6","#4292c6","#2171b5","#08519c","#08306b"],c=t.html_element||"body",t=alamode.getDataFromQuery(e),u=d3.map(),e=d3.geoAlbersUsa().scale(o).translate([o/2,l/2]),m=d3.geoPath().projection(e),c=alamode.addContainerElement(c);d3.select(c).append("div").attr("class","mode-graphic-title").text(i),svg=d3.select(c).append("div").attr("class","mode-county-chorolpleth").append("svg").attr("id","mode-county-chorolpleth-"+a).attr("width",o).attr("height",l),t.forEach(function(t){u.set(t[n],+t[r])}),colorDomain=s||d3.extent(t,function(t){return t[r]});var h=d3.scale.quantize().domain(colorDomain).range(d);queue().defer(d3.json,"https://s3-us-west-2.amazonaws.com/mode-alamode/counties.json").await(function(t,e){d3.select("#mode-county-chorolpleth-"+a).append("g").attr("class","mode-county-chorolpleth-counties").selectAll(".mode-county-chorolpleth-counties"+a).data(topojson.feature(e,e.objects.counties).features).enter().append("path").attr("class","mode-county-chorolpleth-counties-"+a).attr("fill",function(t){return h(u.get(t.id))}).attr("d",m),d3.select("#mode-county-chorolpleth-"+a).append("path").datum(topojson.mesh(e,e.objects.states,function(t,e){return t!==e})).attr("class","mode-county-chorolpleth-states").attr("d",m)})},stateChoropleth:function(t){var a=alamode.makeId(10),e=t.query_name,n=t.state_column,r=t.value_column,o=t.state_code_type,l=t.width||950,i=t.height||l/1.9,s=t.title||e,d=t.color_range,c=t.color_gradient||["#f7fbff","#deebf7","#c6dbef","#9ecae1","#6baed6","#4292c6","#2171b5","#08519c","#08306b"],u=t.html_element||"body",t=alamode.getDataFromQuery(e),m=d3.map(),e=d3.geoAlbersUsa().scale(l).translate([l/2,i/2]),h=d3.geoPath().projection(e),u=alamode.addContainerElement(u);d3.select(u).append("div").attr("class","mode-graphic-title").text(s),svg=d3.select(u).append("div").attr("class","mode-state-chorolpleth").append("svg").attr("id","mode-state-chorolpleth-"+a).attr("width",l).attr("height",i),t.forEach(function(t){m.set(t[n],+t[r])}),colorDomain=d||d3.extent(t,function(t){return t[r]});var p=d3.scale.quantize().domain(colorDomain).range(c);queue().defer(d3.json,"https://s3-us-west-2.amazonaws.com/mode-alamode/states.json").await(function(t,e){d3.select("#mode-state-chorolpleth-"+a).append("g").attr("class","mode-state-chorolpleth-states").selectAll(".mode-state-chorolpleth-states-"+a).data(e.features).enter().append("path").attr("class","mode-state-chorolpleth-states-"+a).attr("fill",function(t){return p(m.get(t.properties[o]))}).attr("d",h)})},worldChoropleth:function(t){var a=alamode.makeId(10),e=t.query_name,n=t.country_column,r=t.value_column,o=t.country_code_type,l=t.width||950,i=t.height||.8*l,s=t.title||e,d=t.color_range,c=t.color_gradient||["#9ecae1","#6baed6","#4292c6","#2171b5","#08519c","#08306b"],u=t.html_element||"body",t=alamode.getDataFromQuery(e),m=d3.map(),e=d3.geo.mercator().scale((l+1)/2/Math.PI).translate([l/2,i/2+.1*i]).precision(.1),h=d3.geoPath().projection(e),u=alamode.addContainerElement(u);d3.select(u).append("div").attr("class","mode-graphic-title").text(s),d3.select(u).append("div").attr("class","mode-world-chorolpleth-legend").attr("id","mode-world-chorolpleth-legend-"+a).text("Hover over a country to see details"),svg=d3.select(u).append("div").attr("class","mode-world-chorolpleth").append("svg").attr("id","mode-world-chorolpleth-"+a).attr("width",l).attr("height",i),t.forEach(function(t){m.set(t[n],+t[r])}),d?colorDomain=d:(colorDomain=d3.extent(t,function(t){return t[r]}),t.length<=1&&colorDomain.unshift(0));var p=d3.scale.quantize().domain(colorDomain).range(c);queue().defer(d3.json,"https://s3-us-west-2.amazonaws.com/mode-alamode/world.json").await(function(t,e){d3.select("#mode-world-chorolpleth-"+a).append("g").attr("class","mode-world-chorolpleth-countries-base").selectAll(".mode-world-chorolpleth-countries").data(topojson.feature(e,e.objects.countries).features).enter().append("path").attr("class","mode-world-chorolpleth-countries").attr("fill",function(t){return p(m.get(t.properties[o]))}).attr("d",h).on("mouseover",function(t){var e=t.properties.name;value=m.get(t.properties[o])?m.get(t.properties[o]):"--",d3.select("#mode-world-chorolpleth-legend-"+a).text(e+": "+value)}).on("mouseout",function(t){d3.select("#mode-world-chorolpleth-legend-"+a).text("Hover over a country to see details")}),d3.select("#mode-world-chorolpleth-"+a).append("path").datum(topojson.mesh(e,e.objects.countries,function(t,e){return t!==e})).attr("class","mode-world-chorolpleth-boundaries").attr("d",h)})},pivotTable:function(t){var e=alamode.makeId(10),a=t.query_name,n=t.default_columns,r=t.default_rows,o=t.default_values,l=t.editable,i=t.aggregate_function||"Count",s=t.pivot_table_type||"Table",d=t.title||a,c=t.html_element||"body",u=t.default_exclusions||[],m=t.default_inclusions||[];Array.isArray(o)||(o=[o]);var t=alamode.getDataFromQuery(a),a=alamode.getColumnsFromQuery(a),h=_.map(a,"name"),c=alamode.addContainerElement(c);d3.select(c).append("div").attr("class","mode-graphic-title").text(d),d3.select(c).append("div").attr("class","mode-pivot-table").attr("id",e);var p=[];p.push(h),t.forEach(function(e){var a=[];h.forEach(function(t){a.push(e[t])}),p.push(a)}),l?$("#"+e).pivotUI(p,{cols:n,rows:r,aggregatorName:i,vals:o,rendererName:s,exclusions:u,inclusions:m}):(s=(l=$.pivotUtilities).renderers[s],i=l.aggregators[i],$("#"+e).pivot(p,{cols:n,rows:r,aggregator:i(o),renderer:s,exclusions:u,inclusions:m}))},pieChartLabels:function(t){var e=t.show_all_labels,t=t.chart_id,a=$("#"+t),o={};setInterval(function(){var t,r;r=e,(t=a).find(".nv-legendWrap .nv-series text").each(function(a){var n=$(this).text();t.find(".nv-pieWrap .nv-pieLabels text").each(function(t){var e=a+"-"+t;text=$(this).text(),a==t&&""!=text&&text!=o[e]?text=n+" - "+text:a==t&&r&&text!=o[e]&&(text=n),o[e]=text,$(this).text(text)})})},300)},bigNumberSparkline:function(t){var e="#"+t.chart_id,a=t.x_axis_column,n=t.value_column,r=t.query_name,o=alamode.makeId(12),l=alamode.getDataFromQuery(r),t=alamode.getColumnsFromQuery(r);xType=a?(xMatch=_.filter(t,{name:a}),xMatch[0].type):"string";var i=[];l.forEach(function(t){formattedX="datetime"==xType||"timestamp"==xType||"date"==xType?d3.time.format.utc("%Y-%m-%d")(new Date(Date.parse(t[a]))):t[a];t={x:formattedX,y:t[n]};i.push(t)});r=$(e+" .chart-wrapper"),t=$(e+" .chart-big-number"),l=r.height(),r=r.width(),t=t.height(),l=l-t-20;d3.select(e+" .chart-big-number").append("svg").attr("height",l).attr("width",r).attr("id",o).append("g").attr("transform","translate(0,"+t+")"),nv.addGraph(function(){var t=nv.models.sparklinePlus().margin({left:15,right:15}).x(function(t,e){return e}).xTickFormat(function(t){return i[t].x}).showLastValue(!1);return d3.select("#"+o).datum(i).call(t),t}),setTimeout(function(){d3.selectAll(e+" path").style("stroke-width","2px").style("stroke-linejoin","round").style("stroke","#646C6C"),d3.selectAll(e+" .nv-minValue").style("fill","#EE7437").style("stroke","#EE7437").attr("r",3),d3.selectAll(e+" .nv-maxValue").style("fill","#37B067").style("stroke","#37B067").attr("r",3),d3.selectAll(e+" .nv-currentValue").style("fill","#22A3C0").style("stroke","#22A3C0").attr("r",3)},1e3)},addLinkToBigNumber:function(t){window.ALAMODE_CHARTS=window.ALAMODE_CHARTS||{};var e=t.chart_id.split("_")[1];window.ALAMODE_CHARTS[e]=t.url},forceDirectedGraph:function(t){var e=alamode.makeId(10),a=t.node_query,n=t.edge_query,r=t.html_element||"body",o=t.title||queryName,l=t.chart_width||"800",i=t.chart_height||"800",s=t.group_colors||"",t=t.links_to_show||100,d=alamode.getDataFromQuery(a),n=alamode.getDataFromQuery(n),c=[];n.forEach(function(e){var t=c.filter(function(t){return t.target==e.source}).filter(function(t){return t.source==e.target});0!=t.length?t.edge_size+=e.edge_size:c.push(e)}),c=(c=c.sort(function(t,e){return e.edge_size-t.edge_size})).slice(0,t),nameMap={},d.forEach(function(t,e){t.id=e,nameMap[t.node]=e}),c.forEach(function(t){t.source_id=nameMap[t.source],t.target_id=nameMap[t.target]});r=alamode.addContainerElement(r);d3.select(r).append("div").attr("class","mode-graphic-title").text(o),d3.select(r).append("div").attr("class","mode-force-directed-graph").style("width",l).attr("id",e);o=d3.tip().attr("class","mode-force-directed-graph-tooltip").offset([-10,0]).html(function(t){return t.node}),r=d3.layout.force().linkDistance(40).linkStrength(1).size([l,i]),l=d3.select("#"+e).append("svg").attr("width",l).attr("height",i);l.call(o);var i={nodes:d,links:c},u=d3.scale.linear().domain(d3.extent(d,function(t){return t.node_size})).range([2,20]),m=d3.scale.linear().domain(d3.extent(c,function(t){return t.edge_size})).range([1,10]),h=d3.scale.linear().domain(d3.extent(c,function(t){return t.edge_size})).range([.1,.9]),d=i.nodes.slice(),c=[],p=[];i.links.forEach(function(t){var e=d[t.source_id],a=d[t.target_id],n={};n.connections=t.edge_size,d.push(n),c.push({source:e,target:n},{source:n,target:a}),p.push([e,n,a])}),r.nodes(d).links(c).start();var f=l.selectAll(".mode-force-directed-graph-link ").data(p).enter().append("path").attr("class","mode-force-directed-graph-link").style("stroke-width",function(t){return m(t[1].connections)}).style("opacity",function(t){return h(t[1].connections)}),g=l.selectAll(".mode-force-directed-graph-node").data(i.nodes).enter().append("g").attr("class","mode-force-directed-graph-node").call(r.drag);g.append("circle").attr("r",function(t){return u(t.node_size)}).style("fill",function(t){return s?s[t.node_group]:"#0E819A"}).on("mouseover",o.show).on("mouseout",o.hide),r.on("tick",function(){f.attr("d",function(t){return"M"+t[0].x+","+t[0].y+"S"+t[1].x+","+t[1].y+" "+t[2].x+","+t[2].y}),g.attr("transform",function(t){return"translate("+t.x+","+t.y+")"})})},networkMatrix:function(t){var e=alamode.makeId(10),a=t.node_query,n=t.edge_query,r=t.html_element||"body",o=t.title||queryName,l=t.padding_for_names||"200",i=t.chart_width||"800",s=t.chart_height||"800",d=t.group_colors||"",c=t.left_label||"",u=t.top_label||"",m=l,h=10,t=10,l=l,p=alamode.getDataFromQuery(a),n=alamode.getDataFromQuery(n);nameMap={},p.forEach(function(t,e){t.id=e,nameMap[t.node]=e}),n.forEach(function(t){t.source_id=nameMap[t.source],t.target_id=nameMap[t.target]});var f=d3.scale.ordinal().rangeBands([0,i]),g=d3.scale.linear().domain(d3.extent(n,function(t){return t.edge_size})).clamp(!0),r=alamode.addContainerElement(r);d3.select(r).append("div").attr("class","mode-graphic-title").text(o),d3.select(r).append("div").attr("class","mode-network-matrix-order-picker").html('<p>Order: <select id="mode-network-matrix-order-picker-'+e+'"><option value="name">Name</option><option value="count">Frequency</option><option value="group">Group</option></select>'),d3.select(r).append("div").attr("class","mode-network-matrix").style("width",i).attr("id",e);var v=d3.tip().attr("class","mode-network-matrix-tooltip").offset([-10,0]).html(function(t){return t.z}),r=d3.select("#"+e).append("svg").attr("width",i+l+h).attr("height",s+m+t);r.call(v);var y=r.append("g").attr("transform","translate("+l+","+m+")");graph={nodes:p,links:n};var x=[],b=(p=graph.nodes).length;p.forEach(function(t,e){t.index=e,t.count=0,x[e]=d3.range(b).map(function(t){return{x:t,y:e,z:0}})}),graph.links.forEach(function(t){void 0!==x[t.source_id][t.target_id]?(x[t.source_id][t.target_id].z+=t.edge_size,p[t.source_id].count+=t.edge_size,p[t.target_id].count+=t.edge_size):(x[t.source_id][t.target_id]={},x[t.source_id][t.target_id].z=0)});var _,w={name:d3.range(b).sort(function(t,e){return d3.ascending(p[t].node,p[e].node)}),count:d3.range(b).sort(function(t,e){return p[e].count-p[t].count}),group:d3.range(b).sort(function(t,e){return d3.ascending(p[t].node_group,p[e].node_group)})};f.domain(w.name),r.append("text").attr("class","mode-network-matrix-axis-label").attr("x",(i+l+h)/2).attr("y",25).attr("text-anchor","middle").text(u),r.append("text").attr("class","mode-network-matrix-axis-label").attr("x",(s+m+t)/-2).attr("y",25).attr("transform","rotate(-90)").attr("text-anchor","middle").text(c),y.append("rect").attr("class","mode-network-matrix-background").attr("width",i).attr("height",s),(_=y.selectAll(".mode-network-matrix-row").data(x).enter().append("g").attr("class","mode-network-matrix-row").attr("transform",function(t,e){return"translate(0,"+f(e)+")"}).each(_)).append("line").attr("class","mode-network-matrix-line").attr("x2",i),_.append("text").attr("class","mode-network-matrix-row-text").attr("x",-6).attr("y",f.rangeBand()/2).attr("dy",".32em").attr("text-anchor","end").text(function(t,e){return p[e].node});s=y.selectAll(".mode-network-matrix-column").data(x).enter().append("g").attr("class","mode-network-matrix-column").attr("transform",function(t,e){return"translate("+f(e)+")rotate(-90)"});function _(t){d3.select(this).selectAll(".mode-network-matrix-cell").data(t.filter(function(t){return t.z})).enter().append("rect").attr("class","mode-network-matrix-cell").attr("x",function(t){return f(t.x)}).attr("width",f.rangeBand()).attr("height",f.rangeBand()).style("fill-opacity",function(t){return g(t.z)}).style("fill",function(t){return p[t.x].node_group==p[t.y].node_group?d[p[t.x].node_group]:"#2B2B2B"}).on("mouseover",function(t){var a;a=t,d3.selectAll(".mode-network-matrix-row-text").classed("active",function(t,e){return e==a.y}),d3.selectAll(".mode-network-matrix-column-text").classed("active",function(t,e){return e==a.x}),v.show(t)}).on("mouseout",function(t){d3.selectAll("text").classed("active",!1),v.hide(t)})}s.append("line").attr("class","mode-network-matrix-line").attr("x1",-i),s.append("text").attr("class","mode-network-matrix-column-text").attr("x",6).attr("y",f.rangeBand()/2).attr("dy",".32em").attr("text-anchor","start").text(function(t,e){return p[e].node}),d3.select("#mode-network-matrix-order-picker-"+e).on("change",function(){!function(t){f.domain(w[t]);t=y.transition().duration(1e3);t.selectAll(".mode-network-matrix-row").attr("transform",function(t,e){return"translate(0,"+f(e)+")"}).selectAll(".mode-network-matrix-cell").attr("x",function(t){return f(t.x)}),t.selectAll(".mode-network-matrix-column").attr("transform",function(t,e){return"translate("+f(e)+")rotate(-90)"})}(this.value)})},hive:function(t){d3.hive={},d3.hive.link=function(){function e(t,e){var a=r(o,this,t,e),t=r(l,this,t,e);a.a>t.a&&(n=t,t=a,a=n),t.a-a.a>Math.PI&&(a.a+=2*Math.PI);var e=a.a+(t.a-a.a)/3,n=t.a-(t.a-a.a)/3;return a.r0-a.r1||t.r0-t.r1?"M"+Math.cos(a.a)*a.r0+","+Math.sin(a.a)*a.r0+"L"+Math.cos(a.a)*a.r1+","+Math.sin(a.a)*a.r1+"C"+Math.cos(e)*a.r1+","+Math.sin(e)*a.r1+" "+Math.cos(n)*t.r1+","+Math.sin(n)*t.r1+" "+Math.cos(t.a)*t.r1+","+Math.sin(t.a)*t.r1+"L"+Math.cos(t.a)*t.r0+","+Math.sin(t.a)*t.r0+"C"+Math.cos(n)*t.r0+","+Math.sin(n)*t.r0+" "+Math.cos(e)*a.r0+","+Math.sin(e)*a.r0+" "+Math.cos(a.a)*a.r0+","+Math.sin(a.a)*a.r0:"M"+Math.cos(a.a)*a.r0+","+Math.sin(a.a)*a.r0+"C"+Math.cos(e)*a.r1+","+Math.sin(e)*a.r1+" "+Math.cos(n)*t.r1+","+Math.sin(n)*t.r1+" "+Math.cos(t.a)*t.r1+","+Math.sin(t.a)*t.r1}function r(t,e,a,n){var r=t.call(e,a,n),t=+("function"==typeof i?i.call(e,r,n):i)+c,a=+("function"==typeof s?s.call(e,r,n):s);return{r0:a,r1:s===d?a:+("function"==typeof d?d.call(e,r,n):d),a:t}}var o=function(t){return t.source},l=function(t){return t.target},i=function(t){return t.angle},s=function(t){return t.radius},d=s,c=-Math.PI/2;return e.source=function(t){return arguments.length?(o=t,e):o},e.target=function(t){return arguments.length?(l=t,e):l},e.angle=function(t){return arguments.length?(i=t,e):i},e.radius=function(t){return arguments.length?(s=d=t,e):s},e.startRadius=function(t){return arguments.length?(s=t,e):s},e.endRadius=function(t){return arguments.length?(d=t,e):d},e};var e=alamode.makeId(10),a=t.node_query,n=t.edge_query,r=t.groups_are_numeric,o=t.html_element||"body",l=t.title||queryName,i=t.chart_width||"800",s=t.chart_height||"800",d=t.group_colors||"",c=Math.min(i,s)/2-30,t=.2*c,a=alamode.getDataFromQuery(a),u=alamode.getDataFromQuery(n),m=_.uniq(_.map(a,"node_group")),h={};a.forEach(function(t){t.x=r?t.node_group:m.indexOf(t.node_group),t.y=t.node_size,h[t.node]=t}),u.forEach(function(t){t.source=h[t.source],t.target=h[t.target]});o=alamode.addContainerElement(o);d3.select(o).append("div").attr("class","mode-graphic-title").text(l),d3.select(o).append("div").attr("class","mode-network-matrix").style("width",i).attr("id",e),angle=r?d3.scale.linear().domain(d3.extent(a,function(t){return t.node_group})).range([0,2*Math.PI]):d3.scale.ordinal().domain(d3.range(m.length+1)).rangePoints([0,2*Math.PI]);var p=d3.scale.linear().domain(d3.extent(a,function(t){return t.node_size})).range([t,c]),f=d3.tip().attr("class","mode-hive-tooltip").offset([-10,0]).html(function(t){return t.node}),s=d3.select("#"+e).append("svg").attr("width",i).attr("height",s).append("g").attr("transform","translate("+i/2+","+s/2+")");function g(t){return t/Math.PI*180-90}s.call(f),s.selectAll(".mode-hive-axis").data(d3.range(m.length)).enter().append("line").attr("class","mode-hive-axis").attr("transform",function(t){return"rotate("+g(angle(t))+")"}).attr("x1",p.range()[0]).attr("x2",p.range()[1]),s.selectAll(".mode-hive-link").data(u).enter().append("path").attr("class","mode-hive-link").attr("d",d3.hive.link().angle(function(t){return angle(t.x)}).radius(function(t){return p(t.y)})).style("stroke",function(t){return d[t.source.node_group]}),s.selectAll(".mode-hive-node").data(a).enter().append("circle").attr("class","mode-hive-node").attr("transform",function(t){return"rotate("+g(angle(t.x))+")"}).attr("cx",function(t){return p(t.y)}).attr("r",5).style("fill",function(t){return d[t.node_group]}).on("mouseover",function(e){f.show(e),d3.select(this).attr("class","mode-hive-node mode-hive-node-selected"),d3.selectAll(".mode-hive-link").data(u).attr("class",function(t){return t.source.node==e.node||t.target.node==e.node?"mode-hive-link-selected":"mode-hive-link"})}).on("mouseout",function(t){f.hide(t),d3.select(this).attr("class","mode-hive-node"),d3.selectAll(".mode-hive-link-selected").attr("class","mode-hive-link")})},conditionalFormattingByColumn:function(a){var g="#"+a.table_id,t=a.query_name,e=a.column_rules,v=alamode.getDataFromQuery(t),y=(alamode.getColumnsFromQuery(t),{});function n(t){var e,a=$(g+" table"),n=$(g+" .js-header-table"),n=n?$(g+" .js-col-header"):$(n).find("th");a.find("tr");n.each(function(){text=$(this).find(".axel-table-header-label").text(),e=$(this).attr("data-axel-column"),y[text]=e}),t.forEach(function(f){f.rules.forEach(function(t){var a,n,r,o,l,i,s,d,e,c,u,m,h,p=t.shade_text||!1;"gradient"==t.type?(d=f.column,e=t.color,c=p,u=d3.extent(_.map(v,d)),m=d3.scale.linear().domain(u).interpolate(d3.interpolateHsl).range(e),h=y[d],v.forEach(function(t,e){var a=g+" table [data-axel-rowkey='"+e+"'][data-axel-column='"+h+"']",e=m(t[d]),t=x(e),a=$(a);c?a.css("color",e):a.css({background:e,color:t})})):"above"!=t.type&&"below"!=t.type&&"equal"!=t.type||(a=f.column,n=t.type,r=t.value,o=t.color,l=p,i=y[a],s=x(o),v.forEach(function(t,e){e=$(g+" table [data-axel-rowkey='"+e+"'][data-axel-column='"+i+"']");("above"==n&&t[a]>=r||"below"==n&&t[a]<=r||"equal"==n&&t[a]==r)&&(l?e.css("color",o):e.css({background:o,color:s}))}))})})}function x(t){var e=/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(t);return 125<(a=e?(rgb=(t=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(t=t))?{r:parseInt(t[1],16),g:parseInt(t[2],16),b:parseInt(t[3],16)}:null,Math.round((299*parseInt(rgb.r)+587*parseInt(rgb.g)+114*parseInt(rgb.b))/1e3)):255)?"#2B2B2B":"#FCFCFC"}setTimeout(function(){n(e)},1e3),$(g).mousemove(function(){n(e)})},customizeTable:function(t){window.dispatchAction({type:"Embed.AlamodeCustomizeTable",payload:t})},conditionalFormattingByTable:function(a){var u="#"+a.table_id,t=a.query_name,m=a.columns;rules=a.rules;var h=alamode.getDataFromQuery(t),p=(alamode.getColumnsFromQuery(t),{}),e=[];m.forEach(function(t){t=d3.extent(_.map(h,t));e=e.concat(t)});var f=d3.extent(e);function n(t){var e,a=$(u+" table"),n=$(u+" .js-header-table"),n=n?$(u+" .js-col-header"):$(n).find("th");a.find("tr");n.each(function(){text=$(this).find(".axel-table-header-label").text(),e=$(this).attr("data-axel-column"),p[text]=e}),t.forEach(function(t){var r,o,l,i,s,e,d,c,a=t.shade_text||!1;"gradient"==t.type?(e=t.color,d=a,c=d3.scale.linear().domain(f).interpolate(d3.interpolateHsl).range(e),h.forEach(function(n,r){m.forEach(function(t){var e=p[t],a=u+" table [data-axel-rowkey='"+r+"'][data-axel-column='"+e+"']",e=c(n[t]),t=g(e),a=$(a);d?a.css("color",e):a.css({background:e,color:t})})})):"above"!=t.type&&"below"!=t.type&&"equal"!=t.type||(r=t.type,o=t.value,l=t.color,i=a,s=g(l),h.forEach(function(a,n){m.forEach(function(t){var e=p[t],e=$(u+" table [data-axel-rowkey='"+n+"'][data-axel-column='"+e+"']");("above"==r&&a[t]>=o||"below"==r&&a[t]<=o||"equal"==r&&a[t]==o)&&(i?e.css("color",l):e.css({background:l,color:s}))})}))})}function g(t){var e=/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(t);return 125<(a=e?(rgb=(t=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(t=t))?{r:parseInt(t[1],16),g:parseInt(t[2],16),b:parseInt(t[3],16)}:null,Math.round((299*parseInt(rgb.r)+587*parseInt(rgb.g)+114*parseInt(rgb.b))/1e3)):255)?"#2B2B2B":"#FCFCFC"}setTimeout(function(){n(rules)},1e3),$(u).mousemove(function(){n(rules)})},addTableOfContents:function(t){void 0===t&&(t="default");var e=t.text_color,a=t.background_color,n=t.hover_color;$(".mode-header").addClass("has-nav");var r=$("<nav class='fixed-nav-bar'></nav>");$(".row").each(function(){$(this).children().each(function(){var t=$(this).find("mode-chart").attr("id")||$(this).find("mode-table").attr("id")||$(this).find("mode-python").attr("id");if(!t)return!0;t.includes("chart")||t.includes("table")?(e=document.getElementById(t),e=(0<$(e).find("mode-pivot-table").length?document.getElementById(t).getElementsByClassName("in-place-edit-text"):document.getElementById(t).getElementsByClassName("chart-title"))[0].innerText):t.includes("python")&&(e=document.getElementById(t).getElementsByClassName("in-place-edit-text")[0].innerText);var e=$("<a class='scroll-link' href=#"+t+">"+(e.includes("Click to add title")?"Untitled":e)+"</a>");r.append(e)})});var o=$("<div class='mode-grid container''></div>");$(".mode-content").prepend(o);t=$("<div class='row'></div>");o.prepend(t);o=$("<div class='col-md-12'></div>");t.prepend(o),o.prepend(r),e&&$(".fixed-nav-bar a").css("color",e),a&&$(".fixed-nav-bar").css("background-color",a),n&&$(".fixed-nav-bar a").hover(function(){$(this).css("color",n)},function(){e?$(this).css("color",e):$(this).css("color","")}),setTimeout(function(){$(".scroll-link").on("click",function(t){t.preventDefault(),function(t,e){t=$(t).offset().top-50;$("html,body").animate({scrollTop:t},e)}($(this).attr("href"),750)})},100)},xAnnotations:function(t){var a=t.chart_id,r=t.comment_values,o=t.comments,l=t.color||[],s=t.is_date||!1;setTimeout(function(){var e=$("#"+a).find("div.highcharts-container")[0].id,t=Highcharts.charts;if(chart=t.filter(function(t){if(t)return t.container.id==e})[0],data=chart.series[0].data,s)for(i=0;i<r.length;i++)r[i]=new Date(r[i]).getTime();var n=data.filter(function(t){if(t)return 0<=r.indexOf(t.category)});chart.update({chart:{events:{load:function(t){for(i=0;i<n.length;i++){var e=n[i],a=l[i]||e.color||"#FCFCFC";t.renderer.label(o[i],e.plotX+t.plotLeft,10,"callout",e.plotX+t.plotLeft,e.plotY+t.plotTop).attr({fill:"#FCFCFC",stroke:a,"stroke-width":1,radius:10,zIndex:4}).add()}}(chart)}}})},250)},barChartFunnel:function(t){var n=t.chart_id;setTimeout(function(){var e=$("#"+n).find("div.highcharts-container")[0].id,t=Highcharts.charts;chart=t.filter(function(t){if(t)return t.container.id==e})[0],data=chart.series[0].data;var a=d3.format(".1%"),t=chart.series[1].color;chart.update({series:[{color:t,opacity:.6},{dataLabels:{enabled:!0,useHTML:!0,borderRadius:5,padding:20,formatter:function(){return 0==this.point.index?null:a(this.percentage/100)},border:"black",shape:"square",backgroundColor:"rgba(0, 0, 0, 0.75)",style:{fontSize:"11px",color:"#FFFFFF",fontWeight:100,textOutline:"none"}}}],plotOptions:{series:{dataLabels:{x:-210,y:150}}}});t=a(chart.yAxis[0].dataMin/chart.yAxis[0].dataMax);chart.renderer.text("Overall Rate<br/><br/>"+t,chart.plotSizeX-20,chart.plotTop+20).attr({zIndex:5}).css({fontSize:"14px",color:"#FFFFFF"}).add(),chart.renderer.rect(chart.plotSizeX-30,chart.plotTop,124,50,2).attr({"stroke-width":2,stroke:"black",fill:"black",zIndex:4}).add()},250)},highChartsSeriesColor:function(t){var a=t.series_Colors;var n=[];n.push(function(){}),H=Highcharts,H.Chart.prototype.callbacks.push(function(t){for(var e=0;e<n.length;++e)n[e].call(this,event);t.update({series:function(e,a){let n=new Array(e.length);for(var r=0;r<n.length;r++){let t=a.filter(function(t){return t.seriesName===e[r].name});null==t[0]&&(console.log("Jon Color"),t[0]=""),console.log(t[0].color),n[r]={color:t[0].color}}return n}(this.series,a)})})}};