Reputation: 578
I'm trying to specify a number of ticks and a number of tick labels for a d3 v4 bar chart with time series data as shown in the following image:
Based on this tutorial I've got a bar chart working with time series data, but I cannot get the ticks to display the way I want. For example, to show every 10th tick to leave room for the label. Any help is greatly appreciated!
@prmko Thanks for your responses. I've seen both of the posts you referenced in the comments, but applying the solution with the tickValues removes the ticks from the x-axis altogether, which led me to conclude that my situation is somehow different. This is why I commented out the tickValues line in my code snippet. I'd love to be wrong though, perhaps I'm not reading the solution correctly. Can you provide a working example that implements this approach? Thanks for your help.
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>
Upvotes: 3
Views: 3980
Reputation: 2550
I updated your snippet. When specifying .tickValues(x.domain().filter(function(d,i){ return !(i%10)}));
, the x scale domain must be set, which it was not yet when its called in your original snippet. I moved the axis creation to after the domain is set and it works.
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); });
// });
.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>
Upvotes: 2