|
| 1 | +<meta charset="utf-8"> |
| 2 | +<style> /* set the CSS */ |
| 3 | + |
| 4 | +.bar { fill: steelblue; } |
| 5 | + |
| 6 | +body { |
| 7 | + font: 10px sans-serif; |
| 8 | +} |
| 9 | + |
| 10 | +.axis path, |
| 11 | +.axis line { |
| 12 | + fill: none; |
| 13 | + stroke: #000; |
| 14 | + shape-rendering: crispEdges; |
| 15 | +} |
| 16 | + |
| 17 | +.bar { |
| 18 | + fill: orange; |
| 19 | +} |
| 20 | + |
| 21 | +.bar:hover { |
| 22 | + fill: orangered ; |
| 23 | +} |
| 24 | + |
| 25 | +.x.axis path { |
| 26 | + display: none; |
| 27 | +} |
| 28 | + |
| 29 | +.d3-tip { |
| 30 | + line-height: 1; |
| 31 | + padding: 6px; |
| 32 | + background: rgba(0, 0, 0, 0.8); |
| 33 | + color: #fff; |
| 34 | + border-radius: 4px; |
| 35 | + font-size: 12px; |
| 36 | +} |
| 37 | + |
| 38 | +/* Creates a small triangle extender for the tooltip */ |
| 39 | +.d3-tip:after { |
| 40 | + box-sizing: border-box; |
| 41 | + display: inline; |
| 42 | + font-size: 10px; |
| 43 | + width: 100%; |
| 44 | + line-height: 1; |
| 45 | + color: rgba(0, 0, 0, 0.8); |
| 46 | + content: "\25BC"; |
| 47 | + position: absolute; |
| 48 | + text-align: center; |
| 49 | +} |
| 50 | + |
| 51 | +/* Style northward tooltips specifically */ |
| 52 | +.d3-tip.n:after { |
| 53 | + margin: -2px 0 0 0; |
| 54 | + top: 100%; |
| 55 | + left: 0; |
| 56 | +} |
| 57 | +</style> |
| 58 | +<body> |
| 59 | + <h2>{{ container_name }} Similarity to Docker OS</h2> |
| 60 | +<!-- load the d3.js library --> |
| 61 | +<script src="//d3js.org/d3.v4.min.js"></script> |
| 62 | +<script src="https://rawgit.com/VACLab/d3-tip/master/d3-tip.js"></script> |
| 63 | +<script> |
| 64 | + |
| 65 | +// set the dimensions and margins of the graph |
| 66 | +var margin = {top: 20, right: 20, bottom: 30, left: 40}, |
| 67 | + width = 960 - margin.left - margin.right, |
| 68 | + height = 500 - margin.top - margin.bottom, |
| 69 | + axis_height = height - 100; |
| 70 | + |
| 71 | +// set the ranges |
| 72 | +var x = d3.scaleBand() |
| 73 | + .range([0, width]) |
| 74 | + .padding(0.1); |
| 75 | + |
| 76 | +var y = d3.scaleLinear() |
| 77 | + .range([height, 0]); |
| 78 | + |
| 79 | +// Setup the tool tip. Note that this is just one example, and that many styling options are available. |
| 80 | +// See original documentation for more details on styling: http://labratrevenge.com/d3-tip/ |
| 81 | +var tool_tip = d3.tip() |
| 82 | + .attr("class", "d3-tip") |
| 83 | + .offset([-8, 0]) |
| 84 | + .html(function(d) { return "Similarity Score: " + d.score; }); |
| 85 | + |
| 86 | +// acppend the svg object to the body of the page |
| 87 | +// append a 'group' element to 'svg' |
| 88 | +// moves the 'group' element to the top left margin |
| 89 | +var svg = d3.select("body").append("svg") |
| 90 | + .attr("width", width + margin.left + margin.right) |
| 91 | + .attr("height", height + margin.top + margin.bottom + 200) |
| 92 | + .append("g") |
| 93 | + .attr("transform", |
| 94 | + "translate(" + margin.left + "," + margin.top + ")"); |
| 95 | + |
| 96 | + svg.call(tool_tip); |
| 97 | + raw = {{ sim_scores | safe }}; |
| 98 | + data = []; |
| 99 | + |
| 100 | + // Format the data |
| 101 | + for (var os in raw) { |
| 102 | + if (raw.hasOwnProperty(os)) { |
| 103 | + var new_os = {dist:os, score: parseFloat(raw[os]).toFixed(8)}; |
| 104 | + data.push(new_os); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Scale the range of the data in the domains |
| 109 | + x.domain(data.map(function(d) { return d.dist; })); |
| 110 | + y.domain([0.0, 1.0]); |
| 111 | + |
| 112 | + // append the rectangles for the bar chart |
| 113 | + svg.selectAll(".bar") |
| 114 | + .data(data) |
| 115 | + .enter().append("rect") |
| 116 | + .attr("class", "bar") |
| 117 | + .attr("x", function(d) { return x(d.dist); }) |
| 118 | + .attr("width", 15) |
| 119 | + .attr("y", function(d) { return y(d.score); }) |
| 120 | + .attr("height", function(d) { return height - y(d.score); }) |
| 121 | + .on('mouseover', tool_tip.show) |
| 122 | + .on('mouseout', tool_tip.hide) |
| 123 | + |
| 124 | + // add the x Axis |
| 125 | + svg.append("g") |
| 126 | + .classed('axis',true) |
| 127 | + .attr("transform", "translate(0," + height + ")") |
| 128 | + .call(d3.axisBottom(x)) |
| 129 | + .selectAll("text") |
| 130 | + .style("text-anchor", "end") |
| 131 | + .attr("dx", "-.8em") |
| 132 | + .attr("dy", ".15em") |
| 133 | + .attr("transform", "rotate(-65)"); |
| 134 | + |
| 135 | + // add the y Axis |
| 136 | + svg.append("g") |
| 137 | + .call(d3.axisLeft(y)); |
| 138 | + |
| 139 | + |
| 140 | +</script> |
| 141 | +</body> |
0 commit comments