gpt4 book ai didi

javascript - D3 如何计算此图表上显示的日期值?

转载 作者:行者123 更新时间:2023-11-30 00:11:45 24 4
gpt4 key购买 nike

我正在研究这个例子中的代码: https://bl.ocks.org/mbostock/978f6c03c9aab8af8594#data.tsv

var formatPercent = d3.format("+.0%"),
formatChange = function(x) { return formatPercent(x - 1); },
parseDate = d3.time.format("%d-%b-%y").parse;

var x = d3.time.scale()
.range([0, width]);

var y = d3.scale.log()
.range([height, 0]);

var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");

var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickSize(-width, 0)
.tickFormat(formatChange);

var line = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.ratio); });

var area = d3.svg.area()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.ratio); });

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 + ")");

var gX = svg.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")");

var gY = svg.append("g")
.attr("class", "axis axis--y");

gY.append("text")
.attr("class", "axis-title")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.text("Change in Price");

d3.tsv("data.tsv", function(error, data) {
if (error) throw error;

// Compute price relative to base value (hypothetical purchase price).
var baseValue = +data[0].close;
data.forEach(function(d) {
d.date = parseDate(d.date);
d.ratio = d.close / baseValue;
});

x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain(d3.extent(data, function(d) { return d.ratio; }));

area.y0(y(1));

// Use a second linear scale for ticks.
yAxis.tickValues(d3.scale.linear()
.domain(y.domain())
.ticks(20));

gX.call(xAxis);

gY.call(yAxis)
.selectAll(".tick")
.classed("tick--one", function(d) { return Math.abs(d - 1) < 1e-6; });

var defs = svg.append("defs");

defs.append("clipPath")
.attr("id", "clip-above")
.append("rect")
.attr("width", width)
.attr("height", y(1));

defs.append("clipPath")
.attr("id", "clip-below")
.append("rect")
.attr("y", y(1))
.attr("width", width)
.attr("height", height - y(1));

svg.append("path")
.datum(data)
.attr("clip-path", "url(#clip-above)")
.attr("class", "area area--above")
.attr("d", area);

svg.append("path")
.datum(data)
.attr("clip-path", "url(#clip-below)")
.attr("class", "area area--below")
.attr("d", area);

svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
});

D3 如何计算 x 轴上显示的时间格式?

我看到了这行代码:

parseDate = d3.time.format("%d-%b-%y").parse;

但这如何导致几年内的日期显示为“4 月”、“2009 年”等?这是 D3 中的自动设置吗?它与刻度线间距有关吗?我不确定在哪里进行 x 轴设置。

最佳答案

您引用的行与 D3 输出日期的方式无关:它是将 CSV 中的日期(例如 11-Feb-08)转换为可用日期的函数。

x 轴刻度定义为 time scale通过 var x = d3.time.scale 格式由 scale.tickFormat 中的默认值给出

Returns a time format function suitable for displaying a tick value.You don't have to use the scale's built-in tick format, but itautomatically computes the appropriate display based on the inputdate.

The following time formats are considered:

  • %Y - for year boundaries, such as "2011".

  • %B - for month boundaries, such as "February".

  • %b %d - for weekboundaries, such as "Feb 06".

  • %a %d - for day boundaries, such as "Mon 07".

  • %I %p - for hour boundaries, such as "01 AM".

  • %I:%M - for minuteboundaries, such as "01:23".

  • :%S - for second boundaries, such as ":45".

  • .%L - milliseconds for all other times, such as ".012".

基本上,D3 的魔力在起作用

关于javascript - D3 如何计算此图表上显示的日期值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36193671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com