Built with blockbuilder.org
Last active
September 22, 2017 15:41
-
-
Save jbelmont/fa75876b679907a613d61805c37231a8 to your computer and use it in GitHub Desktop.
Area Chart
This file contains hidden or 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
license: mit |
This file contains hidden or 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
[ | |
{ | |
"ticker": "MSFT", | |
"values": [ | |
{ | |
"date": "2017-08-22", | |
"close": 73.160004 | |
}, | |
{ | |
"date": "2017-08-23", | |
"close": 72.720001 | |
}, | |
{ | |
"date": "2017-08-24", | |
"close": 72.690002 | |
}, | |
{ | |
"date": "2017-08-25", | |
"close": 72.82 | |
}, | |
{ | |
"date": "2017-08-28", | |
"close": 72.830002 | |
}, | |
{ | |
"date": "2017-08-29", | |
"close": 73.050003 | |
}, | |
{ | |
"date": "2017-08-30", | |
"close": 74.010002 | |
}, | |
{ | |
"date": "2017-08-31", | |
"close": 74.769997 | |
}, | |
{ | |
"date": "2017-09-01", | |
"close": 73.940002 | |
}, | |
{ | |
"date": "2017-09-05", | |
"close": 73.610001 | |
}, | |
{ | |
"date": "2017-09-06", | |
"close": 73.400002 | |
}, | |
{ | |
"date": "2017-09-07", | |
"close": 74.339996 | |
}, | |
{ | |
"date": "2017-09-08", | |
"close": 73.980003 | |
}, | |
{ | |
"date": "2017-09-11", | |
"close": 74.760002 | |
}, | |
{ | |
"date": "2017-09-12", | |
"close": 74.68 | |
}, | |
{ | |
"date": "2017-09-13", | |
"close": 75.209999 | |
}, | |
{ | |
"date": "2017-09-14", | |
"close": 74.769997 | |
}, | |
{ | |
"date": "2017-09-15", | |
"close": 75.309998 | |
}, | |
{ | |
"date": "2017-09-18", | |
"close": 75.160004 | |
}, | |
{ | |
"date": "2017-09-19", | |
"close": 75.440002 | |
}, | |
{ | |
"date": "2017-09-20", | |
"close": 74.940002 | |
}, | |
{ | |
"date": "2017-09-21", | |
"close": 74.209999 | |
}, | |
{ | |
"date": "2017-09-22", | |
"close": 74.349998 | |
} | |
] | |
}, | |
{ | |
"ticker": "AAPL", | |
"values": [ | |
{ | |
"date": "2017-08-22", | |
"close": 73.160004 | |
}, | |
{ | |
"date": "2017-08-23", | |
"close": 72.720001 | |
}, | |
{ | |
"date": "2017-08-24", | |
"close": 72.690002 | |
}, | |
{ | |
"date": "2017-08-25", | |
"close": 72.82 | |
}, | |
{ | |
"date": "2017-08-28", | |
"close": 72.830002 | |
}, | |
{ | |
"date": "2017-08-29", | |
"close": 73.050003 | |
}, | |
{ | |
"date": "2017-08-30", | |
"close": 74.010002 | |
}, | |
{ | |
"date": "2017-08-31", | |
"close": 74.769997 | |
}, | |
{ | |
"date": "2017-09-01", | |
"close": 73.940002 | |
}, | |
{ | |
"date": "2017-09-05", | |
"close": 73.610001 | |
}, | |
{ | |
"date": "2017-09-06", | |
"close": 73.400002 | |
}, | |
{ | |
"date": "2017-09-07", | |
"close": 74.339996 | |
}, | |
{ | |
"date": "2017-09-08", | |
"close": 73.980003 | |
}, | |
{ | |
"date": "2017-09-11", | |
"close": 74.760002 | |
}, | |
{ | |
"date": "2017-09-12", | |
"close": 74.68 | |
}, | |
{ | |
"date": "2017-09-13", | |
"close": 75.209999 | |
}, | |
{ | |
"date": "2017-09-14", | |
"close": 74.769997 | |
}, | |
{ | |
"date": "2017-09-15", | |
"close": 75.309998 | |
}, | |
{ | |
"date": "2017-09-18", | |
"close": 75.160004 | |
}, | |
{ | |
"date": "2017-09-19", | |
"close": 75.440002 | |
}, | |
{ | |
"date": "2017-09-20", | |
"close": 74.940002 | |
}, | |
{ | |
"date": "2017-09-21", | |
"close": 74.209999 | |
}, | |
{ | |
"date": "2017-09-22", | |
"close": 74.349998 | |
} | |
] | |
} | |
] |
This file contains hidden or 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> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<style> | |
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
.area-chart { | |
background: lightgray; | |
border: 1px solid black; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="area-chart"><div> | |
<script> | |
function setDimensions(selection, key, val) { | |
selection.attr(key, val); | |
} | |
function responsivefy(svg) { | |
// get container + svg aspect ratio | |
var container = d3.select(svg.node().parentNode), | |
width = parseInt(svg.style("width")), | |
height = parseInt(svg.style("height")), | |
aspect = width / height; | |
// add viewBox and preserveAspectRatio properties, | |
// and call resize so that svg resizes on inital page load | |
svg.attr("viewBox", "0 0 " + width + " " + height) | |
.attr("preserveAspectRatio", "xMinYMid") | |
.call(resize); | |
// to register multiple listeners for same event type, | |
// you need to add namespace, i.e., 'click.foo' | |
// necessary if you call invoke this function for multiple svgs | |
// api docs: https://github.com/mbostock/d3/wiki/Selections#on | |
d3.select(window).on("resize." + container.attr("id"), resize); | |
// get width of container and resize svg to fit it | |
function resize() { | |
var targetWidth = parseInt(container.style("width")); | |
svg.attr("width", targetWidth); | |
svg.attr("height", Math.round(targetWidth / aspect)); | |
} | |
} | |
const margin = { | |
top: 10, | |
right: 20, | |
bottom: 30, | |
left: 30 | |
}; | |
const dimensions = { | |
width: 400, | |
height: 565 | |
}; | |
const svg = d3.select('.area-chart') | |
.append('svg') | |
.call(setDimensions, 'width', dimensions.width + margin.left + margin.right) | |
.call(setDimensions, 'height', dimensions.height + margin.top + margin.bottom) | |
.call(responsivefy) | |
.append('g') | |
.attr('transform', `translate(${margin.left},${margin.right})`); | |
d3.json('data.json', (err, data) => { | |
if (err) console.error(err); | |
const parseTime = d3.timeParse('%Y-%m-%d'); | |
data.forEach(company => { | |
company.values.forEach(d => { | |
d.date = parseTime(d.date); | |
d.close = +d.close; | |
}); | |
}); | |
let xScale = d3.scaleTime() | |
.domain([ | |
d3.min(data, co => d3.min(co.values, d => d.date)), | |
d3.max(data, co => d3.max(co.values, d => d.date)) | |
]) | |
.range([0, dimensions.width]) | |
svg.append('g') | |
.attr('transform', `translate(0, ${dimensions.height})`) | |
.call(d3.axisBottom(xScale).ticks(5)) | |
let yScale = d3.scaleLinear() | |
.domain([ | |
d3.min(data, co => d3.min(co.values, d => d.close)), | |
d3.max(data, co => d3.max(co.values, d => d.close)) | |
]) | |
.range([0, dimensions.height]) | |
svg.append('g') | |
.call(d3.axisLeft(yScale)); | |
let area = d3.area() | |
.x(d => xScale(d.date)) | |
.y0(yScale(yScale.domain()[0])) | |
.y1(d => yScale(d.close)) | |
.curve(d3.curveCatmullRom.alpha(0.5)); | |
svg | |
.selectAll('.area') | |
.data(data) | |
.enter() | |
.append('path') | |
.attr('class', 'area') | |
.attr('d', d => area(d.values)) | |
.style('stroke', (d, i) => ['#FF9900', '#3369E8'][i]) | |
.style('stroke-width', 2) | |
.style('fill', (d, i) => ['#FF9900', '#3369E8'][i]) | |
.style('fill-opacity', 0.25); | |
}); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment