Skip to content

Instantly share code, notes, and snippets.

@jpmarindiaz
Created March 8, 2016 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpmarindiaz/c056a3a54b0b059e6993 to your computer and use it in GitHub Desktop.
Save jpmarindiaz/c056a3a54b0b059e6993 to your computer and use it in GitHub Desktop.
fresh block
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
svg { width:100%; height: 100% }
</style>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see!
var svg = d3.select("body").append("svg").append("g")
var rectPalette = [
{ "x": 0, "y": 40, "width": 20,"height": 20, "color" : "#e8e8e8" },
{ "x": 20, "y": 40, "width": 20,"height": 20, "color" : "#e4acac"},
{ "x": 40, "y": 40, "width": 20,"height": 20, "color" : "#c85a5a"},
{ "x": 0, "y": 20, "width": 20,"height": 20, "color" : "#b0d5df" },
{ "x": 20, "y": 20, "width": 20,"height": 20, "color" : "#ad93a5"},
{ "x": 40, "y": 20, "width": 20,"height": 20, "color" : "#985356"},
{ "x": 0, "y": 0, "width": 20,"height": 20, "color" : "#64acbe" },
{ "x": 20, "y": 0, "width": 20,"height": 20, "color" : "#62718c"},
{ "x": 40, "y": 0, "width": 20,"height": 20, "color" : "#574249"}
];
d3.select("svg g").attr("transform", "translate(100,50)");
var rects = svg.selectAll("rects")
.data(rectPalette)
.enter()
.append("rect");
var rectAttributes = rects
.attr("x", function (d) { return d.x; })
.attr("y", function (d) { return d.y; })
.attr("width", function (d) { return d.width; })
.attr("height", function (d) { return d.height; })
.style("fill", function(d) { return d.color; });
//Create the Scale we will use for the Axis
var axisScale = d3.scale.ordinal()
.domain(["low", "high"])
.range([0, 60]);
//Create the Axis
var xAxis = d3.svg.axis().scale(axisScale)
var xAxisGroup = svg.append("g").call(xAxis).attr("transform", "translate(0,70)");
;
//Create the Axis
var yAxis = d3.svg.axis().scale(axisScale).orient("left")
var yAxisGroup = svg.append("g").call(yAxis).attr("transform", "translate(-10,0)")
.style("fill","none")
.style("stroke","black")
.style("stroke-width",1);
;
var legend = d3.select("svg g");
legend.append("text")
.attr("class", "x label")
.attr("text-anchor", "end")
.attr("x", 30)
.attr("y", 120)
.text("Variable 1");
legend.append("text")
.attr("class", "y label")
.attr("text-anchor", "end")
.attr("y", -80)
.attr("dy", ".75em")
.attr("transform", "rotate(-90)")
.text("Variable 2");
console.log("you are now rocking with d3", d3);
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment