[50a3f7]: / modules / HTTPRequest / playground / dataPlot.js

Download this file

165 lines (159 with data), 5.2 kB

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