gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/website/_includes/graph.html (about) 1 {::nomarkdown} 2 {% assign fn = include.id | remove: " " | remove: "-" | downcase %} 3 <figure><a href="{{ include.url }}"><svg id="{{ include.id }}" width=500 height=200><title>{{ include.title }}</title></svg></a></figure> 4 <script> 5 function render_{{ fn }}() { 6 d3.csv("{{ include.url }}", function(d, i, columns) { 7 return d; // Transformed below. 8 }, function(error, data) { 9 if (error) throw(error); 10 11 // Create a new data that pivots on runtime. 12 // 13 // To start, we have: 14 // runtime, ..., result 15 // runc, ..., 1 16 // runsc, ..., 2 17 // 18 // In the end we want: 19 // ..., runsc, runc 20 // ..., 1, 2 21 22 // Filter by metric, if required. 23 if ("{{ include.metric }}" != "") { 24 orig_columns = data.columns; 25 data = data.filter(d => d.metric == "{{ include.metric }}"); 26 data.columns = orig_columns; 27 } 28 29 // Filter by method, if required. 30 if ("{{ include.method }}" != "") { 31 orig_columns = data.columns; 32 data = data.filter(d => d.method == "{{ include.method }}"); 33 data.columns = orig_columns.filter(key => key != "method"); 34 } 35 36 // Enumerate runtimes. 37 var runtimes = Array.from(new Set(data.map(d => d.runtime))); 38 var metrics = Array.from(new Set(data.map(d => d.metric))); 39 if (metrics.length < 1) { 40 console.log(data); 41 throw("need at least one metric"); 42 } else if (metrics.length == 1) { 43 metric = metrics[0]; 44 data.columns = data.columns.filter(key => key != "metric"); 45 } else { 46 metric = ""; // Used for grouping. 47 } 48 49 var isSubset = function(a, sup) { 50 var ap = Object.getOwnPropertyNames(a); 51 for (var i = 0; i < ap.length; i++) { 52 if (a[ap[i]] !== sup[ap[i]]) { 53 return false; 54 } 55 } 56 return true; 57 }; 58 59 // Execute a pivot to include runtimes as attributes. 60 var new_data = data.map(function(data_item) { 61 // Generate a prototype data item. 62 var proto_item = Object.assign({}, data_item); 63 delete proto_item.runtime; 64 delete proto_item.result; 65 var next_item = Object.assign({}, proto_item); 66 67 // Find all matching runtime items. 68 data.forEach(function(d) { 69 if (isSubset(proto_item, d)) { 70 // Add the result result. 71 next_item[d.runtime] = d.result; 72 } 73 }); 74 return next_item; 75 }); 76 77 // Remove any duplication. 78 new_data = Array.from(new Set(new_data)); 79 new_data.columns = data.columns; 80 new_data.columns = new_data.columns.filter(key => key != "runtime" && key != "result"); 81 new_data.columns = new_data.columns.concat(runtimes); 82 data = new_data; 83 84 // Slice based on the first key. 85 if (data.columns.length != runtimes.length) { 86 x0_key = new_data.columns[0]; 87 var x1_domain = data.columns.slice(1); 88 } else { 89 x0_key = "runtime"; 90 var x1_domain = runtimes; 91 } 92 93 // Determine variable margins. 94 var x0_domain = data.map(d => d[x0_key]); 95 var margin_bottom_pad = 0; 96 if (x0_domain.length > 8) { 97 margin_bottom_pad = 50; 98 } 99 100 // Use log scale if required. 101 var y_min = 0; 102 if ({{ include.log | default: "false" }}) { 103 // Need to cap lower end of the domain at 1. 104 y_min = 1; 105 } 106 107 if ({{ include.y_min | default: "false" }}) { 108 y_min = "{{ include.y_min }}"; 109 } 110 111 var svg = d3.select("#{{ include.id }}"), 112 margin = {top: 20, right: 20, bottom: 30 + margin_bottom_pad, left: 50}, 113 width = +svg.attr("width") - margin.left - margin.right, 114 height = +svg.attr("height") - margin.top - margin.bottom, 115 g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")"); 116 117 var x0 = d3.scaleBand() 118 .rangeRound([margin.left / 2, width - (4 * margin.right)]) 119 .paddingInner(0.1); 120 121 var x1 = d3.scaleBand() 122 .padding(0.05); 123 124 var y = d3.scaleLinear() 125 .rangeRound([height, 0]); 126 if ({{ include.log | default: "false" }}) { 127 y = d3.scaleLog() 128 .rangeRound([height, 0]); 129 } 130 131 var z = d3.scaleOrdinal() 132 .range(["#262362", "#FBB03B", "#286FD7", "#6b486b"]); 133 134 // Set all domains. 135 x0.domain(x0_domain); 136 x1.domain(x1_domain).rangeRound([0, x0.bandwidth()]); 137 y.domain([y_min, d3.max(data, d => d3.max(x1_domain, key => parseFloat(d[key])))]).nice(); 138 139 // The data. 140 g.append("g") 141 .selectAll("g") 142 .data(data) 143 .enter().append("g") 144 .attr("transform", function(d) { return "translate(" + x0(d[x0_key]) + ",0)"; }) 145 .selectAll("rect") 146 .data(d => x1_domain.map(key => ({key, value: d[key]}))) 147 .enter().append("rect") 148 .attr("x", d => x1(d.key)) 149 .attr("y", d => y(d.value)) 150 .attr("width", x1.bandwidth()) 151 .attr("height", d => y(y_min) - y(d.value)) 152 .attr("fill", d => z(d.key)); 153 154 // X0 ticks and labels. 155 var x0_axis = g.append("g") 156 .attr("class", "axis") 157 .attr("transform", "translate(0," + height + ")") 158 .call(d3.axisBottom(x0)); 159 if (x0_domain.length > 8) { 160 x0_axis.selectAll("text") 161 .style("text-anchor", "end") 162 .attr("dx", "-.8em") 163 .attr("dy", ".15em") 164 .attr("transform", "rotate(-65)"); 165 } 166 167 // Y ticks and top-label. 168 if (metric == "default") { 169 metric = ""; // Don't display. 170 } 171 g.append("g") 172 .attr("class", "axis") 173 .call(d3.axisLeft(y).ticks(null, ".1s")) 174 .append("text") 175 .attr("x", -30.0) 176 .attr("y", y(y.ticks().pop()) - 10.0) 177 .attr("dy", "0.32em") 178 .attr("fill", "#000") 179 .attr("font-weight", "bold") 180 .attr("text-anchor", "start") 181 .text(metric); 182 183 // The legend. 184 var legend = g.append("g") 185 .attr("font-family", "sans-serif") 186 .attr("font-size", 10) 187 .attr("text-anchor", "end") 188 .selectAll("g") 189 .data(x1_domain.slice().reverse()) 190 .enter().append("g") 191 .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); 192 legend.append("rect") 193 .attr("x", width - 19) 194 .attr("width", 19) 195 .attr("height", 19) 196 .attr("fill", z); 197 legend.append("text") 198 .attr("x", width - 24) 199 .attr("y", 9.5) 200 .attr("dy", "0.32em") 201 .text(function(d) { return d; }); 202 }); 203 } 204 $(render_{{ fn }}); 205 </script> 206 {:/}