Last active
August 29, 2015 13:57
-
-
Save habari2011dunia/9698275 to your computer and use it in GitHub Desktop.
Simple CSV plotter with D3.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment