Skip to content

Instantly share code, notes, and snippets.

@alandunning
Last active October 5, 2017 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alandunning/43e1f92358005270d5c86e32b13bf44f to your computer and use it in GitHub Desktop.
Save alandunning/43e1f92358005270d5c86e32b13bf44f to your computer and use it in GitHub Desktop.
Reading - Line Chart with Circles D3 V4
license: gpl-3.0

This chart is based on mbostock's block: Line Chart

it uses a json file storing year values and population values. The chart employs conventional margins and a number of D3 features:

forked from alandunning's block: Line Chart with Circle Tooltip D3 V4

forked from anonymous's block: Reading - Line Chart with Circle Tooltip D3 V4

[
{"date" : "12/09/17", "value": 83, "module": 0},
{"date" : "19/09/17", "value": 79, "module": 1},
{"date" : "26/09/17", "value": 78, "module": 2},
{"date" : "30/09/17", "value": 73, "module": 3},
{"date" : "08/10/17", "value": 71, "module": 4},
{"date" : "15/10/17", "value": 67, "module": 5},
{"date" : "22/10/17", "value": 65, "module":6}
]
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font-family: 'Droid Sans', sans-serif;
}
.axis {
font-size: 14px;
font-weight: bold;
}
text {
fill: #727075;
stroke: none;
}
.axis path,
.axis line {
fill: none;
stroke: none;
stroke-width: 2px;
shape-rendering: crispEdges;
}
.grid path {
stroke: none;
}
.grid line {
stroke: #E0E0E0;
shape-rendering: crispEdges;
}
.data-line {
fill: none;
stroke: #3C92BA;
stroke-width: 4px;
}
.data-circle {
fill: #3C92BA;
}
.axis-title {
text-anchor: end;
fill: #5D6971;
font-weight: normal;
}
.axis-tspan {
font-size: 12px;
}
.clinical-cut-off-line {
fill: none;
stroke: #89868E;
stroke-dasharray: 8,8;
stroke-width: 4px;
}
.clinical-cut-off-text {
text-transform: uppercase;
text-anchor: start;
font-size: 12px;
font-weight: bold;
}
</style>
<link href="https://fonts.googleapis.com/css?family=Droid+Sans:400,700" rel="stylesheet">
<body>
<svg width="730" height="375"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 30, left: 40},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
var parseTime = d3.timeParse("%d/%m/%y");
bisectDate = d3.bisector(function(d) { return d.module; }).left;
var clinicalCutOffValue = 70;
var x = d3.scaleLinear().range([0, width - (margin.left + margin.right)]);
var y = d3.scaleLinear().range([height - (margin.top * 2), 0]);
var line = d3.line()
.x(function(d) { return x(d.module); })
.y(function(d) { return y(d.value); });
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.json("data.json", function(error, data) {
if (error) throw error;
data.forEach(function(d) {
d.value = +d.value;
d.module = +d.module;
});
x.domain(d3.extent(data, function(d) { return d.module; }));
y.domain([
(Math.floor(d3.min(data, function(d) { return d.value; }) / 10) * 10),
(Math.ceil(d3.max(data, function(d) { return d.value; }) / 10) * 10)
]);
// add the Y gridlines
g.append("g")
.attr("class", "grid")
.call(d3.axisLeft(y)
.tickSize(-width)
.tickFormat("")
.ticks(6)
);
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(" + ((margin.left + margin.right) / 2) + "," + (height - margin.top) + ")")
.call(d3.axisBottom(x)
.ticks(6)
.tickFormat(function(d) { return 'Module ' + d; })
)
.selectAll('.axis--x .tick text')
.append('tspan')
.attr("class", "axis-tspan")
.attr("x", 0)
.attr("y", 0)
.attr("dy", margin.top * 2)
.text(function(d) { return data[d].date; });
g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y).ticks(6))
.append("text")
.attr("class", "axis-title")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.text("T Scores");
// Clinicial cut off line and text group
var clinicalCutOffLineAndText = g.append("g")
.attr("class", "clinical-cut-off-line-and-text")
// Clinicial cut off line
clinicalCutOffLineAndText.append("line")
.attr("class", "clinical-cut-off-line")
.attr("x1", 0)
.attr("y1", y(clinicalCutOffValue))
.attr("x2", width)
.attr("y2", y(clinicalCutOffValue));
// Clinicial cut off text
clinicalCutOffLineAndText.append("text")
.attr("class", "clinical-cut-off-text")
.attr("y", y(clinicalCutOffValue))
.attr("dy","20px")
.text("Clinical Cut-off");
// Data line and dots group
var lineAndDots = g.append("g")
.attr("class", "line-and-dots")
.attr("transform", "translate(" + ((margin.left + margin.right) / 2) + "," + 0 + ")")
// Data line
lineAndDots.append("path")
.datum(data)
.attr("class", "data-line")
.attr("d", line);
// Data dots
lineAndDots.selectAll("line-circle")
.data(data)
.enter().append("circle")
.attr("class", "data-circle")
.attr("r", 5)
.attr("cx", function(d) { return x(d.module); })
.attr("cy", function(d) { return y(d.value); });
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment