Skip to content

Instantly share code, notes, and snippets.

@rob4acre
Last active January 9, 2018 10:51
Show Gist options
  • Save rob4acre/34fc16b468044988cef97d9f041fc9b8 to your computer and use it in GitHub Desktop.
Save rob4acre/34fc16b468044988cef97d9f041fc9b8 to your computer and use it in GitHub Desktop.
Minimum Microlibs
license: mit

This is a simple line graph demonstrating the addition of axis labels. This was written using d3.js v4 and is a follow on to the simple graph example here.

This graph is part of the code samples for the update to the book D3 Tips and Tricks to version 4 of d3.js.

forked from d3noob's block: Axis Labels in v4

date close
1-May-12 58.13
30-Apr-12 53.98
27-Apr-12 67.00
26-Apr-12 89.70
25-Apr-12 99.00
24-Apr-12 130.28
23-Apr-12 166.70
20-Apr-12 234.98
19-Apr-12 345.44
18-Apr-12 443.34
17-Apr-12 543.70
16-Apr-12 580.13
13-Apr-12 605.23
12-Apr-12 622.77
11-Apr-12 626.20
10-Apr-12 628.44
9-Apr-12 636.23
5-Apr-12 633.68
4-Apr-12 624.31
3-Apr-12 629.32
2-Apr-12 618.63
30-Mar-12 599.55
29-Mar-12 609.86
28-Mar-12 617.62
27-Mar-12 614.48
26-Mar-12 606.98
[
{"id": 1, "ibo": 40.0, "response_id": 1, "obo": 32.5390625},
{"id": 2, "ibo": 38.0, "response_id": 1, "obo": 30.8984375},
{"id": 3, "ibo": 36.0, "response_id": 1, "obo": 28.7109375},
{"id": 4, "ibo": 34.0, "response_id": 1, "obo": 26.796875},
{"id": 19, "ibo": 4.0, "response_id": 1, "obo": 0.0},
{"id": 5, "ibo": 32.0, "response_id": 1, "obo": 24.3359375},
{"id": 6, "ibo": 30.0, "response_id": 1, "obo": 22.421875},
{"id": 7, "ibo": 28.0, "response_id": 1, "obo": 20.5078125},
{"id": 8, "ibo": 26.0, "response_id": 1, "obo": 18.3203125},
{"id": 9, "ibo": 24.0, "response_id": 1, "obo": 16.6796875},
{"id": 10, "ibo": 22.0, "response_id": 1, "obo": 14.4921875},
{"id": 11, "ibo": 20.0, "response_id": 1, "obo": 12.3046875},
{"id": 12, "ibo": 18.0, "response_id": 1, "obo": 10.6640625},
{"id": 14, "ibo": 14.0, "response_id": 1, "obo": 7.109375},
{"id": 15, "ibo": 12.0, "response_id": 1, "obo": 5.46875},
{"id": 16, "ibo": 10.0, "response_id": 1, "obo": 3.5546875},
{"id": 17, "ibo": 8.0, "response_id": 1, "obo": 2.1875},
{"id": 13, "ibo": 16.0, "response_id": 1, "obo": 8.753},
{"id": 18, "ibo": 6.0, "response_id": 1, "obo": 0.8203125},
{"id": 20, "ibo": 2.0, "response_id": 1, "obo": 0.0},
{"id": 21, "ibo": 0.0, "response_id": 1, "obo": 0.0}
]
<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */
.line {
fill: none;
stroke: steelblue;
stroke-width: 2px;
}
</style>
<body>
<!-- load the d3.js library -->
<!-- script src="https://d3js.org/d3.v4.min.js"></script-->
<script src="https://d3js.org/d3-selection.v1.min.js"></script>
<script src="https://d3js.org/d3-array.v1.min.js"></script>
<script src="https://d3js.org/d3-axis.v1.min.js"></script>
<script src="https://d3js.org/d3-collection.v1.min.js"></script>
<script src="https://d3js.org/d3-color.v1.min.js"></script>
<script src="https://d3js.org/d3-dispatch.v1.min.js"></script>
<script src="https://d3js.org/d3-format.v1.min.js"></script>
<script src="https://d3js.org/d3-path.v1.min.js"></script>
<script src="https://d3js.org/d3-request.v1.min.js"></script>
<script src="https://d3js.org/d3-interpolate.v1.min.js"></script>
<script src="https://d3js.org/d3-scale.v1.min.js"></script>
<script src="https://d3js.org/d3-shape.v1.min.js"></script>
<script>
// set the dimensions and margins of the graph
var margin = {top: 20, right: 20, bottom: 50, left: 70},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// 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.ibo); })
.y(function(d) { return y(d.obo); });
// 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', function(error, data) {
if (error) throw error;
data.sort(function(a, b){
return a["ibo"]-b["ibo"];
})
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.ibo; }));
y.domain(d3.extent(data, function(d) { return d.obo; }));
// Add the valueline path.
svg.append("path")
.datum(data)
.attr("class", "line")
.style("stroke", "blue")
.attr("d", d3.line()
.curve(d3.curveMonotoneX)
.x(function(d) { return x(d.ibo); })
.y(function(d) { return y(d.obo); })
);
// Add the Legend
svg.append("text")
.attr("x", width/2) // space legend
.attr("y", margin.top)
.attr("class", "legend") // style the legend
.style("fill", "black")
.text("IBO vs OBO");
// Add the scatterplot
svg.selectAll("dot")
.data(data)
.enter().append("circle")
.attr("r", 3)
.attr("cx", function(d) { return x(d.ibo); })
.attr("cy", function(d) { return y(d.obo); });
// Add the x Axis
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// text label for the x axis
svg.append("text")
.attr("transform",
"translate(" + (width/2) + " ," +
(height + margin.top + 20) + ")")
.style("text-anchor", "middle")
.text("IBO");
// Add the y Axis
svg.append("g")
.call(d3.axisLeft(y));
// 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("OBO");
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment