FlowChart.razor.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. using Microsoft.AspNetCore.Components;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Dynamic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using wispro.sp.entity.workflowDefine;
  9. namespace wispro.sp.web.Components
  10. {
  11. public partial class FlowChart
  12. {
  13. [Parameter]
  14. public double TitleHeight { get; set; } = 100;
  15. [Parameter]
  16. public double ChartWidth { get; set; } = 1200;
  17. [Parameter]
  18. public double ChartHeight { get; set; } = 900;
  19. [Parameter]
  20. public double rectWidth { get; set; } = 150;
  21. [Parameter]
  22. public double rectHeight { get; set; } = 60;
  23. [Parameter]
  24. public double initRadius { get; set; } = 20;
  25. [Parameter]
  26. public double EndRadius { get; set; } = 20;
  27. [Parameter]
  28. public double hSeparation { get; set; } = 40;
  29. [Parameter]
  30. public string StepShapeColor { get; set; } = "green";
  31. [Parameter]
  32. public string EndShapColor { get; set; } = "gray";
  33. int rectFontSize { get; set; } = 18;
  34. [Parameter]
  35. public List<entity.workflowDefine.Step> Steps { get; set; }
  36. [Parameter]
  37. public List<entity.workflowDefine.TrasferCondition> Transfers { get; set; }
  38. [Parameter]
  39. public List<entity.workflowDefine.Action> Actions { get; set; }
  40. [Parameter]
  41. public entity.workflowDefine.Workflow workflow { get; set; }
  42. internal class shapeNode
  43. {
  44. public int InCount { get; set; }
  45. public int OutCount { get; set; }
  46. public double x { get; set; }
  47. public double y { get; set; }
  48. public double width { get; set; }
  49. public double height { get; set; }
  50. /// <summary>
  51. /// 形状类型
  52. /// 0:圆
  53. /// 1:矩形
  54. /// 3: 虚拟
  55. /// </summary>
  56. public int Type { get; set; }
  57. public int Level { get; set; } = 1;
  58. public dynamic NodeObject { get; set; }
  59. public List<shapeNode> Childrens { get; set; }
  60. public List<shapeNode> Parents { get; set; }
  61. public string FillColor { get; set; } = "white";
  62. }
  63. private List<shapeNode> shapeTrees = null;
  64. private shapeNode FindNode(int stepId, out int Level, List<shapeNode> lstNodes)
  65. {
  66. Level = 1;
  67. foreach (var sNode in lstNodes)
  68. {
  69. if (sNode.NodeObject is entity.workflowDefine.Step && sNode.NodeObject.Id == stepId)
  70. {
  71. return sNode;
  72. }
  73. else
  74. {
  75. if (sNode.Childrens != null)
  76. {
  77. Level += 1;
  78. var retObj = FindNode(stepId, out Level, sNode.Childrens);
  79. if (retObj != null)
  80. {
  81. return retObj;
  82. }
  83. }
  84. }
  85. }
  86. return null;
  87. }
  88. private void GetShapeLevelNodes(Dictionary<int, List<shapeNode>> levelNodes, List<shapeNode> TreeNodes)
  89. {
  90. if (TreeNodes != null)
  91. {
  92. foreach (var sNode in TreeNodes)
  93. {
  94. List<shapeNode> nodes = new List<shapeNode>();
  95. if (levelNodes.ContainsKey(sNode.Level))
  96. {
  97. nodes = levelNodes[sNode.Level];
  98. nodes.Add(sNode);
  99. }
  100. else
  101. {
  102. nodes.Add(sNode);
  103. levelNodes.Add(sNode.Level, nodes);
  104. }
  105. GetShapeLevelNodes(levelNodes, sNode.Childrens);
  106. }
  107. }
  108. }
  109. shapeNode startNode;
  110. shapeNode InitShape;
  111. shapeNode endNode;
  112. Dictionary<int, List<shapeNode>> LevelNodes = new Dictionary<int, List<shapeNode>>();
  113. private void initShapeTree()
  114. {
  115. shapeTrees = new List<shapeNode>();
  116. LevelNodes = new Dictionary<int, List<shapeNode>>();
  117. startNode = new shapeNode()
  118. {
  119. //NodeObject = workflow.InitAction,
  120. InCount = 0,
  121. OutCount = 0,
  122. width = 2 * initRadius,
  123. height = 2 * initRadius,
  124. Level = 0
  125. };
  126. InitShape = new shapeNode()
  127. {
  128. NodeObject = workflow.InitAction,
  129. InCount = 0,
  130. OutCount = 0,
  131. Type = 1,
  132. Level = 1,
  133. FillColor = StepShapeColor
  134. };
  135. startNode.Childrens = new List<shapeNode>();
  136. startNode.Childrens.Add(InitShape);
  137. InitShape.Parents = new List<shapeNode>();
  138. InitShape.Parents.Add(startNode);
  139. endNode = new shapeNode()
  140. {
  141. InCount = 0,
  142. OutCount = 0,
  143. height = 2 * EndRadius,
  144. width = 2 * EndRadius,
  145. FillColor = EndShapColor
  146. };
  147. shapeTrees.Add(startNode);
  148. //shapeTrees.Add(endNode);
  149. #region 将步骤对象生成形状Node并添加到列表中
  150. if (Steps == null || Steps.Count == 0 || (Steps.FirstOrDefault(s => s.Id == workflow.EndStepId) == null ))
  151. {
  152. InitShape.Childrens = new List<shapeNode>();
  153. InitShape.Childrens.Add(endNode);
  154. InitShape.OutCount += 1;
  155. endNode.Parents = new List<shapeNode>();
  156. endNode.Parents.Add(InitShape);
  157. endNode.InCount += 1;
  158. endNode.Level = InitShape.Level + 1;
  159. }
  160. if (Steps != null)
  161. {
  162. foreach (var step in Steps)
  163. {
  164. var temNode = new shapeNode() { NodeObject = step, InCount = 0, OutCount = 0, Type = 1,FillColor = StepShapeColor };
  165. if (workflow.EndStepId == step.Id && workflow.EndStepId>0)
  166. {
  167. temNode.Childrens = new List<shapeNode>();
  168. temNode.Childrens.Add(endNode);
  169. endNode.Parents = new List<shapeNode>();
  170. endNode.Parents.Add(temNode);
  171. }
  172. shapeTrees.Add(temNode);
  173. }
  174. }
  175. #endregion
  176. #region 遍历转移条件,生成流程树
  177. if (Transfers != null)
  178. {
  179. foreach (var transfer in Transfers)
  180. {
  181. var FromNode = InitShape;
  182. int FromLevel = 0;
  183. if (transfer.StepId != null)
  184. {
  185. FromNode = FindNode(transfer.StepId.Value, out FromLevel, shapeTrees);
  186. }
  187. int ToLevel = 0;
  188. var ToNode = FindNode(transfer.nextStepId, out ToLevel, shapeTrees);
  189. if (FromNode.Childrens == null)
  190. {
  191. FromNode.Childrens = new List<shapeNode>();
  192. }
  193. FromNode.Childrens.Add(ToNode);
  194. if (ToNode.Parents == null)
  195. {
  196. ToNode.Parents = new List<shapeNode>();
  197. }
  198. ToNode.Parents.Add(FromNode);
  199. FromNode.OutCount += 1;
  200. ToNode.InCount += 1;
  201. if (FromNode.Level >= ToLevel)
  202. {
  203. ToNode.Level = FromNode.Level + 1;
  204. }
  205. if (shapeTrees.Contains(ToNode))
  206. {
  207. shapeTrees.Remove(ToNode);
  208. }
  209. }
  210. if (endNode.Parents != null && endNode.Parents[0].NodeObject is entity.workflowDefine.Step)
  211. {
  212. endNode.Level = endNode.Parents[0].Level + 1;
  213. }
  214. }
  215. #endregion
  216. GetShapeLevelNodes(LevelNodes, shapeTrees);
  217. #region 添加跨层连接的中间层的虚拟节点
  218. //foreach (int level in LevelNodes.Keys)
  219. //{
  220. // foreach (var temNode in LevelNodes[level])
  221. // {
  222. // if (temNode.Childrens != null)
  223. // {
  224. // foreach (var temChildrenNode in temNode.Childrens)
  225. // {
  226. // if ((temChildrenNode.Level - temNode.Level) > 1)
  227. // {
  228. // temNode.Childrens.Remove(temChildrenNode);
  229. // temChildrenNode.Parents.Remove(temNode);
  230. // var parentNode = temNode;
  231. // for (int iLevel = temNode.Level + 1; iLevel < temChildrenNode.Level; iLevel++)
  232. // {
  233. // var xnNode = new shapeNode() { Type = 3, Parents = new List<shapeNode>(), Childrens = new List<shapeNode>() };
  234. // parentNode.Childrens.Add(xnNode);
  235. // xnNode.Parents = new List<shapeNode>();
  236. // xnNode.Parents.Add(parentNode);
  237. // parentNode = xnNode;
  238. // }
  239. // parentNode.Childrens.Add(temChildrenNode);
  240. // temChildrenNode.Parents.Add(parentNode);
  241. // }
  242. // }
  243. // }
  244. // }
  245. //}
  246. #endregion
  247. int MaxNodeLevel = 0;
  248. int NodeCount = 0;
  249. foreach (int level in LevelNodes.Keys)
  250. {
  251. if (level > 1)
  252. {
  253. int temLevelCount = 0;
  254. foreach (var temNode in LevelNodes[level])
  255. {
  256. int graterInOrOut = (temNode.OutCount > temNode.InCount) ? temNode.OutCount : temNode.InCount;
  257. if (graterInOrOut <= 1)
  258. {
  259. temLevelCount += 1;
  260. }
  261. else
  262. {
  263. temLevelCount += graterInOrOut;
  264. }
  265. }
  266. if (temLevelCount > MaxNodeLevel)
  267. {
  268. MaxNodeLevel = level;
  269. NodeCount = temLevelCount;
  270. }
  271. }
  272. }
  273. #region 每层节点排序
  274. // 2.1. sort out each layer by looking at where it connects from
  275. for (var i = 1; i < LevelNodes.Count; ++i)
  276. {
  277. var top_layer = LevelNodes[i - 1];
  278. LevelNodes[i] = LevelNodes[i].OrderBy(node =>
  279. {
  280. // calculate average position based on connected nodes in top layer
  281. if (node.Parents == null)
  282. {
  283. return 0;
  284. }
  285. var connected_nodes = node.Parents
  286. .Where(l => l.Level == (i - 1)).ToList();
  287. if (!connected_nodes.Any())
  288. {
  289. return 0;
  290. }
  291. var average_index = connected_nodes.Select(cn => { var i = top_layer.IndexOf(cn); return i; }).Average();
  292. return average_index;
  293. }).ToList();
  294. }
  295. // 2.2. now that all but the first layer are layed out, let's deal with the first
  296. if (LevelNodes.Count > 1)
  297. {
  298. LevelNodes[0] = LevelNodes[0].OrderBy(node =>
  299. {
  300. // calculate average position based on connected nodes in top layer
  301. var connected_nodes = node.Childrens
  302. .Where(l => l.Level == 1)
  303. .ToList();
  304. if (!connected_nodes.Any())
  305. {
  306. return 0;
  307. }
  308. var average_index = connected_nodes.Select(cn => { var i = LevelNodes[1].IndexOf(cn); return i; }).Average();
  309. return average_index;
  310. }).ToList();
  311. }
  312. #endregion
  313. ArrangeNodesInRows(LevelNodes);
  314. }
  315. private void ArrangeNodesInRows(Dictionary<int, List<shapeNode>> LevelNodes)
  316. {
  317. double preBotton = TitleHeight;
  318. for (int iLevel = 0; iLevel < LevelNodes.Count; iLevel++)
  319. {
  320. int iCount = 0;
  321. foreach (var node in LevelNodes[iLevel])
  322. {
  323. int max = (node.InCount > node.OutCount) ? node.InCount : node.OutCount;
  324. if (max == 0)
  325. {
  326. max = 1;
  327. }
  328. iCount += max;
  329. }
  330. double onNodeWidth = ChartWidth / (iCount + 1);
  331. iCount = 0;
  332. double maxHeight = 0;
  333. foreach (var node in LevelNodes[iLevel])
  334. {
  335. int max = (node.InCount > node.OutCount) ? node.InCount : node.OutCount;
  336. if (max == 0)
  337. {
  338. max = 1;
  339. }
  340. node.x = onNodeWidth * (iCount + 1) + (max - 1) * onNodeWidth / 2;
  341. if (node.Type == 0)
  342. {
  343. node.width = 2 * initRadius;
  344. node.height = 2 * initRadius;
  345. }
  346. else
  347. {
  348. node.width = rectWidth;
  349. node.height = rectHeight;
  350. }
  351. iCount += max;
  352. if (node.height > maxHeight)
  353. {
  354. maxHeight = node.height;
  355. }
  356. }
  357. foreach (var node in LevelNodes[iLevel])
  358. {
  359. node.y = preBotton + hSeparation + maxHeight / 2;
  360. }
  361. preBotton = preBotton + hSeparation + maxHeight;
  362. }
  363. }
  364. public dynamic GetLineParater(entity.workflowDefine.TrasferCondition trasferCondition)
  365. {
  366. int level = 0;
  367. var startNode = InitShape;
  368. if (trasferCondition.StepId != null)
  369. {
  370. startNode = FindNode(trasferCondition.StepId.Value, out level, shapeTrees);
  371. }
  372. var endNode = FindNode(trasferCondition.nextStepId, out level, shapeTrees);
  373. dynamic ret = new ExpandoObject();
  374. ret.x1 = startNode.x;
  375. ret.y1 = startNode.y + startNode.height / 2;
  376. ret.x2 = endNode.x;
  377. ret.y2 = endNode.y - endNode.height / 2;
  378. double x3 = 0.0;
  379. double y3 = 0.0;
  380. GetArrorEndPoint(ret, out x3, out y3);
  381. ret.x2 = x3;
  382. ret.y2 = y3;
  383. return ret;
  384. }
  385. public void GetArrorEndPoint(dynamic ret, out double x3, out double y3)
  386. {
  387. if (ret.x1 == ret.x2)
  388. {
  389. x3 = ret.x1;
  390. if (ret.y2 > ret.y1)
  391. {
  392. y3 = ret.y2 - 8;
  393. }
  394. else
  395. {
  396. y3 = ret.y2 + 8;
  397. }
  398. }
  399. else
  400. {
  401. if (ret.y1 == ret.y2)
  402. {
  403. y3 = ret.y1;
  404. if (ret.x1 < ret.x2)
  405. {
  406. x3 = ret.x2 - 8;
  407. }
  408. else
  409. {
  410. x3 = ret.x2 + 8;
  411. }
  412. }
  413. else
  414. {
  415. double k = (ret.y1 - ret.y2) / (ret.x1 - ret.x2);
  416. double b = ret.y2 - k * ret.x2;
  417. double a = k * k + 1;
  418. double B = 2 * k * (b - ret.y2) - 2 * ret.x2;
  419. double c = (b - ret.y2) * (b - ret.y2) + ret.x2 * ret.x2 - 100;
  420. double x3_1 = (Math.Sqrt(B * B - 4 * a * c) - B) / (2 * a);
  421. double x3_2 = ((Math.Sqrt(B * B - 4 * a * c) + B) / (2 * a)) * -1;
  422. if (ret.x1 < ret.x2)
  423. {
  424. x3 = (x3_1 < x3_2) ? x3_1 : x3_2;
  425. }
  426. else
  427. {
  428. x3 = (x3_1 < x3_2) ? x3_2 : x3_1;
  429. }
  430. y3 = k * x3 + b;
  431. }
  432. }
  433. }
  434. public dynamic GetEndStepLine()
  435. {
  436. var startNode = InitShape;
  437. if (endNode.Parents != null)
  438. {
  439. startNode = endNode.Parents[0];
  440. }
  441. dynamic ret = new ExpandoObject();
  442. ret.x1 = startNode.x;
  443. ret.y1 = startNode.y + startNode.height / 2;
  444. ret.x2 = endNode.x;
  445. ret.y2 = endNode.y - endNode.height / 2;
  446. double x3 = 0.0;
  447. double y3 = 0.0;
  448. GetArrorEndPoint(ret, out x3, out y3);
  449. ret.x2 = x3;
  450. ret.y2 = y3;
  451. return ret;
  452. }
  453. public dynamic GetStartInitLine()
  454. {
  455. dynamic ret = new ExpandoObject();
  456. ret.x1 = this.startNode.x;
  457. ret.y1 = this.startNode.y + startNode.height / 2;
  458. ret.x2 = InitShape.x;
  459. ret.y2 = InitShape.y - InitShape.height / 2;
  460. double x3 = 0.0;
  461. double y3 = 0.0;
  462. GetArrorEndPoint(ret, out x3, out y3);
  463. ret.x2 = x3;
  464. ret.y2 = y3;
  465. return ret;
  466. }
  467. protected override void OnInitialized()
  468. {
  469. Refresh();
  470. }
  471. public void Refresh()
  472. {
  473. //System.Text.Json.JsonSerializer.Serialize(Actions);
  474. initShapeTree();
  475. StateHasChanged();
  476. }
  477. void InitAction()
  478. {
  479. }
  480. #region 单击/双击事件
  481. [Parameter]
  482. public EventCallback<entity.workflowDefine.Step> OnClickStep { get; set; }
  483. [Parameter]
  484. public EventCallback<entity.workflowDefine.Action> OnClickAction{ get; set; }
  485. [Parameter]
  486. public EventCallback<entity.workflowDefine.TrasferCondition> OnClickTransfer { get; set; }
  487. [Parameter]
  488. public EventCallback<entity.workflowDefine.Step> OnDoubleClickStep { get; set; }
  489. [Parameter]
  490. public EventCallback<entity.workflowDefine.Action> OnDoubleClickAction { get; set; }
  491. [Parameter]
  492. public EventCallback<entity.workflowDefine.TrasferCondition> OnDoubleClickTransfer { get; set; }
  493. private shapeNode SelectedShape;
  494. void ClickNode(shapeNode node)
  495. {
  496. SelectedShape = node;
  497. StateHasChanged();
  498. if (node.NodeObject is entity.workflowDefine.Step)
  499. {
  500. if (OnClickStep.HasDelegate)
  501. {
  502. OnClickStep.InvokeAsync((entity.workflowDefine.Step)node.NodeObject);
  503. }
  504. }
  505. if (node.NodeObject is entity.workflowDefine.Action)
  506. {
  507. if (OnClickAction.HasDelegate)
  508. {
  509. OnClickAction.InvokeAsync((entity.workflowDefine.Action)node.NodeObject);
  510. }
  511. }
  512. }
  513. void DoubleClickNode(shapeNode node)
  514. {
  515. SelectedShape = node;
  516. StateHasChanged();
  517. if (node.NodeObject is entity.workflowDefine.Step)
  518. {
  519. if (OnDoubleClickStep.HasDelegate)
  520. {
  521. OnDoubleClickStep.InvokeAsync((entity.workflowDefine.Step)node.NodeObject);
  522. }
  523. }
  524. if (node.NodeObject is entity.workflowDefine.Action)
  525. {
  526. if (OnDoubleClickAction.HasDelegate)
  527. {
  528. OnDoubleClickAction.InvokeAsync((entity.workflowDefine.Action)node.NodeObject);
  529. }
  530. }
  531. }
  532. void ClickTrasfer(entity.workflowDefine.TrasferCondition trasferCondition)
  533. {
  534. SelectedShape = null;
  535. if (OnClickTransfer.HasDelegate)
  536. {
  537. OnClickTransfer.InvokeAsync(trasferCondition);
  538. }
  539. }
  540. void DoubleClickTrasfer(entity.workflowDefine.TrasferCondition trasferCondition)
  541. {
  542. SelectedShape = null;
  543. if (OnDoubleClickTransfer.HasDelegate)
  544. {
  545. OnDoubleClickTransfer.InvokeAsync(trasferCondition);
  546. }
  547. }
  548. #endregion
  549. public string GetSvgString()
  550. {
  551. return null;
  552. }
  553. }
  554. }