官术网_书友最值得收藏!

Basic HTML template

When we create our visualizations, we need to first load the D3 library and the CSS styles that we want to apply. For each of the samples, we'll use the following basic HTML skeleton:

<html> 
<head>
<!-- generic stuff -->
<script src="../libs/d3.js"></script>
<script src="../libs/lodash.js"></script>
<link rel="stylesheet" href="../css/default.css">

<!-- specific stuff -->
<script src="./js/D01-01.js"></script>
<link rel="stylesheet" href="./css/D01-01.css" type="text/css">
</head>
<body>

<p id="output">
<svg class="chart"></svg>
</p>

<script>
(function() {
show();
})();
</script>

</body>
</html>

This is a standard HTML page, where we first load the complete D3 sources (./libs/d3/js), the lodash JavaScript library, and CSS styles (../css/default.css) that we want to apply to all the examples in this book. We also load the example specific JavaScript (in this example, ./js/D01-01.js) and the example specific CSS (./css/D01-01.css). In this page, we also define a single p tag that has an id with a value output. This is the location in the page where we add our visualizations. A quick note on lodash. Lodash provides a large set of useful collection-related functions, which makes creating and working with JavaScript arrays a lot more convenient. You can see when we use a lodash function when the function call starts with an underscore: for example, _.range(2010, 2016).

There are different ways to load the D3 libraries. In our examples, we load the complete D3 library as a single JavaScript file (the <script src="../libs/d3.js"></script> import). This will load all the APIs provided by D3. D3, however, also comes in a set of micro-libraries, where each library provides a standalone piece of functionality. You can use this to limit the size of the required JavaScript by only including the APIs you need.

A complete overview of the modules that are available can be found by looking at the D3 API reference (https://github.com/d3/d3/blob/master/API.md). In this book, we'll explore most of the APIs provided by D3 and explain which D3 module provides the specific piece of functionality.

Once the page is loaded, the following code block runs, which calls the show function which we'll implement in the example specific JavaScript (./js/D01-01.js in this case):

<script> 
(function() {
show();
})();
</script>

The show function implementation will differ for each example, but this way we can keep the basic skeleton the same, and we can focus on JavaScript and the D3 APIs. Note that in this book, we won't explain in detail the JavaScript concepts we use. If you need a reminder on how anonymous functions, closures, variable scope, and so on, work in JavaScript, a great resource is the Mozilla Developer Network (MDN) page on JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript.

主站蜘蛛池模板: 麻城市| 长乐市| 桃江县| 凤庆县| 精河县| 榆中县| 宜宾市| 安岳县| 镇坪县| 水富县| 田阳县| 双桥区| 开封县| 凉城县| 碌曲县| 阜新市| 曲沃县| 邛崃市| 曲阜市| 屏东市| 恩平市| 吴忠市| 荣成市| 赤水市| 枣庄市| 雷波县| 迭部县| 陕西省| 卢湾区| 科技| 昭觉县| 大理市| 固原市| 六盘水市| 建德市| 安阳县| 临桂县| 宁明县| 陕西省| 青神县| 宜昌市|