source: Dev/trunk/d3/examples/line/line.js @ 76

Last change on this file since 76 was 76, checked in by fpvanagthoven, 14 years ago

d3

File size: 1.5 KB
Line 
1var data = d3.range(20).map(function(i) {
2  return {x: i / 19, y: (Math.sin(i / 3) + 1) / 2};
3});
4
5var w = 450,
6    h = 275,
7    p = 20,
8    x = d3.scale.linear().domain([0, 1]).range([0, w]),
9    y = d3.scale.linear().domain([0, 1]).range([h, 0]);
10
11var vis = d3.select("body")
12    .data([data])
13  .append("svg:svg")
14    .attr("width", w + p * 2)
15    .attr("height", h + p * 2)
16  .append("svg:g")
17    .attr("transform", "translate(" + p + "," + p + ")");
18
19var rules = vis.selectAll("g.rule")
20    .data(x.ticks(10))
21  .enter().append("svg:g")
22    .attr("class", "rule");
23
24rules.append("svg:line")
25    .attr("x1", x)
26    .attr("x2", x)
27    .attr("y1", 0)
28    .attr("y2", h - 1);
29
30rules.append("svg:line")
31    .attr("class", function(d) { return d ? null : "axis"; })
32    .attr("y1", y)
33    .attr("y2", y)
34    .attr("x1", 0)
35    .attr("x2", w + 1);
36
37rules.append("svg:text")
38    .attr("x", x)
39    .attr("y", h + 3)
40    .attr("dy", ".71em")
41    .attr("text-anchor", "middle")
42    .text(x.tickFormat(10));
43
44rules.append("svg:text")
45    .attr("y", y)
46    .attr("x", -3)
47    .attr("dy", ".35em")
48    .attr("text-anchor", "end")
49    .text(y.tickFormat(10));
50
51vis.append("svg:path")
52    .attr("class", "line")
53    .attr("d", d3.svg.line()
54    .x(function(d) { return x(d.x); })
55    .y(function(d) { return y(d.y); }));
56
57vis.selectAll("circle.line")
58    .data(data)
59  .enter().append("svg:circle")
60    .attr("class", "line")
61    .attr("cx", function(d) { return x(d.x); })
62    .attr("cy", function(d) { return y(d.y); })
63    .attr("r", 3.5);
Note: See TracBrowser for help on using the repository browser.