D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
danijett
Full window
Github gist
Barley Dot Plot
<!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: white; stroke-width:1px; stroke: black; } </style> <body> </body> <script> var margin = {top: 20, right: 40, bottom: 20, left: 150}; var width = 720 - margin.left - margin.right, height = 200 - 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); // console.log(data) 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) // console.log(sampleData) // console.log(barleyYields) // console.log(maxYields, minYields) xScale.domain([minYields, maxYields]); yScale.domain(d3.set(sampleData.map(function(d) { return d.variety; })).values()); svg.append("g") .attr("class","y axis") .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"){ // console.log(d); return "yearOne"; } else { return "yearTwo"; } }) .attr("transform", function(d) { return "translate(" + xScale(d.yield) + "," + yScale(d.variety) + ")"; }); circleGroup.append("circle") .attr("r", 4); var yieldLines = svg.selectAll(".yieldLines") .data(sampleData) .enter() .append("line") .attr("x1", xScale(20)) .attr("x2", xScale(25)) .attr("y1", yScale(sampleData.variety)) .attr("y2", yScale(sampleData.variety)) .style("stroke","#000") console.log(d.variety) // circleGroup.append("line") // .attr("x1", xScale(0)) // .attr("x2", xScale(sampleData[0].yield)) // .attr("y1", yScale(sampleData[0].variety)) // .attr("y2", yScale(sampleData[0].variety)) // .style("stroke","#000") // console.log(sampleData[0].yield) var yearOneGroup = sampleData.filter(function(d) { return d.year == '1931'; }) var yearOneYield = yearOneGroup.map(function(d) { return d.yield; }) var yearTwoGroup = sampleData.filter(function(d) { return d.year == '1932'; }) // console.log("year one yield [0]", yearOneYield) } </script>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js