This line chart is constructed from a TSV file storing the daily average temperatures of New York, San Francisco and Austin over the last year. The chart employs conventional margins and a number of D3 features:
forked from mbostock's block: Multi-Series Line Chart
xxxxxxxxxx
<meta charset="utf-8">
<style>
.line {
fill: none;
stroke: orange;
stroke-width: 3.5px;
}
</style>
<body>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
// set the dimensions and margins of the graph
var margin = {top: 50, right: 20, bottom: 100, left: 50},
width = 700 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// parse the date / time
var parseTime = d3.timeParse("%m/%d/%y");
// set the ranges
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
// define the line
var valueline = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.IncidntNum); });
// 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.csv("data.csv", function(error, data) {
if (error) throw error;
// format the data
data.forEach(function(d) {
d.IncidntNum = +d.IncidntNum;
d.date = parseTime(d.date);
});
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.IncidntNum; })]);
// 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).tickFormat(d3.timeFormat("%m/%d")));
// Add the Y Axis
svg.append("g")
.call(d3.axisLeft(y));
svg.append("text")
.attr("x", (0))
.attr("y", -5 - (margin.top/2) )
.style("font-size", "16px")
.style("font-family", "Avenir")
.text("Line graph showing the number of incidents of a week" +
"in December 2016");
//x axis label for "date"
svg.append("text")
.attr("class", "x label")
.attr("text-anchor", "end")
.attr("x", width/2)
.attr("y", height + 40)
.style("font-family", "Avenir")
.style("font-size", "12px")
.text("Date");
svg.append("text")
.attr("class", "y label")
.attr("text-anchor", "end")
.attr("y", -45)
.attr("x", -120)
.attr("dy", ".75em")
.attr("transform", "rotate(-90)")
.style("font-family", "Avenir")
.style("font-size", "10px")
.text("Number of assaults");
//Caption
svg.append("text")
.attr("class", "x label")
.attr("text-anchor", "end")
.attr("x", width + 1000)
.attr("y", height + 80)
.style("font-family", "Avenir")
.style("font-size", "12px")
.text("It seems that the lowest number of physical assaults in this" +
"week was on a Sunday and Wednesday.While the other days were" +
"relatively high. Although it seems the thursday (12/1) was quite low," +
"but the following thursday the number of assaults became relatively" +
"high just like the other days." +
" " +
"Line graph by Sherry Feng");
});
</script>
</body>
https://d3js.org/d3.v4.min.js