【发布时间】:2020-10-16 15:08:56
【问题描述】:
我想创建一个折线图,其中有两条垂直线标记一连串高值 (10) 的开始(红色)和结束(绿色)。这些行的颜色应根据输出变量中的 start_end 属性指定,如下所示:
关于如何实现这一目标的任何想法?这是我目前的代码(我使用的是 d3 v6)
{
const svg = d3.select(DOM.svg(width, height))
svg.append('g').call(xAxis)
svg.append('g').call(yAxis)
svg.append("line")
.datum(output)
.attr("d", line)
.attr("fill", "none")
.style("stroke", "steelblue")
.style("stroke-width", 4)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("x1", xScale(output.timestamp))
.attr("y1", 0)
.attr("x2", xScale(output.timestamp))
.attr("y2", 10)
svg.append('path')
.datum(segment)
.attr('d', line)
.style('fill', 'none')
.style('stroke', 'black')
return svg.node()
}
output = [
{timestamp: "2020-09-25T04:00:54.857Z", jam_factor: 10, start_end: "start"}
{timestamp: "2020-09-29T18:02:23.282Z", jam_factor: 8.39212, start_end: "end"}
]
【问题讨论】:
-
您使用的是什么版本的 d3?当有多个条纹时会发生什么?
-
抱歉忘了提,我使用的是版本 6
标签: javascript d3.js charts line