|
1 <!DOCTYPE html> |
|
2 <html> |
|
3 <head> |
|
4 <title>Area Chart</title> |
|
5 <script type="text/javascript" src="../../d3.js"></script> |
|
6 <style type="text/css"> |
|
7 |
|
8 body { |
|
9 font: 10px sans-serif; |
|
10 } |
|
11 |
|
12 .rule line { |
|
13 stroke: #eee; |
|
14 shape-rendering: crispEdges; |
|
15 } |
|
16 |
|
17 .rule line.axis { |
|
18 stroke: #000; |
|
19 } |
|
20 |
|
21 .area { |
|
22 fill: lightsteelblue; |
|
23 } |
|
24 |
|
25 .line, circle.area { |
|
26 fill: none; |
|
27 stroke: steelblue; |
|
28 stroke-width: 1.5px; |
|
29 } |
|
30 |
|
31 circle.area { |
|
32 fill: #fff; |
|
33 } |
|
34 |
|
35 </style> |
|
36 </head> |
|
37 <body> |
|
38 <script type="text/javascript"> |
|
39 |
|
40 var data = d3.range(20).map(function(i) { |
|
41 return {x: i / 19, y: (Math.sin(i / 3) + 1) / 2}; |
|
42 }); |
|
43 |
|
44 var w = 450, |
|
45 h = 275, |
|
46 p = 20, |
|
47 x = d3.scale.linear().domain([0, 1]).range([0, w]), |
|
48 y = d3.scale.linear().domain([0, 1]).range([h, 0]); |
|
49 |
|
50 var vis = d3.select("body") |
|
51 .append("svg:svg") |
|
52 .data([data]) |
|
53 .attr("width", w + p * 2) |
|
54 .attr("height", h + p * 2) |
|
55 .append("svg:g") |
|
56 .attr("transform", "translate(" + p + "," + p + ")"); |
|
57 |
|
58 var rules = vis.selectAll("g.rule") |
|
59 .data(x.ticks(10)) |
|
60 .enter().append("svg:g") |
|
61 .attr("class", "rule"); |
|
62 |
|
63 rules.append("svg:line") |
|
64 .attr("x1", x) |
|
65 .attr("x2", x) |
|
66 .attr("y1", 0) |
|
67 .attr("y2", h - 1); |
|
68 |
|
69 rules.append("svg:line") |
|
70 .attr("class", function(d) { return d ? null : "axis"; }) |
|
71 .attr("y1", y) |
|
72 .attr("y2", y) |
|
73 .attr("x1", 0) |
|
74 .attr("x2", w + 1); |
|
75 |
|
76 rules.append("svg:text") |
|
77 .attr("x", x) |
|
78 .attr("y", h + 3) |
|
79 .attr("dy", ".71em") |
|
80 .attr("text-anchor", "middle") |
|
81 .text(x.tickFormat(10)); |
|
82 |
|
83 rules.append("svg:text") |
|
84 .attr("y", y) |
|
85 .attr("x", -3) |
|
86 .attr("dy", ".35em") |
|
87 .attr("text-anchor", "end") |
|
88 .text(y.tickFormat(10)); |
|
89 |
|
90 vis.append("svg:path") |
|
91 .attr("class", "area") |
|
92 .attr("d", d3.svg.area() |
|
93 .x(function(d) { return x(d.x); }) |
|
94 .y0(h - 1) |
|
95 .y1(function(d) { return y(d.y); })); |
|
96 |
|
97 vis.append("svg:path") |
|
98 .attr("class", "line") |
|
99 .attr("d", d3.svg.line() |
|
100 .x(function(d) { return x(d.x); }) |
|
101 .y(function(d) { return y(d.y); })); |
|
102 |
|
103 vis.selectAll("circle.area") |
|
104 .data(data) |
|
105 .enter().append("svg:circle") |
|
106 .attr("class", "area") |
|
107 .attr("cx", function(d) { return x(d.x); }) |
|
108 .attr("cy", function(d) { return y(d.y); }) |
|
109 .attr("r", 3.5); |
|
110 |
|
111 </script> |
|
112 </body> |
|
113 </html> |