Part of the video course: D3.js in Motion.
An example of a basic scatter plot using D3.
This shows the "Iris" dataset. Originally published at UCI Machine Learning Repository: Iris Data Set, this small dataset from 1936 is often used for testing out machine learning algorithms and visualizations. Each row of the table represents an iris flower, including its species and dimensions of its botanical parts, sepal and petal, in centimeters.
xxxxxxxxxx
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://d3js.org/d3.v4.min.js"></script>
<title>Basic Scatter Plot</title>
</head>
<body>
<svg width="960" height="500"></svg>
<script>
const svg = d3.select('svg');
const width = svg.attr('width');
const height = svg.attr('height');
const xValue = d => d.sepalLength;
const yValue = d => d.petalLength;
const xScale = d3.scaleLinear();
const yScale = d3.scaleLinear();
const row = d => {
d.petalLength = +d.petalLength;
d.petalWidth = +d.petalWidth;
d.sepalLength = +d.sepalLength;
d.sepalWidth = +d.sepalWidth;
return d;
};
d3.csv('iris.csv', row, data => {
xScale
.domain(d3.extent(data, xValue))
.range([0, width]);
yScale
.domain(d3.extent(data, yValue))
.range([height, 0]);
svg.selectAll('circle').data(data)
.enter().append('circle')
.attr('cx', d => xScale(xValue(d)))
.attr('cy', d => yScale(yValue(d)))
.attr('r', 8);
});
</script>
</body>
</html>
https://d3js.org/d3.v4.min.js