如何在 d3 中设置 x 轴的年份范围
How to set range of year in x axis in d3
我是 d3js 的新手,我对 mi d3 折线图有疑问,如果你能看到我的图表按年份显示所有数据,但我的问题是我想做但我不想做知道如何,它是逐年出现在 x 轴上,但它是这样显示的。
Image 1
每 5 年一次,我希望它逐年出现。
Image 2
这是我的代码的一部分:
//Set the ranges for the line
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
//Define the 1st line
var valueline = d3.line()
.x(function(d) { return x(d.DATE); })
.y(function(d) { return y(d.DDDM01USA156NWDB); });
//Set the domains for the lines
x.domain(d3.extent(data, function(d) { return d.DATE; }));
y.domain([20, 160]);
//Set axis for the lines
var xAxis = d3.axisBottom()
.scale(x);
var yAxis = d3.axisLeft()
.scale(y);
//Gridlines in x axis function
var gridlineX = d3.axisBottom(x)
.tickSize(-height)
.tickFormat("");
//Add the X gridlines
svg.append("g")
.attr("class", "grid")
.attr("transform", "translate(0," + height + ")")
.call(gridlineX);
//Gridlines in y axis function
var gridlineY = d3.axisLeft()
.tickSize(-width)
.tickFormat("%")
.scale(y);
//Add the Y gridlines
svg.append("g")
.attr("class", "grid2")
.call(gridlineY);
//--------------Add the valueline path------------------
//Path (line Chart) transitions
function lineTransition(path) {
path.transition()
.duration(2000)
.attrTween("stroke-dasharray", tweenDash);
}
function tweenDash() {
var l = this.getTotalLength(),
i = d3.interpolateString("0," + l, l + "," + l);
return function(t) {
return i(t);
};
}
//Add the valueline path for line 1
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("id", "line1")
.style("stroke", "#7C6CAA")
.style("stroke-width", "3")
.attr("d", valueline);
//Transition
svg.select("#line1")
.datum(data)
.attr("d", valueline)
.call(lineTransition);
//xlabel
svg.append("text")
.attr("transform",
"translate(" + (width / 2) + " ," +
(height + margin.top + 20) + ")")
.style("text-anchor", "middle")
.attr("dy", ".80em")
.text("Year");
//ylabel
svg.append("text")
.attr("class", "y label")
.attr("text-anchor", "end")
.attr("y", 6)
.attr("dy", ".75em")
.attr("transform", "rotate(-90)")
.text("Percent");
//Add the X Axis
svg.append("g")
.attr("class", "x axis axisGrey")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.style("font-size", "12px")
.attr("transform", "rotate(-65)");
//Add the y Axis
svg.append("g")
.attr("class", "y axis axisGrey")
.call(yAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.style("font-size", "12px")
我获取数据的 csv 文件是这样的:
日期,DDDM01USA156NWDB,年份,拿督
1/1/1984,42.5146,1984,0
1/1/1985,44.873,1985,0
1/1/1986,52.5561,1986,0
1/1/1987,52.3852,1987,0
1/1/1988,50.667,1988,0
1/1/1989,54.6595,1989,0
1/1/1990,54.5632,1990,160
1/1/1991,58.7687,1991,160
1/1/1992,66.732,1992,0
1/1/1993,71.4563,1993,0
1/1/1994,71.3222,1994,0
1/1/1995,79.144,1995,0
1/1/1996,95.619,1996,0
1/1/1997,112.103,1997,0
1/1/1998,130.576,1998,0
1/1/1999,143.673,1999,0
1/1/2000,146.193,2000,0
1/1/2001,138.332,2001,160
1/1/2002,114.902,2002,0
1/1/2003,110.685,2003,0
1/1/2004,125.554,2004,0
1/1/2005,128.392,2005,0
1/1/2006,133.386,2006,0
1/1/2007,137.361,2007,160
1/1/2008,109.698,2008,160
1/1/2009,92.9573,2009,160
1/1/2010,108.321,2010,0
1/1/2011,107.384,2011,0
1/1/2012,107.025,2012,0
1/1/2013,128.708,2013,0
1/1/2014,146.209,2014,0
1/1/2015,142.648,2015,0
1/1/2016,141.286,2016,0
1/1/2017,153.96,2017,0
您可以使用 axis.ticks() 方法来指定轴的刻度数。
这不能保证 d3
会呈现您请求的滴答数。相反,d3
将尝试选择接近您的请求且对您的数据仍然有意义的报价。
换句话说,它不会显示人类无法直观理解的时间间隔,例如一年的 1.2387,而是会显示更合理的时间,例如半年、一年等
但是,我认为如果您知道时域的最小值和最大值,那么您将能够提供该域中的年数作为 ticks()
方法的参数。
var minYear = d3.min(data, d => d.date).getFullYear();
var maxYear = d3.max(data, d => d.date).getFullYear();
var numYears = maxYear - minYear;
var xAxis = d3.axisBottom(xScale).ticks(numYears);
我是 d3js 的新手,我对 mi d3 折线图有疑问,如果你能看到我的图表按年份显示所有数据,但我的问题是我想做但我不想做知道如何,它是逐年出现在 x 轴上,但它是这样显示的。 Image 1
每 5 年一次,我希望它逐年出现。 Image 2
这是我的代码的一部分:
//Set the ranges for the line
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
//Define the 1st line
var valueline = d3.line()
.x(function(d) { return x(d.DATE); })
.y(function(d) { return y(d.DDDM01USA156NWDB); });
//Set the domains for the lines
x.domain(d3.extent(data, function(d) { return d.DATE; }));
y.domain([20, 160]);
//Set axis for the lines
var xAxis = d3.axisBottom()
.scale(x);
var yAxis = d3.axisLeft()
.scale(y);
//Gridlines in x axis function
var gridlineX = d3.axisBottom(x)
.tickSize(-height)
.tickFormat("");
//Add the X gridlines
svg.append("g")
.attr("class", "grid")
.attr("transform", "translate(0," + height + ")")
.call(gridlineX);
//Gridlines in y axis function
var gridlineY = d3.axisLeft()
.tickSize(-width)
.tickFormat("%")
.scale(y);
//Add the Y gridlines
svg.append("g")
.attr("class", "grid2")
.call(gridlineY);
//--------------Add the valueline path------------------
//Path (line Chart) transitions
function lineTransition(path) {
path.transition()
.duration(2000)
.attrTween("stroke-dasharray", tweenDash);
}
function tweenDash() {
var l = this.getTotalLength(),
i = d3.interpolateString("0," + l, l + "," + l);
return function(t) {
return i(t);
};
}
//Add the valueline path for line 1
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("id", "line1")
.style("stroke", "#7C6CAA")
.style("stroke-width", "3")
.attr("d", valueline);
//Transition
svg.select("#line1")
.datum(data)
.attr("d", valueline)
.call(lineTransition);
//xlabel
svg.append("text")
.attr("transform",
"translate(" + (width / 2) + " ," +
(height + margin.top + 20) + ")")
.style("text-anchor", "middle")
.attr("dy", ".80em")
.text("Year");
//ylabel
svg.append("text")
.attr("class", "y label")
.attr("text-anchor", "end")
.attr("y", 6)
.attr("dy", ".75em")
.attr("transform", "rotate(-90)")
.text("Percent");
//Add the X Axis
svg.append("g")
.attr("class", "x axis axisGrey")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.style("font-size", "12px")
.attr("transform", "rotate(-65)");
//Add the y Axis
svg.append("g")
.attr("class", "y axis axisGrey")
.call(yAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.style("font-size", "12px")
我获取数据的 csv 文件是这样的:
日期,DDDM01USA156NWDB,年份,拿督 1/1/1984,42.5146,1984,0 1/1/1985,44.873,1985,0 1/1/1986,52.5561,1986,0 1/1/1987,52.3852,1987,0 1/1/1988,50.667,1988,0 1/1/1989,54.6595,1989,0 1/1/1990,54.5632,1990,160 1/1/1991,58.7687,1991,160 1/1/1992,66.732,1992,0 1/1/1993,71.4563,1993,0 1/1/1994,71.3222,1994,0 1/1/1995,79.144,1995,0 1/1/1996,95.619,1996,0 1/1/1997,112.103,1997,0 1/1/1998,130.576,1998,0 1/1/1999,143.673,1999,0 1/1/2000,146.193,2000,0 1/1/2001,138.332,2001,160 1/1/2002,114.902,2002,0 1/1/2003,110.685,2003,0 1/1/2004,125.554,2004,0 1/1/2005,128.392,2005,0 1/1/2006,133.386,2006,0 1/1/2007,137.361,2007,160 1/1/2008,109.698,2008,160 1/1/2009,92.9573,2009,160 1/1/2010,108.321,2010,0 1/1/2011,107.384,2011,0 1/1/2012,107.025,2012,0 1/1/2013,128.708,2013,0 1/1/2014,146.209,2014,0 1/1/2015,142.648,2015,0 1/1/2016,141.286,2016,0 1/1/2017,153.96,2017,0
您可以使用 axis.ticks() 方法来指定轴的刻度数。
这不能保证 d3
会呈现您请求的滴答数。相反,d3
将尝试选择接近您的请求且对您的数据仍然有意义的报价。
换句话说,它不会显示人类无法直观理解的时间间隔,例如一年的 1.2387,而是会显示更合理的时间,例如半年、一年等
但是,我认为如果您知道时域的最小值和最大值,那么您将能够提供该域中的年数作为 ticks()
方法的参数。
var minYear = d3.min(data, d => d.date).getFullYear();
var maxYear = d3.max(data, d => d.date).getFullYear();
var numYears = maxYear - minYear;
var xAxis = d3.axisBottom(xScale).ticks(numYears);