html
html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!-- Author: Richard Myatt
Date: 25 January 2019
A simple bar chart using d3's scaleLinear based on a tutorial series by d3Vienno
at https://www.youtube.com/watch?v=n5NcCoa9dDU&list=PL6il2r9i3BqH9PmbOf5wA5E1wOG3FT22p
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
</head>
<body>
<h1>d3js - using scales and colours</h1>
<script src="https://d3js.org/d3.v5.min.js"></script>
</body>
</html>
Enter to Rename, Shift+Enter to Preview
css
css
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* Author: Richard Myatt
Date: 25 January 2019
A simple bar chart using d3's scaleLinear based on a tutorial series by d3Vienno
at https://www.youtube.com/watch?v=n5NcCoa9dDU&list=PL6il2r9i3BqH9PmbOf5wA5E1wOG3FT22p
*/
body {
background-color: #eee;
}
h1 {
font-size: 25px;
}
h1, svg {
margin-left: 20px;
}
Enter to Rename, Shift+Enter to Preview
js
js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/* Author: Richard Myatt
Date: 25 January 2019
A simple bar chart using d3's scaleLinear based on a tutorial series by d3Vienno
at https://www.youtube.com/watch?v=n5NcCoa9dDU&list=PL6il2r9i3BqH9PmbOf5wA5E1wOG3FT22p
*/
window.onload = function() {
// Data for our bar chart
var dataArray = [30, 20, 30, 50, 40, 30];
var width = 300;
var height = 300;
// using a scale to control the length of the input bars
// the domain specifies the anticipated data range
// the range specifies the full length of the bar for the maximum value in
// the domain specification
var widthScale = d3.scaleLinear()
.domain([0, 50])
.range([0, 250]);
// create the svg
var canvas = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
Enter to Rename, Shift+Enter to Preview
BROWSER
Console
Run