这是我的html文件
<!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>
,这是我的javascript文件
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");
});
,最后是我的样式表:
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;
}
因为它明显让我的Y轴标签“Amount Dispensed”弄得一团糟,我想不出改变它的方法,因为我在样式表中的字体大小或我的其他一些错误代码,任何帮助将受到高度赞赏。
这是右轴更改后的输出 y轴标签确实回来了,但是单个条上的数字似乎太大而无法描述是否有办法缩短它们的等于950000到950K等同等
答案 0 :(得分:1)
我将您的margin.left
从50增加到75.我还修改了您的yAxis创建,以修复s
will change numbers into their corresponding prefix(7000到7k等)的比例数字格式
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left").tickFormat(d3.format("s"));
我使用的数据只是随机创建的,因为问题只是轴格式化。
我还将您添加到yAxis的标签从y: -2em
移到y: -3em