Reputation: 365
Here is my html file
<!DOCTYPE html>
<html>
<head>
<!-- meta -->
<meta charset="utf-8">
<title>My Data Record</title>
<!-- CSS stylesheet -->
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<!-- D3.js CDN source -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" charset="utf-8"></script>
</head>
<body>
<!-- Title -->
<h1 style="text-align:center;">Monthly Dispensed Amount</h1>
<!-- Your D3 code for bar graph -->
<script type="text/javascript" src="gdpBarGraph.js"></script>
</body>
</html>
and here is my javascript file
var margin = {top: 20, right: 10, bottom: 100, left:50},
width = 700 - margin.right - margin.left,
height = 500 - margin.top - margin.bottom;
var svg = d3.select("body")
.append("svg")
.attr ({
"width": width + margin.right + margin.left,
"height": height + margin.top + margin.bottom
})
.append("g")
.attr("transform","translate(" + margin.left + "," + margin.right + ")");
// defining x and y scales
var xScale = d3.scale.ordinal()
.rangeRoundBands([0,width], 0.2, 0.2);
var yScale = d3.scale.linear()
.range([height, 0]);
// defining x axis and y axis
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left");
d3.csv("newcashdata.csv", function(error,data) {
if(error) console.log("Error: data could not be loaded!");
data.forEach(function(d) {
d.date = d.date;
d.amount = +d.amount;
console.log(d.amount);
});
// sort the values to show at which date the cash collection was the highest
data.sort(function(a,b) {
return b.amount - a.amount;
});
// Specify the domains of the x and y scales
xScale.domain(data.map(function(d) { return d.date; }) );
yScale.domain([0, d3.max(data, function(d) { return d.amount; } ) ]);
svg.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr("height", 0)
.attr("y", height)
.transition().duration(3000)
.delay( function(d,i) { return i * 200; })
.attr({
"x": function(d) { return xScale(d.date); },
"y": function(d) { return yScale(d.amount); },
"width": xScale.rangeBand(),
"height": function(d) { return height - yScale(d.amount); }
})
.style("fill", function(d,i) { return 'rgb(20, 20, ' + ((i * 30) + 100) + ')'});
svg.selectAll('text')
.data(data)
.enter()
.append('text')
.text(function(d){
return d.amount;
})
.attr({
"x": function(d){ return xScale(d.date) + xScale.rangeBand()/2; },
"y": function(d){ return yScale(d.amount)+ 12; },
"font-family": 'sans-serif',
"font-size": '13px',
"font-weight": 'bold',
"fill": 'white',
"text-anchor": 'middle'
});
// Drawing x axis and positioning the label
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.attr("dx", "-.8em")
.attr("dy", ".25em")
.attr("transform", "rotate(-60)" )
.style("text-anchor", "end")
.attr("font-size", "10px");
// Drawing y Axis and positioning the label
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -height/2)
.attr("dy", "-2em")
.style("text-anchor", "middle")
.text("Amount Dispensed");
});
and lastly my stylesheet:
svg {
margin-left: auto;
margin-right: auto;
display: block;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis text{
font: Times;
font-size: 20px;
font-weight: bold;
}
here's what i am getting as output:
as its evident i made a mess out of my Y axis label "Amount Dispensed" and i can't think of a way to change that is it because of my font size in stylesheet or some other mistake in my code, any help will be highly appreciated.
here is the output after right axis changes The y axis label did came back however the numbers on individual bars seem to be too big to depict is there a way to shorten them for isntance say 950000 to 950K and likewise
Upvotes: 1
Views: 1746
Reputation: 2550
I increased your margin.left
to 75 from 50. I also modified your yAxis creation to fix scale number formatting the s
will change numbers into their corresponding prefix (7000 to 7k etc.)
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left").tickFormat(d3.format("s"));
The data I used was just randomly created as the issues were just in axis formatting.
I also moved over the label you added to the yAxis from y: -2em
to y: -3em
Upvotes: 1