|
1 <!DOCTYPE html> |
|
2 <html> |
|
3 <head> |
|
4 <title>Crimean War</title> |
|
5 <script type="text/javascript" src="../../d3.js"></script> |
|
6 <script type="text/javascript" src="../../d3.layout.js"></script> |
|
7 <script type="text/javascript" src="../../d3.time.js"></script> |
|
8 <script type="text/javascript" src="../../d3.csv.js"></script> |
|
9 <style type="text/css"> |
|
10 |
|
11 svg { |
|
12 width: 960px; |
|
13 height: 500px; |
|
14 font: 10px sans-serif; |
|
15 } |
|
16 |
|
17 .rule { |
|
18 shape-rendering: crispEdges; |
|
19 } |
|
20 |
|
21 path.line { |
|
22 fill: none; |
|
23 } |
|
24 |
|
25 </style> |
|
26 </head> |
|
27 <body> |
|
28 <script type="text/javascript"> |
|
29 |
|
30 var p = [20, 50, 30, 20], |
|
31 w = 960 - p[1] - p[3], |
|
32 h = 500 - p[0] - p[2], |
|
33 x = d3.time.scale().range([0, w]), |
|
34 y = d3.scale.linear().range([h, 0]), |
|
35 z = d3.scale.ordinal().range(["lightpink", "darkgray", "lightblue"]), |
|
36 parse = d3.time.format("%m/%Y").parse, |
|
37 format = d3.time.format("%b"); |
|
38 |
|
39 var svg = d3.select("body").append("svg:svg") |
|
40 .attr("width", w) |
|
41 .attr("height", h) |
|
42 .append("svg:g") |
|
43 .attr("transform", "translate(" + p[3] + "," + p[0] + ")"); |
|
44 |
|
45 d3.csv("crimea.csv", function(crimea) { |
|
46 |
|
47 // Transpose the data into layers by cause. |
|
48 var causes = d3.layout.stack()(["wounds", "other", "disease"].map(function(cause) { |
|
49 return crimea.map(function(d) { |
|
50 return {x: parse(d.date), y: +d[cause]}; |
|
51 }); |
|
52 })); |
|
53 |
|
54 // Compute the x-domain (by date) and y-domain (by top). |
|
55 x.domain([causes[0][0].x, causes[0][causes[0].length - 1].x]); |
|
56 y.domain([0, d3.max(causes[causes.length - 1], function(d) { return d.y0 + d.y; })]); |
|
57 |
|
58 // Add an area for each cause. |
|
59 svg.selectAll("path.area") |
|
60 .data(causes) |
|
61 .enter().append("svg:path") |
|
62 .attr("class", "area") |
|
63 .style("fill", function(d, i) { return z(i); }) |
|
64 .attr("d", d3.svg.area() |
|
65 .x(function(d) { return x(d.x); }) |
|
66 .y0(function(d) { return y(d.y0); }) |
|
67 .y1(function(d) { return y(d.y0 + d.y); })); |
|
68 |
|
69 // Add a line for each cause. |
|
70 svg.selectAll("path.line") |
|
71 .data(causes) |
|
72 .enter().append("svg:path") |
|
73 .attr("class", "line") |
|
74 .style("stroke", function(d, i) { return d3.rgb(z(i)).darker(); }) |
|
75 .attr("d", d3.svg.line() |
|
76 .x(function(d) { return x(d.x); }) |
|
77 .y(function(d) { return y(d.y0 + d.y); })); |
|
78 |
|
79 // Add a label per date. |
|
80 svg.selectAll("text") |
|
81 .data(x.ticks(12)) |
|
82 .enter().append("svg:text") |
|
83 .attr("x", x) |
|
84 .attr("y", h + 6) |
|
85 .attr("text-anchor", "middle") |
|
86 .attr("dy", ".71em") |
|
87 .text(x.tickFormat(12)); |
|
88 |
|
89 // Add y-axis rules. |
|
90 var rule = svg.selectAll("g.rule") |
|
91 .data(y.ticks(5)) |
|
92 .enter().append("svg:g") |
|
93 .attr("class", "rule") |
|
94 .attr("transform", function(d) { return "translate(0," + y(d) + ")"; }); |
|
95 |
|
96 rule.append("svg:line") |
|
97 .attr("x2", w) |
|
98 .style("stroke", function(d) { return d ? "#fff" : "#000"; }) |
|
99 .style("stroke-opacity", function(d) { return d ? .7 : null; }); |
|
100 |
|
101 rule.append("svg:text") |
|
102 .attr("x", w + 6) |
|
103 .attr("dy", ".35em") |
|
104 .text(d3.format(",d")); |
|
105 }); |
|
106 |
|
107 </script> |
|
108 </body> |
|
109 </html> |