-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchart.js
73 lines (56 loc) · 1.76 KB
/
chart.js
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
/*global d3:false*/
var full_chart = function() {
var margin = {top: 80, right: 80, bottom: 200, left: 80},
width = 960,
height = 720;
var x = d3.scale.linear()
.range([0, width - margin.left - margin.right]);
var y = d3.scale.linear()
.range([height - margin.top - margin.bottom, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickPadding(8);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickPadding(8);
function chart(selection) {
selection.each(function(d) {
pad(x.domain(d3.extent(d, xValue)), 40);
pad(y.domain(d3.extent(d, yValue)), 40);
var svg = d3.select(this).append("svg")
.attr("width", width)
.attr("height", height)
.attr("class", "dot chart")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.selectAll(".dot")
.data(d)
.enter().append("circle")
.attr("class", "dot")
.attr("cx", function(d) { return x(d.x); })
.attr("cy", function(d) { return y(d.y); })
.attr("r", 12);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + y.range()[0] + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
});
}
function pad(scale, k) {
var range = scale.range();
if (range[0] > range[1]) k *= -1;
return scale.domain([range[0] - k, range[1] + k].map(scale.invert)).nice();
}
function xValue(d) {
return d.x;
}
function yValue(d) {
return d.y;
}
return chart;
};