Built with blockbuilder.org
xxxxxxxxxx
<meta charset="utf-8">
<style>
body {
font: 12px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #ddd;
shape-rendering: crispEdges;
}
.x.axis path {
//fill: #ddd;
stroke: #ddd;
}
.tick {
fill: #999;
}
.rivarly-line {
fill: none;
stroke: #ddd;
stroke-width: 4px;
}
.expected-line {
fill: none;
stroke: #008281;
stroke-width: 4px;
}
.actual-line {
fill: none;
stroke: #0e1a36;
stroke-width: 4px;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 4px;
}
.selectedline {
fill: none;
stroke: steelblue;
stroke-width: 8px;
}
.lineSelected {
}
circle {
pointer-events: all;
cursor: points;
}
.datapoint {
pointer-events: "all";
cursor: "pointer" !imporantant;
stroke-width: 2px;
}
</style>
<body>
<!-- load the d3.js library -->
<script src="https://d3js.org/d3.v5.min.js"></script>
<script>
/*
#ffbd00 - gold
#0e1a36 - royal
#008281 - teal
#fafafa
#c1c1c1 - line
#f2f2f2;
*/
// set the dimensions and margins of the graph
const 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
const parseTime = d3.timeParse("%d-%b-%y");
// set the ranges
const x = d3.scaleTime().range([0, width]);
const y = d3.scaleLinear().range([height, 0]);
// define the line
// line options https://bl.ocks.org/d3noob/ced1b9b18bd8192d2c898884033b5529
// https://bl.ocks.org/emmasaunders/c25a147970def2b02d8c7c2719dc7502
const valueline = d3.line()
.curve(d3.curveCardinal)
.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
const 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 drawChart = (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 100; })]); //d.close;
// 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));
}
const handleMouseClick = (d) => {
}
const handleMouseOver = (d) => {
}
const handleMouseOut = (d) => {
}
const drawLine = (data, lineClass, bDrawPoints) => {
// 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 100; })]); //d.close;
// Add the valueline path.
const path = svg.append("path")
.data([data])
.attr("class", lineClass)
.attr("d", valueline);
if (bDrawPoints) {
svg.append('g').selectAll('circle')
.data(data.reverse())
.enter().append('circle')
.on("click", function(d) { return handleMouseClick(d)})
.on("mouseover", function(d) {return handleMouseOver(d)})
.on("mouseover", function(d) {return handleMouseOut(d)})
.attr("cx", function(d) { return x(d.date) })
.attr("cy", function(d) { return y(d.close) })
.attr("r", 0)
.attr("class","datapoint")
.style("opacity", function(d){ return (d.selected) ? 1 : 1})
.style("fill", "white")
.style("stroke", function(d) { return "#000" })
.style("pointer-events", "all")
.style("cursor", "pointer")
.transition()
.duration(1000)
.delay( function(d,i){ return 250 + ( 750 * i) })
.attr("r", 10);
}
}
// Get the data
d3.csv("actual.csv").then( data => {
drawChart(data);
},
error => {
// handle the error
});
d3.csv("rivalry.csv").then( data => {
drawLine(data, "rivarly-line");
},
error => {
// handle the error
});
d3.csv("expected.csv").then( data => {
drawLine(data, "expected-line");
},
error => {
// handle the error
});
// Get the data
d3.csv("actual.csv").then( data => {
drawLine(data, "actual-line", true);
},
error => {
// handle the error
});
</script>
</body>
https://d3js.org/d3.v5.min.js