D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
tomshanley
Full window
Github gist
OECD House Price to income index
Data source:
OECD
<!DOCTYPE html> <meta charset="utf-8"> <style> body { font: 10px Arial; margin: 0; } .line { fill: none; stroke: #426789; stroke-width: 2px; } .area { fill: #e8eef4; } g#xAxisG text, g#yAxisG text { stroke: none; fill: silver; font-size: 9px; } header, div#source { margin-left: 40px; margin-bottom: 38px; } </style> <body> <div id="charts"></div> <script src="https://d3js.org/d3.v4.min.js"></script> <script> var margin = {top: 8, right: 15, bottom: 20, left: 40}, width = 800 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var xScale = d3.scalePoint() .range([0, width]); var yScale = d3.scaleLinear() .range([height, 0]); var yAxis = d3.axisLeft() .scale(yScale) //.ticks(3); var xAxis = d3.axisBottom() .scale(xScale); var line = d3.line() .x(function(d) { return xScale(d.TIME); }) .y(function(d) { return yScale(d.Value); }); d3.csv("data-price-income.csv", convertTextToNumbers, function(error, data) { var quarters = d3.nest() .key(function(d) { return d.TIME; }) .sortKeys(d3.ascending) .entries(data); var xScaleDomain = []; quarters.forEach(function(q){ xScaleDomain.push(q.key); }); xScale.domain(xScaleDomain); yScale.domain([0,d3.max(data, function(d) { return d.Value; })]); // Nest data by subject. var countries = d3.nest() .key(function(d) { return d.Country; }) .entries(data); // Add an SVG element for each city var chart = d3.select("#charts").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 country = chart.selectAll(".country") .data(countries) .enter() .append("path") .attr("class", "line") .attr("d", function(d) {return line(d.values); }) .style("stroke", function (d) { if (d.key == "New Zealand") { return "red"; } else if (d.key == "United States") { return "orange"; } else { return "lightgrey"; }; }); country.filter(function(d){ return d.key == "New Zealand" || d.key == "United States"; }) .raise(); // Add a labels /* svg.append("text") .attr("x", (width + 10)/2) .attr("y", height - 85) .style("text-anchor", "middle") .style("font-size", "12px") .attr("fill", "#B35959") .text(function(d) { return d.key; }); svg.append("text") .text(xScale.domain()[0]) .attr("x", 0) .attr("y", height + 15) .style("text-anchor", "start") .style("font-size", "12px") .attr("fill", "#B35959"); svg.append("text") .text(xScale.domain()[1]) .attr("x", width) .attr("y", height + 15) .style("text-anchor", "end") .style("font-size", "12px") .attr("fill", "#B35959");*/ //add axes chart.append("g") .attr("id", "yAxisG").call(yAxis); chart.append("g") .attr("id", "xAxisG") .attr("transform", "translate(0," + height + ")") .call(xAxis); d3.selectAll("path.domain").remove(); d3.selectAll("line").style("stroke", "silver"); }); function convertTextToNumbers(d) { d.Value = +d.Value; return d; } </script>
https://d3js.org/d3.v4.min.js