/*! grafana - v4.5.1 - 2017-09-19
 * Copyright (c) 2017 Torkel Ödegaard; Licensed Apache-2.0 */

System.register(["angular","lodash","jquery","d3","app/core/core","app/core/utils/ticks"],function(a,b){"use strict";function c(a,b,c,d,f,g){var j=p.default(a).find("svg"),k=q.default.select(j.get(0));h(a);var l=Math.floor(j.outerWidth())-30,m=j.attr("height"),n=1;d-c>l&&(n=Math.floor((d-c)/l));var o=l/(d-c),r=q.default.range(c,d,n),s=i(b,f,g);k.selectAll(".heatmap-color-legend-rect").data(r).enter().append("rect").attr("x",function(a){return a*o}).attr("y",0).attr("width",n*o+1).attr("height",m).attr("stroke-width",0).attr("fill",function(a){return s(a)}),e(a,s,c,d,f,g,l)}function d(a,b,c,d,f,g){var i=p.default(a).find("svg"),k=q.default.select(i.get(0));h(a);var l=Math.floor(i.outerWidth())-30,m=i.attr("height"),n=10,o=l/(d-c),r=q.default.range(c,d,n),s=j(b,f,g);k.selectAll(".heatmap-opacity-legend-rect").data(r).enter().append("rect").attr("x",function(a){return a*o}).attr("y",0).attr("width",n*o).attr("height",m).attr("stroke-width",0).attr("fill",b.cardColor).style("opacity",function(a){return s(a)}),e(a,s,c,d,f,g,l)}function e(a,b,c,d,e,f,g){var h=p.default(a).find("svg"),i=q.default.select(h.get(0));if(!(g<=0||0===h.get(0).childNodes.length)){var j=(o.default.sortBy(b.domain()),q.default.scaleLinear().domain([0,d]).range([0,g])),m=l(0,d,e,f),n=q.default.axisBottom(j).tickValues(m).tickSize(2),r=h.find(":first-child"),s=r.height()+2,t=k(r);q.default.select(h.get(0)).append("g").attr("class","axis").attr("transform","translate("+t+","+s+")").call(n),i.select(".axis").select(".domain").remove()}}function f(a,b){var c=p.default(a).find("svg");h(a);var d=Math.floor(c.outerWidth()),e=c.attr("height");if(d){var f=Math.floor(d/2),g=Math.floor(d/f),i=q.default.range(0,d,g),j=q.default.select(c.get(0)),k=j.selectAll(".heatmap-color-legend-rect").data(i);k.enter().append("rect").attr("x",function(a){return a}).attr("y",0).attr("width",g+1).attr("height",e).attr("stroke-width",0).attr("fill",function(a){return b(a)})}}function g(a,b){var c=p.default(a).find("svg");h(a);var d=q.default.select(c.get(0)),e=Math.floor(c.outerWidth()),f=c.attr("height");if(e){var g;"linear"===b.colorScale?g=q.default.scaleLinear().domain([0,e]).range([0,1]):"sqrt"===b.colorScale&&(g=q.default.scalePow().exponent(b.exponent).domain([0,e]).range([0,1]));var i=10,j=q.default.range(0,e,i),k=d.selectAll(".heatmap-opacity-legend-rect").data(j);k.enter().append("rect").attr("x",function(a){return a}).attr("y",0).attr("width",i).attr("height",f).attr("stroke-width",0).attr("fill",b.cardColor).style("opacity",function(a){return g(a)})}}function h(a){var b=p.default(a).find("svg");b.empty()}function i(a,b,c){void 0===c&&(c=0);var d=q.default[a.value],e="always"===a.invert||"dark"===a.invert&&!r.contextSrv.user.lightTheme,f=e?b:c,g=e?c:b;return q.default.scaleSequential(d).domain([f,g])}function j(a,b,c){void 0===c&&(c=0);var d;return"linear"===a.colorScale?d=q.default.scaleLinear().domain([c,b]).range([0,1]):"sqrt"===a.colorScale&&(d=q.default.scalePow().exponent(a.exponent).domain([c,b]).range([0,1])),d}function k(a){var b=a.get(0);return b&&b.x&&b.x.baseVal?a.get(0).x.baseVal.value:0}function l(a,b,c,d){for(var e=b-a,f=s.tickStep(a,b,3),g=Math.round(e/f),h=[],i=0;i<g;i++){var j=f*i;m(d,j,f)?h.push(d):(d<j&&h.push(d),m(c,j,f)?h.push(c):(c<j&&h.push(c),h.push(f*i)))}return m(c,b,f)||h.push(c),h.push(b),h=o.default.sortBy(o.default.uniq(h))}function m(a,b,c){var d=Math.abs(a-b);return d<.3*c}var n,o,p,q,r,s,t;b&&b.id;return{setters:[function(a){n=a},function(a){o=a},function(a){p=a},function(a){q=a},function(a){r=a},function(a){s=a}],execute:function(){t=n.default.module("grafana.directives"),t.directive("colorLegend",function(){return{restrict:"E",template:'<div class="heatmap-color-legend"><svg width="16.8rem" height="24px"></svg></div>',link:function(a,b,c){function d(){var a=p.default(b).find("svg"),c=Math.floor(a.outerWidth());if("spectrum"===h.color.mode){var d=o.default.find(e.colorSchemes,{value:h.color.colorScheme}),j=i(d,c);f(b,j)}else if("opacity"===h.color.mode){var k=h.color;g(b,k)}}var e=a.ctrl,h=a.ctrl.panel;d(),e.events.on("render",function(){d()})}}}),t.directive("heatmapLegend",function(){return{restrict:"E",template:'<div class="heatmap-color-legend"><svg width="100px" height="14px"></svg></div>',link:function(a,b,e){function f(){if(h(b),!o.default.isEmpty(g.data)&&!o.default.isEmpty(g.data.cards)){var a=0,e=g.data.cardStats.max,f=i.color.max||e,j=i.color.min||0;if("spectrum"===i.color.mode){var k=o.default.find(g.colorSchemes,{value:i.color.colorScheme});c(b,k,a,e,f,j)}else if("opacity"===i.color.mode){var l=i.color;d(b,l,a,e,f,j)}}}var g=a.ctrl,i=a.ctrl.panel;f(),g.events.on("render",function(){f()})}}})}}});