D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
manojwajekar
Full window
Github gist
Line_chart
Built with
blockbuilder.org
<!DOCTYPE html> <svg width="960" height="500"></svg> <script src="https://d3js.org/d3.v4.min.js"></script> <script> var svg = d3.select("svg"), margin = {top: 20, right: 20, bottom: 30, left: 50}, width = +svg.attr("width") - margin.left - margin.right, height = +svg.attr("height") - margin.top - margin.bottom, g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var parseTime = d3.timeParse("%Y"); var x = d3.scaleTime() .rangeRound([0, width]); var y = d3.scaleLinear() .rangeRound([height, 0]); var line = d3.line() .x(function(d) { return x(d.leads_created_date_year); }) .y(function(d) { return y(d.total); }); function draw () { var data = [{"total": 1.0, "leads_created_date_year": 2016}, {"total": 0.6, "leads_created_date_year": 2018}, {"total": 0.75, "leads_created_date_year": 2017}]; data.forEach(function(d) { // d.leads_intended_storage_duration_tiers = (d.leads_intended_storage_duration_tiers); d.leads_created_date_year = parseTime(d.leads_created_date_year); }); x.domain(d3.extent(data, function(d) { return d.leads_created_date_year; })); y.domain(d3.extent(data, function(d) { return d.total; })); g.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x).ticks(3).tickFormat(d3.timeFormat("%Y"))) .select(".domain") .remove(); g.append("g") .call(d3.axisLeft(y)) .append("text") .attr("fill", "#000") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", "0.71em") .attr("text-anchor", "end") .text("Conversion Rate"); g.append("path") .datum(data) .attr("fill", "none") .attr("stroke", "steelblue") .attr("stroke-linejoin", "round") .attr("stroke-linecap", "round") .attr("stroke-width", 1.5) .attr("d", line); }; draw(); </script>
https://d3js.org/d3.v4.min.js