1 | <!DOCTYPE html> |
---|
2 | <html> |
---|
3 | <head> |
---|
4 | <title>Dot Plot</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 | path.dot { |
---|
13 | fill: white; |
---|
14 | stroke-width: 1.5px; |
---|
15 | } |
---|
16 | |
---|
17 | rect { |
---|
18 | fill: none; |
---|
19 | stroke: black; |
---|
20 | shape-rendering: crispEdges; |
---|
21 | } |
---|
22 | |
---|
23 | .x line, .y line { |
---|
24 | stroke: #ccc; |
---|
25 | shape-rendering: crispEdges; |
---|
26 | } |
---|
27 | |
---|
28 | </style> |
---|
29 | </head> |
---|
30 | <body> |
---|
31 | <script type="text/javascript"> |
---|
32 | |
---|
33 | var data = d3.range(100).map(function(i) { |
---|
34 | return {x: i / 99, y: Math.random()}; |
---|
35 | }); |
---|
36 | |
---|
37 | var w = 450, |
---|
38 | h = 450, |
---|
39 | p = 20, |
---|
40 | x = d3.scale.linear().range([0, w]), |
---|
41 | y = d3.scale.linear().range([h, 0]), |
---|
42 | symbol = d3.scale.ordinal().range(d3.svg.symbolTypes), |
---|
43 | color = d3.scale.category10(); |
---|
44 | |
---|
45 | var vis = d3.select("body") |
---|
46 | .append("svg:svg") |
---|
47 | .attr("width", w + p * 2) |
---|
48 | .attr("height", h + p * 2) |
---|
49 | .append("svg:g") |
---|
50 | .attr("transform", "translate(" + p + "," + p + ")"); |
---|
51 | |
---|
52 | var xrule = vis.selectAll("g.x") |
---|
53 | .data(x.ticks(10)) |
---|
54 | .enter().append("svg:g") |
---|
55 | .attr("class", "x"); |
---|
56 | |
---|
57 | xrule.append("svg:line") |
---|
58 | .attr("x1", x) |
---|
59 | .attr("x2", x) |
---|
60 | .attr("y1", 0) |
---|
61 | .attr("y2", h); |
---|
62 | |
---|
63 | xrule.append("svg:text") |
---|
64 | .attr("x", x) |
---|
65 | .attr("y", h + 3) |
---|
66 | .attr("dy", ".71em") |
---|
67 | .attr("text-anchor", "middle") |
---|
68 | .text(x.tickFormat(10)); |
---|
69 | |
---|
70 | var yrule = vis.selectAll("g.y") |
---|
71 | .data(y.ticks(10)) |
---|
72 | .enter().append("svg:g") |
---|
73 | .attr("class", "y"); |
---|
74 | |
---|
75 | yrule.append("svg:line") |
---|
76 | .attr("x1", 0) |
---|
77 | .attr("x2", w) |
---|
78 | .attr("y1", y) |
---|
79 | .attr("y2", y); |
---|
80 | |
---|
81 | yrule.append("svg:text") |
---|
82 | .attr("x", -3) |
---|
83 | .attr("y", y) |
---|
84 | .attr("dy", ".35em") |
---|
85 | .attr("text-anchor", "end") |
---|
86 | .text(y.tickFormat(10)); |
---|
87 | |
---|
88 | vis.append("svg:rect") |
---|
89 | .attr("width", w) |
---|
90 | .attr("height", h); |
---|
91 | |
---|
92 | vis.selectAll("path.dot") |
---|
93 | .data(data) |
---|
94 | .enter().append("svg:path") |
---|
95 | .attr("class", "dot") |
---|
96 | .attr("stroke", function(d, i) { return color(i); }) |
---|
97 | .attr("transform", function(d) { return "translate(" + x(d.x) + "," + y(d.y) + ")"; }) |
---|
98 | .attr("d", d3.svg.symbol() |
---|
99 | .type(function(d, i) { return symbol(i); })); |
---|
100 | |
---|
101 | </script> |
---|
102 | </body> |
---|
103 | </html> |
---|