【发布时间】:2016-10-25 06:46:13
【问题描述】:
我有以下代码应该在画布元素中显示淹没线。
var initCanvas = function () {
var episodeLengthInPixels = moment.duration(episodeLogLength).asSeconds() * episodeWidthMultiplication;
console.log("Length of chart is "+episodeLengthInPixels +" px");
try {
canvas = d3.select("body").append("canvas")
.attr("width", 500)
.attr("height", canvasHeight)
.attr("class", canvasSelector);
//Draw the Line
canvas.append("line") // attach a line
.style("stroke", "black") // colour the line
.attr("x1", 0) // x position of the first end of the line
.attr("x2", 500)
.attr("y1", waveHeight)
.attr("y2", waveHeight) ;
} catch (e) {
console.error(e);
}
}
问题是画布和线条在 DOM 模型中可用但不可见(不抛出异常)。当我尝试使用 SVG 而不是画布时,一切正常。
请问如何使用 D3.js 库在画布中显示内容?我试图找到任何例子,但没有运气。我应该在画布使用中使用 D3.js 还是其他东西(例如纯绘图到画布)?
非常感谢您的建议。
【问题讨论】:
-
canvas 不是基于 DOM 的东西。您可以通过 canvas API 获得一个画布上下文并在其上绘制线条。
标签: javascript html canvas d3.js svg