xxxxxxxxxx
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;x`
shape-rendering: crispEdges;
}
.dot {
stroke: none;
opacity: 0.6;
}
.x {
color: red;
}
div.tooltip {
position: absolute;
text-align: center;
font: 12px;
padding: 6px;
background: #F0F0F0;
border: thin lightsteelblue;
border-radius: 8px;
pointer-events: none;
}
.grid line {
stroke: lightgrey;
stroke-opacity: 0.7;
shape-rendering: crispEdges;
}
.grid path {
stroke-width: 0;
}
</style>
<body>
<!-- <script src="//cdnjs.cloudflare.com/ajax/libs/d3/3.4.6/d3.min.js"></script> -->
<script src="//d3js.org/d3.v4.min.js"></script>
<script>
// link to the data
d3.tsv("test-data.tsv", function(error, data) {
// 'clean' the data and add a horizontal jitter
data.forEach(function(d) {
d.score = +d.score;
d.jitter = 0.05+(Math.random())*0.25;
if (d.type == "Alpha") {d.jitter = 0-d.jitter};
d.category = +(d.category);
});
// set up the chart
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// gridlines in x axis function
function make_x_gridlines() {
return d3.axisBottom(x)
.ticks(5)
}
var x = d3.scaleLinear()
.range([0, width]);
var y = d3.scaleLinear()
.range([height, 0]);
var color = d3.scaleOrdinal(d3.schemeCategory10);
var xAxis = d3.axisBottom()
.scale(x)
.ticks(5);
var yAxis = d3.axisLeft()
.scale(y);
x.domain([0.1,5.9]);
y.domain([0,2600]);
// add the X gridlines
svg.append("g")
.attr("class", "grid")
.attr("transform", "translate(0," + height + ")")
.call(make_x_gridlines()
.tickSize(-height)
.tickFormat("")
)
// tooltip div
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity",0);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("Category");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Score")
// add the dots
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 8)
.attr("cx", function(d) { return x(d.category+d.jitter); })
.attr("cy", function(d) { return y(d.score); })
.style("fill", function(d) { return color(d.type); })
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", .9);
div.html(d.category+" | "+d.score+" | "+d.type)
.style("left", (d3.event.pageX) + "px")
.style("top" , (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
});
// add a legend
var legend = svg.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("circle")
.attr("cx", width - 8)
.attr("cy", 9)
.attr("r", 8)
.attr("opacity",0.6)
.style("fill", color);
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
});
</script>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.6/d3.min.js
https://d3js.org/d3.v4.min.js