echarts.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import * as echarts from 'echarts';
  2. /**
  3. * 折线图表初始化
  4. * @param xTitleList X轴标题数据
  5. * @param seriesList 折线数据内容
  6. * @param legendList 图表数据说明
  7. * @param domId 目标DOM元素ID
  8. * @param xAxisName X轴刻度名称
  9. * @param gridRight 右侧边距
  10. */
  11. export function initLineImageTable(xTitleList: any, seriesList: any, legendList: any, domId: any, xAxisName: any, gridRight: any) {
  12. const chartDom = document.getElementById(domId);
  13. let myChart = echarts.init(chartDom);
  14. if (myChart != null) {
  15. // 销毁老的图表实例
  16. myChart.dispose();
  17. myChart = echarts.init(chartDom)
  18. }
  19. let option = {
  20. backgroundColor: '#060b1e',
  21. title: {},
  22. tooltip: {
  23. trigger: 'axis'
  24. },
  25. legend: {
  26. data: legendList,
  27. icon: "circle",
  28. textStyle: {
  29. color: '#69859b'
  30. },
  31. bottom: 'bottom',
  32. },
  33. grid: {
  34. top: '3%',
  35. left: '3%',
  36. right: gridRight,
  37. bottom: '30px',
  38. containLabel: true
  39. },
  40. xAxis: {
  41. type: 'category',
  42. name: xAxisName,
  43. boundaryGap: false,
  44. data: xTitleList,
  45. axisLabel: {
  46. color: '#69859b'
  47. }
  48. },
  49. yAxis: {
  50. type: 'value',
  51. axisLabel: {
  52. color: '#69859b'
  53. },
  54. splitLine: {
  55. lineStyle: {
  56. color: 'rgba(70,70,70,0.5)'
  57. }
  58. }
  59. },
  60. series: seriesList
  61. };
  62. option && myChart.setOption(option);
  63. }
  64. /**
  65. * 数据集柱状图表初始化
  66. * @param dataSetSource 数据集数据
  67. * @param seriesList 柱形图表样式定义
  68. * @param domId 目标DOM元素ID
  69. * @param showTextLength X轴坐标刻度每行最大显示字数
  70. */
  71. export function initDataSetBarImageTable(dataSetSource: any, seriesList: any, domId: any, showTextLength: any) {
  72. const chartDom = document.getElementById(domId);
  73. let myChart = echarts.init(chartDom);
  74. if (myChart != null) {
  75. // 销毁老的图表实例
  76. myChart.dispose();
  77. myChart = echarts.init(chartDom)
  78. }
  79. let option = {
  80. title: {},
  81. toolbox: {},
  82. legend: {
  83. textStyle: {
  84. color: '#69859b'
  85. },
  86. bottom: 'bottom',
  87. icon: "circle",
  88. },
  89. tooltip: {
  90. trigger: 'axis',
  91. axisPointer: {
  92. type: 'shadow'
  93. }
  94. },
  95. grid: {
  96. top: '3%',
  97. left: '3%',
  98. right: '3%',
  99. bottom: '30px',
  100. containLabel: true
  101. },
  102. dataset: {
  103. source: dataSetSource
  104. },
  105. xAxis: {
  106. type: 'category',
  107. axisLabel: {
  108. color: '#69859b',
  109. formatter: function (params: any) {
  110. let newParamsName = '';
  111. const paramsNameNumber = params.length; // 文字总长度
  112. const rowNumber = Math.ceil(paramsNameNumber / showTextLength);
  113. if (paramsNameNumber > showTextLength) {
  114. for (let p = 0; p < rowNumber; p++) {
  115. const start = p * showTextLength;
  116. const end = start + showTextLength;
  117. const tempStr = p === rowNumber - 1 ? params.substring(start, paramsNameNumber) : params.substring(start, end) + '\n';
  118. newParamsName += tempStr;
  119. }
  120. } else {
  121. newParamsName = params;
  122. }
  123. return newParamsName;
  124. },
  125. }
  126. },
  127. yAxis: {
  128. type: 'value',
  129. axisLabel: {
  130. color: '#69859b'
  131. },
  132. splitLine: {
  133. lineStyle: {
  134. color: 'rgba(70,70,70,0.5)'
  135. }
  136. }
  137. },
  138. series: seriesList
  139. };
  140. option && myChart.setOption(option);
  141. }
  142. /**
  143. * 堆叠柱状图表初始化
  144. * @param xTitleList X轴数据
  145. * @param seriesList 图表数据内容-包含堆叠数据
  146. * @param legendList 图表数据说明
  147. * @param domId 目标DOM元素ID
  148. */
  149. export function initStackBarImageTable(xTitleList: any, seriesList: any, legendList: any, domId: any, showTextLength: any) {
  150. const chartDom = document.getElementById(domId);
  151. let myChart = echarts.init(chartDom);
  152. if (myChart != null) {
  153. // 销毁老的图表实例
  154. myChart.dispose();
  155. myChart = echarts.init(chartDom)
  156. }
  157. let option = {
  158. title: {},
  159. toolbox: {},
  160. tooltip: {
  161. trigger: 'axis',
  162. axisPointer: {
  163. type: 'shadow'
  164. }
  165. },
  166. legend: {
  167. data: legendList,
  168. textStyle: {
  169. color: '#69859b'
  170. },
  171. bottom: 'bottom',
  172. icon: "circle",
  173. },
  174. grid: {
  175. top: '3%',
  176. left: '3%',
  177. right: '3%',
  178. bottom: '30px',
  179. containLabel: true
  180. },
  181. xAxis: {
  182. type: 'category',
  183. data: xTitleList,
  184. axisLabel: {
  185. color: '#69859b',
  186. formatter: function (params: any) {
  187. let newParamsName = '';
  188. const paramsNameNumber = params.length; // 文字总长度
  189. const rowNumber = Math.ceil(paramsNameNumber / showTextLength);
  190. if (paramsNameNumber > showTextLength) {
  191. for (let p = 0; p < rowNumber; p++) {
  192. const start = p * showTextLength;
  193. const end = start + showTextLength;
  194. const tempStr = p === rowNumber - 1 ? params.substring(start, paramsNameNumber) : params.substring(start, end) + '\n';
  195. newParamsName += tempStr;
  196. }
  197. } else {
  198. newParamsName = params;
  199. }
  200. return newParamsName;
  201. },
  202. }
  203. },
  204. yAxis: {
  205. type: 'value',
  206. axisLabel: {
  207. color: '#69859b'
  208. },
  209. splitLine: {
  210. lineStyle: {
  211. color: 'rgba(70,70,70,0.5)'
  212. }
  213. }
  214. },
  215. series: seriesList
  216. };
  217. option && myChart.setOption(option);
  218. }
  219. /**
  220. * 条形图图表初始化
  221. * @param yAxisList Y轴刻度内容
  222. * @param seriesList X轴条形定义与数据定义
  223. * @param domId 目标DOM元素ID
  224. */
  225. export function initStripBarImageTable(yAxisList: any, seriesList: any, domId: any) {
  226. const chartDom = document.getElementById(domId);
  227. let myChart = echarts.init(chartDom);
  228. if (myChart != null) {
  229. // 销毁老的图表实例
  230. myChart.dispose();
  231. myChart = echarts.init(chartDom)
  232. }
  233. let option = {
  234. title: {},
  235. toolbox: {},
  236. tooltip: {
  237. trigger: 'axis',
  238. axisPointer: {
  239. type: 'shadow'
  240. }
  241. },
  242. legend: {
  243. textStyle: {
  244. color: '#69859b'
  245. },
  246. bottom: 'bottom',
  247. icon: "circle",
  248. },
  249. grid: {
  250. top: '3%',
  251. left: '3%',
  252. right: '3%',
  253. bottom: '30px',
  254. containLabel: true
  255. },
  256. xAxis: {
  257. type: 'value',
  258. axisLabel: {
  259. color: '#69859b'
  260. },
  261. splitLine: {
  262. lineStyle: {
  263. color: 'rgba(70,70,70,0.5)'
  264. }
  265. }
  266. },
  267. yAxis: {
  268. type: 'category',
  269. axisLabel: {
  270. color: '#69859b'
  271. },
  272. splitLine: {
  273. lineStyle: {
  274. color: 'rgba(70,70,70,0.5)'
  275. }
  276. },
  277. data: yAxisList
  278. },
  279. series: seriesList
  280. };
  281. option && myChart.setOption(option);
  282. }
  283. /**
  284. * 环形图图表初始化
  285. * @param seriesDataList 展示数据
  286. * @param graphicChildren 圆环内部文本
  287. * @param domId 目标DOM元素ID
  288. * @param seriesName 注解信息的名称
  289. */
  290. export function initRingPieImageTable(seriesDataList: any, graphicChildren: any, domId: any, seriesName: any, maxTextNum: any) {
  291. const chartDom = document.getElementById(domId);
  292. let myChart = echarts.init(chartDom);
  293. if (myChart != null) {
  294. // 销毁老的图表实例
  295. myChart.dispose();
  296. myChart = echarts.init(chartDom)
  297. }
  298. const option = {
  299. tooltip: {
  300. trigger: 'item'
  301. },
  302. grid: {
  303. top: '3%',
  304. left: '3%',
  305. right: '3%',
  306. bottom: '3%',
  307. containLabel: true
  308. },
  309. series: [
  310. {
  311. name: seriesName,
  312. type: 'pie',
  313. radius: ['40%', '60%'],
  314. label: {
  315. color: 'inherit',
  316. position: 'outer',
  317. borderWidth: 0,
  318. fontSize: 12, // 缩小字体
  319. formatter: function (params) {
  320. const name = params.name;
  321. const percent = params.percent;
  322. let formattedName = '';
  323. for (let i = 0; i < name.length; i += maxTextNum) {
  324. // 如果是最后一行,则不添加换行符
  325. if (i + maxTextNum >= name.length) {
  326. formattedName += name.slice(i, i + maxTextNum);
  327. } else {
  328. formattedName += name.slice(i, i + maxTextNum) + '\n';
  329. }
  330. }
  331. return `${formattedName}:${percent}%`;
  332. }
  333. },
  334. data: seriesDataList
  335. }
  336. ],
  337. graphic: graphicChildren
  338. };
  339. option && myChart.setOption(option);
  340. }
  341. /**
  342. * 饼图图表初始化
  343. * @param seriesDataList 展示数据
  344. * @param domId 目标DOM元素ID
  345. * @param seriesName 注解信息的名称
  346. */
  347. export function initPieImageTable(seriesDataList: any, domId: any, seriesName: any) {
  348. const chartDom = document.getElementById(domId);
  349. let myChart = echarts.init(chartDom);
  350. if (myChart != null) {
  351. // 销毁老的图表实例
  352. myChart.dispose();
  353. myChart = echarts.init(chartDom)
  354. }
  355. const option = {
  356. tooltip: {
  357. trigger: 'item'
  358. },
  359. grid: {
  360. top: '3%',
  361. left: '3%',
  362. right: '3%',
  363. bottom: '3%',
  364. containLabel: true
  365. },
  366. series: [
  367. {
  368. name: seriesName,
  369. type: 'pie',
  370. radius: '70%',
  371. label: {
  372. color: 'inherit',
  373. position: 'outer',
  374. borderWidth: 0,
  375. formatter: '{b}:{d}%'
  376. },
  377. data: seriesDataList
  378. }
  379. ],
  380. };
  381. option && myChart.setOption(option);
  382. }
  383. export const PieColorData = {
  384. "惠城区": "#726D86",
  385. "惠阳区": "#c8ceda",
  386. "博罗县": "#006FFF",
  387. "惠东县": "#9293AD",
  388. "龙门县": "#77F8FF",
  389. "大亚湾区": "#6DD400",
  390. "仲恺区": "#F7B500",
  391. }
  392. export const CompanyModelColorData = {
  393. "20人以下": "#726D86",
  394. "20-99人": "#c8ceda",
  395. "100-299人": "#006FFF",
  396. "300-499人": "#9293AD",
  397. "500-999人": "#77F8FF",
  398. "1000-9999人": "#6DD400",
  399. "10000人以上": "#F7B500",
  400. }