Created
January 29, 2016 22:32
-
-
Save hunminkoh/c820a9f4f1662aa143af to your computer and use it in GitHub Desktop.
쌀미소슬금 기본 패턴
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> | |
<body> | |
<script src="http://d3js.org/d3.v3.js"></script> | |
<script type="text/javascript"> | |
var angle = Math.PI/3.0; | |
var tWidth = 25.0; | |
var tHeight = 25.0*Math.tan(angle); | |
var tMargin = 5.0; | |
var pMargin = 10.0; | |
var nMargin = 10.0; | |
var vis = d3.select("body").append("svg") | |
.attr("width", 1000) | |
.attr("height", 667), | |
scaleX = d3.scale.linear() | |
.domain([-25,25]) | |
.range([250,750]), | |
scaleY = d3.scale.linear() | |
.domain([-25,25]) | |
.range([750,250]); | |
//color = d3.scale.category10(); | |
var arrayOfPolygons = [{ | |
"name": "polygon 1", | |
"points":[ | |
{"x":-tWidth, "y":0.0}, | |
{"x":0.0,"y":tHeight}, | |
{"x":tWidth,"y":0.0} | |
], | |
"color" : "pink" | |
}, | |
{ | |
"name": "polygon 2", | |
"points":[ | |
{"x":-tWidth+2*tMargin, "y":tMargin}, | |
{"x":0.0,"y":tHeight-2.0*tMargin}, | |
{"x":tWidth-2.0*tMargin,"y":tMargin} | |
], | |
"color" : "red" | |
}, | |
{ | |
"name": "polygon 3", | |
"points":[ | |
{"x":-tWidth, "y":0.0}, | |
{"x":-tWidth+pMargin/Math.tan(angle),"y":pMargin}, | |
{"x":tWidth-pMargin/Math.tan(angle),"y":pMargin}, | |
{"x":tWidth,"y":0.0} | |
], | |
"color" : "blue" | |
}, | |
{ | |
"name": "polygon 4", | |
"points":[ | |
{"x":0.0,"y":tHeight-2.0*nMargin}, | |
{"x":-(tHeight-pMargin-2.0*nMargin)/Math.tan(angle),"y":pMargin}, | |
{"x":-tWidth+nMargin/Math.sin(angle)+2*pMargin/Math.tan(angle), "y":0}, | |
{"x":tWidth-nMargin/Math.sin(angle),"y":0} | |
], | |
"color" : "green" | |
} | |
]; | |
vis.selectAll("polygon") | |
.data(arrayOfPolygons) | |
.enter().append("polygon") | |
.attr("points",function(d) { | |
return d.points.map(function(d) { return [scaleX(d.x),scaleY(d.y)].join(","); }).join(" ");}) | |
.attr("fill", function(d){return d.color}) | |
.attr("stroke","#666") | |
.attr("stroke-width",2); | |
// add legend | |
var legend = vis.append("g") | |
.attr("class", "legend") | |
//.attr("x", w - 65) | |
//.attr("y", 50) | |
.attr("height", 100) | |
.attr("width", 100) | |
.attr('transform', 'translate(10,20)'); | |
legend | |
.selectAll('rect') | |
.data(arrayOfPolygons) | |
.enter() | |
.append("rect") | |
.attr("x", 40) | |
.attr("y", function(d, i){ return i * 20;}) | |
.attr("width", 10) | |
.attr("height", 10) | |
.style("fill", function(d) { return d.color;}); | |
legend.selectAll('text') | |
.data(arrayOfPolygons) | |
.enter() | |
.append("text") | |
.attr("x", 50) | |
.attr("y", function(d, i){ return i * 20 + 9;}) | |
.text(function(d) {return d.name}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment