折线图里如何设置折线的分段样式,比如 末尾虚线?
我有一个折线图的需求,折线的最后一个数据点是预测数据,所以想要折线最后一段能显示成虚线,类似下面这张图。这个要怎么实现更方便?
回复
1个回答

test
2024-06-27
解决方案 Solution
推荐你使用 VChart,官网 demo 正有你需要的效果:https://visactor.io/vchart/demo/line-chart/dash-line
可以在 line 的图元样式回调里,根据数据返回不同的样式。
lineDash
:虚线模式。它使用一组值来指定描述模式的线和间隙的交替长度。
line: {
style: {
stroke: (data) => data.latest ? 'blue': 'green',
lineDash: data => data.latest ? [5, 5]: [0]
}
},
point: {
style: {
fill: (data) => data.latest ? 'blue': 'green',
}
}
代码示例 Code Example
const spec = {
type: 'line',
data: {
values: [
{
x: '1st',
y: 0.012
},
{
x: '2nd',
y: -0.01
},
{
x: '3rd',
y: 0.005
},
{
x: '4th',
y: 0.007
},
{
x: '5th',
y: 0.01
},
{
x: '6th',
y: 0.017
},
{
x: '7th',
y: 0.022
},
{
x: '8th (prediction)',
y: 0.033,
latest: true
}
]
},
xField: 'x',
yField: 'y',
line: {
style: {
stroke: (data) => data.latest ? 'blue': 'green',
lineDash: data => data.latest ? [5, 5]: [0]
}
},
point: {
style: {
fill: (data) => data.latest ? 'blue': 'green',
}
}
};
结果展示 Results
在线效果参考:https://codesandbox.io/s/line-style-s2ns8s
相关文档 Related Documentation
相关 demo:https://visactor.io/vchart/demo/line-chart/dash-line
相关 api:https://visactor.io/vchart/option/lineChart#line.style
github:https://github.com/VisActor/VChart
related demo:https://visactor.io/vchart/demo/line-chart/dash-line
related api:https://visactor.io/vchart/option/lineChart#line.style
回复

适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容