gpt4 book ai didi

javascript - D3轴: aligning dates (x),显示时间(y)

转载 作者:行者123 更新时间:2023-12-02 16:02:38 32 4
gpt4 key购买 nike

我的数据是 (date, start, stop) 元组数组,其中 start 和 stop 是一天中的分钟 [0, 1440],所以上午 1 点表示为 60。我想绘制矩形,其中每个矩形与日期水平对齐,高度是开始时间和停止时间。这就是我得到的,它很接近,但有几个错误(哪些错误是我的问题):

this is what I get, which is close to what I want

  1. 我希望我的矩形与日期列一样宽,但这并不完全是我所看到的。特别是,我对 x_scale.rangeBand() 的调用给了我比列更宽的内容。 (注意水平重叠。)- .5 * x_scale.rangeBand()) 旨在使矩形居中。

  2. 我想在 y 轴上指示时间而不是分钟,但对 Ticks()TickValues() 的带注释的调用不会完全做到这一点。

非常感谢指点。

这是生成此代码的代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>D3 Page Template</title>
<script type="text/javascript" src="d3.js"></script>
<style>
.d3-canvas { background-color: #F1EEFF; }
.icongray { fill: #5B5852; }
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
</style>
</head>
<body>
<script type="text/javascript">
var data = [{"date": new Date("2015-01-01"), "start": 0, "end": 60},
{"date": new Date("2015-01-02"), "start": 720, "end": 780},
{"date": new Date("2015-01-03"), "start": 1300, "end": 1440}]

var do_the_plot = function(width, height) {
margin = {"left": 45, "right": 25, "top": 25, "bottom": 25};
image_width = width - margin.left - margin.right;
image_height = height - margin.top - margin.bottom;
svg = setup_svg(width, height);
draw_it(svg);
}

log_int = function(label, the_int) {
console.log(label, the_int);
return the_int;
}

minutes_in_day = 1440;

// Set up an svg element for us. Return it.
setup_svg = function(width, height) {
num_days = 3
canvas = d3.select("body")
.append("svg")
.attr({"width": width,
"height": height,
"class": "d3-canvas",
});
inside = canvas.append("g")
.attr({"transform": "translate(" + margin.left + "," + margin.top + ")"
})
return inside;
}

// Draw some rectangles representing reading sessions.
draw_it = function(svg, reading_sessions) {
// Assume data is a list of {day, start, stop}, day an int
// (zero-based, days since start), start and stop minutes (int).
x_scale = d3.scale.ordinal()
//.domain([0, num_days])
.domain([new Date("2015-01-01"), new Date("2015-01-03")]) // Use from data.
.rangeRoundBands([0, image_width]);
y_scale = d3.scale.linear()
.domain([0, minutes_in_day])
.range([image_height, 0]);
h_scale = d3.scale.linear()
.domain([0, minutes_in_day])
.range([0, image_height]);
console.log("about to rect");
svg.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr({
"width": function(d) { return log_int("w", x_scale.rangeBand()); },
"height": function(d) { return log_int("h", h_scale(d.end - d.start)); },
"x": function(d) { return log_int("x", x_scale(d.date) - .5 * x_scale.rangeBand()); },
"y": function(d) { return log_int("y", y_scale(d.end)); },
"class": "icongray"
});
console.log("got rects");
var x_axis = d3.svg.axis()
.scale(x_scale)
.orient("bottom")
.tickFormat(d3.time.format("%d-%m"));
svg.append("g")
.attr({"class": "axis",
"transform": "translate(0, " + image_height + ")",
})
.call(x_axis);
var y_axis = d3.svg.axis()
.scale(y_scale)
//.ticks([0, 180, 360, 540, 720, 900, 1080, 1260, 1440])
//.tickValues(["midnight", "3:00", "6:00", "9:00", "noon", "15:00", "18:00", "21:00", "midnight"])
.orient("left");
svg.append("g")
.attr({"class": "axis"})
.call(y_axis);
}

do_the_plot(300, 300);
</script>
</body>
</html>

最佳答案

  1. 我希望我的矩形与日期列一样宽 ...问题是因为您将域指定为范围。域将具有所有值。您在 x 轴上的代码:

.domain([new Date("2015-01-01"), new Date("2015-01-03")]) 
// should have been this.. it will give an array of all dates in data
.domain(data.map(function (d) {return d.date;}))

  • 我想在 y 轴上指示时间而不是分钟 .... 刻度值不会有帮助,刻度格式会有帮助。因此,在 Y 轴上执行以下操作:
  •       //y tick values
    var y_values = [0, 180, 360, 540, 720, 900, 1080, 1260, 1440]
    //y tick display for its corresponding values
    var y_display = ["midnight", "3:00", "6:00", "9:00", "noon", "15:00", "18:00", "21:00", "midnight"]

    .tickValues(y_values)
    .tickFormat(function (d) {
    var index = y_values.indexOf(d)
    return y_display[index];
    })

    此处完整更正的代码:http://jsfiddle.net/cyril123/q4zy96zu/1/

    关于javascript - D3轴: aligning dates (x),显示时间(y),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31047613/

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