dataUtil.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import constant from "@/utils/constant";
  2. const covertDataToEcharts = (obj, params) => {
  3. let res = {
  4. label: []
  5. };
  6. for (let i = 0; i < params.length; i++) {
  7. res[params[i]] = [];
  8. }
  9. if (obj) {
  10. obj.forEach(item => {
  11. res.label.push(item.label);
  12. if (!item.jsonObject) {
  13. for (let i = 0; i < params.length; i++) {
  14. res[params[i]].push('');
  15. }
  16. return;
  17. }
  18. for (let i = 0; i < params.length; i++) {
  19. res[params[i]].push(item.jsonObject[params[i]]);
  20. }
  21. })
  22. }
  23. return res;
  24. }
  25. const refreshEchartsData = (instance, option, data) => {
  26. option.xAxis.data = data.label;
  27. let arr = [];
  28. for (const key in data) {
  29. if (key == 'label') {
  30. continue;
  31. }
  32. arr.push(data[key])
  33. }
  34. option.color = constant.echartsColors;
  35. let stackMap = new Map();
  36. let barNum = {};
  37. for (let i = 0; i < option.series.length; i++) {
  38. if (option.series[i].type=='bar') {
  39. let stack1 = option.series[i].stack;
  40. if (stack1) {
  41. barNum[stack1] = i;
  42. } else {
  43. barNum['index'+i] = i
  44. }
  45. }
  46. option.series[i].data = arr[i];
  47. option.series[i].smooth = true;
  48. option.series[i].showSymbol = false;
  49. if (option.series[i].areaStyle && i <= constant.echartsGradientColors.length) {
  50. option.series[i].areaStyle = {
  51. color: constant.echartsGradientColors[i]
  52. }
  53. }
  54. let stack = option.series[i].stack;
  55. let index = option.series[i].yAxisIndex ? option.series[i].yAxisIndex : 0;
  56. if (stack) {
  57. let key = stack + "---" + index;
  58. if (!stackMap.has(key)) {
  59. stackMap.set(key, []);
  60. }
  61. stackMap.get(key).push(arr[i])
  62. } else {
  63. let obj = getInterval(arr[i]);
  64. let yAxis = option.yAxis[index];
  65. if (!yAxis.max || yAxis.max < obj.max) {
  66. Object.assign(option.yAxis[index], obj)
  67. }
  68. }
  69. }
  70. for (const barNumKey in barNum) {
  71. option.series[barNum[barNumKey]].itemStyle = {
  72. barBorderRadius: [5, 5, 0, 0]
  73. };
  74. }
  75. stackMap = dealStackInterval(stackMap);
  76. stackMap.forEach((value, key, map) => {
  77. let index = key.split("---")[1];
  78. let obj = getInterval(value);
  79. let yAxis = option.yAxis[index];
  80. if (!yAxis.max || yAxis.max < obj.max) {
  81. Object.assign(option.yAxis[index], obj)
  82. }
  83. });
  84. option.textStyle = {
  85. fontSize: '12px',
  86. fontFamily: "PingFangSC-Regular, serif",
  87. color: '#757575'
  88. };
  89. if (!option.legend) {
  90. option.legend = {};
  91. }
  92. option.legend.textStyle = {
  93. fontSize: '12px',
  94. fontFamily: "Microsoft YaHei, PingFangSC-Regular, serif",
  95. color: '#757575'
  96. };
  97. instance.setOption(option)
  98. }
  99. const dealStackInterval = (stackMap) => {
  100. let indexMap = new Map();
  101. stackMap.forEach((value, key, map) => {
  102. let arr = [];
  103. for (let i = 0; i < value[0].length; i++) {
  104. let num = 0;
  105. for (let j = 0; j < value.length; j++) {
  106. num = num + value[j][i]
  107. }
  108. arr.push(num);
  109. }
  110. indexMap.set(key, arr);
  111. })
  112. return indexMap
  113. }
  114. const getInterval = (arr) => {
  115. let splitNum = 5;
  116. let max = Math.ceil(Math.max.apply(null, arr) * 1.2 / splitNum) * splitNum;
  117. let interval = Math.ceil(max / splitNum)
  118. if (max <= 10) {
  119. return {
  120. min: 0,
  121. max: 10,
  122. interval: 2
  123. }
  124. }
  125. return {
  126. max: max,
  127. min: 0,
  128. interval: interval,
  129. };
  130. }
  131. const circleChartConfig = (option) => {
  132. if (option.title) {
  133. option.title.textStyle = {
  134. color: "#B2B2B2",
  135. fontSize: '15px',
  136. align: "center",
  137. lineHeight: 20,
  138. fontWeight: "normal",
  139. }
  140. }
  141. let label = {
  142. show: true,
  143. avoidLabelOverlap: true,
  144. position: 'outer',
  145. alignTo: 'labelLine',
  146. overflow: 'none',
  147. formatter: (params) => {
  148. const arr = [
  149. `{a|${params.name}}`,
  150. `{b|${params.value}}`,
  151. ]
  152. return arr.join('\n\n')
  153. },
  154. rich: {
  155. b: {
  156. color: '#4D4D4D'
  157. },
  158. },
  159. textStyle: {
  160. align: "left",
  161. baseline: "middle",
  162. fontSize: 14,
  163. color: "#b3b3b3",
  164. },
  165. }
  166. if (!option.series[0].label) {
  167. option.series[0].label = {}
  168. }
  169. Object.assign(option.series[0].label, label)
  170. let labelLine = {
  171. show: true,
  172. length: 10,
  173. length2: 30,
  174. };
  175. if (!option.series[0].labelLine) {
  176. option.series[0].labelLine = {
  177. show: true
  178. }
  179. }
  180. option.color = constant.echartsColors;
  181. Object.assign(option.series[0].labelLine, labelLine)
  182. return option;
  183. }
  184. const circleChartArrToObj = (arr) => {
  185. let obj = {};
  186. if (arr) {
  187. for (let i = 0; i < arr.length; i++) {
  188. obj[arr[i].name] = arr[i].value;
  189. }
  190. }
  191. return obj;
  192. }
  193. export default {
  194. covertDataToEcharts,
  195. refreshEchartsData,
  196. circleChartArrToObj,
  197. circleChartConfig,
  198. }