我正在学习D3.js,但是我在x轴的绘图中遇到了一些问题。
尽管手动说明了我要显示的刻度,但并非所有刻度都带有标签显示(在下面的示例中,2500刻度没有标签)。
<!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.0">
<!-- theme info -->
<meta name="description" content="This is web page used to demo functionality of the D3.js library. It is released under CC-4.0 license.">
<meta name="keywords" content="data visualization, d3, javascript, udacity">
<meta name="author" content="Chris Saden">
<!-- site title -->
<title>Udacity | Data Visualization and D3.js</title>
<!-- favicons -->
<link rel="shortcut icon" href="./assets/favicon.ico">
<link rel="icon" sizes="any" mask="" href="./assets/udacity_apple_icon.png">
<link rel="apple-touch-icon" href="./assets/udacity_apple_icon.png">
<!-- Bootstrap -->
<link href="./lib/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<!-- Custom styles -->
<link rel="stylesheet" href="./d3_demo.css">
</head>
<body>
<nav class="navbar navbar-inverse navbar-static-top">
<div class="nav-container container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle Navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand logo" id="header-logo" href="https://classroom.udacity.com">
<img src="./assets/udacity.svg" alt="logo">
</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="https://udacity.com">Udacity Home Page</a></li>
<li><a href="https://www.udacity.com/courses/all">Course Catalog</a></li>
<li><a href="https://udacity.com/me">Classroom</a></li>
<li><a href="https://career-resource-center.udacity.com">Career Resource Center</a></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<h1 class="main-title">Data Visualization and D3.js</h1>
<div class="main">
<div class="row">
<div class="col-sm-offset-1 col-sm-10 col-md-8 col-md-offset-2">
<h3 class="mobmid">Skills</h3>
<p>Data Encodings</p>
<div class="progress">
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="85" aria-valuemin="0" aria-valuemax="100" style="width: 85%">
<span class="sr-only">85% Complete (success)</span>
</div>
</div><!--skill end-->
<p>Visual Design Principles</p>
<div class="progress">
<div class="progress-bar progress-bar-info" role="progressbar" aria-valuenow="55" aria-valuemin="0" aria-valuemax="100" style="width: 55%">
<span class="sr-only">55% Complete</span>
</div>
</div><!--skill end-->
<p>D3.js</p>
<div class="progress">
<div class="progress-bar progress-bar-warning" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width: 40%">
<span class="sr-only">40% Complete (warning)</span>
</div>
</div><!--skill end-->
<p>Storytelling</p>
<div class="progress">
<div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="23" aria-valuemin="0" aria-valuemax="100" style="width: 23%">
<span class="sr-only">23% Complete (danger)</span>
</div>
</div><!--skill end-->
</div><!--info end-->
</div><!--data vis skills end-->
</div>
</div>
<div class="footer">
<div id="footer">
<h1 class="footer-header">Be in Demand</h1>
<span>Nanodegree is a trademark of Udacity</span>
<span>© 2011-<script>document.write(new Date().getFullYear())</script> Udacity, Inc.</span>
<nav class="social">
<ul class="social_links">
<li>
<a href="https://twitter.com/udacity">
<img src="./assets/twitter.svg" alt="Twitter">
</a>
</li>
<li>
<a href="https://www.facebook.com/Udacity">
<img src="./assets/facebook.svg" alt="Facebook">
</a>
</li>
<li>
<a href="https://plus.google.com/+Udacity/posts">
<img src="./assets/google_plus.svg" alt="Google +">
</a>
</li>
</ul>
</nav>
</div>
</div>
<!-- Load D3.js -->
<script src="./lib/d3.min.js"></script>
<script>
d3
.select(".main")
.html('');
var main = document.querySelector(".main");
// 2. Use the margin convention practice
var margin = {top: 50, right: 50, bottom: 50, left: 50}
, width = main.clientWidth - margin.left - margin.right // Use the main's width
, height = 400 - margin.top - margin.bottom; // Use a fixed height
var yScale = d3.scaleLinear().domain([15, 100]).range([height, 0]);
var xScale = d3.scaleLog().base(100).domain([250, 20000]).range([0, width]);
var rScale = d3.scaleSqrt().domain([52070, 1380000000]).range([10, 40]);
// 1. Add the SVG to the page and employ #2
var svg = d3.select(".main").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// 3. Call the x axis in a group tag
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(xScale).tickValues([300, 500, 1000, 2500, 10000, 20000])); // Create an axis component with d3.axisBottom
// 4. Call the y axis in a group tag
svg.append("g")
.attr("class", "y axis")
.call(d3.axisLeft(yScale).ticks(10)); // Create an axis component with d3.axisLeft
svg.append("circle")
.attr("fill", "red")
.attr("r", rScale(1380000000))
.attr("cx", xScale(13330))
.attr("cy", yScale(77));
svg.append("circle")
.attr("fill", "blue")
.attr("r", rScale(150000))
.attr("cx", xScale(2000))
.attr("cy", yScale(44));
svg.append("circle")
.attr("fill", "green")
.attr("r", rScale(500000))
.attr("cx", xScale(400))
.attr("cy", yScale(92));
</script>
</body>
</html>
我在做什么错了?
顺便说一句,我正在尝试手动执行此操作,因为它是对数刻度,因此所得的刻度有些混乱。我想删除很多刻度线(所有未标记的刻度线),使刻度线更干净:
http://jsfiddle.net/fsxdbtjL/2/
<!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.0">
<!-- theme info -->
<meta name="description" content="This is web page used to demo functionality of the D3.js library. It is released under CC-4.0 license.">
<meta name="keywords" content="data visualization, d3, javascript, udacity">
<meta name="author" content="Chris Saden">
<!-- site title -->
<title>Udacity | Data Visualization and D3.js</title>
<!-- favicons -->
<link rel="shortcut icon" href="./assets/favicon.ico">
<link rel="icon" sizes="any" mask="" href="./assets/udacity_apple_icon.png">
<link rel="apple-touch-icon" href="./assets/udacity_apple_icon.png">
<!-- Bootstrap -->
<link href="./lib/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<!-- Custom styles -->
<link rel="stylesheet" href="./d3_demo.css">
</head>
<body>
<nav class="navbar navbar-inverse navbar-static-top">
<div class="nav-container container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle Navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand logo" id="header-logo" href="https://classroom.udacity.com">
<img src="./assets/udacity.svg" alt="logo">
</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="https://udacity.com">Udacity Home Page</a></li>
<li><a href="https://www.udacity.com/courses/all">Course Catalog</a></li>
<li><a href="https://udacity.com/me">Classroom</a></li>
<li><a href="https://career-resource-center.udacity.com">Career Resource Center</a></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<h1 class="main-title">Data Visualization and D3.js</h1>
<div class="main">
<div class="row">
<div class="col-sm-offset-1 col-sm-10 col-md-8 col-md-offset-2">
<h3 class="mobmid">Skills</h3>
<p>Data Encodings</p>
<div class="progress">
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="85" aria-valuemin="0" aria-valuemax="100" style="width: 85%">
<span class="sr-only">85% Complete (success)</span>
</div>
</div><!--skill end-->
<p>Visual Design Principles</p>
<div class="progress">
<div class="progress-bar progress-bar-info" role="progressbar" aria-valuenow="55" aria-valuemin="0" aria-valuemax="100" style="width: 55%">
<span class="sr-only">55% Complete</span>
</div>
</div><!--skill end-->
<p>D3.js</p>
<div class="progress">
<div class="progress-bar progress-bar-warning" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width: 40%">
<span class="sr-only">40% Complete (warning)</span>
</div>
</div><!--skill end-->
<p>Storytelling</p>
<div class="progress">
<div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="23" aria-valuemin="0" aria-valuemax="100" style="width: 23%">
<span class="sr-only">23% Complete (danger)</span>
</div>
</div><!--skill end-->
</div><!--info end-->
</div><!--data vis skills end-->
</div>
</div>
<div class="footer">
<div id="footer">
<h1 class="footer-header">Be in Demand</h1>
<span>Nanodegree is a trademark of Udacity</span>
<span>© 2011-<script>document.write(new Date().getFullYear())</script> Udacity, Inc.</span>
<nav class="social">
<ul class="social_links">
<li>
<a href="https://twitter.com/udacity">
<img src="./assets/twitter.svg" alt="Twitter">
</a>
</li>
<li>
<a href="https://www.facebook.com/Udacity">
<img src="./assets/facebook.svg" alt="Facebook">
</a>
</li>
<li>
<a href="https://plus.google.com/+Udacity/posts">
<img src="./assets/google_plus.svg" alt="Google +">
</a>
</li>
</ul>
</nav>
</div>
</div>
<!-- Load D3.js -->
<script src="./lib/d3.min.js"></script>
<script>
d3
.select(".main")
.html('');
var main = document.querySelector(".main");
// 2. Use the margin convention practice
var margin = {top: 50, right: 50, bottom: 50, left: 50}
, width = main.clientWidth - margin.left - margin.right // Use the main's width
, height = 400 - margin.top - margin.bottom; // Use a fixed height
var yScale = d3.scaleLinear().domain([15, 100]).range([height, 0]);
var xScale = d3.scaleLog().base(100).domain([250, 20000]).range([0, width]);
var rScale = d3.scaleSqrt().domain([52070, 1380000000]).range([10, 40]);
// 1. Add the SVG to the page and employ #2
var svg = d3.select(".main").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// 3. Call the x axis in a group tag
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(xScale).ticks(6)); // Create an axis component with d3.axisBottom
// 4. Call the y axis in a group tag
svg.append("g")
.attr("class", "y axis")
.call(d3.axisLeft(yScale).ticks(10)); // Create an axis component with d3.axisLeft
svg.append("circle")
.attr("fill", "red")
.attr("r", rScale(1380000000))
.attr("cx", xScale(13330))
.attr("cy", yScale(77));
svg.append("circle")
.attr("fill", "blue")
.attr("r", rScale(150000))
.attr("cx", xScale(2000))
.attr("cy", yScale(44));
svg.append("circle")
.attr("fill", "green")
.attr("r", rScale(500000))
.attr("cx", xScale(400))
.attr("cy", yScale(92));
</script>
</body>
</html>
如果也有解决方案,我也会接受。
谢谢!