Created
October 11, 2018 08:33
-
-
Save aidiary/606f754e32745170669ec304904f3346 to your computer and use it in GitHub Desktop.
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
var dataset = [80, 100, 56, 120, 180, 30, 40, 120, 160]; | |
// var dataset = [1, 2, 3, 4, 5]; | |
var svgWidth = 500, svgHeight = 300, barPadding = 5; | |
var barWidth = (svgWidth / dataset.length); // 各バーの幅 | |
var svg = d3.select('svg') | |
.attr('width', svgWidth) | |
.attr('height', svgHeight); | |
var xScale = d3.scaleLinear() | |
.domain([0, d3.max(dataset)]) | |
.range([0, svgWidth]); | |
var yScale = d3.scaleLinear() | |
.domain([0, d3.max(dataset)]) | |
.range([svgHeight, 0]); | |
var x_axis = d3.axisBottom() | |
.scale(xScale); | |
var y_axis = d3.axisLeft() | |
.scale(yScale); | |
svg.append("g") | |
.attr("transform", "translate(50, 10)") | |
.call(y_axis); | |
var xAxisTranslate = svgHeight - 20; | |
svg.append("g") | |
.attr("transform", "translate(50, " + xAxisTranslate + ")") | |
.call(x_axis); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment