Last active
December 19, 2015 00:59
-
-
Save phobson/5872894 to your computer and use it in GitHub Desktop.
Attempting to make a status widget (DNA chart) in 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> | |
<meta charset="utf-8"> | |
<body> | |
<script src="http://d3js.org/d3.v3.js"></script> | |
<script> | |
// basic dimensions | |
var widgetsize = 100; | |
var margin = {top: 10, right: 10, bottom: 10, left: 10} | |
var width = widgetsize - margin.left - margin.right; | |
var height = widgetsize - margin.top - margin.bottom; | |
// date parser | |
var parseDate = d3.time.format("%Y-%m-%dT%H:%M:%SZ").parse; | |
// SVG container | |
var svgContainer = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
// fake data | |
var response = { | |
Items: [ | |
{timeValue: "2013-05-09T00:00:00Z", status: 0, xvalue: 10}, | |
{timeValue: "2013-05-08T23:00:00Z", status: 0, xvalue: 20}, | |
{timeValue: "2013-05-08T22:00:00Z", status: 0, xvalue: 30}, | |
{timeValue: "2013-05-08T21:00:00Z", status: 1, xvalue: 40}, | |
{timeValue: "2013-05-08T20:00:00Z", status: 1, xvalue: 50}, | |
{timeValue: "2013-05-08T19:00:00Z", status: 0, xvalue: 60} | |
], | |
NextPageLink: "fakeurl", | |
Count: 6 | |
}; | |
// function to return the color based on the status | |
function statusColor(status) { | |
var colors = { | |
0: 'firebrick', | |
1: 'forestgreen' | |
}; | |
return colors[status]; | |
}; | |
var xscale = d3.time.scale() | |
.range([0, width]) | |
.domain([0, 100]); | |
//.domain([parseDate("2013-05-08T00:00:00Z"), parseDate("2013-05-09T00:00:00Z")]); | |
var yscale = d3.scale.linear() | |
.range([0, 30]); | |
var dna = svgContainer.selectAll("rect") | |
.data(response.Items) | |
.enter() | |
.append("rect"); | |
dna.attr("x", function(d, i) { | |
//return parseDate(d.timeValue) - parseDate("2013-05-09T00:00:00Z"); | |
return d.xvalue; | |
}) | |
.attr("y", 10) | |
.attr("width", 10) | |
.attr("height", 30) | |
.attr("fill", function(d, i) { | |
return statusColor(d.status) | |
}); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment