D3将条件格式应用于线元素
我的数据集看起来像这样:
var data = [{
"Date": "31/12/2019",
"Value": 23,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "31/01/2020",
"Value": 49,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "29/02/2020",
"Value": 23,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "31/03/2020",
"Value": 12,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "30/04/2020",
"Value": 33,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "31/05/2020",
"Value": 62,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "30/06/2020",
"Value": 65,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "31/07/2020",
"Value": 77,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "31/08/2020",
"Value": 65,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "30/09/2020",
"Value": 58,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "31/10/2020",
"Value": 87,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "30/11/2020",
"Value": 120,
"Type": "Actual",
"Plan": 100,
"Over": "Y"
},
{
"Date": "31/12/2020",
"Value": 103,
"Type": "Fcst",
"Plan": 100,
"Over": "Y"
},
{
"Date": "31/01/2021",
"Value": 110,
"Type": "Fcst",
"Plan": 100,
"Over": "Y"
},
{
"Date": "28/02/2021",
"Value": 117,
"Type": "Fcst",
"Plan": 100,
"Over": "Y"
}
]
我正在尝试绘制具有以下格式的线图:
d.type ===“实际”
然后将行显示为 solid and 蓝色
d.type ===“ fcst”
将行显示为 dashed (stroke> stroke-dasharray
) 和绿色
至关重要的是我要实现的目标:
...这就是我所拥有的far:
这是我尝试添加条件燃料的方法:
//add Trend Line
svg.append('path')
.datum(data)
.attr('class', 'data-line glowed')
.style('stroke', function(d, i) {
if (d.Type == "Fcst")
return "#00D4B2"
else {
return "#3087CD"
}
})
.style('stroke-width', 2)
.style("stroke-dasharray",function(d, i) {
if (d.Type == "Fcst")
return ("3, 1"); else {
return ("0, 0")
}
})
.style('fill', 'none')
.attr('d', line)
这是我的 jsfiddle
My dataset looks like this:
var data = [{
"Date": "31/12/2019",
"Value": 23,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "31/01/2020",
"Value": 49,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "29/02/2020",
"Value": 23,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "31/03/2020",
"Value": 12,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "30/04/2020",
"Value": 33,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "31/05/2020",
"Value": 62,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "30/06/2020",
"Value": 65,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "31/07/2020",
"Value": 77,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "31/08/2020",
"Value": 65,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "30/09/2020",
"Value": 58,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "31/10/2020",
"Value": 87,
"Type": "Actual",
"Plan": 100,
"Over": ""
},
{
"Date": "30/11/2020",
"Value": 120,
"Type": "Actual",
"Plan": 100,
"Over": "Y"
},
{
"Date": "31/12/2020",
"Value": 103,
"Type": "Fcst",
"Plan": 100,
"Over": "Y"
},
{
"Date": "31/01/2021",
"Value": 110,
"Type": "Fcst",
"Plan": 100,
"Over": "Y"
},
{
"Date": "28/02/2021",
"Value": 117,
"Type": "Fcst",
"Plan": 100,
"Over": "Y"
}
]
I'm trying to plot a line chart that has the following formatting:
When d.Type === "Actual"
then show the line as SOLID and BLUE
When d.Type === "Fcst"
show the line as dashed (stroke-dasharray
) and GREEN
Essential this is what I'm trying to achieve:
...and this is what I have so far:
This is what I tried doing to add the conditional fomatting:
//add Trend Line
svg.append('path')
.datum(data)
.attr('class', 'data-line glowed')
.style('stroke', function(d, i) {
if (d.Type == "Fcst")
return "#00D4B2"
else {
return "#3087CD"
}
})
.style('stroke-width', 2)
.style("stroke-dasharray",function(d, i) {
if (d.Type == "Fcst")
return ("3, 1"); else {
return ("0, 0")
}
})
.style('fill', 'none')
.attr('d', line)
Here is my JSFIDDLE
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
因为您使用
datum
您不在行的每个点上迭代 - 相反,整个路径是由line
生成的,并且是一个元素,只能接受一组样式。下面的方法绘制了3行(一条透明)相互覆盖。较短的“实际”线位于“预测”线上。您需要这样做才能获得良好的曲率(我使用
d3.curvemonotonex
)。步骤:
type
),但getTotallength()
stroke-dasharray
,使其具有一个破折号(从步骤(2)中计算的长度开始),没有重复的破折号,因此原始'''''预测“线路”通过虚线的单个“差距”显示。请参阅下面的工作示例并检查评论:
Because you use
datum
you don't iterate over each point of the line - instead the entire path is generated byline
and is a single element and can only accept one set of styles.The approach below draws 3 lines (one transparent) overlaid to one another. The shorter 'actual' line is on top of the 'forecast' line. You need to do it this way to get the nice curvature (I use
d3.curveMonotoneX
).The steps:
Type
) but make it transparentgetTotalLength()
stroke-dasharray
such that it has one dash (from start for length calculated in step (2)) and no repeating dash so the end of the original 'forecast' line shows through this single 'gap' of the dashed line.See working example below and check the comments: