D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
flwedemeyer
Full window
Github gist
Barley Dot Plot with Wild Lines
<!DOCTYPE html> <meta charset="utf-8"> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" charset="utf-8"></script> <style type="text/css"> .axis text { font-size: 12px; fill: #777; } .axis path { display: none; } .axis line { stroke-width:1px; stroke: #ccc; stroke-dasharray: 2px 2px; } .yearOne { fill: none; stroke-width: 1px; stroke: red; } </style> <body> </body> <script> var margin = {top: 20, right: 40, bottom: 20, left: 150}; var width = 720 - margin.left - margin.right, height = 400 - margin.top - margin.bottom; var xScale = d3.scale.linear() .range([0, width]); var yScale = d3.scale.ordinal() .rangePoints([height, 0]); var xAxis = d3.svg.axis() .scale(xScale) .tickSize(-height) .tickPadding(8) .orient("bottom"); var yAxis = d3.svg.axis() .scale(yScale) .tickSize(-width) .tickPadding(8) .orient("left"); 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 + ")"); d3.tsv("barley.tsv", ready) function ready(error, data) { if (error) return console.warn(error); var sampleData = data.filter(function(d) { return d.site == 'Morris'; }) var barleyYields = data.map(function(d) { return d.yield;}) var maxYields = d3.max(barleyYields) var minYields = d3.min(barleyYields) xScale.domain([minYields, maxYields]); yScale.domain(d3.set(sampleData.map(function(d) { return d.variety; })).values()); svg.append("g") .attr("class","y axis") // .attr("transform","translate(0," + height + ")") .call(yAxis); svg.append("g") .attr("class","x axis") .attr("transform","translate(0," + height + ")") .call(xAxis); var circleGroup = svg.selectAll(".yieldCircles") .data(sampleData) .enter() .append("g") .attr("class", "yieldCircles") .attr("class", function(d) { if(d.year === "1931"){ return "yearOne"; } else { return "yearTwo"; } }) .attr("transform", function(d) { return "translate(" + xScale(d.yield) + "," + yScale(d.variety) + ")"; }); circleGroup.append("circle") .attr("r", 3); var yearOneGroup = data.filter(function(d) { return d.year == '1931'; }) var yearTwoGroup = data.filter(function(d) { return d.year == '1932'; }) console.log(yearOneGroup) console.log(yearOneGroup) // begin of drawing lines var line = d3.svg.line() .x(function(data){ console.log(data) return xScale(data.yield);}) .y(function(data){ console.log(data) return yScale(data.variety);}) .interpolate("linear"); svg.append("path") .attr("d", function(d) { return line(sampleData)}) .attr("transform", "translate(0,0)") .style("stroke-width", 2) .style("stroke", "steelblue") .style("fill", "none"); // end of drawing lines } </script>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js