JSFiddle - React, Tailwind, and code Playground
CSS
svg {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
fill:none;
stroke:#000;
shape-rendering: crispEdges;
}
.line {
fill: none;
stroke-width: 1.5px;
}
JavaScript
var margin = {top: 20, right: 80, bottom: 30, left: 50},
width = 700 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format("%Y").parse;
var x = d3.time.scale()
.range([0,width]);
var y = d3.scale.linear()
.range([height,0]);
var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.interpolate("basis")
.x(function(d) { return x(d.YEAR); })
.y(function(d) { return y(d.VALUE); });
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 + ")");
// add the tooltip area to the webpage
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
d3.csv("http://www.sfu.ca/~etc3/data.csv", function(error, data) {
color.domain(d3.keys(data[0]).filter(function(key) { return key == "CAUSES"; }));
// first we need to corerce the data into the right formats
data = data.map( function (d) {
return {
CAUSES: d.CAUSES,
YEAR: parseDate(d.YEAR.toString()),
VALUE: +d.VALUE };
});
// then we need to nest the data on CAUSES since we want to only draw one
// line per CAUSES
data = d3.nest().key(function(d) { return d.CAUSES; }).entries(data);
x.domain([d3.min(data, function(d) { return d3.min(d.values, function (d) { return d.YEAR; }); }),
d3.max(data, function(d) { return d3.max(d.values, function (d) { return d.YEAR; }); })]);
y.domain([0, d3.max(data, function(d) { return d3.max(d.values, function (d) { return d.VALUE; }); })]);
// var path1 = svg.append("g").append("path").data([data1]).attr("class", "line1");
...