JSFiddle - React, Tailwind, and code Playground
HTML
<svg>
<path class="handle--custom" stroke="#000" cursor="ew-resize" d="M-0.5,46A6,6 0 0 0 -6.5,52V86A6,6 0 0 0 -0.5,92ZM-2.5,54V84M-4.5,54V84" transform="translate(29,-23)"></path>
<path class="handle--custom" stroke="#000" fill="none" cursor="ew-resize" d="M-0.5,25.5A6,6 0 0 0 -6.5,31.5V45A6,6 0 0 0 -0.5,51ZM-2.5,33.5V43M-4.5,33.5V43" transform="translate(141, -20)"></path>
</svg>
CSS
pre {
display:none;
}
path
{
fill: none;
stroke: #000;
}
.line {
fill: none;
// stroke: green;
// stroke-width: 3.5px;
}
JavaScript
var margin = {top: 20, right: 40, bottom: 100, left: 100},
width = 500 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
var x = d3.scale.linear().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
var xAxis = d3.svg.axis().scale(x).orient("bottom");
var yAxis = d3.svg.axis().scale(y).orient("left");
var line = d3.svg.line()
.x(function(d) { return x(d.depth); })
.y(function(d) { return y(d.carat); });
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 + ")");
var data = d3.csv.parse(d3.select("pre#data").text());
data.forEach(function(d) {
d.x = +d.depth; // or Number(d.depth)
d.carat = +d.carat;
//d.cut = +d.cut;
})
data.sort(function(a,b) { return a.x - b.x; });
x.domain(d3.extent(data, function(d) { return d.depth; })).nice();
y.domain(d3.extent(data, function(d) { return d.carat; })).nice();
// Nest the entries by symbol
var dataNest = d3.nest()
.key(function(d) {return d.cut;})
.entries(data);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.attr("y", 6)
.attr("x", 5)
.attr("dy", ".35em")
.attr("transform", "rotate(45)")
.style("text-anchor", "start")
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("Price");
var color = d3.scale.category10(); // set the colour scale
// Loop through each symbol / key
dataNest.forEach(function(d) {
svg.append("path")
.attr("class", "line")
.style("stroke", function() { // Add the colours dynamically
return d.color = color(d.key); })
.attr("d", line(d.values));
});