-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
144 lines (119 loc) · 3.21 KB
/
index.html
File metadata and controls
144 lines (119 loc) · 3.21 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
<!DOCTYPE html>
<!-- obtained from https://gist.github.com/habari2011dunia/9698275 -->
<html lang="ja">
<head>
<meta charset="utf-8">
<title>Simple CSV plotter</title>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<style>
#input {
padding-left: 10px;
width: 300px;
float: left;
}
#result {
float: left;
}
textarea {
width: 250px;
height: 450px;
resize: none;
}
.axis path {
fill: none;
stroke: grey;
}
.axis line {
stroke: grey;
}
</style>
</head>
<body>
<div id="input">
CSV
<input type="button" value="plot!" id="plot"><br>
<textarea id="csv">x,y
1,10
2,2
3,4
4,5
5,9
6,5
7,9
8,2
9,6
10,5</textarea>
</div>
<div id="result"><svg></svg></div>
<script>
(function() {
document.getElementById("plot").onclick = plot;
var w = 600, h = 500;
var margin = {left: 60, top: 30, right: 20, bottom: 60};
var svg = d3.select("svg").style({width: w, height: h});
var xScale = d3.scale.linear().range([margin.left, w-margin.right]);
var yScale = d3.scale.linear().range([h-margin.bottom, margin.top]);
var xAxis = d3.svg.axis().scale(xScale).orient("bottom");
var yAxis = d3.svg.axis().scale(yScale).orient("left");
svg.append("g")
.attr("class", "x axis")
.append("text")
.attr("class", "x label")
.text("x")
.attr("font-size", 12)
.attr("dx", (w-margin.right)/2)
.attr("dy", "3.8em");
svg.append("g")
.attr("class", "y axis")
.append("text") // ラベル
.attr("class", "y label")
.text("y")
.attr("font-size", 12)
.attr("transform", "rotate(-90)")
.attr("dy", "-3.8em")
.attr("dx", -(h+margin.top-margin.bottom)/2)
.attr("text-anchor", "middle");
var line = d3.svg.line();
svg.append("path")
.attr("class", "line")
.attr("fill", "none")
.attr("stroke", "steelblue");
plot();
function plot() {
var raw = document.getElementById("csv").value;
var data = d3.csv.parse(raw);
var keys = Object.keys(data[0]);
var xExtent = d3.extent(data, function(e) { return +e[keys[0]]; });
var yExtent = d3.extent(data, function(e) { return +e[keys[1]]; });
xScale.domain(xExtent).nice();
yScale.domain(yExtent).nice();
xAxis.scale(xScale);
yAxis.scale(yScale);
d3.select(".x.axis")
.attr("transform", "translate(0,"+yScale.range()[0]+")")
.call(xAxis);
d3.select(".y.axis")
.attr("transform", "translate("+xScale.range()[0]+",0)")
.call(yAxis);
d3.select(".x.label").text(keys[0]);
d3.select(".y.label").text(keys[1]);
line
.x(function(d) { return xScale(d[keys[0]]); })
.y(function(d) { return yScale(d[keys[1]]); });
svg.select(".line").datum(data).attr("d", line);
var points = svg.selectAll(".point").data(data);
points.enter()
.append("circle")
.attr("class", "point")
.attr("r", 3)
.attr("fill", "steelblue")
.attr("stroke", "steelblue");
points.exit().remove();
points
.attr("cx", function(d){ return xScale(d[keys[0]]); })
.attr("cy", function(d){ return yScale(d[keys[1]]); });
}
})();
</script>
</body>
</html>