Reputation: 37
I am trying to write a tutorial about d3 and I found couple websites that can help but with not enough details.
I have the following code that outputs a bar chart:
<html>
<head>
<div id="mainGraph">
</div>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<script type="text/javascript">
var t = 1, // start time (seconds since epoch)
v = 0, // start value (subscribers)
data = d3.range(33).map(next); // starting dataset
function next() {
return {
time: ++t,
value: v = ~~Math.max(10, Math.min(80, Math.exp(t)))
};
}
setInterval(function() {
data.shift();
data.push(next());
}, 1500);
var w = 40,
h = 100;
var x = d3.scale.linear()
.domain([0, 1])
.range([0, w]);
var y = d3.scale.linear()
.domain([0, 100])
.rangeRound([0, h]);
var chart = d3.select("body")
.append("svg:svg")
.attr("class", "chart")
.attr("width", w * data.length + 10)
.attr("height", h);
chart.selectAll("rect")
.data(data)
.enter().append("svg:rect")
.attr("x", function(d, i) { return x(i) + 5; })
.attr("y", function(d) { return h - y(d.value) + 5; })
.attr("width", w)
.attr("height", function(d) { return y(d.value); });
chart.append("svg:line")
.attr("x1", 0)
.attr("x2", w * data.length)
.attr("y1", h + 5)
.attr("y2", h + 5)
.attr("stroke", "#000");
</script>
</body>
</html>
The first question is what does function()
the second question is when I try to change the set of data so i am trying to put v in function of t like v = Math.exp (t) this is not working and its giving me a black line only even though I changed the interval of max and min. Thank you.
Upvotes: 0
Views: 1080
Reputation: 8113
function()
as in :
setInterval(function() { ... });
is called a lambda function (or anonymus if you prefer). This means that the function has no name, and cannot be called outside it's scope. In this case, the setInterval
function requires a function as parameter, so we send in the full function definition instead of just a reference to it.
Upvotes: 2