Skip to content

Instantly share code, notes, and snippets.

@piroyon
Last active July 16, 2019 01:46
Show Gist options
  • Save piroyon/4287ef694d36a3646039659b07370716 to your computer and use it in GitHub Desktop.
Save piroyon/4287ef694d36a3646039659b07370716 to your computer and use it in GitHub Desktop.
D3 put svg on table
<!DOCTYPE html>
<meta charset="utf-8">
<meta http-equiv="Refresh" content="60">
<script src="http://d3js.org/d3.v3.min.js"></script>
<div id="vis"></div>
<script>
var url = 'http://aaa.bbb.ccc/cgi-bin/getstatus.pl';
d3.json(url, function(error, jobs) {
var header = ['#1','#2','#3','#4','#5','#6'];
var table = d3.select("#vis")
.append("table");
table.append("thead")
.append("tr")
.selectAll("th")
.data(header)
.enter()
.append("th")
.style("padding","2px")
.text(function(d) { return d; });
var tbody = table.append("tbody")
.selectAll("tr")
.data(jobs)
.enter()
.append("tr");
tbody.append("td")
.text(function(d) { return d.node; })
.classed("names", true)
.style("padding","2px");
tbody.append("td")
.text(function(d) { return d.status; })
.classed("names", true)
.style("padding","2px")
.style("background-color",function(d) {
if ( d.status && d.status.match("busy")) {
return "#e5abbe";
} else if (d.status && d.status.match("down")) {
return "red";
} else {return "#fff";}});
tbody.append("td")
.text(function(d) {
var cnt = d.run + '/' + d.max;
return cnt; })
.classed("names", true)
.style("padding","2px");
tbody.append("td")
.text(function(d) {
var mm = d.ocmem + '/' + d.mem;
return mm; })
.classed("names", true)
.style("padding","2px")
.style("background-color", function(d) {
return "#fff";
}
});
var mem = tbody.append("td").classed("graph", true);
mem.append('svg').attr("height",18).attr("width",100).append('g').append('rect')
.attr({
width: function(d) {
var hi = 100 / d.mem;
return hi * d.ocmem;
},
height: 18,
})
.style("fill",function(d) {
var color = "";
if (d.node.match("Total")) {
color = "#006e54";
} else {
color = d.node.match("smp") ? "#b7282e" : "#2a83a2";
} return color;
});
var run = tbody.append("td").classed("graph", true);
run.append('svg').attr("height",18).attr("width",100).append('g').append('rect')
.attr({
width: function(d) {
var hi = 100 / d.max;
return hi * d.run;
},
height: 18,
})
.style("fill",function(d) {
var color = "";
if (d.node.match("Total")) {
color = "#68699b";
} else {
color = d.node.match("smp") ? "#e6b422" : "#716246";
} return color;
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment