-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (48 loc) · 1.51 KB
/
index.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { scatterPlot } from './scatterPlot.js';
const csvUrl = "https://gist.githubusercontent.com/curran/a08a1080b88344b0c8a7/raw/0e7a9b0a5d22642a06d3d5b9bcbad9890c8ee534/iris.csv";
// Adding a "+" before a string, will convert it into a number.
const parseRow = (d) => {
d.sepal_length = +d.sepal_length;
d.sepal_width = +d.sepal_width;
d.petal_length = +d.petal_length;
d.petal_width = +d.petal_width;
return d;
}
const width = window.innerWidth;
const height = window.innerHeight;
const svg = d3.select('body').append('svg')
.attr('width', width)
.attr('height', height);
// // The second argument is a funtion that takes as input a single row and replace it.
// // First way to import data.
// d3.csv(csvUrl, parseRow).then(data => {
// console.log(data);
// });
// This is a better way to import data.
const main = async () => {
const data = await d3.csv(csvUrl, parseRow);
const plot = scatterPlot()
.width(width)
.height(height)
.data(data)
// Columns for the range.
.xValue((d) => d.petal_length)
.yValue((d) => d.sepal_length)
// d3 margin convention.
.margin({top: 20, right: 20, bottom: 40, left:50})
svg.call(plot);
const columns = [
'petal_width',
'sepal_width',
'petal_length',
'sepal_length'
];
let i = 0;
setInterval(() => {
console.log(i % columns.length);
plot.xValue((d) => d[columns[i % columns.length]])
svg.call(plot);
i++;
}, 4000);
};
main();