Last active
February 14, 2019 07:33
-
-
Save LyssenkoAlex/1317e8dcf3d40f33b341552cf82a10b2 to your computer and use it in GitHub Desktop.
d3.js v5 line chart with dates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
font-family: sans-serif; | |
color: #444; | |
} | |
.line { | |
fill: none; | |
stroke: #ffab00; | |
stroke-width: 3; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.axis text { | |
font-size: 10px; | |
} | |
</style> | |
<body> | |
<h4>Week of Year Example</h4> | |
<div id="svg"></div> | |
<script src="https://d3js.org/d3.v5.min.js"></script> | |
<script> | |
var lineData = []; | |
lineData.push({date:new Date(2019, 01, 04), nps:89}); | |
lineData.push({date:new Date(2019, 01, 11), nps:96}); | |
lineData.push({date:new Date(2019, 01, 18), nps:87}); | |
lineData.push({date:new Date(2019, 01, 25), nps:99}); | |
lineData.push({date:new Date(2019, 02, 04), nps:83}); | |
lineData.push({date:new Date(2019, 02, 11), nps:93}); | |
lineData.push({date:new Date(2019, 02, 18), nps:79}); | |
lineData.push({date:new Date(2019, 02, 25), nps:94}); | |
lineData.push({date:new Date(2019, 03, 4), nps:89}); | |
lineData.push({date:new Date(2019, 03, 11), nps:93}); | |
lineData.push({date:new Date(2019, 03, 18), nps:81}); | |
lineData.sort(function(a,b){ | |
return new Date(b.date) - new Date(a.date); | |
}); | |
var height = 200; | |
var width = 700; | |
var hEach = 40; | |
var margin = {top: 20, right: 15, bottom: 25, left: 25}; | |
width = width - margin.left - margin.right; | |
height = height - margin.top - margin.bottom; | |
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 + ")"); | |
// set the ranges | |
var x = d3.scaleTime().range([0, width]); | |
x.domain(d3.extent(lineData, function(d) { return d.date; })); | |
var y = d3.scaleLinear().range([height, 0]); | |
y.domain([d3.min(lineData, function(d) { return d.nps; }) - 5, 100]); | |
var valueline = d3.line() | |
.x(function(d) { return x(d.date); }) | |
.y(function(d) { return y(d.nps); }) | |
.curve(d3.curveMonotoneX); | |
svg.append("path") | |
.data([lineData]) | |
.attr("class", "line") | |
.attr("d", valueline); | |
var xAxis_woy = d3.axisBottom(x).tickFormat(d3.timeFormat("Week %V")).tickValues(lineData.map(d=>d.date)); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis_woy); | |
// Add the Y Axis | |
// svg.append("g").call(d3.axisLeft(y)); | |
svg.selectAll(".dot") | |
.data(lineData) | |
.enter() | |
.append("circle") // Uses the enter().append() method | |
.attr("class", "dot") // Assign a class for styling | |
.attr("cx", function(d) { return x(d.date) }) | |
.attr("cy", function(d) { return y(d.nps) }) | |
.attr("r", 5); | |
svg.selectAll(".text") | |
.data(lineData) | |
.enter() | |
.append("text") // Uses the enter().append() method | |
.attr("class", "label") // Assign a class for styling | |
.attr("x", function(d, i) { return x(d.date) }) | |
.attr("y", function(d) { return y(d.nps) }) | |
.attr("dy", "-5") | |
.text(function(d) {return d.nps; }); | |
svg.append('text') | |
.attr('x', 10) | |
.attr('y', -5) | |
.text('Almaty'); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment