【发布时间】:2017-06-06 16:28:53
【问题描述】:
我的数据如下所示:
LABEL,VALUE
2011/2012,6267.783164
2012/2013,6385.832768
2013/2014,6334.048158
2014/2015,6359.295548
2015/2016,6380.218338
2016/2017,6519.040832
2017/2018,6370.332847
标签是我的 x 轴值,虽然它们代表一个财政年度,但它们本质上只是一个标签,我希望在 x 轴上等距分布
这是我目前拥有的代码...注意我有 2 个类似的数据集,一个用于员工人数,另一个用于 FTE
我认为这与这条线有关,我也不确定要更改什么:
var x = d3.time.scale().range([0, width]);
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>Self Identification Percentages</title>
<style>
.dataDisplay {
font-size:1em;
}
body { font: 12px Arial;}
path {
stroke-width: 2;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
}
</style>
<script src="./jquery.min.js" type="text/javascript"></script>
<script src="./jquery-ui.min.js" type="text/javascript"></script>
<script src="./jquery.corner.js"></script>
</head>
<body>
<script src="./d3/d3.v3.min.js"></script>
<div id="graph"></div>
<script>
// Set the dimensions of the canvas / graph
var margin = {top: 0, right: 20, bottom: 20, left: 60},
width = 400 - margin.left - margin.right,
height = 350 - margin.top - margin.bottom;
// Parse the date / time
///var parseDate = d3.time.format("%Y-%m-%d").parse;
// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
// Define the axes
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(5);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5);
// Define the line
var valueline = d3.svg.line()
.x(function(d) { return x(d.LABEL); })
.y(function(d) { return y(d.VALUE); });
// 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("transform",
"translate(" + margin.left + "," + margin.top + ")");
// Get the data
var dataFile = ".\\temp\\hc.csv";
d3.csv(dataFile, function(error, data) {
data.forEach(function(d) {
d.LABEL = d.LABEL;
d.VALUE = d.VALUE;
});
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.LABEL; }));
y.domain([0, d3.max(data, function(d) { return d.VALUE; })]);
svg.append("path")
.attr("class", "line")
.attr("d", valueline(data))
.attr("stroke", "#7CA2C8");
// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", -60)
.attr("dy", ".71em")
.style("text-anchor", "end")
.style("font-size", "12px")
.style("font-weight", "900")
.style("font", "sans-serif")
.text("Count");
});
dataFile = ".\\temp\\fte.csv";
d3.csv(dataFile, function(error, data) {
data.forEach(function(d) {
d.LABEL = d.LABEL;
d.VALUE = d.VALUE;
});
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.LABEL; }));
y.domain([0, d3.max(data, function(d) { return d.VALUE; })]);
// Add the valueline path.
svg.append("path")
.attr("class", "line")
.attr("d", valueline(data))
.attr("stroke", "#ECAD6F");
});
</script>
<div class=Legend>
<b>
<font size=2 color=#7CA2C8>HeadCount<font><!--input size 2 type="checkbox" id="HeadCount" name="HeadCount" value="HC" onChange="this.form.submit()"-->
<font size=2 color=#7CC8A2>FTE<font><!--input size 2 type="checkbox" id="FTE" name="FTE" value="FTE" onChange="this.form.submit()"-->
</b>
</div>
</body>
</html>
【问题讨论】: