"时序图"-->先简单解释一下,忽然接到任务要求作一个时序图(后面会附上参考图片),因为需求不是很完善,设计也还没给出图纸,暂时就先作一个知足基本功能的demo。javascript
需求整理:html
暂时的需求整理结束,下面来看看实现上遇到的一些问题,及解决方法。 注:前端
判断一个主任务的时刻步长:java
step = (主任务结束时间 - 主任务开始时间)/3
// 这里为何是除以3呢?好比从12:00 - 15:00(12:00为刻度1,12 + step,12 + 2step, 15:00)
复制代码
注: 实现的代码有点长,所有贴出来的话阅读会比较困难,因此我会贴出一些重要的部分,以及遇到问题的部分。node
按照antd给出的示例去引入Tree组件,而后给参数添加类型。react
public renderTree = () => {
return (
// (JSX attribute) loadData?: ((node: AntTreeNode) => PromiseLike<any>) | undefined
<Tree className="tree-box" loadData={this.onLoadData}>
{this.renderTreeNodes(this.state.treeData)}
</Tree>
);
}
复制代码
使用TreeNodes加载数据的话,ts里会遇到一个这样的报错提示:前端工程师
AntTreeNode上不存在dataRef,这时我选择去Tree.d.ts里面的AntTreeNodeProps接口里本身定义一个antd
dataRef: Partial<{children?: React.ReactNode}>;
复制代码
在咱们的组件里还须要这样去定义:性能
interface IRef extends AntTreeNodeProps{
dataRef: Partial<{children?: React.ReactNode}>;
}
// 由于咱们的TreedNode是一个组件类型,咱们须要在组件上注入dataRef这样在使用TreeNode时就不会有错误提示了
const TreeNode = Tree.TreeNode as React.ComponentClass<IRef>;
复制代码
这里算是antd对ts写做支持的一个遗漏。好了ts的问题就到这里。大数据
使用TreeNode时咱们发现他的Title属性支持ReactNode,那么咱们的进度条能够利用这个属性去显示。进度条的位置能够利用子任务的开始时间去判断。
antd Tree组件的子组件都是块级元素,而且为了表示层级,antd里treeNode都有一个固定的padding:0,0,0,18px,咱们设置左侧任务名字的最大长度为400px.那么咱们能够计算一下treeNode的长度:
400 - (index - 1) * 18 // 第一层的tree Node 的index为1,第一层的tree Node 的index为2,第一层的tree Node 的index为3以此类推。
复制代码
解释: 子任务无论是串行仍是并行,咱们这里先不作考虑,由于子任务开始时会记录它开始的时间段,咱们只须要用它结束的时间段,和开始的时间段就能够知道任务的运行时长,以及子任务所占主任务的比例,经过比例咱们能够设定它不一样的颜色及状态。
(子任务结束时间 - 子任务开始时间) / (主任务结束时间 - 主任务开始时间) * (屏幕宽度 - 400)
// 400任务名所占的最大宽度
复制代码
// color 设置子任务的颜色。
public color = {
done: '#52c41a',
failed: '#f5222d',
running: '#1890ff'
};
public running_color = (max: number, min: number, item_width: number, color: string) => {
if (min < item_width && item_width <= max) {
this.color.running = color;
}
}
// 经过运行状态取判断子任务的颜色
if (item.status === 'running') {
this.running_color(total_width, 0, item_width, '#b0afcc');
this.running_color((total_width * 2), total_width, item_width, '#7e7bc9');
this.running_color((total_width * 3), (total_width * 2), item_width, '#3a34c9');
this.running_color((total_width * 4), (total_width * 3), item_width, '#1427e1');
}
else this.progress_color = this.color[item.status];
复制代码
原文连接:tech.gtxlab.com/react-tree.…
做者信息:宁文飞,人和将来大数据前端工程师