echarts+js生成profile图。根据 [ { name:'分站', startTime: "2023-05-28 00:00:01", endTime:"2023-05-29 00:00:01", value:0 }, { name:'读卡器', startTime: "2023-05-28 00:00:01", endTime:"2023-05-29 00:00:01", value:0 }, { name:'电源', startTime: "2023-05-28 00:00:01", endTime:"2023-05-29 00:00:01", value:0 }, { name:'分站', startTime: "2023-05-29 00:00:01", endTime:"2023-05-30 00:00:01", value:1 }, { name:'读卡器', startTime: "2023-05-29 00:00:01", endTime:"2023-05-30 00:00:01", value:1 }, { name:'电源', startTime: "2023-05-29 00:00:01", endTime:"2023-05-30 00:00:01", value:1 }, { name:'分站', startTime: "2023-05-30 00:00:01", endTime:"2023-05-31 00:00:01", value:2 }, { name:'读卡器', startTime: "2023-05-30 00:00:01", endTime:"2023-05-31 00:00:01", value:0 }, { name:'电源', startTime: "2023-05-30 00:00:01", endTime:"2023-05-31 00:00:01", value:2 } ]内对象的两个时间值生成x轴时间轴,再由对象中的name值分成不同组生成y轴,中间的图形的值由同时间,同一name对应生成
时间: 2023-07-19 08:18:20 浏览: 342
一个简单的实现方式是先将数据按照时间排序,然后按照name分组,再遍历每个分组,将同一时间的数据合并,最后生成图表。
具体实现步骤如下:
1. 对数据按照startTime进行排序
```
data.sort((a, b) => new Date(a.startTime) - new Date(b.startTime));
```
2. 按照name进行分组
```
const groups = {};
data.forEach(item => {
if (!groups[item.name]) {
groups[item.name] = [];
}
groups[item.name].push(item);
});
```
3. 遍历每个分组,将同一时间的数据合并
```
const series = Object.keys(groups).map(name => {
const group = groups[name];
const data = [];
let current = null;
group.forEach(item => {
if (!current || item.startTime !== current) {
current = item.startTime;
data.push({ name, value: item.value, time: current });
} else {
data[data.length - 1].value += item.value;
}
});
return { name, data };
});
```
4. 生成图表
```
const chart = echarts.init(document.getElementById('chart'));
chart.setOption({
xAxis: {
type: 'time',
},
yAxis: {
type: 'category',
data: Object.keys(groups),
},
series,
});
```
这样就可以生成一个profile图,x轴是时间轴,y轴是name分组,中间的图形的值由同时间,同一name对应生成。
阅读全文