Skip to content

Instantly share code, notes, and snippets.

@bunkat
Forked from adamgreenhall/index.html
Created May 4, 2012 16:25
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 bunkat/2595899 to your computer and use it in GitHub Desktop.
Save bunkat/2595899 to your computer and use it in GitHub Desktop.
d3 time with custom range (for simple time scatter plot)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang='en'>
<head>
<title>The d3 test</title>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.layout.js"></script>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.time.js"></script>
</head>
<body>
<script type="text/javascript" src="scatterchart.js"></script>
<h1>here's the chart</h1>
<div class='content'>
<!-- /the chart goes here -->
</div>
</body>
</html>
var chart_axis_margin, chart_height, chart_width, data, format, g, t, times, times_raw, vis, x, y;
data = [246, 245, 241, 240.5];
times_raw = ["Aug-3", "Aug-4", "Aug-5", "Aug-6"];
format = d3.time.format("%b-%d");
times = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = times_raw.length; _i < _len; _i++) {
t = times_raw[_i];
_results.push(format.parse(t));
}
return _results;
})();
chart_width = 640;
chart_height = 240;
chart_axis_margin = 20;
x = d3.time.scale().domain(times).range([0 + chart_axis_margin, chart_width - chart_axis_margin]);
y = d3.scale.linear().domain([200, 255]).range([0 + chart_axis_margin, chart_height - chart_axis_margin]);
vis = d3.select(".content").append("svg:svg").attr("class", "chart").attr("width", chart_width).attr("height", chart_height);
g = vis.append("svg:g").attr("transform", 'translate(0,' + chart_height + ')');
g.selectAll("scatter-dots").data(data).enter().append("svg:circle").attr("cy", function(d) {
return flipy(y(d));
}).attr("cx", function(d, i) {
return x(i);
}).attr("r", 2).style("opacity", 0.6);
data = [246, 245, 241, 240.5]
times_raw = ["Aug-3", "Aug-4", "Aug-5", "Aug-6"]
format = d3.time.format("%b-%d")
times = (format.parse(t) for t in times_raw)
chart_width=640
chart_height=240
chart_axis_margin=20
x = d3.time.scale()
.domain(times)
.range([ 0 + chart_axis_margin, chart_width - chart_axis_margin ])
y = d3.scale.linear()
.domain([ 200, 255 ])
.range([ 0 + chart_axis_margin, chart_height - chart_axis_margin ])
vis = d3.select(".content")
.append("svg:svg")
.attr("class", "chart")
.attr("width", chart_width)
.attr("height", chart_height)
g = vis.append("svg:g")
.attr("transform", 'translate(0,'+chart_height+')') #flip the graph over the yaxis
g.selectAll("scatter-dots")
.data(data)
.enter().append("svg:circle")
.attr("cy", (d) -> flipy(y(d)) )
.attr("cx", (d,i) -> x(i) )
.attr("r", 2)
.style("opacity", 0.6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment