d3 v4 X-axis ticks
d3 v4 X-axis ticks
艰难地描绘标普500指数年复一年的历史表现图表。不管我怎么尝试,x轴的"ticks()"方法都会被忽略,只使用一个值1。这是很难看到的。
var margin = {top: 20, right: 20, bottom: 70, left: 40},
width = $('#sp501').width() - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// Parse the date / time
var parseDate = d3.timeParse("%Y-%m");
var x = d3.scaleBand().range([0, width], .05);
var y = d3.scaleLinear().range([height, 0]);
var xAxis = d3.axisBottom(x)
.tickFormat(d3.timeFormat("%Y"))
.ticks(10); // I can't figure out why this is ignored!
var yAxis = d3.axisLeft(y)
.ticks(10);
var svg = d3.select("#sp501")
.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("data/sp500-historical.csv", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.value = +d.value;
});
x.domain(data.map(function(d) { return d.date; }));
y.domain([-60, 60]);
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)" );
数据采用约定格式:
date,value
1928-01,43.81
1929-01,-8.30
1930-01,-25.12
1931-01,-43.84
1932-01,-8.64
1933-01,49.98
1934-01,-1.19
1935-01,46.74
1936-01,31.94
我试过把日期转换成整数年,看看是否有效,但无济于事。
是我疯了还是x轴刻度使用日期有问题?
谢谢!
虽然没有显式地设置刻度,但此代码片段在 axisBottom调用之前将范围提供给d3 ,然后清理显示。
svg.append("g")
.attr("class", "e4rAxis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
相关文章:
- 使用D3将Ticks值更改为文本
- 如何在HTML5/Java脚本的Teechart中使Axis标签美观
- ChartJS - remove axis?
- reversed Y-axis D3
- 如何在d3.axis.tickFormat中换行
- .Net Ticks 使用 JavaScript 转换为 ISO 8601 日期格式
- d3 axis labeling
- jqplot axis renderer:a.jqplot.CanvasFontRenderer不是构造函数
- Y-Axis Highchart
- 构建 WidgetCellSpan 时的 Axis 是什么用
- 使用Highcharts显示S0 Ticks
- iOS onTouchMove x or y axis
- 使用三个js获取3D对象的Axis
- D3/SVG:如何通过修改范围来调整带有时间刻度的D3.SVG.axis的大小
- 如何使ticks函数在d3.js中工作
- Highcharts:访问Axis Formatter中的Series或Axis上下文
- jqPlot -样式化自定义功能(例如:ticks: CustomTicks)
- vis graph2d y-axis ints only
- 为fooTable获取正确的数据值(ticks或unix时间戳)
- d3 v4 X-axis ticks