Skip to content

Instantly share code, notes, and snippets.

@AnsonT
Last active August 9, 2018 22:03
Show Gist options
  • Save AnsonT/68bdc390292289d5ecdd9daa6de949a4 to your computer and use it in GitHub Desktop.
Save AnsonT/68bdc390292289d5ecdd9daa6de949a4 to your computer and use it in GitHub Desktop.
Daily Tracks
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
.axis { font: 14px sans-serif; }
.line {
fill: none;
stroke: steelblue;
stroke-width: 2px;
}
</style>
</head>
<body>
<script type="text/babel" data-presets="es2017">
// set the dimensions and margins of the graph
var margin = {top: 20, right: 20, bottom: 70, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// parse the date / time
var parseTime = d3.timeParse("%d-%b-%y");
// set the ranges
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
// define the line
var valueline = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.close); });
// append the svg obgect to the body of the page
// appends a 'group' element to 'svg'
// moves the 'group' element to the top left margin
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 + ")");
const data = [
{date: '1-May-12', close: 93.64},
{date: '1-May-12', close: 74.64},
{date: '3-May-12', close: 52.12},
{date: '4-May-12', close: 44.64}
]
// Get the data
// format the data
data.forEach(function(d) {
d.date = parseTime(d.date);
d.close = +d.close;
});
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.close; })]);
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 5)
.attr("cx", function(d) { return x(d.date); })
.attr("cy", function(d) { return y(d.close); })
.style("fill", 'black')
svg.selectAll(".dot2")
.data(data)
.enter().append("circle")
.attr("class", "dot2")
.attr("r", 5)
.attr("cx", function(d) { return x(d.date); })
.attr("cy", function(d) { return y(d.close*1.1); })
.style("fill", 'black')
svg.append('g')
.selectAll('line')
.data(data).enter()
.append('line')
.attr("class", "line")
.attr('x1', d => x(d.date))
.attr('y1', d => y(d.close))
.attr('x2', d => x(d.date))
.attr('y2', d => y(d.close * 1.1))
svg.append('g')
.selectAll('rect')
.data(data).enter()
.append('rect')
.attr('width', 10)
.attr('height', d => y(d.close) - y(d.close*1.1))
.attr('x', d => x(d.date) - 5)
.attr('y', d => y(d.close*1.1))
// Add the X Axis
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x).ticks(10))
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("transform", "rotate(-65)");
// Add the Y Axis
svg.append("g")
.attr("class", "axis")
.call(d3.axisLeft(y));
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment