Skip to content

Instantly share code, notes, and snippets.

@glimpsedchaos
Created December 16, 2019 14:39
Show Gist options
  • Save glimpsedchaos/bd38074ceae386fd094cff95ef8f24a3 to your computer and use it in GitHub Desktop.
Save glimpsedchaos/bd38074ceae386fd094cff95ef8f24a3 to your computer and use it in GitHub Desktop.
Double Line graph using JSON
license: mit
[
{
"date": "1-May-12",
"close": 53.13,
"open": 3.41
}, {
"date": "30-Apr-12",
"close": 54.12,
"open": 4.12
}, {
"date": "27-Apr-12",
"close": 5.12,
"open": 2.65
}, {
"date": "26-Apr-12",
"close": 55.415,
"open": 5.65
}, {
"date": "25-Apr-12",
"close": 65.12,
"open": 6.12
}, {
"date": "24-Apr-12",
"close": 75.12,
"open": 7.12
}, {
"date": "23-Apr-12",
"close": 85.65,
"open": 8.50
}, {
"date": "20-Apr-12",
"close": "",
"open": 9.6
}, {
"date": "19-Apr-12",
"close": 95.25,
"open": 10.87
}, {
"date": "18-Apr-12",
"close": 105.645,
"open": 11.54
}, {
"date": "17-Apr-12",
"close": 104.841,
"open": 12.98
}, {
"date": "16-Apr-12",
"close": 204.958,
"open": 14.01
}, {
"date": "13-Apr-12",
"close": 154.9,
"open": 15.55
}, {
"date": "12-Apr-12",
"close": 210.6,
"open": 18.564
}, {
"date": "11-Apr-12",
"close": 220.56,
"open": 19.54
}, {
"date": "10-Apr-12",
"close": 230.98,
"open": 20.54
}, {
"date": "9-Apr-12",
"close": 248.984,
"open": 21.95
}, {
"date": "5-Apr-12",
"close": 260.4948,
"open": 20.98
}, {
"date": "4-Apr-12",
"close": 250.94,
"open": 22.48
}, {
"date": "3-Apr-12",
"close": 310.65,
"open": 23.97
}, {
"date": "2-Apr-12",
"close": 350.494,
"open": 24.8974
}, {
"date": "30-Mar-12",
"close": 365.145,
"open": 29.94
}, {
"date": "29-Mar-12",
"close": 145.564,
"open": 41.54
}, {
"date": "28-Mar-12",
"close": 370.23,
"open": 30.12
}, {
"date": "27-Mar-12",
"close": 380.65,
"open": 31.98
}, {
"date": "26-Mar-12",
"close": 390.64,
"open": 3.94
}
]
<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */
.line {
fill: none;
stroke: steelblue;
stroke-width: 2px;
}
.axisSteelBlue text{
fill: steelblue;
}
.axisRed text{
fill: red;
}
</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: 20, right: 40, bottom: 30, 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 y0 = d3.scaleLinear().range([height, 0]);
var y1 = d3.scaleLinear().range([height, 0]);
// define the 1st line
var valueline = d3.line()
.x((d) => x(d.date))
.y((d) => y0(d.close));
// define the 2nd line
var valueline2 = d3.line()
.x((d) => x(d.date))
.y((d) => y1(d.open));
// 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 + ")");
// Get the data
d3.json("data.json", (error, data) => {
if (error) throw error;
// format the data
data.forEach((d) => {
d.date = parseTime(d.date);
d.close = +d.close;
d.open = +d.open;
});
// Scale the range of the data
x.domain(d3.extent(data, d => d.date));
y0.domain([0, d3.max(data, d => Math.max(d.close))]);
y1.domain([0, d3.max(data, d => Math.max(d.open))]);
// Add the valueline path.
svg.append("path")
.data([data])
.attr("class", "line")
.attr("d", valueline);
// Add the valueline2 path.
svg.append("path")
.data([data])
.attr("class", "line")
.style("stroke", "red")
.attr("d", valueline2);
// Add the X Axis
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// Add the Y0 Axis
svg.append("g")
.attr("class", "axisSteelBlue")
.call(d3.axisLeft(y0));
// Add the Y1 Axis
svg.append("g")
.attr("class", "axisRed")
.attr("transform", "translate( " + width + ", 0 )")
.call(d3.axisRight(y1));
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment