Skip to content

Instantly share code, notes, and snippets.

@sohaibghani1
Last active May 10, 2017 12:06
Show Gist options
  • Save sohaibghani1/ad0f0ccd37bfb8c34099067d27cb80de to your computer and use it in GitHub Desktop.
Save sohaibghani1/ad0f0ccd37bfb8c34099067d27cb80de to your computer and use it in GitHub Desktop.
chord test
license: mit
name color
United Arab Emirates #9ACD32
Djibouti #F5F5F5
Algeria #F5DEB3
Egypt #EE82EE
Israel #40E0D0
Iraq #FF6347
Jordan #D8BFD8
Comoros #D2B48C
// Below are the functions that handle actual exporting:
// getSVGString ( svgNode ) and svgString2Image( svgString, width, height, format, callback )
function getSVGString( svgNode ) {
svgNode.setAttribute('xlink', 'http://www.w3.org/1999/xlink');
var cssStyleText = getCSSStyles( svgNode );
appendCSS( cssStyleText, svgNode );
var serializer = new XMLSerializer();
var svgString = serializer.serializeToString(svgNode);
svgString = svgString.replace(/(\w+)?:?xlink=/g, 'xmlns:xlink='); // Fix root xlink without namespace
svgString = svgString.replace(/NS\d+:href/g, 'xlink:href'); // Safari NS namespace fix
return svgString;
function getCSSStyles( parentElement ) {
var selectorTextArr = [];
// Add Parent element Id and Classes to the list
selectorTextArr.push( '#'+parentElement.id );
for (var c = 0; c < parentElement.classList.length; c++)
if ( !contains('.'+parentElement.classList[c], selectorTextArr) )
selectorTextArr.push( '.'+parentElement.classList[c] );
// Add Children element Ids and Classes to the list
var nodes = parentElement.getElementsByTagName("*");
for (var i = 0; i < nodes.length; i++) {
var id = nodes[i].id;
if ( !contains('#'+id, selectorTextArr) )
selectorTextArr.push( '#'+id );
var classes = nodes[i].classList;
for (var c = 0; c < classes.length; c++)
if ( !contains('.'+classes[c], selectorTextArr) )
selectorTextArr.push( '.'+classes[c] );
}
// Extract CSS Rules
var extractedCSSText = "";
for (var i = 0; i < document.styleSheets.length; i++) {
var s = document.styleSheets[i];
try {
if(!s.cssRules) continue;
} catch( e ) {
if(e.name !== 'SecurityError') throw e; // for Firefox
continue;
}
var cssRules = s.cssRules;
for (var r = 0; r < cssRules.length; r++) {
if ( contains( cssRules[r].selectorText, selectorTextArr ) )
extractedCSSText += cssRules[r].cssText;
}
}
return extractedCSSText;
function contains(str,arr) {
return arr.indexOf( str ) === -1 ? false : true;
}
}
function appendCSS( cssText, element ) {
var styleElement = document.createElement("style");
styleElement.setAttribute("type","text/css");
styleElement.innerHTML = cssText;
var refNode = element.hasChildNodes() ? element.children[0] : null;
element.insertBefore( styleElement, refNode );
}
}
function svgString2Image( svgString, width, height, format, callback ) {
var format = format ? format : 'png';
var imgsrc = 'data:image/svg+xml;base64,'+ btoa( unescape( encodeURIComponent( svgString ) ) ); // Convert SVG string to data URL
var canvas = document.createElement("canvas");
var context = canvas.getContext("2d");
canvas.width = width;
canvas.height = height;
var image = new Image();
console.log("in s2" + image);
image.onload = function() {
context.clearRect ( 0, 0, width, height );
context.drawImage(image, 0, 0, width, height);
canvas.toBlob( function(blob) {
var filesize = Math.round( blob.length/1024 ) + ' KB';
if ( callback ) callback( blob, filesize );
});
console.log("in s2" + filesize);
};
image.src = imgsrc;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Species Chord Diagram</title>
<style>
@import url(style.css);
#circle circle {
fill: none;
pointer-events: all;
}
.group path {
fill-opacity: 1;
stroke: #000;
stroke-width: .25px;
}
path.chord {
stroke: #000;
stroke-width: .25px;
fill-opacity: .45;
stroke-opacity: .8;
}
#circle:hover path.fade {
display: none;
}
</style>
</head>
<!-- <p>Built with <a href="http://d3js.org/">d3.js</a>. -->
<body>
<div>
<button id='saveButton'>Export my D3 visualization to PNG</button>
</div>
<div id="crowbar-workspace">
</div>
</body>
<script src="http://d3js.org/d3.v2.min.js?2.8.1"></script>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<script src="https://cdn.rawgit.com/eligrey/canvas-toBlob.js/f1a01896135ab378aa5c0118eadd81da55e698d8/canvas-toBlob.js"></script>
<script src="https://cdn.rawgit.com/eligrey/FileSaver.js/e9d941381475b5df8b7d7691013401e171014e89/FileSaver.min.js"></script>
<!-- <script src="imagesave.js"></script> -->
<script type="text/javascript">
var width = 1200,
height = $(window).height()-20,
outerRadius = Math.min(width, height) / 2 - 80,
innerRadius = outerRadius - 24;
var formatPercent = d3.format(".3f");
var arc = d3.svg.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius);
var layout = d3.layout.chord()
.padding(.045)
.sortSubgroups(d3.descending)
.sortChords(d3.ascending);
var path = d3.svg.chord()
.radius(innerRadius-5);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("id", "circle")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
svg.append("circle")
.attr("r", outerRadius);
var cities = ["X","Y","Z","Dummy","C","B","A","Dummy"];
// var matrix = [
// [0,1,0,0,1,1,1,0], //X
// [1,0,0,0,1,1,1,0], //Y
// [0,0,0,0,1,1,1,0], //Z
// [0,0,1,0,1,0,0,1], //Dummy stroke
// [1,1,1,0,0,0,0,0], //C
// [1,1,1,0,0,0,0,0], //B
// [1,1,1,0,0,0,0,0], //A
// [0,0,0,1,0,0,0,0] //Dummy stroke
// ];
var matrix = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.25,0,0,2,0,0.5,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1.666667,0.25,0,0,0,0,0,0,2,0,0,1,2.5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0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0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1.5,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,-0.6666667,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
]
d3.csv("species.csv", function(cities) {
d3.json("matrix2.json", function(matrix2) {
// Compute the chord layout.
layout.matrix(matrix);
// Add a group per neighborhood.
var group = svg.selectAll(".group")
.data(layout.groups)
.enter().append("g")
.attr("class", "group")
.on("mouseover", mouseover);
// Add a mouseover title.
group.append("title").text(function(d, i) {
return cities[i].name + ", Total Value:" + formatPercent(d.value)
});
// Add the group arc.
var groupPath = group.append("path")
.attr("id", function(d, i) { return "group" + i; })
.attr("d", arc)
.style("fill", function(d, i) { return cities[i].color; });
// Add a text label.
var groupText = group.append("text")
//.attr("x", function(d, i){console.log( groupPath[0][i].getTotalLength()/2);}) //6
.attr("dy", 15)
// groupText.append("textPath")
// .attr("xlink:href", function(d, i) { return "#group" + i; })
// .text(function(d, i) { return cities[i].name; });
// Remove the labels that don't fit. :(
// groupText.filter(function(d, i) { return groupPath[0][i].getTotalLength() / 2 - 30 < this.getComputedTextLength(); })
// .remove();
group.append("svg:text")
.each(function(d,i) { d.angle = (d.startAngle + d.endAngle) / 2; })
.attr("dy", ".35em")
.attr("text-anchor", function(d) { return d.angle > Math.PI ? "end" : null; })
.attr("transform", function(d) {
return "rotate(" + (d.angle * 180 / Math.PI - 90) + ")"
+ "translate(" + (innerRadius + 30) + ")"
+ (d.angle > Math.PI ? "rotate(180)" : "");
})
.text(function(d,i) { return cities[i].name; })
.style("fill", function(d, i) { return cities[i].color; })
.attr("shape-rendering","crispEdges")
.attr("font-family","cursive")
.attr("font-size","15px");
// Add the chords.
var chord = svg.selectAll(".chord")
.data(layout.chords)
.enter().append("path")
.attr("class", "chord")
.style("fill", function(d,i) {
//console.log(d);
//console.log(matrix2[d.source.index][d.target.index]);
if(matrix2[d.source.index][d.target.index] < 0) return 'red';
else return 'green';
//return cities[d.source.index].color;
})
.attr("d", path);
//Add an elaborate mouseover title for each chord.
chord.append("title").text(function(d) {
return cities[d.source.index].name
+ " → " + cities[d.target.index].name
+ ": " + formatPercent(d.source.value)
+ "\n" + cities[d.target.index].name
+ " → " + cities[d.source.index].name
+ ": " + formatPercent(d.target.value);
});
function mouseover(d, i) {
chord.classed("fade", function(p) {
return p.source.index != i
&& p.target.index != i;
});
}
//Append Legend
var teams = [
"Invasive Marines" ,
"Native Marines",
"Terrestrial Marines",
"Positive Chords",
"Negative Chords",
];
var colors = [ "#DF7C00", "#BFAE40", "#083E77","green","red"];
var color = d3.scale.ordinal() //custom color scale for teams
.range(colors)
.domain(teams);
svg.append("rect")
.attr("x",-width/2)
.attr("y",-height/2)
.attr("width",150)
.attr("height",120)
.attr("style","fill-opacity:0;stroke:black;stroke-width:1.5px");
var legend = svg.append("g")
.attr("class", "legend")
.attr("width", 150)
.attr("height", 150)
.attr("transform", "translate(-"+ ((width/2)-5) + ",-"+ ((height/2)-10) +" )");
var gs = legend.selectAll("g.keybox")
.data(teams).enter().append("g")
.attr("class", "keybox")
.attr("width", 80)
.attr("height", 15);
gs.append("text")
.attr("class","keybox").attr("x",function(d,i){
if(i<5){return 15;}
else{return 90;}})
.attr("y",function(d,i){
if(i<5){return i*20 +9;}
else{return (i-4)*20 +9;}})
.text(function(d,i){
return teams[i];})
;
gs.append("rect")
.attr("x",function(d,i){
if(i<5){return 0;}
else{return 75;}})
.attr("y",function(d,i){
if(i<5){return i*20;}
else{return (i-4) * 20;}})
.attr("width",10)
.attr("height",10)
.style("fill",function(d, i){
return colors[i];});
});
});
d3.select("#saveButton").on("click", download_png)
function download_png () {
// this is a shitty hack that should probably be embedded in the
// svg_crowbar function
var svg_el = d3.select("svg")
.attr("version", 1.1)
.attr("xmlns", "http://www.w3.org/2000/svg")
.node();
// this is the main thing that does the work
svg_crowbar(svg.node(), {
filename: "export-me.png",
width: 100,
height: 100,
crowbar_el: d3.select("#crowbar-workspace").node(),
})
}
</script>
</html>
[
[
0,
0,
0,
1,
0,
0,
1,
0,
1,
1,
0,
1,
0,
1,
1,
1,
0,
1,
0,
0
],
[
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
1,
1,
0,
0
],
[
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
1,
0,
0,
0,
0,
0,
1,
1,
0
],
[
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0
],
[
0,
0,
0,
0,
0,
1,
0,
0,
0,
1,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0
],
[
0,
0,
0,
1,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
1,
1,
0,
0
],
[
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0
],
[
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
[
1,
0,
0,
1,
0,
0,
1,
0,
0,
1,
0,
1,
0,
1,
1,
1,
0,
1,
0,
0
],
[
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0
],
[
0,
0,
1,
1,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
1,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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1.5,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,-0.6666667,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
]
name color
bacteria #DF7C00
Lichens/bryophytes #DF7C00
algae #DF7C00
plant #DF7C00
seagrass #DF7C00
sponge #DF7C00
cnidarian #DF7C00
annelid #DF7C00
echinoderm #DF7C00
mollusk #DF7C00
crustacean #DF7C00
insect #DF7C00
bird #DF7C00
fish #DF7C00
multiple #DF7C00
algae #BFAE40
plant #BFAE40
seagrass #BFAE40
bryozoan #BFAE40
cnidarian #BFAE40
annelid #BFAE40
echinoderm #BFAE40
mollusk #BFAE40
crustacean #BFAE40
insect #BFAE40
tunicate #BFAE40
fish #BFAE40
mammal #BFAE40
bacteria #083E77
Lichens/bryophytes #083E77
algae #083E77
plant #083E77
seagrass #083E77
sponge #083E77
cnidarian #083E77
annelid #083E77
echinoderm #083E77
mollusk #083E77
crustacean #083E77
insect #083E77
bird #083E77
fish #083E77
multiple #083E77
/* From http://bost.ocks.org/mike/style.css?20120427 */
@import url(http://fonts.googleapis.com/css?family=PT+Serif|PT+Serif:b|PT+Serif:i|PT+Sans|PT+Sans:b);
html {
min-width: 1040px;
}
body {
background: #fcfcfa;
color: #333;
font-family: "PT Serif", serif;
margin: 1em auto 4em auto;
position: relative;
width: 960px;
}
header,
footer,
h1,
h2,
h3,
h4,
aside {
color: #000;
font-family: "PT Sans", sans-serif;
}
h1 {
font-size: 64px;
font-weight: 300;
letter-spacing: -2px;
margin: .3em 0 .1em 0;
}
h2 {
margin-top: 2em;
}
h1, h2 {
text-rendering: optimizeLegibility;
}
h2 a {
color: #ccc;
left: -20px;
position: absolute;
width: 740px;
}
footer {
font-size: small;
margin-top: 8em;
}
header aside {
margin-top: 88px;
}
header aside,
footer aside {
color: #636363;
text-align: right;
}
aside {
font-size: small;
right: 0;
position: absolute;
width: 180px;
}
.attribution {
font-size: small;
margin-bottom: 2em;
}
body > p, li > p {
line-height: 1.5em;
}
body > p {
width: 720px;
}
body > blockquote {
width: 640px;
}
blockquote q {
display: block;
font-style: oblique;
}
li {
width: 680px;
}
a {
color: steelblue;
}
a:not(:hover) {
text-decoration: none;
}
pre, code, textarea {
font-family: "Menlo", monospace;
}
code {
line-height: 1em;
}
textarea {
font-size: 100%;
}
body > pre {
border-left: solid 2px #ccc;
padding-left: 18px;
margin: 2em 0 2em -20px;
}
.html .value,
.javascript .string,
.javascript .regexp {
color: #756bb1;
}
.html .tag,
.css .tag,
.javascript .keyword {
color: #3182bd;
}
.comment {
color: #636363;
}
.html .doctype,
.javascript .number {
color: #31a354;
}
.html .attribute,
.css .attribute,
.javascript .class,
.javascript .special {
color: #e6550d;
}
svg {
font: 10px sans-serif;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
sup, sub {
line-height: 0;
}
q:before {
content: "“";
}
q:after {
content: "”";
}
blockquote:before {
position: absolute;
left: 2em;
}
blockquote:after {
position: absolute;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment