Built with blockbuilder.org
Last active
November 1, 2016 13:47
-
-
Save adry34160/805c104179671db0591757b9487ac877 to your computer and use it in GitHub Desktop.
3 - Lline graph with dual Y axes
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
license: mit |
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
date | close | open | |
---|---|---|---|
1-May-12 | 58.13 | 3.41 | |
30-Apr-12 | 53.98 | 4.55 | |
27-Apr-12 | 67.00 | 6.78 | |
26-Apr-12 | 89.70 | 7.85 | |
25-Apr-12 | 99.00 | 8.92 | |
24-Apr-12 | 130.28 | 9.92 | |
23-Apr-12 | 166.70 | 10.13 | |
20-Apr-12 | 234.98 | 12.23 | |
19-Apr-12 | 345.44 | 13.45 | |
18-Apr-12 | 443.34 | 16.04 | |
17-Apr-12 | 543.70 | 18.03 | |
16-Apr-12 | 580.13 | 21.02 | |
13-Apr-12 | 605.23 | 22.34 | |
12-Apr-12 | 622.77 | 20.15 | |
11-Apr-12 | 626.20 | 21.26 | |
10-Apr-12 | 628.44 | 31.04 | |
9-Apr-12 | 636.23 | 35.04 | |
5-Apr-12 | 633.68 | 41.02 | |
4-Apr-12 | 624.31 | 43.05 | |
3-Apr-12 | 629.32 | 46.03 | |
2-Apr-12 | 618.63 | 51.03 | |
30-Mar-12 | 599.55 | 53.42 | |
29-Mar-12 | 609.86 | 57.82 | |
28-Mar-12 | 617.62 | 59.01 | |
27-Mar-12 | 614.48 | 56.03 | |
26-Mar-12 | 606.98 | 58.01 |
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> /* set the CSS */ | |
body { font: 12px Arial;} | |
path { | |
stroke: steelblue; | |
stroke-width: 2; | |
fill: none; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: grey; | |
stroke-width: 1; | |
shape-rendering: crispEdges; | |
} | |
.grid .tick { | |
stroke: lightgrey; | |
stroke-opacity: 0.7; | |
shape-rendering: crispEdges; | |
} | |
.grid path { | |
stroke-width: 0; | |
} | |
text.shadow { | |
stroke: white; | |
stroke-width: 2.5px; | |
opacity: 0.9; | |
} | |
</style> | |
<body> | |
<!-- load the d3.js library --> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script> | |
// Set the dimensions of the canvas / graph | |
var margin = {top: 30, right: 60, bottom: 30, left: 50}, | |
width = 600 - margin.left - margin.right, | |
height = 270 - margin.top - margin.bottom; | |
// Parse the date / time | |
var parseDate = d3.time.format("%d-%b-%y").parse; | |
// Set the ranges | |
var x = d3.time.scale().range([0, width]); | |
var y0 = d3.scale.linear().range([height, 0]); | |
var y1 = d3.scale.linear().range([height, 0]); | |
// Define the axes | |
var xAxis = d3.svg.axis().scale(x) | |
.orient("bottom").ticks(5); | |
var yAxisLeft = d3.svg.axis().scale(y0) | |
.orient("left").ticks(5); | |
var yAxisRight = d3.svg.axis().scale(y1) | |
.orient("right").ticks(5); | |
// Define the line | |
var valueline = d3.svg.line() | |
.x(function(d) { return x(d.date); }) | |
.y(function(d) { return y0(d.close); }); | |
var valueline2 = d3.svg.line() | |
.x(function(d) { return x(d.date); }) | |
.y(function(d) { return y1(d.open); }); | |
// Adds the svg canvas | |
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 + ")"); | |
// Get the data | |
d3.csv("data2a.csv", function(error, data) { | |
data.forEach(function(d) { | |
d.date = parseDate(d.date); | |
d.close = +d.close; | |
d.open = +d.open; | |
}); | |
// Scale the range of the data; | |
x.domain(d3.extent(data, function(d) { return d.date; })); | |
y0.domain([0, d3.max(data, function(d) { return Math.max(d.close); })]); | |
y1.domain([0, d3.max(data, function(d) { return Math.max(d.open); })]); | |
// Add the valueline path. | |
svg.append("path") | |
.attr("class", "line") | |
//.style("stroke-dasharray", ("3,3")) | |
.attr("d", valueline(data)); | |
svg.append("path") | |
.attr("class", "line") | |
.style("stroke-dasharray", ("3,3")) | |
.style("stroke", "red") | |
.attr("d", valueline2(data)); | |
// Add the X Axis | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
// Add the Y0 Axis Left | |
svg.append("g") | |
.attr("class", "y axis") | |
.attr("fill", "steelblue") | |
.call(yAxisLeft); | |
// Add the Y1 Axis Right | |
svg.append("g") | |
.attr("class", "y axis") | |
.attr("transform", "translate(" + width + " ,0)") | |
.attr("fill", "red") | |
.call(yAxisRight); | |
//White shadow Title | |
svg.append("text") | |
.attr("x", (width/2)) | |
.attr("y", 0 - (margin.top / 2)) | |
.attr("text-anchor", "middle") | |
.style("font-size", "16px") | |
.style("text-decoration", "underline") | |
.attr("class", "shadow") | |
.text("Lline graph with dual Y axes"); | |
//Title of the graph | |
svg.append("text") | |
.attr("x", (width/2)) | |
.attr("y", 0 - (margin.top / 2)) | |
.attr("text-anchor", "middle") | |
.style("font-size", "16px") | |
.style("text-decoration", "underline") | |
.text("Lline graph with dual Y axes"); | |
//Text label for the X Axis | |
svg.append("text") | |
.attr("x", width/2) | |
.attr("y", height + margin.bottom) | |
.style("text-anchor", "middle") | |
.text("Date"); | |
//Text label for the Y Axis | |
svg.append("text") | |
.attr("transform", "rotate(-90)") | |
.attr("y", 0 - margin.left) | |
.attr("x", 0 - (height/2)) | |
.attr("dy", "1em") | |
.style("text-anchor", "middle") | |
.text("Value"); | |
svg.append("text") | |
.attr("transform", "translate(" + (width+20) + "," + y0(data[0].open) + ")") | |
.attr("dy", ".35em") | |
.attr("text-anchor", "start") | |
.style("fill", "red") | |
.text("Open"); | |
svg.append("text") | |
.attr("transform", "translate(" + (width+20) + "," + y0(data[0].close) + ")") | |
.attr("dy", ".35em") | |
.attr("text-anchor", "start") | |
.style("fill", "blue") | |
.text("Close"); | |
}); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment