echart.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. import echarts from 'echarts';
  2. import { objectMerge } from '@/components/thrid/em-element-ui/src/tools/utils'
  3. export function formatLegendData(params) {
  4. let result = '',
  5. number = 4,
  6. name = params.name.toString(),
  7. str = name.replace(/^\s+|\s+$/g, ''),
  8. len = str.length;
  9. number = len > number ? Math.ceil(len / 2) : number;
  10. for (let i = 0; i < len; i++) {
  11. if (i > 0 && i < len && (i % number) == 0) {
  12. result += '\n';
  13. }
  14. result += str.charAt(i);
  15. }
  16. //str.replace(/(?=(?:.{4})+$)/g, '\n');
  17. return result;
  18. }
  19. export function debounce(func, wait, immediate) {
  20. let timeout, args, context, timestamp, result
  21. const later = function () {
  22. // 据上一次触发时间间隔
  23. const last = +new Date() - timestamp
  24. // 上次被包装函数被调用时间间隔last小于设定时间间隔wait
  25. if (last < wait && last > 0) {
  26. timeout = setTimeout(later, wait - last)
  27. } else {
  28. timeout = null
  29. // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
  30. if (!immediate) {
  31. result = func.apply(context, args)
  32. if (!timeout) context = args = null
  33. }
  34. }
  35. }
  36. return function (...args) {
  37. context = this
  38. timestamp = +new Date()
  39. const callNow = immediate && !timeout
  40. // 如果延时不存在,重新设定延时
  41. if (!timeout) timeout = setTimeout(later, wait)
  42. if (callNow) {
  43. result = func.apply(context, args)
  44. context = args = null
  45. }
  46. return result
  47. }
  48. }
  49. //控制浏览器宽度变化
  50. export function chartsInitBAutoResize(obj) {
  51. if (obj.autoResize) {
  52. obj.__resizeHanlder = debounce(() => {
  53. if (obj.chart) {
  54. obj.chart.resize()
  55. }
  56. }, 100)
  57. window.addEventListener('resize', obj.__resizeHanlder)
  58. }
  59. // 监听侧边栏的变化
  60. if (document.getElementsByClassName('sidebar-container').length > 0) {
  61. const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
  62. sidebarElm.addEventListener('transitionend', obj.__resizeHanlder)
  63. }
  64. }
  65. export function chartsDestroyData(obj) {
  66. if (!obj.chart) {
  67. return
  68. }
  69. if (obj.autoResize) {
  70. window.removeEventListener('resize', obj.__resizeHanlder)
  71. }
  72. // 监听侧边栏的变化
  73. if (document.getElementsByClassName('sidebar-container').length > 0) {
  74. const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
  75. sidebarElm.removeEventListener('transitionend', obj.__resizeHanlder)
  76. }
  77. obj.chart.dispose()
  78. obj.chart = null
  79. }
  80. /*
  81. *
  82. * type: 1: color数组,2: objColor数组
  83. * index对象的序号,循环获取值
  84. *
  85. * */
  86. export function getColorValue(type, index) {
  87. let number = 15,
  88. objColor = [{
  89. normal: ['#39A1FF', '#5AB1FF'],
  90. emphasis: ['#5AB1FF', '#39A1FF']
  91. }, {
  92. normal: ['#5CDBD3', '#61EBE2'],
  93. emphasis: ['#61EBE2', '#5CDBD3']
  94. }, {
  95. normal: ['#4ECB73', '#51CB92'],
  96. emphasis: ['#51CB92', '#4ECB73']
  97. }, {
  98. normal: ['#FBD437', '#C1C417'],
  99. emphasis: ['#C1C417', '#FBD437']
  100. }],
  101. color = ['#5B8FF9', '#F6BD16', '#E86452', '#41CC7C', '#F2627B', '#2db9e3', '#90e3aa', '#fd8c6c', '#f75881', '#d6479e', '#de78c7', '#e0aaf0', '#8b7ff2', '#6f5ee4', '#85afff'];
  102. if (type == 1) {
  103. if (typeof (index) == 'number') {
  104. return color[index % number];
  105. } else {
  106. return color;
  107. }
  108. } else {
  109. return objColor;
  110. }
  111. }
  112. //页面data初始化
  113. export function chartsInitData() {
  114. let options = {},
  115. option = {
  116. chart: null,
  117. barGap: '9%',
  118. barWidth: '10',
  119. scrollDataIndex: 0,
  120. legrendUnSelect: true,
  121. color: getColorValue(1),
  122. itemColorArr: getColorValue(2)
  123. };
  124. Object.assign(options, option);
  125. if (arguments.length > 1) {
  126. Object.assign(options, arguments[1]())
  127. }
  128. return options;
  129. }
  130. //option设置
  131. export function chartsOptionGrid() {
  132. return {
  133. left: '10%',
  134. right: '10%',
  135. top: '8%',
  136. bottom: '22%'
  137. };
  138. }
  139. //init merge data
  140. function getObjectValue(currObj, paramObj) {
  141. let result = Object.create(currObj ? currObj : null);
  142. if (currObj) {
  143. objectMerge(result, paramObj);
  144. return result;
  145. } else {
  146. return paramObj;
  147. }
  148. }
  149. function initChartsTitle(option, obj) {
  150. let title = obj.options.title;
  151. if (title) {
  152. option.title = getObjectValue(option.title, title);
  153. }
  154. }
  155. function initChartsColorArray(option, obj) {
  156. let color = obj.options.color;
  157. if (color) {
  158. option.color = getObjectValue(option.color, color);
  159. }
  160. }
  161. function initChartsToolbox(option, obj) {
  162. let toolbox = obj.options.toolbox;
  163. if (toolbox) {
  164. option.toolbox = getObjectValue(option.toolbox, toolbox);
  165. }
  166. }
  167. function initChartsTooltip(option, obj) {
  168. let tooltip = obj.options.tooltip;
  169. if (tooltip) {
  170. option.tooltip = getObjectValue(option.tooltip, tooltip);
  171. }
  172. }
  173. function initChartsGrid(option, obj) {
  174. let grid = obj.options.grid;
  175. if (grid) {
  176. option.grid = getObjectValue(option.grid, grid);
  177. } else {
  178. option.grid = chartsOptionGrid();
  179. }
  180. }
  181. function initChartsLegend(option, obj) {
  182. let legend = obj.options.legend;
  183. if (legend) {
  184. option.legend = getObjectValue(option.legend, legend);
  185. }
  186. }
  187. function initChartsLegendData(option, obj) {
  188. let legendData = obj.options.legendData;
  189. if (legendData) {
  190. option.legendData = getObjectValue(option.legendData, legendData);
  191. }
  192. }
  193. function initChartsxAxis(option, obj) {
  194. if (obj.options.xAxisDel) return false;
  195. let xAxis = obj.options.xAxis;
  196. if (xAxis) {
  197. option.xAxis = getObjectValue(option.xAxis, xAxis);
  198. }
  199. }
  200. function initChartsyAxis(option, obj) {
  201. if (obj.options.yAxisDel) return false;
  202. let yAxis = obj.options.yAxis;
  203. if (yAxis) {
  204. option.yAxis = getObjectValue(option.yAxis, yAxis);
  205. }
  206. }
  207. function initChartsDataZoom(option, obj) {
  208. if (obj.options.dataZoomDel) return false;
  209. let dataZoom = obj.options.dataZoom;
  210. if (dataZoom) {
  211. option.dataZoom = getObjectValue(option.dataZoom, [{
  212. type: 'slider',//图表下方的伸缩条
  213. show: false, //是否显示
  214. realtime: true, //
  215. start: 0, //伸缩条开始位置(1-100),可以随时更改
  216. end: 100, //伸缩条结束位置(1-100),可以随时更改
  217. xAxisIndex: [0],
  218. filterMode: 'none'
  219. }]);
  220. }
  221. }
  222. function initChartsAxisPointer(option, obj) {
  223. if (obj.options.axisPointerDel) return false;
  224. let axisPointer = obj.options.axisPointer
  225. if (axisPointer) {
  226. option.axisPointer = getObjectValue(option.axisPointer, axisPointer);
  227. }
  228. }
  229. function getSeriesEmphasis(obj, item, index) {
  230. let result = {};
  231. return result;
  232. }
  233. function getSeriesAreaStyle(item) {
  234. let result = {};
  235. if (item.areaColor && Array.isArray(item.areaColor)) {
  236. result = {
  237. normal: {
  238. color: new echarts.graphic.LinearGradient(
  239. 0, 0, 0, 1,
  240. [
  241. { offset: 0, color: item.areaColor[0] },
  242. { offset: 0.5, color: item.areaColor[1] },
  243. { offset: 1, color: item.areaColor[2] }
  244. ]
  245. )
  246. }
  247. }
  248. }
  249. return result;
  250. }
  251. function getSeriesItemStyle(obj, item, index) {
  252. let result = {},
  253. type = item.type ? item.type : obj.type;
  254. if (item.gradual) {
  255. let len = obj.itemColorArr.length,
  256. color = obj.itemColorArr[index % len];
  257. result = {
  258. normal: {
  259. color: new echarts.graphic.LinearGradient(
  260. 0, 0, 0, 1,
  261. [
  262. { offset: 0, color: color.normal[0] },
  263. { offset: 0.5, color: color.normal[1] },
  264. { offset: 1, color: color.normal[1] }
  265. ]
  266. )
  267. },
  268. emphasis: {
  269. color: new echarts.graphic.LinearGradient(
  270. 0, 0, 0, 1,
  271. [
  272. { offset: 0, color: color.emphasis[0] },
  273. { offset: 0.7, color: color.emphasis[0] },
  274. { offset: 1, color: color.emphasis[1] }
  275. ]
  276. )
  277. }
  278. };
  279. } else {
  280. if (type == 'pie') {
  281. result = {
  282. normal: {
  283. color: function (params) {
  284. return obj.color[params.dataIndex]
  285. },
  286. label: {
  287. show: true,
  288. //position: 'inner',
  289. formatter: function (a, b, c, d) {
  290. return c;
  291. }
  292. }
  293. }
  294. };
  295. } else {
  296. result = {
  297. normal: {
  298. color: item.color ? item.color : obj.color[index],
  299. borderColor: item.borderColor ? item.borderColor : obj.color[index],
  300. shadowOffsetX: item.shadowOffsetX ? item.shadowOffsetX : 0,
  301. opacity: item.opacity ? item.opacity : 1,
  302. lineStyle: {
  303. type: item.borderType ? item.borderType : 'solid' //'dotted'虚线 'solid'实线
  304. }
  305. }
  306. };
  307. }
  308. }
  309. return result;
  310. }
  311. function getSeriesData(obj) {
  312. let result = [],
  313. series = obj.chartData.series;
  314. series.forEach((item, index) => {
  315. let serData = Object.assign({}, item),
  316. type = item.type ? item.type : obj.type;
  317. //series必须的值
  318. serData.type = type;
  319. serData.name = item.name || 'null';
  320. serData.data = item.data;
  321. serData.label = item.label || { show: false }
  322. serData.itemStyle = item.itemStyle || getSeriesItemStyle(obj, item, index);
  323. serData.emphasis = item.emphasis || getSeriesEmphasis(obj, item, index);
  324. serData.animationEasing = item.animationEasing || 'cubicInOut';
  325. if (item.yAxisIndex) {
  326. serData.yAxisIndex = item.yAxisIndex;
  327. }
  328. if (item.stack) {
  329. serData.stack = item.stack;
  330. }
  331. if (item.markLine) {
  332. serData.markLine = item.markLine;
  333. }
  334. switch (type) {
  335. case 'bar':
  336. serData.animationDuration = 800;
  337. serData.barGap = item.barGap || obj.barGap;
  338. serData.barWidth = item.barWidth || obj.barWidth;
  339. serData.showBackground = item.showBackground || false
  340. serData.backgroundStyle = item.backgroundStyle || {
  341. color: '#5D7092',
  342. opacity: 0.1
  343. }
  344. break;
  345. case 'line':
  346. serData.symbol = item.symbol || 'none'
  347. serData.showSymbol = item.showSymbol || false
  348. serData.symbolSize = item.symbolSize || 4
  349. serData.lineStyle = {}
  350. serData.animation = false;
  351. serData.animationDuration = 1500;
  352. serData.areaStyle = item.areaStyle || getSeriesAreaStyle(item);
  353. serData.lineStyle.width = (item.lineStyle && item.lineStyle.width) || 2
  354. break;
  355. case 'pie':
  356. serData.smooth = item.smooth || true;
  357. serData.radius = item.radius || 40;
  358. serData.center = item.center || ['30%', '50%'];
  359. serData.animationDuration = 800;
  360. serData.label = item.label || {
  361. normal: {
  362. formatter: function (params) {
  363. return params.percent + '%';
  364. }
  365. }
  366. };
  367. serData.labelLine = item.labelLine || {
  368. normal: {
  369. smooth: 0.4,
  370. length: 2,
  371. length2: 20,
  372. }
  373. };
  374. }
  375. result.push(serData)
  376. // 立体效果
  377. if (item.stereoscopic) {
  378. result.push(...stereoscopicCharts(serData))
  379. }
  380. // 是否显示阴影背景
  381. if (item.ishadow) {
  382. result.push(...shadowCharts(serData))
  383. }
  384. });
  385. return result;
  386. }
  387. // 立体效果
  388. function stereoscopicCharts(item) {
  389. const data = []
  390. const minList = []
  391. const barMaxWidth = Math.round(item.barWidth * 2 / 3)
  392. item.data.forEach(num => {
  393. minList.push(1)
  394. })
  395. data.push({
  396. data: minList,
  397. symbol: 'diamond',
  398. type: 'pictorialBar',
  399. symbolOffset: [0, '-50%'],
  400. symbolSize: [item.barWidth, Math.round(item.barWidth / 2)],
  401. barMaxWidth: barMaxWidth
  402. })
  403. data.push({
  404. data: item.data,
  405. symbol: 'diamond',
  406. symbolPosition: 'end',
  407. type: 'pictorialBar',
  408. symbolOffset: [0, '-50%'],
  409. symbolSize: [item.barWidth, Math.round(item.barWidth / 2) - 2],
  410. barMaxWidth: barMaxWidth,
  411. zlevel: 2
  412. })
  413. return data
  414. }
  415. // 是否显示阴影背景
  416. function shadowCharts(item) {
  417. const data = []
  418. const maxData = Math.max(...item.data)
  419. const maxList = []
  420. const minList = []
  421. const barMaxWidth = Math.round(item.barWidth * 2 / 3)
  422. item.data.forEach(num => {
  423. minList.push(1)
  424. maxList.push(maxData)
  425. })
  426. data.push({
  427. data: maxList,
  428. type: 'bar',
  429. barGap: '-100%',
  430. barWidth: item.barWidth,
  431. barMaxWidth: 'auto',
  432. zlevel: -1
  433. })
  434. data.push({
  435. data: minList,
  436. symbol: 'diamond',
  437. type: 'pictorialBar',
  438. symbolOffset: [0, '-50%'],
  439. symbolSize: [item.barWidth, Math.round(item.barWidth / 2)],
  440. barMaxWidth: barMaxWidth,
  441. zlevel: -2
  442. })
  443. data.push({
  444. data: maxList,
  445. symbol: 'diamond',
  446. symbolPosition: 'end',
  447. type: 'pictorialBar',
  448. symbolOffset: [0, '-50%'],
  449. symbolSize: [item.barWidth, Math.round(item.barWidth / 2) - 2],
  450. barMaxWidth: barMaxWidth,
  451. zlevel: -1
  452. })
  453. return data
  454. }
  455. function initChartsSeries(option, obj) {
  456. if (obj.chartData.series) {
  457. option.series = getSeriesData(obj);
  458. }
  459. }
  460. export function mergeChartsOptions(obj) {
  461. let options = {};
  462. initChartsTitle(options, obj);//初始化title
  463. initChartsColorArray(options, obj);//初始化title
  464. initChartsToolbox(options, obj);//初始化toolbox
  465. initChartsTooltip(options, obj);//初始化tooltip
  466. initChartsGrid(options, obj);//初始化图上下左右的空白
  467. initChartsAxisPointer(options, obj); //初始化坐标轴指示器
  468. initChartsLegend(options, obj);//实例初始化
  469. initChartsLegendData(options, obj);//实例初始化
  470. initChartsxAxis(options, obj);//x坐标轴
  471. initChartsyAxis(options, obj);//y坐标轴
  472. initChartsDataZoom(options, obj);//坐标轴
  473. initChartsSeries(options, obj);//数值初始化
  474. return options;
  475. }
  476. export function legendSelecthanged(obj, option) {
  477. if (!option.legendData) return;
  478. obj.chart.on('legendselectchanged', function (params) {
  479. let num = 0,
  480. name = params.name;
  481. option.series[num].data.forEach((item, index) => {
  482. let value = 0;
  483. option.legendData[index].forEach(legend => {
  484. if (name == legend.subjectName) {
  485. value = Number(legend.fee);
  486. }
  487. });
  488. if (params.selected[name]) {
  489. option.series[num].data[index] = item + value;
  490. } else {
  491. option.series[num].data[index] = item - value;
  492. }
  493. });
  494. obj.chart.setOption(option);
  495. });
  496. }
  497. export function drawCharts(obj) {
  498. let option = mergeChartsOptions(obj);
  499. obj.chart.setOption(option);
  500. legendSelecthanged(obj, option);//图例选择改变事件
  501. }