Skip to content

Instantly share code, notes, and snippets.

@derbyth
Last active February 21, 2017 16:49
Show Gist options
  • Save derbyth/5ec8fe1b62de8d5b058b1c21362fb882 to your computer and use it in GitHub Desktop.
Save derbyth/5ec8fe1b62de8d5b058b1c21362fb882 to your computer and use it in GitHub Desktop.
Gene Visualization
license: mit
[{"orientation": "fwd", "stop": 250, "start": 49, "gene": "1"},
{"orientation": "fwd", "stop": 305, "start": 265, "gene": "2"},
{"orientation": "rev", "stop": 440, "start": 361, "gene": "3"},
{"orientation": "fwd", "stop": 620, "start": 500, "gene": "4"}]
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<script>
// first, make a general svg:
var svg = d3.select("body").append("svg")
.attr("width", 1000)
.attr("height", 1000);
// next, read in the data from the json file:
d3.json("data.json", function(error,Data) {
// then, create a group and append it to the svg
var Group = d3.select("svg").selectAll("g")
.data(Data)
.enter()
.append("svg:g")
// then, add a rectangle for each data element to the group (in the svg)
Group.append("rect")
.attr("x", function(d) {return d.start})
.attr("y", function(d) {
if (d.orientation == "fwd") {return 400;}
else if (d.orientation == "rev") {return 440} })
.attr("width", function(d) {return d.stop - d.start} )
.attr("height", 50)
.attr("stroke", "black")
.attr("stroke-width", "2")
.attr("fill", function(d) {
if (d.orientation == "fwd") {return "navy";}
else if (d.orientation == "rev") {return "skyblue"}; })
// finally, add a gene label to the gene in the group
Group.append("text")
.text(function(d) {return d.gene})
.attr("height", 50)
.attr("width", 50)
.attr("y", function(d) {
if (d.orientation == "fwd") {return 435;}
else if (d.orientation == "rev") {return 475}; })
.attr("x", function(d) {return (d.start + (d.stop - d.start)/2)-8})
.style("font-size", 30)
.style("fill", "white")
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment