上部ボタンで線形グラフと片対数グラフを切り替えます。
データはy=1/2^xです。指数関数は片対数グラフにすると直線になります。
Created
November 23, 2013 14:34
-
-
Save habari2011dunia/7615216 to your computer and use it in GitHub Desktop.
線形軸-対数軸変換
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>線形-対数軸変換</title> | |
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
<style> | |
.axis path { | |
fill: none; | |
stroke: black; | |
} | |
.axis line { | |
stroke: black; | |
} | |
.line { | |
fill: none; | |
stroke: steelblue; | |
stroke-width: 2 | |
} | |
.point { | |
fill: white; | |
stroke: steelblue; | |
stroke-width: 2 | |
} | |
</style> | |
</head> | |
<body> | |
<p><input type="button" value="軸変換!" id="toggle"></p> | |
<svg></svg> | |
<script> | |
(function() { | |
var log = false; | |
var data = []; | |
for(var i=0; i<10; i++) { | |
data.push({x: i, y: Math.pow(0.5, i)}); | |
} | |
var w = 600, h = 400; | |
var margin = {top: 20, left: 60, bottom: 30, right: 20}; | |
var svg = d3.select("svg") | |
.attr({width: w, height: h}); | |
var xScaleLinear = d3.scale.linear() | |
.range([margin.left, w-margin.right]) | |
.domain([0, 9]); | |
var yScaleLinear = d3.scale.linear() | |
.range([h-margin.bottom, margin.top]) | |
.domain([0, 1]); | |
var yScaleLog = d3.scale.log() | |
.range([h-margin.bottom, margin.top]) | |
.domain([0.001, 1]); | |
var xAxis = d3.svg.axis() | |
.scale(xScaleLinear) | |
.orient("bottom"); | |
var yAxis = d3.svg.axis() | |
.scale(yScaleLinear) | |
.orient("left") | |
.tickFormat(d3.format(".3f")); | |
var line = d3.svg.line() | |
.x(function(d) { return xScaleLinear(d.x); }) | |
.y(function(d) { return yScaleLinear(d.y); }); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0,"+(h-margin.bottom)+")") | |
.call(xAxis); | |
svg.append("g") | |
.attr("class", "y axis") | |
.attr("transform", "translate("+margin.left+",0)") | |
.call(yAxis); | |
svg.append("path") | |
.datum(data) | |
.attr("class", "line") | |
.attr("d", line); | |
svg.selectAll(".point") | |
.data(data) | |
.enter() | |
.append("circle") | |
.attr("class", "point") | |
.attr("cx", function(d) { return xScaleLinear(d.x); }) | |
.attr("cy", function(d) { return yScaleLinear(d.y); }) | |
.attr("r", 3); | |
d3.select("#toggle") | |
.on("click", function() { log ? logToLinear() : linearToLog(); }); | |
function linearToLog() { | |
line.y(function(d) { return yScaleLog(d.y); }); | |
svg.select(".line") | |
.transition() | |
.duration(1000) | |
.attr("d", line); | |
d3.selectAll(".point") | |
.transition() | |
.duration(1000) | |
.attr("cy", function(d) { return yScaleLog(d.y); }); | |
svg.select(".y.axis") | |
.transition() | |
.duration(1000) | |
.call(yAxis.scale(yScaleLog)); | |
log = true; | |
} | |
function logToLinear() { | |
line.y(function(d) { return yScaleLinear(d.y); }); | |
svg.select(".line") | |
.transition() | |
.duration(1000) | |
.attr("d", line); | |
d3.selectAll(".point") | |
.transition() | |
.duration(1000) | |
.attr("cy", function(d) { return yScaleLinear(d.y); }); | |
svg.select(".y.axis") | |
.transition() | |
.duration(1000) | |
.call(yAxis.scale(yScaleLinear)); | |
log = false; | |
} | |
})(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment