echarts图表中如何设置部分线段为虚线?
在ECharts图表中,灵活控制线段样式,例如将部分线段设置为虚线,可以提升图表可读性和表达力。本文将详细讲解如何实现这一功能。
我们以一个示例代码为例,假设需要将名为“混钢筋混凝土安装1(15天)”的线段设置为虚线。
原始代码结构:
let charts = { nodes: [ // ... 节点数据 ], linesData: [ { name: '降水1(10天)', coords: [[20, 700], [190, 700]], type: "dotted" }, { name: '开挖1n(5天)', coords: [[220, 700], [290, 700]] }, { name: '砂石垫层1n(5天)', coords: [[320, 700], [390, 700]] }, { name: '混凝土基础1(10天)', coords: [[420, 700], [590, 700]] }, { name: '混钢筋混凝土安装1(15天)', coords: [[620, 700], [890, 700]] }, { name: '土方回填1n(3天)', coords: [[920, 700], [950, 700]] }, ] }; let option = { // ... 其他配置项 series: [{ type: "lines", lineStyle: { color: '#65B7E3', width: 2, }, data: charts.linesData, }], };
实现部分线段虚线:
关键在于对linesData数组中特定线段的lineStyle属性进行单独设置。 我们将’dashed’样式应用于目标线段:
let charts = { nodes: [ // ... 节点数据 ], linesData: [ { name: '降水1(10天)', coords: [[20, 700], [190, 700]], type: "dotted" }, { name: '开挖1n(5天)', coords: [[220, 700], [290, 700]] }, { name: '砂石垫层1n(5天)', coords: [[320, 700], [390, 700]] }, { name: '混凝土基础1(10天)', coords: [[420, 700], [590, 700]] }, { name: '混钢筋混凝土安装1(15天)', coords: [[620, 700], [890, 700]], lineStyle: { type: 'dashed' } }, { name: '土方回填1n(3天)', coords: [[920, 700], [950, 700]] }, ] }; let option = { // ... 其他配置项 series: [{ type: "lines", lineStyle: { color: '#65B7E3', width: 2, }, data: charts.linesData, }], };
通过在linesData数组中,为“混钢筋混凝土安装1(15天)”对象添加lineStyle: { type: ‘dashed’ },我们就成功地将其设置为虚线,而其他线段保持原样。 这种方法允许对每条线段进行独立的样式控制,实现灵活的图表定制。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END