score:7
Accepted answer
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Unemployment by Ward Bar Chart</title>
<style type="text/css">
.axis text{
font-family: Arial;
font-size: 13px;
color: #333333;
text-anchor: end;
}
path {
stroke: steelblue;
stroke-width: 2;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
}
.textlabel{
font-family: Arial;
font-size:13px;
color: #333333;
text-anchor: middle;
}
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
// Set the dimensions of the canvas / graph
var margin = {top: 20, right: 0, bottom: 60, left: 60},
width = 475;
height = 350;
padding = 100;
// 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("viewBox", "0 0 " + width + " " + height);
// Parse the date / time
var parseDate = d3.time.format("%m/%d/%y").parse;
var formatTax = d3.format(",.2f");
// Set the ranges
var x = d3.time.scale()
.range([0, width - margin.right - margin.left], .1);
var y = d3.scale.linear()
.range([height - margin.top - margin.bottom, 0]);
// Define the axes
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(5);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(function(d) {return "$" + d + "B"});
// Define the line
var valueline = d3.svg.line()
.x(function(d) { return x(d.Date); })
.y(function(d) { return y(d["Tax Collections"]); });
// Get the data
d3.csv("Yearly Tax Collections.csv", function(error, data) {
data.forEach(function(d) {
d.Date = parseDate(d.Date);
d["Tax Collections"] = formatTax(+d["Tax Collections"]/1000000000);
});
// 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["Tax Collections"]; })]);
// Add the valueline path.
svg.append("path")
.attr("class", "line")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.attr("d", valueline(data));
// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(" + margin.left + "," + (height - margin.bottom) + ")")
.call(xAxis);
// Add the Y Axis
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.call(yAxis);
// Y-axis labels
svg.append("text")
.attr("text-anchor", "middle")
.style("font-size", "13px")
.style("color", "#333333")
.attr("transform", "translate ("+ (padding/4) + "," +(height/2)+") rotate(-90)")
.text("Tax Revenue")
.style("font-family", "Arial");
// X-axis labels
svg.append("text")
.attr("text-anchor", "middle")
.style("font-size", "13px")
.style("color", "#333333")
.attr("transform", "translate("+ (width/2) + "," +(height-(padding/4)) + ")")
.text("Fiscal Year")
.style("font-family", "Arial");
//source
svg.append("text")
.attr("text-anchor", "middle")
.style("font-size", "13px")
.style("color", "#333333")
.attr("transform", "translate("+ (width/4.5) + "," +(height/1) + ")")
.text("Source: DC OCFO")
.style("font-family", "Arial")
//title for the chart
svg.append("text")
.attr("text-anchor", "middle")
.style("font-size", "16px")
.style("color", "#333333")
.attr("transform", "translate("+ (width/3) + "," +(height/30) + ")")
.text("DC Total Tax Revenues by Fiscal Year")
.style("font-family", "Arial");
svg.append("text")
.attr("text-anchor", "left")
.style("font-size", "10x")
.style("color", "#333333")
.attr("transform", "translate("+ (width/20) + "," +(height/12) + ")")
.text("2000 to 2015")
.style("font-family", "Arial")
//line labels
svg.append('g')
.classed('labels-group', true)
.selectAll('text')
.data(data)
.enter()
.append('text')
.filter(function(d, i) { return i === 0 || i === (data.length - 1) })
.classed('label',true)
.attr({
'x':function(d,i) {
return x(d.Date);
},
'y':function(d,i) {
return y(d["Tax Collections"]);
}
})
.text(function(d,i){
return "$" + d["Tax Collections"] + "B";
})
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
});
</script>
</body>
Assumes that the data array is sorted by date.
Source: stackoverflow.com
Related Query
- D3 Line Chart to display first and last point values
- NVD3 Line Plus Bar With Focus Chart only displaying half width of first and last bar
- Nvd3: How prevent to display chart between -1 and 1 if have all y values 0?
- nvd3.js-Line Chart with View Finder: rotate axis labels and show line values when mouse over
- tickSizeOuter not equal zero, but just first and last tick of yAxis shows line
- Align lines and dots with x-axis values in line chart
- how to display data from 1st point on words on y axis for line chart in d3.js
- How do I display an icon at a point on a line chart using d3.js
- How do you find the last data point in d3 and draw a circle and a line on it?
- Highlight closest point d3js line and dot chart
- Show values on Y Axis as it is with Higher and lower limits NVD3 Line Chart
- why d3 line chart and bar chart display in same morment?
- Why does my D3js line chart fail to render values and x-axis?
- Is there a function to find the approximate x and y co-ordinate values on a line chart using D3.js?
- D3 Line chart - display value label on Y axis tick and a not scale
- Display Bar Chart for Last week, Last Month, and Last Year Data from JSON using D3.js
- D3js vertical and horizontal custom line display overflow in zoombable chart
- d3 line chart and dates values
- nvd3 line chart with string values on x-axis
- D3.JS time-series line chart with real-time data, panning and zooming
- D3.js: Line chart - tooltip and vertical line of hover
- Solid and dashed line in D3 line chart
- D3.js combining bar and line chart
- Force a d3 line chart to ignore null values
- Special donut chart with different rings/arcs for positive and negative values
- Line chart using ordinal x-axis with d3.js and nvd3.js
- dc.js barChart first and last bar not displaying fully
- How to display values in Stacked Multi-bar chart - nvd3 Graphs
- d3 line chart and clip-path
- d3 v4 drag line chart with x and y axes
More Query from same tag
- D3: set bar colors according to property selected from drop down menu
- What does `Ot` means in Chrome dev tools?
- Append an SVG Element to HTML Table Cell Using D3.js
- Using Sharp to turn svg to png corrupts text in node.js
- Find domain values in inner arrays
- Call the axis after pressing a menu button in D3
- how to style style line chart points in c3js
- d3 get all nodes of tree
- Convert array of values to array of objects (key-value) pairs in JavaScript
- D3js collapsible tree entering transition using depth first style
- Reseting zoom: only updates on pan?
- How to hierarchically append "composed" elements based on elements' attributes using D3 d3js?
- Trying to draw a bar chart in D3 and I am not seeing the output
- D3 timescale with date object returning NaN
- JavaScript `this` works different in different class methods
- smash: command not found (npm)
- D3js Export SVG to Image
- Random fill colors in Chart.js
- d3.js stacked bar with toggleable series
- Pan UI to location of node in D3.js
- D3.js - How to use inline JSON as dataset for D3 charts, instead of csv/tsv/json file
- Load JSON object into D3 Force Directed Graph
- Dynamically update directive data
- d3 Directed Graph Editor additions
- doubts regarding an "official" example of D3 pack layout
- d3.js brush fill color histogram
- Grouping a nested array
- d3js with node, how do I add a lines to pie chart?
- D3 Line chart - display value label on Y axis tick and a not scale
- D3JS v5 Zoom - The transform value changes between zoom on button and mouse wheel