我尝试为时间序列数据指定d3 v4条形图的多个刻度和多个刻度标签,如下图所示:
基于this tutorial我有一个使用时间序列数据的条形图,但我无法获得显示我想要的方式的标记。例如,要显示每隔10个刻度以留出标签的空间。非常感谢任何帮助!
@prmko 谢谢你的回复。我已经看过你在评论中引用的两个帖子,但是使用带有tickValues的解决方案完全删除了x轴上的刻度,这使我得出结论我的情况有所不同。这就是我在代码片段中注释掉tickValues行的原因。我喜欢错,但也许我没有正确地阅读解决方案。你能提供一个实现这种方法的工作实例吗?谢谢你的帮助。
var margin = {top: 20, right: 20, bottom: 70, left: 40},
width = 600 - margin.left - margin.right,
height = 200 - margin.top - margin.bottom;
// Parse the date / time
var parseDate = d3.isoParse
var x = d3.scaleBand().rangeRound([0, width], .05).padding(0.1);
var y = d3.scaleLinear().range([height, 0]);
var xAxis = d3.axisBottom()
.scale(x)
.tickFormat(d3.timeFormat("%Y-%m-%d"))
//.tickValues(x.domain().filter(function(d, i) {return !(i%4);}));
var yAxis = d3.axisLeft()
.scale(y)
.ticks(4);
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 + ")");
// d3.csv("bar-data.csv", function(error, data) {
var data = [
{
"date": "2016-03-02T00:00:00-05:00",
"value": "53"
},
{
"date": "2016-03-03T00:00:00-05:00",
"value": "165"
},
{
"date": "2016-03-04T00:00:00-05:00",
"value": "269"
},
{
"date": "2016-03-05T00:00:00-05:00",
"value": "344"
},
{
"date": "2016-03-06T00:00:00-05:00",
"value": "376"
},
{
"date": "2016-03-07T00:00:00-05:00",
"value": "410"
},
{
"date": "2016-03-08T00:00:00-05:00",
"value": "421"
},
{
"date": "2016-03-09T00:00:00-05:00",
"value": "405"
},
{
"date": "2016-03-10T00:00:00-05:00",
"value": "376"
},
{
"date": "2016-03-11T00:00:00-05:00",
"value": "359"
},
{
"date": "2016-03-12T00:00:00-05:00",
"value": "392"
},
{
"date": "2016-03-13T00:00:00-05:00",
"value": "433"
},
{
"date": "2016-03-14T00:00:00-05:00",
"value": "455"
},
{
"date": "2016-03-15T00:00:00-05:00",
"value": "478"
}
];
data.forEach(function(d) {
d.date = parseDate(d.date);
d.value = +d.value;
});
x.domain(data.map(function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.value; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", "-.55em")
.attr("transform", "rotate(-90)" );
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Value");
svg.selectAll("bar")
.data(data)
.enter().append("rect")
.style("fill", "steelblue")
.attr("x", function(d) { return x(d.date); })
.attr("width", x.bandwidth())
.attr("y", function(d) { return y(d.value); })
.attr("height", function(d) { return height - y(d.value); });
// });

.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}

<div class="chart"></div>
<script src="https://d3js.org/d3.v4.min.js" charset="utf-8"></script>
&#13;
答案 0 :(得分:1)
我更新了您的代码段。指定.tickValues(x.domain().filter(function(d,i){ return !(i%10)}));
时,必须设置x比例域,在原始代码段中调用它时尚未设置。我将轴创建移动到域设置后它可以工作。
var margin = {top: 20, right: 20, bottom: 70, left: 40},
width = 600 - margin.left - margin.right,
height = 200 - margin.top - margin.bottom;
// Parse the date / time
var parseDate = d3.isoParse
var x = d3.scaleBand().rangeRound([0, width], .05).padding(0.1);
var y = d3.scaleLinear().range([height, 0]);
var yAxis = d3.axisLeft()
.scale(y)
.ticks(4);
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 + ")");
// d3.csv("bar-data.csv", function(error, data) {
var data = [
{
"date": "2016-03-02T00:00:00-05:00",
"value": "53"
},
{
"date": "2016-03-03T00:00:00-05:00",
"value": "165"
},
{
"date": "2016-03-04T00:00:00-05:00",
"value": "269"
},
{
"date": "2016-03-05T00:00:00-05:00",
"value": "344"
},
{
"date": "2016-03-06T00:00:00-05:00",
"value": "376"
},
{
"date": "2016-03-07T00:00:00-05:00",
"value": "410"
},
{
"date": "2016-03-08T00:00:00-05:00",
"value": "421"
},
{
"date": "2016-03-09T00:00:00-05:00",
"value": "405"
},
{
"date": "2016-03-10T00:00:00-05:00",
"value": "376"
},
{
"date": "2016-03-11T00:00:00-05:00",
"value": "359"
},
{
"date": "2016-03-12T00:00:00-05:00",
"value": "392"
},
{
"date": "2016-03-13T00:00:00-05:00",
"value": "433"
},
{
"date": "2016-03-14T00:00:00-05:00",
"value": "455"
},
{
"date": "2016-03-15T00:00:00-05:00",
"value": "478"
}
];
data.forEach(function(d) {
d.date = parseDate(d.date);
d.value = +d.value;
});
x.domain(data.map(function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.value; })]);
var xAxis = d3.axisBottom()
.scale(x)
.tickFormat(d3.timeFormat("%Y-%m-%d"))
.tickValues(x.domain().filter(function(d,i){ return !(i%4)}));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", "-.55em")
.attr("transform", "rotate(-90)" );
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Value");
svg.selectAll("bar")
.data(data)
.enter().append("rect")
.style("fill", "steelblue")
.attr("x", function(d) { return x(d.date); })
.attr("width", x.bandwidth())
.attr("y", function(d) { return y(d.value); })
.attr("height", function(d) { return height - y(d.value); });
// });
&#13;
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
&#13;
<div class="chart"></div>
<script src="https://d3js.org/d3.v4.min.js" charset="utf-8"></script>
&#13;