[ Launch: x and y axis ] 631af6058eb7639f7e99 by rickdg
[ Launch: helping pessimism (lol) ] 5940269 by gelicia
-
-
Save rickdg/631af6058eb7639f7e99 to your computer and use it in GitHub Desktop.
x and y axis
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
{"description":"x and y axis","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"thumbnail":"http://i.imgur.com/eNHpEVb.png","ajax-caching":true} |
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
//Width and height | |
var w = 500; | |
var h = 300; | |
var padding = 30; | |
var now = d3.time.hour.utc(new Date()); | |
//Static dataset | |
var dataset = [ | |
{x: d3.time.hour.utc.offset(now, -5), y1: 1, y2: 10}, | |
{x: d3.time.hour.utc.offset(now, -4), y1: 2, y2: 20}, | |
{x: d3.time.hour.utc.offset(now, -3), y1: 3, y2: 30}, | |
{x: d3.time.hour.utc.offset(now, -2), y1: 4, y2: 40}, | |
{x: d3.time.hour.utc.offset(now, -1), y1: 5, y2: 50}, | |
{x: now, y1: 6, y2: 60} | |
]; | |
var data = []; | |
dataset.forEach(function(d){ | |
var newObj = {}; | |
newObj.x = d.x; | |
newObj.y = d.y1; | |
newObj.type = 1; | |
data.push(newObj); | |
newObj = {}; | |
newObj.x = d.x; | |
newObj.y = d.y2; | |
newObj.type = 2; | |
data.push(newObj); | |
}); | |
console.log(data) | |
var xDomain = d3.extent(dataset, function(i) { return i.x; }); | |
//Create scale functions | |
var xScale = d3.time.scale() | |
.domain(xDomain) | |
.range([padding, w - padding * 2]); | |
var maxY = d3.max(dataset, function(i) { | |
return Math.max(i.y1, i.y2); | |
}); | |
var yScale = d3.scale.linear() | |
.domain([0, maxY]) | |
.range([h - padding, padding]); | |
//Define X axis | |
var xAxis = d3.svg.axis() | |
.scale(xScale) | |
.orient("bottom") | |
.ticks(5); | |
//Define Y axis | |
var yAxis = d3.svg.axis() | |
.scale(yScale) | |
.orient("left") | |
.ticks(5); | |
//Create SVG element | |
var svg = d3.select("svg") | |
.attr("width", w) | |
.attr("height", h); | |
//Create circles | |
svg.selectAll("circle") | |
.data(data) | |
.enter() | |
.append("circle") | |
.attr("cx", function(d) { | |
return xScale(d.x); | |
}) | |
.attr("cy", function(d) { | |
return yScale(d.y); | |
}) | |
.attr("class", function(d){ return (d.type == 1? "y1" : "y2"); }) | |
.attr("r", 2); | |
//Create X axis | |
svg.append("g") | |
.attr("class", "axis") | |
.attr("transform", "translate(0," + (h - padding) + ")") | |
.call(xAxis); | |
//Create Y axis | |
svg.append("g") | |
.attr("class", "axis") | |
.attr("transform", "translate(" + padding + ",0)") | |
.call(yAxis); |
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
.axis path, | |
.axis line { | |
fill: none; | |
stroke: black; | |
shape-rendering: crispEdges; | |
} | |
.axis text { | |
font-family: sans-serif; | |
font-size: 11px; | |
} | |
.y1 { fill: #FF0000;;} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment