Built with blockbuilder.org
Last active
September 17, 2018 02:12
-
-
Save scresawn/3774d2c9df50e690225333699a3f2c61 to your computer and use it in GitHub Desktop.
fresh block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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; } | |
svg { | |
overflow: scroll; | |
} | |
</style> | |
</head> | |
<body> | |
<script> | |
// Feel free to change or delete any of the code you see in this editor! | |
var svg = d3.select("body").append("svg") | |
.attr("width", 9600) | |
.attr("height", 500) | |
var genes = [ | |
{name: '1', start: 25, end: 100, direction: 'forward'}, | |
{name: 2, start: 125, end: 200, direction: 'forward'}, | |
{name: 3, start: 225, end: 300, direction: 'reverse'}, | |
{name: 4, start: 325, end: 400, direction: 'forward'} | |
] | |
forward = 0 | |
reverse = 0 | |
genes.forEach(function (g) { | |
if (g.direction == 'forward') { forward += 1} | |
else if (g.direction == 'reverse') { reverse += 1} | |
}) | |
directionCounts = [ | |
{direction: 'forward', size: forward}, | |
{direction: 'reverse', size: reverse} | |
] | |
console.log(directionCounts); | |
var mapEnter = svg.selectAll('rect') | |
.data(genes) | |
.enter(); | |
mapEnter | |
.append("rect") | |
.attr("y", function (d) { | |
if (d.direction == 'forward') { return 100} | |
return 135 | |
}) | |
.attr('width', function (d) {return d.end - d.start}) | |
.attr('height', 35) | |
.attr('fill', function (d) { | |
if (d.direction == 'forward') { return 'steelblue'} | |
return 'darkorange' | |
}) | |
.attr('stroke-width', 1) | |
.attr('stroke', 'black') | |
.attr('x', function (d) { | |
if (d.direction == 'forward') { | |
return -(d.end - d.start) | |
} | |
return 960 | |
}) | |
.on('click', function (d) { d3.select(this).attr('fill', 'green') }) | |
.transition() | |
.duration(1000) | |
.delay(function (d,i) { return i * 1000}) | |
.attr("x", function (d) { return d.start}) | |
mapEnter | |
.append('text') | |
.text(function (d) { return d.name }) | |
.attr("y", function (d) { | |
if (d.direction == 'forward') { return 100 + 35/2} | |
return 135 + 35/2 | |
}) | |
.attr("x", function (d) { | |
var width = d.end - d.start; | |
return (d.start + (width/2))}) | |
.attr('alignment-baseline',"middle") | |
.attr('text-anchor',"middle") | |
.attr('font-family', 'Arial') | |
.attr('opacity', 0) | |
.transition() | |
.duration (750) | |
.delay(function (d,i) { return 1000 + i * 1000}) | |
.attr('opacity', 1) | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment