我是d3.js的新手,我想根据示例here生成图表。
如果我可以单独存储此文件并运行它可以正常工作。但我不知道如何将其添加到我的html页面的特定元素中。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 3, D3.JS implementation</title>
<SCRIPT type="text/javascript" src="d3.v2.min.js"></SCRIPT>
<meta name="description" content="Source code generated using layoutit.com">
<meta name="author" content="LayoutIt!">
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
</head>
<style>
nav {
margin-top: 40px;
padding: 24px;
text-align: center;
font-family: Raleway;
box-shadow: 2px 2px 8px rgba(0, 0, 0, 0.5);
}
#nav-3 {
background: #EEA200;
}
.link-3 {
transition: 0.4s;
color: #ffffff;
font-size: 20px;
text-decoration: none;
padding: 0 10px;
margin: 0 10px;
}
.link-3:hover {
background-color: #ffffff;
color: #EEA200;
padding: 24px 10px;
}
.axis text {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
</style>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<nav class="navbar navbar-inverse" role="navigation">
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active">
<a class="link-3" href="#">HOME</a>
</li>
<li>
<a class="link-3" href="#">LOGIN</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div class="row">
<div class="col-md-12">
<h3>Sample page</h3>
</div>
</div>
<div class="row">
<div class="col-md-12" >
<div id="svgHere" />
</div>
</div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/scripts.js"></script>
<script>
var n = 20, // number of samples
m = 4; // number of series
var data = d3.range(m).map(function() { return d3.range(n).map(Math.random); });
var margin = {top: 20, right: 30, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var y = d3.scale.linear()
.domain([0, 1])
.range([height, 0]);
var x0 = d3.scale.ordinal()
.domain(d3.range(n))
.rangeBands([0, width], .2);
var x1 = d3.scale.ordinal()
.domain(d3.range(m))
.rangeBands([0, x0.rangeBand()]);
var z = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x0)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var svg = d3.select("#svgHere").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("svg:g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g").selectAll("g")
.data(data)
.enter().append("g")
.style("fill", function(d, i) { return z(i); })
.attr("transform", function(d, i) { return "translate(" + x1(i) + ",0)"; })
.selectAll("rect")
.data(function(d) { return d; })
.enter().append("rect")
.attr("width", x1.rangeBand())
.attr("height", y)
.attr("x", function(d, i) { return x0(i); })
.attr("y", function(d) { return height - y(d); });
</script>
</body>
</html>
从上面的代码&#34;这里我想用d3.js&#34;绘制图形。在本节中,我想添加我的图表。
答案 0 :(得分:2)
在定义SVG变量时,按ID选择此<div>
。
首先,给它一个(唯一的)ID:
<div id="svgHere"></div>
然后:
var svg = d3.select("#svgHere")
.append("svg")
.attr("width", someValue)//the width value goes here
.attr("height", someValue);//the height value goes here