在d3.js时间序列图上插入分钟和小时
Insert minutes and hour on d3.js timeseries plot
我用这个片段来理解d3.js是如何工作的。
我更改了这行代码-> var parseDate = d3.time.format('%H-%M-%e-%b-%y').parse;
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 1000 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
var parseDate = d3.time.format('%H-%M-%e-%b-%y').parse;
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.close); });
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.tsv("data/plottingData/first.tsv", function(error, data) {
if (error) throw error;
data.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
});
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain(d3.extent(data, function(d) { return d.close; }));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
});
</script>
,因为文件是这样格式化的:
11-30-1-May-12 582.13
10-30-1-Apr-12 382.13
9-30-1-Mar-12 482.13
...
,但我继续只查看day-month
到x-Axis
。
怎么了?
您正在以正确的格式解析日期,但是在x轴上您没有提供要在其上显示日期的格式。因此,只需在代码中使用"xAxis.tickFormat(parseDate)"。示例如下:
d3.svg.axis()
.scale(x)
.orient("bottom").tickFormat(d3.time.format('%H-%M-%e-%b-%y'));
相关文章:
- 将样式表插入iframe
- Ckeditor-plugin:插入虚假元素add不情愿<p>标签前后
- 如何动态插入jquery代码
- 动态插入的表:JQuery未检测到最后一行
- 使用JavaScript动态插入DIV的成本有多高
- 带有填充属性的SVG矩形显示在包含元素的上方插入框阴影
- 将对象动态插入到现有对象中
- 将数组中的值插入到表中
- 在 Java 中的 JavaScript 函数中插入 Wicket 值
- 我如何在数字插入中使用逗号,这样它就不会'不要破坏我的输入字段
- 使用PHP插入HTML在JavaScript上不起作用
- javascript:将数据库中的数据插入到html5文本字段中
- 从数据库中获取数据并插入JavaScript变量
- 如何使用innerHtml正确插入html
- AngularJS指令,在元素后插入HTML
- 当用户按下回车键时,自动在text区域/text中插入消息
- 如何使用JS禁用表行,并在MYSQL中插入所选选项
- 将文本插入光标所在的文本区域
- 属性gridster项应插入到何处
- 在d3.js时间序列图上插入分钟和小时