FlowChart.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. using Microsoft.AspNetCore.Components;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Dynamic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. namespace wispro.sp.winClient
  8. {
  9. public partial class FlowChart
  10. {
  11. [Parameter]
  12. public double TitleHeight { get; set; } = 100;
  13. [Parameter]
  14. public double ChartWidth { get; set; } = 1200;
  15. [Parameter]
  16. public double ChartHeight { get; set; } = 900;
  17. [Parameter]
  18. public double rectWidth { get; set; } = 150;
  19. [Parameter]
  20. public double rectHeight { get; set; } = 60;
  21. [Parameter]
  22. public double initRadius { get; set; } = 25;
  23. [Parameter]
  24. public double EndRadius { get; set; } = 25;
  25. [Parameter]
  26. public double hSeparation { get; set; } = 40;
  27. int rectFontSize { get; set; } = 18;
  28. public List<entity.workflowDefine.Step> Steps { get; set; }
  29. public List<entity.workflowDefine.TrasferCondition> Transfers { get; set; }
  30. public List<entity.workflowDefine.Action> Actions { get; set; }
  31. public entity.workflowDefine.Workflow workflow { get; set; }
  32. internal class shapeNode
  33. {
  34. public int InCount { get; set; }
  35. public int OutCount { get; set; }
  36. public double x { get; set; }
  37. public double y { get; set; }
  38. public double width { get; set; }
  39. public double height { get; set; }
  40. /// <summary>
  41. /// 形状类型
  42. /// 0:圆
  43. /// 1:矩形
  44. /// 3: 虚拟
  45. /// </summary>
  46. public int Type { get; set; }
  47. public int Level { get; set; } = 1;
  48. public dynamic NodeObject { get; set; }
  49. public List<shapeNode> Childrens { get; set; }
  50. public List<shapeNode> Parents { get; set; }
  51. }
  52. private List<shapeNode> shapeTrees = null;
  53. private shapeNode FindNode(int stepId, out int Level, List<shapeNode> lstNodes)
  54. {
  55. Level = 1;
  56. foreach (var sNode in lstNodes)
  57. {
  58. if (sNode.NodeObject is entity.workflowDefine.Step && sNode.NodeObject.Id == stepId)
  59. {
  60. return sNode;
  61. }
  62. else
  63. {
  64. if (sNode.Childrens != null)
  65. {
  66. Level += 1;
  67. var retObj = FindNode(stepId, out Level, sNode.Childrens);
  68. if (retObj != null)
  69. {
  70. return retObj;
  71. }
  72. }
  73. }
  74. }
  75. return null;
  76. }
  77. private void GetShapeLevelNodes(Dictionary<int, List<shapeNode>> levelNodes, List<shapeNode> TreeNodes)
  78. {
  79. if (TreeNodes != null)
  80. {
  81. foreach (var sNode in TreeNodes)
  82. {
  83. List<shapeNode> nodes = new List<shapeNode>();
  84. if (levelNodes.ContainsKey(sNode.Level))
  85. {
  86. nodes = levelNodes[sNode.Level];
  87. nodes.Add(sNode);
  88. }
  89. else
  90. {
  91. nodes.Add(sNode);
  92. levelNodes.Add(sNode.Level, nodes);
  93. }
  94. GetShapeLevelNodes(levelNodes, sNode.Childrens);
  95. }
  96. }
  97. }
  98. shapeNode startNode;
  99. shapeNode InitShape;
  100. shapeNode endNode;
  101. Dictionary<int, List<shapeNode>> LevelNodes = new Dictionary<int, List<shapeNode>>();
  102. private void initShapeTree()
  103. {
  104. shapeTrees = new List<shapeNode>();
  105. LevelNodes = new Dictionary<int, List<shapeNode>>();
  106. startNode = new shapeNode()
  107. {
  108. //NodeObject = workflow.InitAction,
  109. InCount = 0,
  110. OutCount = 0,
  111. width = 2 * initRadius,
  112. height = 2 * initRadius,
  113. Level = 0
  114. };
  115. InitShape = new shapeNode()
  116. {
  117. NodeObject = workflow.InitAction,
  118. InCount = 0,
  119. OutCount = 0,
  120. Type = 1,
  121. Level = 1
  122. };
  123. startNode.Childrens = new List<shapeNode>();
  124. startNode.Childrens.Add(InitShape);
  125. InitShape.Parents = new List<shapeNode>();
  126. InitShape.Parents.Add(startNode);
  127. endNode = new shapeNode()
  128. {
  129. InCount = 0,
  130. OutCount = 0,
  131. height = 2 * EndRadius,
  132. width = 2 * EndRadius
  133. };
  134. shapeTrees.Add(startNode);
  135. #region 将步骤对象生成形状Node并添加到列表中
  136. if (Steps == null || Steps.Count == 0 || Steps.FirstOrDefault(s => s.Id == workflow.EndStepId) == null)
  137. {
  138. InitShape.Childrens = new List<shapeNode>();
  139. InitShape.Childrens.Add(endNode);
  140. InitShape.OutCount += 1;
  141. endNode.Parents = new List<shapeNode>();
  142. endNode.Parents.Add(InitShape);
  143. endNode.InCount += 1;
  144. endNode.Level = InitShape.Level + 1;
  145. }
  146. foreach (var step in Steps)
  147. {
  148. var temNode = new shapeNode() { NodeObject = step, InCount = 0, OutCount = 0, Type = 1 };
  149. if (workflow.EndStepId == step.Id)
  150. {
  151. temNode.Childrens = new List<shapeNode>();
  152. temNode.Childrens.Add(endNode);
  153. endNode.Parents = new List<shapeNode>();
  154. endNode.Parents.Add(temNode);
  155. }
  156. shapeTrees.Add(temNode);
  157. }
  158. #endregion
  159. #region 遍历转移条件,生成流程树
  160. if (Transfers != null)
  161. {
  162. foreach (var transfer in Transfers)
  163. {
  164. var FromNode = InitShape;
  165. int FromLevel = 0;
  166. if (transfer.StepId != null)
  167. {
  168. FromNode = FindNode(transfer.StepId.Value, out FromLevel, shapeTrees);
  169. }
  170. int ToLevel = 0;
  171. var ToNode = FindNode(transfer.nextStepId, out ToLevel, shapeTrees);
  172. if (FromNode.Childrens == null)
  173. {
  174. FromNode.Childrens = new List<shapeNode>();
  175. }
  176. FromNode.Childrens.Add(ToNode);
  177. if (ToNode.Parents == null)
  178. {
  179. ToNode.Parents = new List<shapeNode>();
  180. }
  181. ToNode.Parents.Add(FromNode);
  182. FromNode.OutCount += 1;
  183. ToNode.InCount += 1;
  184. if (FromNode.Level >= ToLevel)
  185. {
  186. ToNode.Level = FromNode.Level + 1;
  187. }
  188. if (shapeTrees.Contains(ToNode))
  189. {
  190. shapeTrees.Remove(ToNode);
  191. }
  192. }
  193. endNode.Level = endNode.Parents[0].Level + 1;
  194. }
  195. #endregion
  196. GetShapeLevelNodes(LevelNodes, shapeTrees);
  197. #region 添加跨层连接的中间层的虚拟节点
  198. //foreach (int level in LevelNodes.Keys)
  199. //{
  200. // foreach (var temNode in LevelNodes[level])
  201. // {
  202. // if (temNode.Childrens != null)
  203. // {
  204. // foreach (var temChildrenNode in temNode.Childrens)
  205. // {
  206. // if ((temChildrenNode.Level - temNode.Level) > 1)
  207. // {
  208. // temNode.Childrens.Remove(temChildrenNode);
  209. // temChildrenNode.Parents.Remove(temNode);
  210. // var parentNode = temNode;
  211. // for (int iLevel = temNode.Level + 1; iLevel < temChildrenNode.Level; iLevel++)
  212. // {
  213. // var xnNode = new shapeNode() { Type = 3, Parents = new List<shapeNode>(), Childrens = new List<shapeNode>() };
  214. // parentNode.Childrens.Add(xnNode);
  215. // xnNode.Parents = new List<shapeNode>();
  216. // xnNode.Parents.Add(parentNode);
  217. // parentNode = xnNode;
  218. // }
  219. // parentNode.Childrens.Add(temChildrenNode);
  220. // temChildrenNode.Parents.Add(parentNode);
  221. // }
  222. // }
  223. // }
  224. // }
  225. //}
  226. #endregion
  227. int MaxNodeLevel = 0;
  228. int NodeCount = 0;
  229. foreach (int level in LevelNodes.Keys)
  230. {
  231. if (level > 1)
  232. {
  233. int temLevelCount = 0;
  234. foreach (var temNode in LevelNodes[level])
  235. {
  236. int graterInOrOut = (temNode.OutCount > temNode.InCount) ? temNode.OutCount : temNode.InCount;
  237. if (graterInOrOut <= 1)
  238. {
  239. temLevelCount += 1;
  240. }
  241. else
  242. {
  243. temLevelCount += graterInOrOut;
  244. }
  245. }
  246. if (temLevelCount > MaxNodeLevel)
  247. {
  248. MaxNodeLevel = level;
  249. NodeCount = temLevelCount;
  250. }
  251. }
  252. }
  253. #region 每层节点排序
  254. // 2.1. sort out each layer by looking at where it connects from
  255. for (var i = 1; i < LevelNodes.Count; ++i)
  256. {
  257. var top_layer = LevelNodes[i - 1];
  258. LevelNodes[i] = LevelNodes[i].OrderBy(node =>
  259. {
  260. // calculate average position based on connected nodes in top layer
  261. if (node.Parents == null)
  262. {
  263. return 0;
  264. }
  265. var connected_nodes = node.Parents
  266. .Where(l => l.Level == (i - 1)).ToList();
  267. if (!connected_nodes.Any())
  268. {
  269. return 0;
  270. }
  271. var average_index = connected_nodes.Select(cn => { var i = top_layer.IndexOf(cn); return i; }).Average();
  272. return average_index;
  273. }).ToList();
  274. }
  275. // 2.2. now that all but the first layer are layed out, let's deal with the first
  276. if (LevelNodes.Count > 1)
  277. {
  278. LevelNodes[0] = LevelNodes[0].OrderBy(node =>
  279. {
  280. // calculate average position based on connected nodes in top layer
  281. var connected_nodes = node.Childrens
  282. .Where(l => l.Level == 1)
  283. .ToList();
  284. if (!connected_nodes.Any())
  285. {
  286. return 0;
  287. }
  288. var average_index = connected_nodes.Select(cn => { var i = LevelNodes[1].IndexOf(cn); return i; }).Average();
  289. return average_index;
  290. }).ToList();
  291. }
  292. #endregion
  293. ArrangeNodesInRows(LevelNodes);
  294. }
  295. private void ArrangeNodesInRows(Dictionary<int, List<shapeNode>> LevelNodes)
  296. {
  297. double preBotton = TitleHeight;
  298. for (int iLevel = 0; iLevel < LevelNodes.Count; iLevel++)
  299. {
  300. int iCount = 0;
  301. foreach (var node in LevelNodes[iLevel])
  302. {
  303. int max = (node.InCount > node.OutCount) ? node.InCount : node.OutCount;
  304. if (max == 0)
  305. {
  306. max = 1;
  307. }
  308. iCount += max;
  309. }
  310. double onNodeWidth = ChartWidth / (iCount + 1);
  311. iCount = 0;
  312. double maxHeight = 0;
  313. foreach (var node in LevelNodes[iLevel])
  314. {
  315. int max = (node.InCount > node.OutCount) ? node.InCount : node.OutCount;
  316. if (max == 0)
  317. {
  318. max = 1;
  319. }
  320. node.x = onNodeWidth * (iCount + 1) + (max - 1) * onNodeWidth / 2;
  321. if (node.Type == 0)
  322. {
  323. node.width = 2 * initRadius;
  324. node.height = 2 * initRadius;
  325. }
  326. else
  327. {
  328. node.width = rectWidth;
  329. node.height = rectHeight;
  330. }
  331. iCount += max;
  332. if (node.height > maxHeight)
  333. {
  334. maxHeight = node.height;
  335. }
  336. }
  337. foreach (var node in LevelNodes[iLevel])
  338. {
  339. node.y = preBotton + hSeparation + maxHeight / 2;
  340. }
  341. preBotton = preBotton + hSeparation + maxHeight;
  342. }
  343. }
  344. public dynamic GetLineParater(entity.workflowDefine.TrasferCondition trasferCondition)
  345. {
  346. int level = 0;
  347. var startNode = InitShape;
  348. if (trasferCondition.StepId != null)
  349. {
  350. startNode = FindNode(trasferCondition.StepId.Value, out level, shapeTrees);
  351. }
  352. var endNode = FindNode(trasferCondition.nextStepId, out level, shapeTrees);
  353. dynamic ret = new ExpandoObject();
  354. ret.x1 = startNode.x;
  355. ret.y1 = startNode.y + startNode.height / 2;
  356. ret.x2 = endNode.x;
  357. ret.y2 = endNode.y - endNode.height / 2;
  358. return ret;
  359. }
  360. public dynamic GetEndStepLine()
  361. {
  362. var startNode = endNode.Parents[0];
  363. dynamic ret = new ExpandoObject();
  364. ret.x1 = startNode.x;
  365. ret.y1 = startNode.y + startNode.height / 2;
  366. ret.x2 = endNode.x;
  367. ret.y2 = endNode.y - endNode.height / 2;
  368. return ret;
  369. }
  370. public void Refresh()
  371. {
  372. System.Text.Json.JsonSerializer.Serialize(Actions);
  373. initShapeTree();
  374. }
  375. void ClickStep(entity.workflowDefine.Step step)
  376. {
  377. }
  378. void ClickTrasfer(entity.workflowDefine.TrasferCondition trasferCondition)
  379. {
  380. }
  381. void InitAction()
  382. {
  383. }
  384. void ClickNode(shapeNode node)
  385. {
  386. }
  387. }
  388. }