|
<!DOCTYPE html> |
|
<meta charset="utf-8"> |
|
<style> /* set the CSS */ |
|
|
|
.line { |
|
fill: none; |
|
stroke: steelblue; |
|
stroke-width: 2px; |
|
} |
|
|
|
.grid line { |
|
stroke: lightgrey; |
|
stroke-opacity: 0.7; |
|
shape-rendering: crispEdges; |
|
} |
|
|
|
.grid path { |
|
stroke-width: 0; |
|
} |
|
|
|
</style> |
|
<body> |
|
|
|
<!-- load the d3.js library --> |
|
<script src="https://d3js.org/d3.v4.min.js"></script> |
|
<script> |
|
|
|
// set the dimensions and margins of the graph |
|
var margin = {top: 120, right: 120, bottom: 120, left: 120}, |
|
width = 756 - margin.left - margin.right, |
|
height = 576 - margin.top - margin.bottom; |
|
|
|
// parse the date / time |
|
var parseTime = d3.timeParse("%d-%b-%y"); |
|
|
|
var format = d3.format(".3f"); |
|
|
|
// set the ranges |
|
var x = d3.scaleLinear().range([0, width]) |
|
var y = d3.scaleLinear().range([height, 0]); |
|
|
|
// define the line |
|
var valueline = d3.line() |
|
.x(function(d) { return x(d.x); }) |
|
.y(function(d) { return y(d.y); }) |
|
.curve(d3.curveMonotoneX) |
|
|
|
// 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 + ")"); |
|
|
|
// gridlines in x axis function |
|
function make_x_gridlines() { |
|
return d3.axisBottom(x) |
|
.ticks(10) |
|
} |
|
|
|
// gridlines in y axis function |
|
function make_y_gridlines() { |
|
return d3.axisLeft(y) |
|
.ticks(5) |
|
} |
|
|
|
// Get the data |
|
d3.csv("data2.csv", function(error, data) { |
|
if (error) throw error; |
|
|
|
// format the data |
|
data.forEach(function(d) { |
|
// d.date = parseTime(d.date); |
|
// d.close = +d.close; |
|
d.x = +d.x; |
|
d.y = +d.y; |
|
}); |
|
|
|
console.log(data); |
|
|
|
var minY = 1.33; // d3.min(data, d => d.y); |
|
var maxY = d3.max(data, d => d.y); |
|
var range = d3.range(minY, maxY, 0.005); |
|
console.log(minY, maxY, range) |
|
|
|
// Scale the range of the data |
|
x.domain(d3.extent(data, function(d) { return d.x; })); |
|
y.domain([minY, maxY]); |
|
|
|
// add the X gridlines |
|
svg.append("g") |
|
.attr("class", "grid") |
|
.attr("transform", "translate(0," + height + ")") |
|
.call(make_x_gridlines() |
|
.tickSize(-height) |
|
.tickFormat("") |
|
) |
|
|
|
// add the Y gridlines |
|
svg.append("g") |
|
.attr("class", "grid") |
|
.call(make_y_gridlines() |
|
.tickSize(-width) |
|
.tickFormat("") |
|
) |
|
|
|
// add the valueline path. |
|
svg.append("path") |
|
.data([data]) |
|
.attr("class", "line") |
|
.attr("d", valueline); |
|
|
|
// add the X Axis |
|
svg.append("g") |
|
.attr("transform", "translate(0," + height + ")") |
|
.call(d3.axisBottom(x)); |
|
|
|
svg.append("g") |
|
.attr("transform", `translate(0, ${height})`) |
|
.call(customXAxis) |
|
|
|
// add the Y Axis |
|
svg.append("g") |
|
.call(d3.axisLeft(y) |
|
.tickFormat(format) |
|
.tickValues(range) |
|
); |
|
|
|
svg.append("g") |
|
.attr("id", "custom-y-axis") |
|
.call(customYAxis); |
|
|
|
function customXAxis(g) { |
|
g.call(d3.axisBottom(x) |
|
.ticks(100) |
|
.tickFormat("")) |
|
g.select(".domain").remove(); |
|
g.selectAll(".tick").select("line").attr("y2", -6); |
|
} |
|
|
|
function customYAxis(g) { |
|
g.call(d3.axisLeft(y) |
|
.ticks(100) |
|
.tickFormat("")) |
|
g.select(".domain").remove(); |
|
g.selectAll(".tick").select("line").attr("x2", 6); |
|
} |
|
|
|
}); |
|
|
|
</script> |
|
</body> |