Last active
March 12, 2018 03:14
-
-
Save navio/37d20567919a7d6b8f6cfc2e656fb0ac to your computer and use it in GitHub Desktop.
Example of D3
This file contains hidden or 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> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<body> </body> | |
</html> |
This file contains hidden or 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
var width = 700, | |
height = 100, | |
cellSize = 10; | |
var formatPercent = d3.format(".1%"); | |
var color = d3.scaleQuantize() | |
.domain([1, 11]) | |
.range(["#a50026", "#d73027", "#f46d43", "#fdae61", "#fee08b", "#ffffbf", "#d9ef8b", "#a6d96a", "#66bd63", "#1a9850", "#006837"]); | |
var svg = d3.select("body") | |
.selectAll("svg") | |
.data([2016]) | |
.enter().append("svg") | |
.attr("width", width) | |
.attr("height", height) | |
.append("g") | |
.attr("transform", "translate(" + ((width - cellSize * 53) / 2) + "," + (height - cellSize * 7 - 1) + ")"); | |
svg.append("text") | |
.attr("transform", "translate(-6," + cellSize * 3.5 + ")rotate(-90)") | |
.attr("font-family", "sans-serif") | |
.attr("font-size", 10) | |
.attr("text-anchor", "middle") | |
.text(function(d) { return d; }); | |
var rect = svg.append("g") | |
.attr("fill", "none") | |
.attr("stroke", "#ccc") | |
.selectAll("rect") | |
.data(function(d) { return d3.timeDays(new Date(d, 0, ), new Date(d + 1, 0, 1)); }) | |
.enter().append("rect") | |
.attr("width", cellSize) | |
.attr("height", cellSize) | |
.attr("x", function(d) { return d3.timeWeek.count(d3.timeYear(d), d) * cellSize; }) | |
.attr("y", function(d) { return d.getDay() * cellSize; }) | |
.datum(d3.timeFormat("%Y-%m-%d")); | |
svg.append("g") | |
.attr("fill", "none") | |
.attr("stroke", "#000") | |
.selectAll("path") | |
.data(function(d) { return d3.timeMonths(new Date(d, 0, 1), new Date(d + 1, 0, 1)); }) | |
.enter().append("path") | |
.attr("d", pathMonth); | |
// d3.csv("dji.csv", function(error, csv) { | |
// if (error) throw error; | |
// var data = d3.nest() | |
// .key(function(d) { return d.Date; }) | |
// .rollup(function(d) { return (d[0].Close - d[0].Open) / d[0].Open; }) | |
// .object(csv); | |
// rect.filter(function(d) { return d in data; }) | |
// .attr("fill", function(d) { return color(data[d]); }) | |
// .append("title") | |
// .text(function(d) { return d + ": " + formatPercent(data[d]); }); | |
// }); | |
d3.json('https://api.nextbigsound.com/events/v1/entity/356?start=16690&end=17168&access_token=8c089170d31ea3b11f1ea65dbfc8ea46',function(err,data){ | |
if (err) throw err; | |
var progress = {}; | |
Object.keys(data).forEach(function(day){ | |
var current = new Date(data[day].date_short); | |
var busy = Object.keys(data[day]['event_types']).length; | |
progress[current.toISOString().slice(0,10)] = {day:current,busy:busy}; | |
}); | |
rect.filter(function(d) { return d in progress; }) | |
.attr("fill", function(d) { console.log(progress[d]); return color(progress[d].busy); }) | |
.append("title") | |
// .text(function(d) { return d + ": " + formatPercent(data[d]); }); | |
}) | |
function pathMonth(t0) { | |
var t1 = new Date(t0.getFullYear(), t0.getMonth() + 1, 0), | |
d0 = t0.getDay(), w0 = d3.timeWeek.count(d3.timeYear(t0), t0), | |
d1 = t1.getDay(), w1 = d3.timeWeek.count(d3.timeYear(t1), t1); | |
return "M" + (w0 + 1) * cellSize + "," + d0 * cellSize | |
+ "H" + w0 * cellSize + "V" + 7 * cellSize | |
+ "H" + w1 * cellSize + "V" + (d1 + 1) * cellSize | |
+ "H" + (w1 + 1) * cellSize + "V" + 0 | |
+ "H" + (w0 + 1) * cellSize + "Z"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment