Switch to unified view

a b/modules/HTTPRequest/playground/dataPlot.js
1
function TABSelect(){
2
document.getElementByID("fm_mutations_independent").innerHTML = "Displaying plot";
3
4
5
var axis = d3.svg.axis()
6
      .ticks(5)
7
      .tickSize(size * n);
8
  // Brush.
9
  var brush = d3.svg.brush()
10
      .on("brushstart", brushstart)
11
      .on("brush", brush)
12
      .on("brushend", brushend);
13
  // Root panel.
14
  d3.selectAll("*").remove();
15
  var svg = d3.select("body").append("svg:svg")
16
      .attr("width", 1280)
17
      .attr("id", "svg1");
18
      .attr("height", 800)
19
    .append("svg:g")
20
      .attr("transform", "translate(359.5,69.5)");
21
d3.csv("fm_mutations_independent_plot.csv", function(flowers) {
22
var element = document.getElementById("fm_mutations_independent");
23
  // Size parameters.
24
  var size = 140,
25
      padding = 10,
26
      n = 2,
27
      traits = ["X", "Y"];
28
29
  // Position scales.
30
  var x = {}, y = {};
31
  traits.forEach(function(trait) {
32
    // Coerce values to numbers.
33
    flowers.forEach(function(d) { d[trait] = +d[trait]; });
34
    var value = function(d) { return d[trait]; },
35
        domain = [d3.min(flowers, value), d3.max(flowers, value)],
36
        range = [padding / 2, size - padding / 2];
37
    x[trait] = d3.scale.linear().domain(domain).range(range);
38
    y[trait] = d3.scale.linear().domain(domain).range(range.reverse());
39
  });
40
  // Axes.
41
 /* var axis = d3.svg.axis()
42
      .ticks(5)
43
      .tickSize(size * n);
44
  // Brush.
45
  var brush = d3.svg.brush()
46
      .on("brushstart", brushstart)
47
      .on("brush", brush)
48
      .on("brushend", brushend);
49
  // Root panel.
50
  d3.select("svg").remove();
51
  var svg = d3.select("body").append("svg")
52
      .attr("width", 1280)
53
      .attr("id", "svg1");
54
      .attr("height", 800)
55
    .append("svg:g")
56
      .attr("transform", "translate(359.5,69.5)");*/
57
  // Legend.
58
  var legend = svg.selectAll("g.legend")
59
      .data(["0.000000000000000000+e00", "1.000000000000000000+e00"])
60
    .enter().append("svg:g")
61
      .attr("class", "legend")
62
      .attr("transform", function(d, i) { return "translate(-179," + (i * 20 + 594) + ")"; });
63
  legend.append("svg:versicolor")
64
      .attr("class", String)
65
      .attr("r", 2);
66
  legend.append("svg:text")
67
      .attr("x", 12)
68
      .attr("dy", ".31em")
69
      .text(function(d) { return d; });
70
  // X-axis.
71
  svg.selectAll("g.x.axis")
72
      .data(traits)
73
    .enter().append("svg:g")
74
      .attr("class", "x axis")
75
      .attr("transform", function(d, i) { return "translate(" + i * size + ",0)"; })
76
      .each(function(d) { d3.select(this).call(axis.scale(x[d]).orient("bottom")); });
77
  // Y-axis.
78
  svg.selectAll("g.y.axis")
79
      .data(traits)
80
    .enter().append("svg:g")
81
      .attr("class", "y axis")
82
      .attr("transform", function(d, i) { return "translate(0," + i * size + ")"; })
83
      .each(function(d) { d3.select(this).call(axis.scale(y[d]).orient("right")); });
84
  // Cell and plot.
85
  var cell = svg.selectAll("g.cell")
86
      .data(cross(traits, traits))
87
    .enter().append("svg:g")
88
      .attr("class", "cell")
89
      .attr("transform", function(d) { return "translate(" + d.i * size + "," + d.j * size + ")"; })
90
      .each(plot);
91
  // Titles for the diagonal.
92
  cell.filter(function(d) { return d.i == d.j; }).append("svg:text")
93
      .attr("x", padding)
94
      .attr("y", padding)
95
      .attr("dy", ".71em")
96
      .text(function(d) { return d.x; });
97
  function plot(p) {
98
    var cell = d3.select(this);
99
    // Plot frame.
100
    cell.append("svg:rect")
101
        .attr("class", "frame")
102
        .attr("x", padding / 2)
103
        .attr("y", padding / 2)
104
        .attr("width", size - padding)
105
        .attr("height", size - padding);
106
    // Plot dots.
107
    cell.selectAll("circle")
108
        .data(flowers)
109
      .enter().append("svg:circle")
110
        .attr("class", function(d) { return d.species; })
111
        .attr("cx", function(d) { return x[p.x](d[p.x]); })
112
        .attr("cy", function(d) { return y[p.y](d[p.y]); })
113
        .attr("r", 3);
114
    // Plot brush.
115
    cell.call(brush.x(x[p.x]).y(y[p.y]));
116
  }
117
  // Clear the previously-active brush, if any.
118
  function brushstart(p) {
119
    if (brush.data !== p) {
120
      cell.call(brush.clear());
121
      brush.x(x[p.x]).y(y[p.y]).data = p;
122
    }
123
  }
124
  // Highlight the selected circles.
125
  function brush(p) {
126
    var e = brush.extent();
127
    svg.selectAll(".cell circle").attr("class", function(d) {
128
      return e[0][0] <= d[p.x] && d[p.x] <= e[1][0]
129
          && e[0][1] <= d[p.y] && d[p.y] <= e[1][1]
130
          ? d.species : null;
131
    });
132
  }
133
  // If the brush is empty, select all circles.
134
  function brushend() {
135
    if (brush.empty()) svg.selectAll(".cell circle").attr("class", function(d) {
136
      return d.species;
137
    });
138
  }
139
  function cross(a, b) {
140
    var c = [], n = a.length, m = b.length, i, j;
141
    for (i = -1; ++i < n;) for (j = -1; ++j < m;) c.push({x: a[i], i: i, y: b[j], j: j});
142
    return c;
143
  }
144
});
145
    </script>
146
147
  <script>
148
d3.select("body")
149
    .datum(irwinHallDistribution(10000, 10))
150
  .call(histogramChart()
151
    .bins(d3.scale.linear().ticks(20))
152
    .tickFormat(d3.format(".02f")));
153
function irwinHallDistribution(n, m) {
154
  var distribution = [];
155
  for (var i = 0; i < n; i++) {
156
    for (var s = 0, j = 0; j < m; j++) {
157
      s += Math.random();
158
    }
159
    distribution.push(s / m);
160
  }
161
  return distribution;
162
}
163
164
}