-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscatterplot.js
More file actions
56 lines (49 loc) · 2.1 KB
/
Copy pathscatterplot.js
File metadata and controls
56 lines (49 loc) · 2.1 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
var scatterplot_width = 530,scatterplot_height = 530;
var svg;
function drawScatterplot(dataset,xValue,yValue) {
svg = svg || d3.select("#scatterplot")
.append("svg")
.attr("width",scatterplot_width)
.attr("height",scatterplot_height);
var padding = 30;
var xMax,xMin,yMax,yMin;
if(xValue == "quality") {
xMin = 1;
xMax = 10;
} else {
xMax = d3.max(dataset, function(d) {return parseFloat(d[xValue]);});
xMin = d3.min(dataset, function(d) {return parseFloat(d[xValue]);});
}
if(yValue == "quality") {
yMin = 1;
yMax = 10;
} else {
yMax = d3.max(dataset, function(d) {return parseFloat(d[yValue]);});
yMin = d3.min(dataset, function(d) {return parseFloat(d[yValue]);});
}
var xScale = d3.scale.linear().domain([xMin,xMax]).range([0 + padding,scatterplot_width - padding]).nice();
var yScale = d3.scale.linear().domain([yMin,yMax]).range([scatterplot_height - padding,0 + padding]).nice();
$("#scatterplot svg").empty();
var circles = svg.selectAll("circle")
.data(dataset)
.enter()
.append("circle")
.attr("cx", function (d) {
return xScale(d[xValue]);
})
.attr("cy",function (d) {
return yScale(d[yValue]);
})
.attr("r", function (d) { return Math.pow(d["quality"]/2,1.8);}) // exponential sizing for great good
.attr("class",function(d){return (d.type=="red"?"red_wine":"white_wine") +" q"+d.quality;});
updateScatterplotVisibility();
var xAxis = d3.svg.axis().scale(xScale).orient("top");
var yAxis = d3.svg.axis().scale(yScale).orient("right");
svg.append("g")
.attr("class","axis s_axis")
.attr("transform", "translate(0," + scatterplot_height + ")")
.call(xAxis);
svg.append("g")
.attr("class","axis s_axis")
.call(yAxis);
}