utils.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // import { dictionary } from "./dictionary";
  2. /**为数组条件增加order属性 */
  3. function appendOrder(pageResult) {
  4. let r = [];
  5. let start = (pageResult.pageNumber - 1) * pageResult.pageSize + 1;
  6. if (pageResult.list) {
  7. pageResult.list.forEach((element) => {
  8. r.push({ order: start++, ...element });
  9. });
  10. }
  11. return r;
  12. }
  13. function findMaxIndex(array) {
  14. if (!array || array.length === 0) {
  15. return;
  16. }
  17. let max = Math.max(...array);
  18. return array.indexOf(max);
  19. }
  20. function setEchartsTooltip(
  21. myChart,
  22. xTooptipFormatter,
  23. yTooltipFormatter,
  24. seriesTooltipFormatter
  25. ) {
  26. myChart.on("mouseover", (params) => {
  27. xLabel = params.value;
  28. if (params.componentType === "yAxis") {
  29. myChart.setOption({
  30. tooltip: {
  31. formatter: yTooltipFormatter ? yTooltipFormatter : formateTooltip,
  32. alwaysShowContent: true,
  33. },
  34. });
  35. } else if (params.componentType === "series") {
  36. myChart.setOption({
  37. tooltip: {
  38. formatter: seriesTooltipFormatter,
  39. alwaysShowContent: true,
  40. },
  41. });
  42. } else if (params.componentType === "xAxis") {
  43. myChart.setOption({
  44. tooltip: {
  45. formatter: xTooptipFormatter ? xTooptipFormatter : formateTooltip,
  46. alwaysShowContent: true,
  47. },
  48. });
  49. } else {
  50. return;
  51. }
  52. let offsetX = params.event.offsetX + 10;
  53. let offsetY = params.event.offsetY + 10;
  54. myChart.dispatchAction({
  55. type: "showTip",
  56. seriesIndex: 0,
  57. dataIndex: 0,
  58. position: [offsetX, offsetY],
  59. });
  60. });
  61. myChart.on("mouseout", (params) => {
  62. xLabel = "";
  63. myChart.setOption({
  64. tooltip: {
  65. formatter: null,
  66. alwaysShowContent: false,
  67. },
  68. });
  69. });
  70. }
  71. var xLabel = "";
  72. function formateTooltip(params) {
  73. return xLabel;
  74. }
  75. function getChartDOMSize(chart) {
  76. if (!chart) {
  77. return;
  78. }
  79. let width = chart._dom.clientWidth;
  80. let height = chart._dom.clientHeight;
  81. return { width, height };
  82. }
  83. /**
  84. * 生成树
  85. * treelist:所有机构列表
  86. * topOrgId:顶级机构id,没有则自动查找
  87. * miniorType:显示到的最低级机构类型,没有则显示全部
  88. */
  89. function getOrgTree(treelist, topOrgId, miniorType) {
  90. let data = treelist;
  91. if (!data || data.length === 0) {
  92. this.selectedOrg = null;
  93. this.defaultExpandedKeys = [];
  94. return [];
  95. } else {
  96. if (!topOrgId) {
  97. const topOrg = data.find(
  98. (d) => !data.find((d2) => d2.orgId == d.parentId)
  99. );
  100. if (topOrg) {
  101. topOrgId = topOrg.orgId;
  102. } else {
  103. return [];
  104. }
  105. }
  106. let temp = data.find((d) => d.orgId == topOrgId);
  107. if (!temp) {
  108. return [];
  109. }
  110. let top = { id: temp.orgId, label: temp.orgName };
  111. top.children = convertToTreeData(data, temp, miniorType);
  112. return [top];
  113. }
  114. }
  115. //将机构列表转换为树结构
  116. function convertToTreeData(array, parent, miniorType) {
  117. if (!parent || parent.orgType == miniorType) {
  118. return [];
  119. }
  120. let nodes = array.filter((i) => i.parentId == parent.orgId);
  121. let list = [];
  122. nodes.forEach((child) => {
  123. let c = { id: child.orgId, label: child.orgName };
  124. let children = convertToTreeData(array, child, miniorType);
  125. if (children && children.length > 0) {
  126. c.children = children;
  127. }
  128. list.push(c);
  129. });
  130. return list;
  131. }
  132. // /**找到该机构的上级分行 */
  133. // function getBranchOrg(orgList, current) {
  134. // if (dictionary.orgType.isBranchType(current.orgType)) {
  135. // return current;
  136. // }
  137. // var up = current;
  138. // while (!dictionary.orgType.isBranchType(up.orgType)) {
  139. // up = orgList.find((o) => o.orgId == up.parentId);
  140. // if (!up) {
  141. // return;
  142. // }
  143. // }
  144. // return up;
  145. // }
  146. // function getBranchOrgById(orgList, orgId) {
  147. // let org = orgList.find((org) => org.orgId == orgId);
  148. // return getBranchOrg(orgList, org);
  149. // }
  150. function getURLParam(url, queryName) {
  151. if (!url) {
  152. return null;
  153. }
  154. let strs = url.split("?");
  155. if (strs.length < 2) {
  156. return "";
  157. }
  158. var query = strs[1];
  159. var vars = query.split("&");
  160. for (var i = 0; i < vars.length; i++) {
  161. var pair = vars[i].split("=");
  162. if (pair[0] == queryName) {
  163. return pair[1];
  164. }
  165. }
  166. return null;
  167. }
  168. function replaceEmptyProperty(obj, defaultValue) {
  169. let copy = { ...obj };
  170. for (var p in copy) {
  171. if ((typeof copy[p] == "string" && copy[p] == "") || copy[p] == null) {
  172. copy[p] = defaultValue;
  173. }
  174. }
  175. return copy;
  176. }
  177. /**********************时间周期定制 **************/
  178. /**返回时间周期tab中的下一个 */
  179. function getNextPeriod(periods, current) {
  180. let currentIndex = periods.map((t) => t.value).indexOf(current);
  181. var nextIndex = periods.length - 1 == currentIndex ? 0 : currentIndex + 1;
  182. let next = periods[nextIndex].value;
  183. return next;
  184. }
  185. /**返回时间周期对应的文件描述 */
  186. function getPeriodText(periods, value) {
  187. const period = periods.find((p) => p.value == value);
  188. if (period) {
  189. return period.text;
  190. }
  191. return "";
  192. }
  193. /**********************时间周期定制 **************/
  194. export {
  195. appendOrder,
  196. findMaxIndex,
  197. setEchartsTooltip,
  198. getChartDOMSize,
  199. getOrgTree,
  200. getURLParam,
  201. replaceEmptyProperty,
  202. getNextPeriod,
  203. getPeriodText
  204. };