Built with blockbuilder.org
xxxxxxxxxx
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.dot {
stroke: #000;
stroke-width: .5px;
}
.tooltip {
position: absolute;
width: 200px;
height: 28px;
pointer-events: none;
}
.chart {
display: inline-block;
height: 300px;
width: 400px;
margin: 30px;
}
</style>
<body>
<div id="charts">
<div id="chart-left" class="chart">
<div class="title">Left chart</div>
</div>
<div id="chart-right" class="chart">
<div class="title">Right chart</div>
</div>
</div>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 20, left: 30},
width = 800 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
var color = d3.scaleOrdinal(d3.schemeCategory20);
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
function scatterplot(el, data, w, h, var_x, var_y, var_r, var_color) {
var x = d3.scaleLinear()
.range([0, w]);
var y = d3.scaleLinear()
.range([h, 0]);
var r = d3.scaleSqrt()
.range([2,10]);
var xAxis = d3.axisBottom()
.scale(x);
var yAxis = d3.axisLeft()
.scale(y);
x.domain(d3.extent(data, function(d){
return d[var_x];
})).nice();
y.domain(d3.extent(data, function(d){
return d[var_y];
})).nice();
r.domain(d3.extent(data, function(d){
return d[var_r];
})).nice();
var svg = el.append("svg")
.attr("width", w + margin.left + margin.right)
.attr("height", h + margin.bottom + margin.top)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append('g')
.attr('transform', 'translate(0,' + h + ')')
.attr('class', 'x axis')
.call(xAxis);
svg.append('g')
.attr('transform', 'translate(0,0)')
.attr('class', 'y axis')
.call(yAxis);
svg.append('text')
.attr('x', 10)
.attr('y', 10)
.attr('class', 'label')
.text('Sepal Width');
svg.append('text')
.attr('x', width)
.attr('y', height - 10)
.attr('text-anchor', 'end')
.attr('class', 'label')
.text('Sepal Length');
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", function(d) { return r(d[var_r]); })
.attr("cx", function(d) { return x(d[var_x]); })
.attr("cy", function(d) { return y(d[var_y]); })
.style("fill", function(d) { return color(d[var_color]); })
.on("mouseover", function(d){
// highlight on
d3.selectAll(".dot").filter(function(e) {
return e === d;
})
.style("stroke-width", 10)
})
.on("mouseout", function(d){
d3.selectAll(".dot").filter(function(e) {
return e === d;
})
.style("stroke-width", .5)
})
.on("click", function(d){
// selection on/off
});
}
d3.csv('iris.csv', function(error, data){
data.forEach(function(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;
});
scatterplot(d3.select("#chart-left"), data, width/2, height/2, "sepal_length", "sepal_width", "petal_length", "species")
scatterplot(d3.select("#chart-right"), data, width/2, height/2, "petal_length", "petal_width", "sepal_length", "species")
});
</script>
Modified http://d3js.org/d3.v4.min.js to a secure url
https://d3js.org/d3.v4.min.js