-
Notifications
You must be signed in to change notification settings - Fork 0
/
散点图.html
95 lines (84 loc) · 2.47 KB
/
散点图.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>d3</title>
<style type="text/css">
body {
padding-top: 50px;
padding-left: 100px;
}
#chartArea {
width: 400px;
height: 300px;
background-color: #CCC;
}
.bar {
display: inline-block;
width: 20px;
height: 75px;
/* Gets overriden by D3-assigned height below */
margin-right: 2px;
/* fill: teal; */
/* SVG doesn't have background prop, use fill instead*/
z-index: 99;
}
.bubble {
display: inline-block;
fill: purple;
fill-opacity: 0.5;
stroke: black;
stroke-weight: 1px;
}
</style>
</head>
<body>
<script src="js/underscore-min.js"></script>
<script src="js/d3.js"></script>
<section id="chartArea"></section>
<script>
var dataset = _.map(_.range(30), function(num) {
return {
x: Math.random() * 100,
y: Math.random() * 100,
r: Math.random() * 30
}
}), //reandom generate 15 data from 1 to 50
margin = {
top: 0,
bottom: 0,
left: 0,
right: 0
},
w = 400 - margin.left - margin.right,
h = 300 - margin.top - margin.bottom;
var svg = d3.select('#chartArea').append('svg')
.attr('width', w + margin.left + margin.right)
.attr('height', h + margin.top + margin.bottom)
.append('g') //The last step is to add a G element which is a graphics container in SBG.
.attr('transform', 'translate(' + margin.left + ', ' + margin.top + ')'); //Then offset that graphic element by our left and top margins.
var yScale = d3.scaleLinear()
.domain([0, d3.max(dataset, function(d) {
return d.y; //tell the max function just need to care about y prop
})])
.range([h, 0]);
var xScale = d3.scaleLinear()
.domain([0, 100])
.range([0, w]);
svg.selectAll('circle')
.data(dataset)
.enter()
.append('circle') // svg doesn't have div, use rect instead
.attr('class', "bubble")
.attr('cx', function(each_data, index) {
return xScale(each_data.x);
})
.attr('cy', function(each_data) {
return yScale(each_data.y);
})
.attr('r', function(each_data, i) {
return each_data.r;
});
</script>
</body>
</html>