xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script type="text/javascript" src="https://d3js.org/d3.v3.min.js"></script>
</head>
<body>
<style>
div.tooltip {
position: absolute;
text-align: center;
width: 60px;
height: 28px;
padding: 2px;
font: 12px sans-serif;
background: lightsteelblue;
border: 0px;
border-radius: 8px;
/* pointer-events: none; This line needs to be removed */
}
</style>
<script>
// Feel free to change or delete any of the code you see!
var genomelength = 7800;
var tickMarks = {thousand: [], fivehundred: [], onehundred: []};
var genome_positions = [];
for (var i = 1; i <= genomelength; i++) {
genome_positions.push(i);
}
genome_positions.forEach(function(currentValue, index, myArray){
if (currentValue % 1000 === 0) {
tickMarks.thousand.push(currentValue);
}
else if (currentValue % 500 === 0) {
tickMarks.fivehundred.push(currentValue);
}
else if (currentValue % 100 === 0) {
tickMarks.onehundred.push(currentValue);
}
});
var svg = d3.select("body").append("svg").attr({height: 500,width: genomelength/10});
svg.append("g")
.append("rect")
.attr({x: 0, y: 100, width: genomelength/10, height: 30})
.style({"stroke-width": "2px", "fill": "white", "stroke": "black"});
// Define 'div' for tooltips
var div = d3.select("body")
.append("div") // declare the tooltip div
.attr("class", "tooltip") // apply the 'tooltip' class
.style("opacity", 0); // set the opacity to nil
d3.json("genes.json.txt", function(error, json) {
if (error) return console.warn(error);
var rects = svg.selectAll(".genes")
.data(json)
.enter()
.append("rect");
var rectAttributes = rects
.attr("x", function (d) { return d.start/10; })
.attr("y", function (d) { return 100; })
.attr("height", function(d) { return 0;})
.attr("width", function (d) { return (d.stop-d.start)/10; })
.style({"fill":"green","stroke":"black", "stroke-width": "2px"})
//.transition().delay(3000).duration(1000)
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", .9);
div .html(d.name)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.attr("y", function (d) {
if(d.direction=="forward" && d.name%2==0){
return 70;
}
else if (d.direction=="forward"&& d.name%2!=0){
return 30
}
else if(d.direction=="reverse" && d.name%2==0){
return 130}
else if(d.direction=="reverse" && d.name%2!==0){
return 160
}
})
.attr("height", function (d) {return 30;})
.style({"fill":function(d){if(d.direction=="forward"){return "green"}else if (d.direction=="reverse"){return "red"}}
});
});
d3.json("genes.json.txt", function(error, json) {
if (error) return console.warn(error);
var text = svg.selectAll(".genes")
.data(json)
.enter()
.append("text");
var texts = text
.text(function(d)
{
var percent = 0;
var GC = 0;
for (var i=0;i<d.sequence.length;i++){
// calculate % of G and C
if (d.sequence[i] === "G" || d.sequence[i] === "C") {
GC++;
// this symbol: || means "or"
}
}
percent = (GC/d.sequence.length) * 100;
return percent;
})
.attr("y", function(d){
if(d.direction=="forward" && d.name%2==0){
return 90;
}
else if (d.direction=="forward"&& d.name%2!=0){
return 50
}
else if(d.direction=="reverse" && d.name%2==0){
return 150}
else if(d.direction=="reverse" && d.name%2!==0){
return 180
}})
.attr("x",function(d){return ((d.start+(d.stop - d.start)/2)/10)})
})
var group = svg.selectAll(".a")
.data(tickMarks.thousand)
.enter()
.append("g");
group.append("rect")
.style({"fill": "black"})
.attr({x: 0, y: 100, width: "1px", height: 30})
//.transition().duration(3000)
.attr("transform", function (d) { return "translate(" + d/10 + ",0)"; });
group.append("text")
.text(function(d){return d/1000})
.attr("y", 112)
.attr("x", function (d){return d/10})
.style("font-size","12")
var group2 = svg.selectAll(".b")
.data(tickMarks.fivehundred)
.enter()
.append("g");
group2.append("rect")
.style({"fill": "black"})
.attr({x: 0, y: 100, width: "1px", height: 15})
//.transition().duration(2000)
.attr("transform", function (d) { return "translate(" + d/10 + ",0)"; });
var group3 = svg.selectAll(".c")
.data(tickMarks.onehundred)
.enter()
.append("g");
group3.append("rect")
.style({"fill": "black"})
.attr({x: 0, y: 115, width: "1px", height: 15})
//.transition().duration(1000)
.attr("transform", function (d) { return "translate(" + d/10 + ",0)"; });
</script>
</body>
Modified http://d3js.org/d3.v3.min.js to a secure url
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js
https://d3js.org/d3.v3.min.js