d3-latency-heatmap
Version:
A reusable D3 latency heatmap chart.
2 lines • 3.7 kB
JavaScript
// https://github.com/sengelha/d3-latency-heatmap Version 1.2.4. Copyright 2018 Steven Engelhardt <sengelha@gmail.com>.
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("d3-array"),require("d3-axis"),require("d3-collection"),require("d3-color"),require("d3-interpolate"),require("d3-scale")):"function"==typeof define&&define.amd?define(["exports","d3-array","d3-axis","d3-collection","d3-color","d3-interpolate","d3-scale"],e):e(t.d3=t.d3||{},t.d3,t.d3,t.d3,t.d3,t.d3,t.d3)}(this,function(t,e,n,r,l,i,a){"use strict";t.latencyHeatmap=function(){var t={top:1,right:1,bottom:11,left:1},o=600,c=400,s=void 0,u=function(t){return t[0]},f=void 0,d=function(t){return t[1]},p=void 0,g=function(t){return t[2]},h=void 0,x=[l.rgb("#FFFFFF"),l.rgb("#F03524")],y="#ddd",m="#999",v="#000",k=11;function A(l){l.each(function(A){A=A.map(function(t,e){return[u.call(A,t,e),d.call(A,t,e),g.call(A,t,e)]});var F=r.nest().key(function(t){return t[0]}).entries(A).length,b=r.nest().key(function(t){return t[1]}).entries(A).length;s?(o=s[0]*F+t.left+t.right,c=s[1]*b+t.top+t.bottom):s=[(o-t.left-t.right)/F,(c-t.top-t.bottom)/b];var z=o-t.left-t.right,q=c-t.top-t.bottom,S=e.extent(A,function(t){return t[0]}),L=e.extent(A,function(t){return t[1]}),w=e.extent(A,function(t){return t[2]}),O=a.scaleTime().domain(S).range([0,z-s[0]]),j=a.scaleLinear().domain(L).range([q,0]),E=a.scaleLinear().domain(L).range([q-s[1],0]),I=a.scaleLinear().domain(w).interpolate(i.interpolateRgb).range(x),R=l.selectAll("svg").data([A]),T=R.enter().append("svg").append("g");T.append("g").attr("class","elems"),T.append("g").attr("class","x axis"),T.append("g").attr("class","y axis"),(R=l.selectAll("svg")).attr("width",o).attr("height",c);var _=R.select(".elems").attr("class","elems").attr("transform","translate("+t.left+","+t.top+")").selectAll("rect").data(A),B=_.enter();_.exit().remove(),_.attr("x",function(t){return O(t[0])}).attr("y",function(t){return E(t[1])}).attr("fill",function(t){return I(t[2])}),h&&_.selectAll("title").html(function(t){return h(t)});var H=B.append("rect").attr("x",function(t){return O(t[0])}).attr("y",function(t){return E(t[1])}).attr("width",s[0]).attr("height",s[1]).attr("fill",function(t){return I(t[2])});h&&H.append("title").html(function(t){return h(t)});var M=n.axisBottom(O).tickSizeInner(-q).tickSizeOuter(0);f&&M.tickFormat(f);var P=T.select(".x.axis").attr("transform","translate(0,"+q+")").style("font-size",k+"px").call(M);P.selectAll("path").remove(),P.selectAll("line").style("shape-rendering","crispEdges").style("stroke",y),P.selectAll("text").style("text-anchor","start").style("fill",m);var C=n.axisLeft(j).tickSizeInner(-z).tickSizeOuter(0).ticks(1);p&&C.tickFormat(p);var D=T.select(".y.axis").style("font-size",k+"px").call(C);D.selectAll("path").remove(),D.selectAll("line").style("shape-rendering","crispEdges").style("stroke",y),D.selectAll(".tick text").attr("x",2).style("text-anchor","start").style("fill",v),D.selectAll(".tick:last-of-type text").attr("y",8),D.selectAll(".tick:first-of-type text").attr("y",-8)})}return A.width=function(t){return arguments.length?(o=t,A):o},A.height=function(t){return arguments.length?(c=t,A):c},A.rectSize=function(t){return arguments.length?(s=t,A):s},A.x=function(t){return arguments.length?(u=t,A):u},A.y=function(t){return arguments.length?(d=t,A):d},A.count=function(t){return arguments.length?(g=t,A):g},A.xFormat=function(t){return arguments.length?(f=t,A):f},A.yFormat=function(t){return arguments.length?(p=t,A):p},A.colorRange=function(t){return arguments.length?(x=t,A):x},A.tooltipText=function(t){return arguments.length?(h=t,A):h},A},Object.defineProperty(t,"__esModule",{value:!0})});