CRECharts.js 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  1. /**
  2. * 创建者:王成
  3. * 创建日期:2021年12月10日
  4. * 描述:自定义ECharts
  5. */
  6. /**
  7. * 构造函数
  8. * @param {JSON} options 选项
  9. */
  10. function CrCharts(options) {
  11. this._init(options);
  12. this.click = options.click;
  13. }
  14. /**
  15. * 原型设置 必须的
  16. * @param {JSON} options 选项
  17. */
  18. CrCharts.prototype.extend = function(options) {
  19. for (var key in options) {
  20. CrCharts.prototype[key] = options[key];
  21. }
  22. }
  23. /**
  24. * 内部函数集合
  25. */
  26. CrCharts.prototype.extend({
  27. /**
  28. * 初始化函数
  29. * @param {JSON} options 选项
  30. */
  31. _init: function(options) {
  32. if (options.domId == undefined) {
  33. console.error('未设置显示容器!')
  34. return;
  35. }
  36. var self = this;
  37. //局部变量
  38. this.selector = options.domId; //3D地图容器
  39. this.labelColor = options.labelColor; //标签颜色
  40. this.legendTextColor = options.legendTextColor; //图例文字颜色
  41. this.xAxisLineColor = options.xAxisLineColor; //X轴颜色
  42. this.yAxisLineColor = options.yAxisLineColor; //Y轴颜色
  43. this.splitLineColor = options.splitLineColor; //分割辅助线颜色
  44. this.chart = echarts.init(document.getElementById(this.selector));
  45. /* 添加事件 */
  46. this.chart.on('click', function(params) {
  47. if (self.click != undefined) {
  48. self.click(params);
  49. }
  50. });
  51. /* 辅助线样式 */
  52. this.splitLine = {
  53. show: true,
  54. lineStyle: {
  55. type: 'dashed',
  56. color: this.splitLineColor == undefined ? 'rgba(69,69,69,0.3)' : this.splitLineColor,
  57. }
  58. };
  59. /* X轴线样式 */
  60. this.xAxisLine = {
  61. show: true,
  62. lineStyle: {
  63. color: this.xAxisLineColor == undefined ? "rgba(69,69,69,1.0)" : this.xAxisLineColor,
  64. },
  65. };
  66. /* X轴样式 */
  67. if (options.xAxisLabel != undefined) {
  68. this.xAxisLabel = options.xAxisLabel; //X轴样式
  69. } else {
  70. this.xAxisLabel = {
  71. color: "rgba(69,69,69,1.0)",
  72. fontSize: 12,
  73. fontFamily: 'TTTGB-Medium',
  74. interval: 0,
  75. };
  76. }
  77. this.yAxisLine = {
  78. show: true,
  79. lineStyle: {
  80. color: this.yAxisLineColor == undefined ? "rgba(69,69,69,1.0)" : this.yAxisLineColor,
  81. },
  82. };
  83. if (options.yAxisLabel != undefined) {
  84. this.yAxisLabel = options.yAxisLabel; //Y轴样式
  85. } else {
  86. this.yAxisLabel = {
  87. color: "rgba(69,69,69,1.0)",
  88. fontSize: 12,
  89. fontFamily: 'TTTGB-Medium',
  90. };
  91. }
  92. /* 获取页面宽度 */
  93. this.pageSize = {
  94. width: document.body.clientWidth,
  95. height: document.body.clientHeight
  96. };
  97. /* 尺寸配置 */
  98. this.PageSize = {
  99. smartSize: 600,
  100. middleSize: 1400,
  101. }
  102. },
  103. /**
  104. * 结合坐标Map和数值数组 构建创建地图点的数据数组
  105. * @param {Map} geoCoordMap 包含name字段
  106. * @param {JSON数组} data 内部的JSON包含{name: value:}
  107. */
  108. _convertData: function(geoCoordMap, data) {
  109. var res = [];
  110. for (var i = 0; i < data.length; i++) {
  111. var geoCoord = geoCoordMap[data[i].name];
  112. if (geoCoord) {
  113. res.push({
  114. name: data[i].name,
  115. value: geoCoord.concat(data[i].name).concat(data[i].value),
  116. });
  117. }
  118. }
  119. return res;
  120. },
  121. /**
  122. * 动态轨迹线数据转换
  123. * @param {JSON数组} data [{from:[] to:}]
  124. * @param {String} type 类型 undefined 代表创建动态路径 而其他则代表创建底线
  125. */
  126. _convertLines: function(data, fromMap, toMap, type) {
  127. var res = [];
  128. for (var i = 0; i < data.length; i++) {
  129. var dataLines = data[i];
  130. for (var idx = 0; idx < dataLines.from.length; idx++) {
  131. var coords = [];
  132. coords.push(fromMap[dataLines.from[idx]]);
  133. coords.push(toMap[dataLines.to]);
  134. if (type == undefined) {
  135. res.push({
  136. coords: coords,
  137. })
  138. } else {
  139. res.push({
  140. coords: coords,
  141. lineStyle: {
  142. color: 'rgb(34,115,141)',
  143. width: 1.0,
  144. curveness: -0.4,
  145. opacity: 0.9,
  146. }
  147. })
  148. }
  149. }
  150. }
  151. return res;
  152. },
  153. /**
  154. * 创建地图配置
  155. * @param {Object} minValue 区间最小值
  156. * @param {Object} maxValue 区间最大值
  157. */
  158. _createVisualMap: function(minValue, maxValue) {
  159. return {
  160. min: minValue,
  161. max: maxValue,
  162. realtime: true, //拖拽时是否实时更新
  163. calculable: false, //是否显示拖拽的手柄
  164. show: false, //是否显示拖拽手柄
  165. inRange: {
  166. color: ['lightskyblue', 'yellow', 'orangered']
  167. }
  168. }
  169. },
  170. /**
  171. * 创建地图
  172. * @param {Object} registerName 注册名称
  173. * @param {Object} propertyName 默认属性名称
  174. */
  175. _createGeo: function(registerName, propertyName) {
  176. return {
  177. map: registerName,
  178. roam: false,
  179. aspectScale: 1.0, //默认显示的地图宽高比
  180. label: {
  181. show: false,
  182. color: "#fff"
  183. },
  184. itemStyle: {
  185. areaColor: 'rgb(20,41,87)',
  186. borderColor: 'rgb(34,115,141)',
  187. borderWidth: 3,
  188. shadowColor: 'rgb(0, 180, 255)',
  189. shadowBlur: 20,
  190. opacity: 0.8,
  191. },
  192. emphasis: {
  193. itemStyle: {
  194. areaColor: 'rgb(128,128,128)',
  195. }
  196. },
  197. scaleLimit: { //滚轮缩放的极限控制
  198. min: 1.2, //缩放最小大小
  199. max: 6 //缩放最大大小
  200. },
  201. nameProperty: propertyName,
  202. }
  203. },
  204. /**
  205. * 斜线纹理
  206. */
  207. _decalSlant: function() {
  208. return {
  209. color: "rgba(0, 0, 0, 0.2)",
  210. dashArrayX: [1, 0],
  211. dashArrayY: [2, 5],
  212. symbolSize: 1,
  213. rotation: 0.523598775598298,
  214. dirty: false,
  215. symbol: "rect",
  216. symbolKeepAspect: true,
  217. backgroundColor: null,
  218. maxTileWidth: 512,
  219. maxTileHeight: 512,
  220. };
  221. },
  222. /**
  223. * 圆形纹理
  224. */
  225. _decalCircle: function() {
  226. return {
  227. color: "rgba(0, 0, 0, 0.2)",
  228. dashArrayX: [
  229. [8, 8],
  230. [0, 8, 8, 0]
  231. ],
  232. dashArrayY: [6, 0],
  233. symbolSize: 0.8,
  234. rotation: 0,
  235. dirty: false,
  236. symbol: "circle",
  237. symbolKeepAspect: true,
  238. backgroundColor: null,
  239. maxTileWidth: 512,
  240. maxTileHeight: 512,
  241. };
  242. },
  243. /**
  244. * 十字交叉纹理
  245. */
  246. _decalCross: function() {
  247. return {
  248. color: "rgba(0, 0, 0, 0.2)",
  249. dashArrayX: [
  250. [1, 0],
  251. [1, 6]
  252. ],
  253. dashArrayY: [1, 0, 6, 0],
  254. symbolSize: 1.0,
  255. rotation: 0.7853981633974483,
  256. dirty: false,
  257. symbol: "rect",
  258. symbolKeepAspect: true,
  259. backgroundColor: null,
  260. maxTileWidth: 512,
  261. maxTileHeight: 512,
  262. }
  263. },
  264. /**
  265. * 矩形方块
  266. */
  267. _decalRect: function() {
  268. return {
  269. color: "rgba(0, 0, 0, 0.2)",
  270. dashArrayX: [
  271. [6, 6],
  272. [0, 6, 6, 0]
  273. ],
  274. dashArrayY: [6, 0],
  275. symbolSize: 1,
  276. rotation: 0,
  277. dirty: false,
  278. symbol: "rect",
  279. symbolKeepAspect: true,
  280. backgroundColor: null,
  281. maxTileWidth: 512,
  282. maxTileHeight: 512,
  283. };
  284. },
  285. /**
  286. * 三角形
  287. */
  288. _decalTriangle: function() {
  289. return {
  290. color: "rgba(0, 0, 0, 0.2)",
  291. dashArrayX: [
  292. [9, 9],
  293. [0, 9, 9, 0]
  294. ],
  295. dashArrayY: [7, 2],
  296. symbolSize: 1,
  297. rotation: 0,
  298. dirty: false,
  299. symbol: "triangle",
  300. symbolKeepAspect: true,
  301. backgroundColor: null,
  302. maxTileWidth: 512,
  303. maxTileHeight: 512,
  304. };
  305. }
  306. });
  307. /**
  308. * 基础方法
  309. */
  310. CrCharts.prototype.extend({
  311. /**
  312. * 清除全部内容
  313. */
  314. clear: function() {
  315. this.chart.clear();
  316. },
  317. /**
  318. * 释放资源
  319. */
  320. dispose: function() {
  321. if (this.chart != null && this.chart != "" && this.chart != undefined)
  322. this.chart.dispose();
  323. },
  324. });
  325. /**
  326. * 对外函数
  327. */
  328. CrCharts.prototype.extend({
  329. /**
  330. * 创建搬迁地图
  331. * @param {Object} jsonFile 地图的JSON文件
  332. * @param {Object} lines 运动线集合
  333. * @param {Object} villageMap 村庄坐标Map
  334. * @param {Object} villageValue 村庄数据集合
  335. * @param {Object} communityMap 社区坐标Map
  336. * @param {Object} communityValue 社区数据集合
  337. */
  338. CreateMap: function(jsonFile, lines, villageMap, villageValue, communityMap, communityValue) {
  339. var self = this;
  340. var series = [];
  341. series.push({
  342. type: 'map',
  343. map: 'village',
  344. roam: false,
  345. itemStyle: {
  346. areaColor: 'rgb(20,41,87)',
  347. borderColor: 'rgb(34,115,141)',
  348. borderWidth: 1,
  349. },
  350. emphasis: {
  351. itemStyle: {
  352. areaColor: 'rgb(128,128,128)',
  353. },
  354. label: {
  355. color: 'rgb(255,255,255)',
  356. }
  357. },
  358. nameProperty: 'XZQMC',
  359. aspectScale: 1.0, //默认显示的地图宽高比
  360. scaleLimit: { //滚轮缩放的极限控制
  361. min: 1.2, //缩放最小大小
  362. max: 6 //缩放最大大小
  363. },
  364. });
  365. series.push({
  366. type: 'lines',
  367. zlevel: 999,
  368. animation: false,
  369. effect: {
  370. show: true, //是否显示特效
  371. period: 7, //特效动画的重复时间
  372. trailLength: 0.1, //尾迹线的长度[0-1]值越大,越长
  373. color: 'rgb(255,255,255)',
  374. symbol: 'circle',
  375. symbolSize: [4, 4]
  376. },
  377. lineStyle: {
  378. color: '#fff',
  379. width: 2.0,
  380. curveness: -0.4
  381. },
  382. data: self._convertLines(lines, villageMap, communityMap, undefined)
  383. });
  384. series.push({
  385. name: '线条',
  386. type: 'lines',
  387. coordinateSystem: 'geo',
  388. data: self._convertLines(lines, villageMap, communityMap, '')
  389. })
  390. series.push({
  391. name: '户数',
  392. type: 'scatter',
  393. coordinateSystem: 'geo',
  394. data: self._convertData(villageMap, villageValue),
  395. symbolSize: function(val) {
  396. return (val[3] * 4) / 1036 + 6;
  397. },
  398. label: {
  399. show: false,
  400. position: 'right',
  401. color: 'rgb(162,200,82)',
  402. fontWeight: 'bold',
  403. fontFamily: 'TTTGB-Medium',
  404. shadowColor: 'rgb(255,255,255)',
  405. shadowBlur: '200',
  406. textBorderColor: 'rgb(255,255,255)',
  407. textBorderWidth: 0,
  408. formatter: '{b}',
  409. },
  410. emphasis: {
  411. itemStyle: {
  412. borderColor: '#fff',
  413. borderWidth: 1
  414. }
  415. }
  416. });
  417. series.push({
  418. name: '社区',
  419. type: 'effectScatter',
  420. coordinateSystem: 'geo',
  421. data: self._convertData(communityMap, communityValue),
  422. symbolSize: function(val) {
  423. return 10;
  424. },
  425. label: {
  426. show: true,
  427. position: 'right',
  428. color: 'rgb(255,255,255)',
  429. fontWeight: 'bold',
  430. fontFamily: 'SimHei',
  431. fontSize: 14,
  432. formatter: '{b}',
  433. },
  434. emphasis: {
  435. itemStyle: {
  436. borderColor: '#fff',
  437. borderWidth: 1
  438. }
  439. }
  440. });
  441. $.get(jsonFile, function(geoJson) {
  442. echarts.registerMap('village', geoJson);
  443. var option = {
  444. visualMap: self._createVisualMap(93, 1129),
  445. geo: self._createGeo('village', 'XZQMC'),
  446. series: series,
  447. }
  448. self.chart.setOption(option);
  449. });
  450. }
  451. });
  452. /**
  453. * 图形函数
  454. */
  455. CrCharts.prototype.extend({
  456. /**
  457. * 创建饼图
  458. * @param {JOSN} options 配置项
  459. * data{Array}:数据集
  460. * isAria{boolean}:是否开启纹理渲染,建议大数据量不渲染,否则影响动画速度
  461. * radius{int}:饼圆角的大小
  462. * startAngle{float}:其实渲染角度,即绘制第一个饼的角度
  463. * labelColor{string}:标注文字颜色,接收内容为rgb(),rgba(),16进制颜色值
  464. * lineColor{string}:引线颜色,接收内容为rgb(),rgba(),16进制颜色值
  465. * perLabelColor{string}:比值颜色,接收内容为rgb(),rgba(),16进制颜色值
  466. * @param {String} title 标题
  467. */
  468. createPie: function(options, title) {
  469. var self = this;
  470. var decals = [];
  471. for (var i = 0; i < options.data.length; i++) {
  472. var idx = i % 5;
  473. if (idx == 0) {
  474. decals.push(self._decalSlant());
  475. } else if (idx == 1) {
  476. decals.push(self._decalCircle());
  477. } else if (idx == 2) {
  478. decals.push(self._decalRect());
  479. } else if (idx == 3) {
  480. decals.push(self._decalCross());
  481. } else if (idx == 4) {
  482. decals.push(self._decalTriangle());
  483. }
  484. }
  485. /* 配置项 */
  486. var option = {
  487. tooltip: {
  488. trigger: 'item',
  489. formatter: function(params) {
  490. var strHtml = "";
  491. strHtml += "<div class='echarts-tooltip-title'>" + params.seriesName +
  492. "</div>";
  493. var unit = options.unit ? options.unit : '';
  494. strHtml += params.marker + params.name +
  495. " <font class='echarts-tooltip-row-value'>" + params.value + unit +
  496. "</font><br>";
  497. return strHtml;
  498. },
  499. position: function(point, params, dom, rect, size) {
  500. if (size.contentSize[1] + point[1] < size.viewSize[1]) {
  501. return {
  502. left: 10,
  503. top: point[1]
  504. };
  505. } else {
  506. return {
  507. left: 10,
  508. top: point[1] - size.contentSize[1],
  509. };
  510. }
  511. },
  512. },
  513. aria: {
  514. enabled: options.isAria == undefined ? true : options.isAria,
  515. decal: {
  516. show: true,
  517. decals: decals,
  518. }
  519. },
  520. stateAnimation: {
  521. duration: 300,
  522. easing: "cubicOut",
  523. },
  524. animation: "auto",
  525. animationDuration: 1000,
  526. animationDurationUpdate: 500,
  527. animationEasing: "cubicInOut",
  528. animationEasingUpdate: "cubicInOut",
  529. animationThreshold: 2000,
  530. series: [{
  531. name: title,
  532. type: 'pie',
  533. radius: options.radius == undefined ? [10, 90] : options.radius,
  534. center: ['50%', '50%'],
  535. roseType: options.roseType == false ? false : 'radius',
  536. startAngle: options.startAngle == undefined ? 10 : options.startAngle,
  537. label: {
  538. formatter: '{a|{b}}{abg|}\n{hr|}\n{per|{d}%}',
  539. rich: {
  540. a: {
  541. color: options.labelColor == undefined ? 'rgb(69,69,69)' : options
  542. .labelColor,
  543. lineHeight: 22,
  544. align: 'center',
  545. fontFamily: 'TTTGB-Medium',
  546. fontSize: 13,
  547. },
  548. hr: {
  549. borderColor: options.lineColor == undefined ? 'rgb(69,69,69)' :
  550. options.lineColor,
  551. width: '100%',
  552. borderWidth: 1,
  553. height: 0
  554. },
  555. b: {
  556. color: '#4C5058',
  557. fontSize: 10,
  558. fontWeight: 'bold',
  559. lineHeight: 33
  560. },
  561. per: {
  562. color: options.perLabelColor == undefined ? 'rgb(123,149,248)' :
  563. options.perLabelColor,
  564. align: 'center',
  565. fontFamily: 'TTTGB-Medium',
  566. lineHeight: 20,
  567. }
  568. }
  569. },
  570. labelLine: {
  571. lineStyle: {
  572. color: options.lineColor == undefined ? 'rgb(69,69,69)' : options
  573. .lineColor,
  574. },
  575. },
  576. labelLayout: {
  577. draggable: false, //是否可以拖动标注
  578. },
  579. itemStyle: {
  580. borderRadius: 0,
  581. shadowColor: '#5cfbff',
  582. shadowBlur: 5,
  583. },
  584. data: options.data,
  585. minAngle: options.minAngle ? options.minAngle : 5,
  586. }]
  587. };
  588. self.chart.setOption(option);
  589. },
  590. /**
  591. * 创建普通饼图
  592. * @param {JOSN} jsonData 数据
  593. * @param {String} title 标题
  594. */
  595. createNormalPie: function(jsonData, title) {
  596. var self = this;
  597. var option = {
  598. tooltip: {
  599. trigger: 'item'
  600. },
  601. legend: {
  602. left: 'center',
  603. show: false,
  604. },
  605. series: [{
  606. name: title,
  607. type: 'pie',
  608. radius: ['40%', '80%'],
  609. center: ['50%', '50%'],
  610. label: {
  611. position: 'inside',
  612. formatter: '{a|{b}}{abg|}\n{per|{d}%}',
  613. rich: {
  614. a: {
  615. color: 'rgb(0,51,102)',
  616. lineHeight: 22,
  617. align: 'center',
  618. fontFamily: 'TTTGB-Medium',
  619. fontSize: 13,
  620. },
  621. hr: {
  622. borderColor: '#fff',
  623. width: '100%',
  624. borderWidth: 2,
  625. height: 0
  626. },
  627. b: {
  628. color: '#4C5058',
  629. fontSize: 10,
  630. fontWeight: 'bold',
  631. lineHeight: 33
  632. },
  633. per: {
  634. color: '#ffff00',
  635. align: 'center',
  636. fontFamily: 'TTTGB-Medium',
  637. }
  638. }
  639. },
  640. itemStyle: {
  641. borderRadius: 5,
  642. borderColor: 'rgb(255,255,255)',
  643. borderWidth: 0,
  644. shadowBlur: 1,
  645. },
  646. data: jsonData,
  647. }]
  648. };
  649. self.chart.setOption(option);
  650. },
  651. /**
  652. * 创建横向柱状图
  653. * @param {JSON} jsonData 横向柱状图数据
  654. * grid:{JSON}{left: right: bottom: top}
  655. * labels:[]
  656. * datas:[{name: values:[] color:[] lblColor: unit:}]
  657. * enabledAria:true/false
  658. */
  659. createHorizontalBar: function(jsonData, units) {
  660. var self = this;
  661. /* 更改标签 如果文字超过4个长度 自动换行 */
  662. self.yAxisLabel.formatter = function(label) {
  663. let result = '';
  664. const length = label.length;
  665. const lineLabelCnt = 6;
  666. const rowCnt = Math.ceil(length / lineLabelCnt);
  667. if (length > lineLabelCnt) {
  668. for (let p = 0; p < rowCnt; p++) {
  669. let tempStr = '';
  670. const start = p * lineLabelCnt;
  671. const end = start + lineLabelCnt;
  672. if (p == rowCnt - 1) {
  673. tempStr = label.substring(start, length);
  674. } else {
  675. tempStr = label.substring(start, end) + '\n';
  676. }
  677. result += tempStr;
  678. }
  679. } else {
  680. result = label;
  681. }
  682. return result;
  683. }
  684. var decals = [];
  685. decals.push(self._decalSlant());
  686. var legendData = [];
  687. var series = [];
  688. for (var i = 0; i < jsonData.datas.length; i++) {
  689. var data = jsonData.datas[i];
  690. legendData.push(data.name);
  691. /* 标签配置 */
  692. var labelOption = {
  693. show: false,
  694. position: data.lblPosition == undefined ? 'left' : data.lblPosition,
  695. distance: data.lblPosition == undefined ? -10 : 10,
  696. align: 'left',
  697. verticalAlign: 'middle',
  698. rotate: 0,
  699. formatter: function(params) {
  700. let value = parseFloat(params.value);
  701. if (value > 0) {
  702. return value;
  703. } else {
  704. return "";
  705. }
  706. },
  707. fontSize: 10,
  708. color: data.lblColor,
  709. fontFamily: 'TTTGB-Medium',
  710. rich: {
  711. name: {}
  712. }
  713. };
  714. /* 追加单位 */
  715. if (data.unit != undefined) {
  716. labelOption.formatter += data.unit;
  717. }
  718. /* 追加服务 */
  719. var item = {
  720. name: data.name,
  721. label: labelOption,
  722. type: 'bar',
  723. data: data.values,
  724. itemStyle: {
  725. shadowColor: '#5cfbff',
  726. shadowBlur: 5,
  727. },
  728. showBackground: true,
  729. backgroundStyle: {
  730. color: 'rgba(180,180,180,0.2)'
  731. },
  732. barWidth: 10,
  733. }
  734. /* 设置柱状图颜色 */
  735. if (data.color) {
  736. if (Array.isArray(data.color)) {
  737. if (data.color.length == 1) {
  738. item.itemStyle.color = data.color[0];
  739. } else if (data.color.length >= 2) {
  740. item.itemStyle.color = {
  741. type: 'linear',
  742. x: 0,
  743. y: 0,
  744. x2: 1,
  745. y2: 0,
  746. colorStops: [{
  747. offset: 0,
  748. color: data.color[0], // 0% 处的颜色
  749. }, {
  750. offset: 1,
  751. color: data.color[1], // 100% 处的颜色
  752. }],
  753. global: false, // 缺省为 false
  754. }
  755. }
  756. } else {
  757. item.itemStyle.color = data.color
  758. }
  759. }
  760. /* 加入到服务 */
  761. series.push(item);
  762. }
  763. /* 配置项 */
  764. var option = {
  765. tooltip: {
  766. trigger: 'axis',
  767. axisPointer: {
  768. type: 'shadow'
  769. },
  770. position: function(point, params, dom, rect, size) {
  771. // console.log("鼠标位置=" + point[1] + ",内容高度=" + size.contentSize[1] + ",容器高度=" +
  772. // size.viewSize[1])
  773. if (size.contentSize[1] + point[1] < size.viewSize[1]) {
  774. return {
  775. left: 10,
  776. top: point[1]
  777. };
  778. } else {
  779. return {
  780. left: 10,
  781. top: point[1] - size.contentSize[1],
  782. };
  783. }
  784. },
  785. formatter: function(params) {
  786. var strHtml = "";
  787. strHtml += "<div class='echarts-tooltip-title'>" + params[0].axisValue +
  788. "</div>";
  789. for (var i = 0; i < params.length; i++) {
  790. var unit = units != undefined && units[i] != undefined ? units[
  791. i] : "";
  792. var value = params[i].data > 0 ? params[i].data : params[i]
  793. .data * (-1);
  794. if (value < 0.0001) {
  795. continue;
  796. }
  797. strHtml += params[i].marker + params[i].seriesName +
  798. " <font class='echarts-tooltip-row-value'>" +
  799. value + unit + "</font>";
  800. if (i != params.length - 1) {
  801. strHtml += "<br>";
  802. }
  803. }
  804. return strHtml;
  805. }
  806. },
  807. aria: {
  808. enabled: jsonData.enabledAria == undefined ? false : jsonData.enabledAria,
  809. decal: {
  810. show: true,
  811. decals: decals,
  812. }
  813. },
  814. grid: {
  815. left: 100,
  816. right: 30,
  817. bottom: 30,
  818. top: 30,
  819. },
  820. legend: {
  821. type: 'scroll',
  822. pageTextStyle: {
  823. color: 'rgb(255,255,255)',
  824. },
  825. pageIconColor: 'rgb(255,255,0)',
  826. pageIconInactiveColor: 'rgb(203,203,203)',
  827. data: legendData,
  828. textStyle: {
  829. color: this.legendTextColor == undefined ? 'rgb(69,69,69)' : this.legendTextColor,
  830. fontSize: 13,
  831. fontFamily: 'TTTGB-Medium',
  832. },
  833. itemGap: 5,
  834. },
  835. xAxis: {
  836. type: 'value',
  837. boundaryGap: [0, 0.01],
  838. axisLabel: self.xAxisLabel,
  839. axisLine: self.xAxisLine,
  840. splitLine: self.splitLine,
  841. },
  842. yAxis: {
  843. type: 'category',
  844. data: jsonData.labels,
  845. axisLabel: self.yAxisLabel,
  846. axisLine: self.yAxisLine,
  847. splitLine: self.splitLine,
  848. },
  849. series: series,
  850. };
  851. /* 此处进行修改 取消设置dataZoomY 改为根据容器高度自动设置 */
  852. let visibleBarCount = Math.ceil((self.chart.getHeight() * 20) / 480);
  853. let visibleSize = Math.ceil(visibleBarCount / option.series.length);
  854. let endvalue = visibleSize > 2 ? visibleSize - 1 : '1';
  855. option.dataZoom = [{
  856. width: 20,
  857. startValue: 0,
  858. endValue: endvalue,
  859. yAxisIndex: [0],
  860. zoomOnMouseWheel: false, // 关闭滚轮缩放
  861. moveOnMouseWheel: true, // 开启滚轮平移
  862. moveOnMouseMove: true, // 鼠标移动能触发数据窗口平移
  863. zoomLock: true,
  864. }];
  865. if (jsonData.grid != undefined) {
  866. if (jsonData.grid.left && jsonData.grid.left < 100) jsonData.grid.left = 100;
  867. option.grid = jsonData.grid;
  868. }
  869. self.chart.setOption(option);
  870. },
  871. /**
  872. * 创建竖向柱状图
  873. * @param {JSON} jsonData 数据集
  874. * @param {int} left 距离左侧的距离
  875. * @param {boolean} enabledAria 是否使用花纹
  876. * @param {array} units 单位名称
  877. */
  878. createVerticalBar: function(jsonData, left, enabledAria, units) {
  879. var self = this;
  880. var grid = {
  881. right: 3,
  882. bottom: jsonData.gridBottom != undefined ? jsonData.gridBottom : 20,
  883. top: jsonData.gridTop != undefined ? jsonData.gridTop : 35,
  884. left: left == undefined ? 80 : left,
  885. };
  886. if (jsonData.xrotate != undefined) {
  887. self.xAxisLabel.rotate = jsonData.xrotate;
  888. }
  889. var legendData = [];
  890. var xData = jsonData.labels;
  891. var servies = [];
  892. var decals = [];
  893. decals.push(self._decalSlant());
  894. /*数据配置*/
  895. for (var i = 0; i < jsonData.datas.length; i++) {
  896. var sData = jsonData.datas[i];
  897. legendData.push({
  898. name: sData.name,
  899. });
  900. /* 标签配置 */
  901. var labelOption = {
  902. show: false,
  903. position: 'top',
  904. distance: 5,
  905. align: 'left',
  906. verticalAlign: 'middle',
  907. rotate: 90,
  908. formatter: function(param) {
  909. return param.value == 0.1 ? "" : param.value;
  910. },
  911. fontSize: 10,
  912. textColor: self.labelColor == undefined ? 'rgb(69,69,69)' : self.labelColor,
  913. fontFamily: 'TTTGB-Medium',
  914. rich: {
  915. name: {}
  916. }
  917. };
  918. if (sData.label != undefined) {
  919. if (sData.label.position != undefined) labelOption.position = sData.label
  920. .position;
  921. if (sData.label.rotate != undefined) labelOption.rotate = sData.label.rotate;
  922. if (sData.label.formatter != undefined) labelOption.formatter = sData.label
  923. .formatter;
  924. }
  925. var service = {
  926. name: sData.name,
  927. type: 'bar',
  928. label: labelOption,
  929. emphasis: {
  930. focus: 'series'
  931. },
  932. data: sData.data,
  933. itemStyle: {
  934. color: sData.color,
  935. shadowColor: '#5cfbff',
  936. shadowBlur: 5,
  937. },
  938. showBackground: true,
  939. backgroundStyle: {
  940. color: 'rgba(180,180,180,0.2)'
  941. },
  942. barWidth: 10,
  943. };
  944. if (sData.stack != undefined) service.stack = sData.stack;
  945. servies.push(service);
  946. }
  947. /* 图表配置 */
  948. var option = {
  949. tooltip: {
  950. show: true,
  951. trigger: 'axis',
  952. axisPointer: {
  953. type: 'shadow'
  954. },
  955. position: function(point, params, dom, rect, size) {
  956. if (size.contentSize[1] + point[1] < size.viewSize[1]) {
  957. return {
  958. left: 10,
  959. top: point[1]
  960. };
  961. } else {
  962. return {
  963. left: 10,
  964. top: 0, //point[1] - size.contentSize[1],
  965. };
  966. }
  967. },
  968. formatter: function(params) {
  969. var strHtml = "";
  970. strHtml += "<div class='echarts-tooltip-title'>" + params[0].axisValue +
  971. "</div>";
  972. for (var i = 0; i < params.length; i++) {
  973. var unit = units != undefined && units[i] != undefined ? units[
  974. i] : "";
  975. var value = params[i].data > 0 ? params[i].data : params[i]
  976. .data * (-1);
  977. if (value < 0.0001) {
  978. continue;
  979. }
  980. strHtml += params[i].marker + params[i].seriesName +
  981. " <font class='echarts-tooltip-row-value'>" +
  982. value + unit + "</font>";
  983. if (i != params.length - 1) {
  984. strHtml += "<br>";
  985. }
  986. }
  987. return strHtml;
  988. }
  989. },
  990. aria: {
  991. enabled: enabledAria == undefined ? false : enabledAria,
  992. decal: {
  993. show: true,
  994. decals: decals,
  995. }
  996. },
  997. legend: {
  998. type: 'scroll',
  999. pageTextStyle: {
  1000. color: 'rgb(255,255,255)',
  1001. },
  1002. pageIconColor: 'rgb(255,255,0)',
  1003. pageIconInactiveColor: 'rgb(203,203,203)',
  1004. data: legendData,
  1005. textStyle: {
  1006. color: this.legendTextColor == undefined ? 'rgb(69,69,69)' : this.legendTextColor,
  1007. fontSize: 13,
  1008. fontFamily: 'TTTGB-Medium',
  1009. },
  1010. itemGap: 5,
  1011. },
  1012. grid: grid,
  1013. xAxis: [{
  1014. type: 'category',
  1015. axisTick: {
  1016. show: false
  1017. },
  1018. data: xData,
  1019. axisLabel: self.xAxisLabel,
  1020. axisLine: self.xAxisLine,
  1021. }],
  1022. yAxis: [{
  1023. type: 'log',
  1024. min: 1,
  1025. axisLabel: self.yAxisLabel,
  1026. axisLine: self.yAxisLine,
  1027. splitLine: self.splitLine,
  1028. }],
  1029. series: servies,
  1030. };
  1031. /* 通过自动配置 使以上方法失效 */
  1032. /* 获取柱子总数 */
  1033. let barCount = servies[0].data.length * servies.length;
  1034. let endValue = '1';
  1035. let isZoom = false;
  1036. if (self.pageSize.width <= self.PageSize.smartSize && barCount >= 15) {
  1037. endValue = Math.floor(15 / servies.length);
  1038. isZoom = true;
  1039. } else if (self.pageSize.width > self.PageSize.smartSize && self.pageSize.width <= self.PageSize
  1040. .middleSize && barCount >= 30) {
  1041. endValue = Math.floor(30 / servies.length);
  1042. isZoom = true;
  1043. }
  1044. /* 设置是否显示水平滚动 */
  1045. //jsonData.dataZoomX != undefined && jsonData.dataZoomX == true
  1046. if (isZoom) {
  1047. option.dataZoom = [{
  1048. type: 'slider',
  1049. xAxisIndex: [0],
  1050. filterMode: 'filter',
  1051. startValue: 0,
  1052. endValue: endValue,
  1053. height: 20,
  1054. zoomLock: true,
  1055. }]
  1056. }
  1057. self.chart.setOption(option);
  1058. },
  1059. /**
  1060. * 创建折线图
  1061. * @param {JSON} jsonData 数据集
  1062. * @param {Array} units 单位数组
  1063. */
  1064. createLine: function(jsonData, units) {
  1065. var self = this;
  1066. var series = [];
  1067. var legendData = [];
  1068. var xData = jsonData.labels;
  1069. var grid = {
  1070. right: jsonData.gridRight != undefined ? jsonData.gridRight : 20,
  1071. bottom: jsonData.gridBottom != undefined ? jsonData.gridBottom : 20,
  1072. top: jsonData.gridTop != undefined ? jsonData.gridTop : 40,
  1073. left: jsonData.gridLeft == undefined ? 30 : jsonData.gridLeft,
  1074. };
  1075. for (var i = 0; i < jsonData.datas.length; i++) {
  1076. var lData = jsonData.datas[i];
  1077. legendData.push(lData.name);
  1078. series.push({
  1079. name: lData.name,
  1080. type: 'line',
  1081. yAxisIndex: 0,
  1082. symbolSize: 4,
  1083. itemStyle: {
  1084. color: lData.color,
  1085. borderWidth: 2,
  1086. shadowColor: '#5cfbff',
  1087. shadowBlur: 5,
  1088. },
  1089. data: lData.values,
  1090. smooth: true,
  1091. areaStyle: {
  1092. opacity: jsonData.areaStyle == undefined ? 0 : jsonData.areaStyle
  1093. .opacity,
  1094. },
  1095. barMinWidth: 30,
  1096. });
  1097. if (lData.label != undefined) {
  1098. var unit = lData.label.unit == undefined ? '' : lData.label.unit;
  1099. series[i].label = {
  1100. show: lData.label.show == undefined ? false : lData.label.show,
  1101. position: 'top',
  1102. distance: 10,
  1103. align: 'center',
  1104. verticalAlign: 'top',
  1105. rotate: lData.label.rotate == undefined ? 0 : lData.label.rotate,
  1106. formatter: '{c}' + unit,
  1107. fontSize: 12,
  1108. color: lData.label.color == undefined ? 'rgb(255,255,255)' : lData.label
  1109. .color,
  1110. fontFamily: 'TTTGB-Medium',
  1111. rich: {
  1112. name: {}
  1113. }
  1114. }
  1115. }
  1116. }
  1117. /* 设置横轴标注旋转角度 */
  1118. if (jsonData.xrotate != undefined) self.xAxisLabel.rotate = jsonData.xrotate;
  1119. /* 配置项 */
  1120. var option = {
  1121. tooltip: {
  1122. trigger: 'axis',
  1123. axisPointer: {
  1124. type: 'shadow'
  1125. },
  1126. position: function(point, params, dom, rect, size) {
  1127. if (size.contentSize[1] + point[1] < size.viewSize[1]) {
  1128. return {
  1129. left: 10,
  1130. top: point[1]
  1131. };
  1132. } else {
  1133. return {
  1134. left: 10,
  1135. top: point[1] - size.contentSize[1],
  1136. };
  1137. }
  1138. },
  1139. formatter: function(params) {
  1140. var strHtml = "";
  1141. strHtml += "<div class='echarts-tooltip-title'>" + params[0].axisValue +
  1142. "</div>";
  1143. for (var i = 0; i < params.length; i++) {
  1144. var unit = units != undefined && units[i] != undefined ? units[
  1145. i] : "";
  1146. var value = params[i].data > 0 ? params[i].data : params[i]
  1147. .data * (-1);
  1148. if (value < 0.0001) {
  1149. continue;
  1150. }
  1151. strHtml += params[i].marker + params[i].seriesName +
  1152. " <font class='echarts-tooltip-row-value'>" +
  1153. value + unit + "</font>";
  1154. if (i != params.length - 1) {
  1155. strHtml += "<br>";
  1156. }
  1157. }
  1158. return strHtml;
  1159. }
  1160. },
  1161. legend: {
  1162. type: 'scroll',
  1163. x: 'center',
  1164. y: '0px',
  1165. icon: 'circle',
  1166. data: legendData,
  1167. pageTextStyle: {
  1168. color: 'rgb(255,255,255)',
  1169. },
  1170. pageIconColor: 'rgb(255,255,0)',
  1171. pageIconInactiveColor: 'rgb(203,203,203)',
  1172. data: legendData,
  1173. textStyle: {
  1174. color: this.legendTextColor == undefined ? 'rgb(69,69,69)' : this.legendTextColor,
  1175. fontSize: 13,
  1176. fontFamily: 'TTTGB-Medium',
  1177. },
  1178. },
  1179. grid: grid,
  1180. xAxis: [{
  1181. type: 'category',
  1182. data: xData,
  1183. axisLine: self.xAxisLine,
  1184. axisLabel: self.xAxisLabel,
  1185. axisTick: {
  1186. show: false,
  1187. },
  1188. }],
  1189. yAxis: [{
  1190. type: 'value',
  1191. axisLine: self.yAxisLine,
  1192. axisLabel: self.yAxisLabel,
  1193. splitLine: self.splitLine,
  1194. }],
  1195. series: series,
  1196. };
  1197. self.chart.setOption(option);
  1198. },
  1199. /**
  1200. * 创建桑基图
  1201. * @param {JSON} jsonData 数据
  1202. * @param {string} unit 单位
  1203. */
  1204. createSankey: function(jsonData, unit) {
  1205. var self = this;
  1206. var data = [];
  1207. var links = [];
  1208. for (var i in jsonData.data) {
  1209. var item = jsonData.data[i];
  1210. /* 加入数据 */
  1211. data.push({
  1212. name: item.name,
  1213. })
  1214. for (var idx in item.childs) {
  1215. var child = item.childs[idx];
  1216. data.push({
  1217. name: child.name,
  1218. })
  1219. /* 加入链表 */
  1220. links.push({
  1221. source: item.name,
  1222. target: child.name,
  1223. value: child.value,
  1224. fvalue: item.value,
  1225. unit: unit,
  1226. });
  1227. if (child.childs && child.childs.length > 0) {
  1228. for (let sChild of child.childs) {
  1229. data.push({
  1230. name: sChild.name,
  1231. })
  1232. links.push({
  1233. source: child.name,
  1234. target: sChild.name,
  1235. value: sChild.value,
  1236. fvalue: child.value,
  1237. unit: unit,
  1238. });
  1239. }
  1240. }
  1241. }
  1242. }
  1243. option = {
  1244. tooltip: {
  1245. trigger: 'item',
  1246. position: function(point, params, dom, rect, size) {
  1247. if (size.contentSize[1] + point[1] < size.viewSize[1]) {
  1248. return {
  1249. left: 10,
  1250. top: point[1]
  1251. };
  1252. } else {
  1253. return {
  1254. left: 10,
  1255. top: point[1] - size.contentSize[1],
  1256. };
  1257. }
  1258. },
  1259. formatter: function(params) {
  1260. if (params.dataType == 'node') {
  1261. var dataName = params.data.name;
  1262. var item = undefined;
  1263. for (let link of links) {
  1264. /* 说明含有子节点 */
  1265. if (link.source == dataName && !item) {
  1266. item = {
  1267. name: link.source,
  1268. value: link.fvalue,
  1269. unit: link.unit,
  1270. childs: [link],
  1271. }
  1272. } else if (link.source == dataName && item) {
  1273. item.childs.push(link);
  1274. }
  1275. }
  1276. /* 说明该节点没有节点 */
  1277. if (!item) {
  1278. for (let link of links) {
  1279. if (link.target === dataName) {
  1280. item = {
  1281. name: link.target,
  1282. value: link.fvalue,
  1283. unit: link.unit,
  1284. }
  1285. break;
  1286. }
  1287. }
  1288. }
  1289. /* 通过判断是否存在节点 进行展示 */
  1290. if (item) {
  1291. var strHtml = "";
  1292. strHtml += "<div class='echarts-tooltip-title'>" + item.name +
  1293. "[" + item.value.toFixed(2) + item.unit + "]</div>";
  1294. if (item.childs && item.childs.length > 0) {
  1295. for (let i in item.childs) {
  1296. var child = item.childs[i];
  1297. strHtml += child.target +
  1298. " <font class='echarts-tooltip-row-value'>" +
  1299. parseFloat(child.value).toFixed(2) + child.unit + "";
  1300. strHtml += "<占比" + ((child.value / child.fvalue) * 100).toFixed(
  1301. 2) +
  1302. "%>";
  1303. strHtml += "</font>";
  1304. if (i < item.childs.length - 1) {
  1305. strHtml += "<br>";
  1306. }
  1307. }
  1308. }
  1309. return strHtml;
  1310. } else {
  1311. return;
  1312. }
  1313. }
  1314. }
  1315. },
  1316. series: {
  1317. type: 'sankey',
  1318. layout: 'none',
  1319. right: jsonData.labelWidth ? jsonData.labelWidth + 10 : 70,
  1320. nodeGap: jsonData.nodeGap ? jsonData.nodeGap : 8,
  1321. draggable: false, //禁止拖拽
  1322. emphasis: {
  1323. focus: 'adjacency'
  1324. },
  1325. data: data,
  1326. links: links,
  1327. label: {
  1328. rotate: 0,
  1329. formatter: function(params) {
  1330. // console.log(JSON.stringify(params));
  1331. return params.name;
  1332. },
  1333. color: "rgba(255,255,255,1.0)",
  1334. fontSize: 12,
  1335. fontFamily: 'TTTGB-Medium',
  1336. width: jsonData.labelWidth ? jsonData.labelWidth : 60,
  1337. overflow: 'break',
  1338. },
  1339. itemStyle: {
  1340. shadowColor: '#5cfbff',
  1341. shadowBlur: 5,
  1342. },
  1343. lineStyle: {
  1344. color: 'gradient',
  1345. opacity: 0.2,
  1346. }
  1347. }
  1348. };
  1349. self.chart.setOption(option);
  1350. },
  1351. /**
  1352. * 创建混合图
  1353. * @param {Object} jsonData 混合Data '{value} ml'
  1354. */
  1355. createBlend: function(jsonData) {
  1356. var self = this;
  1357. var legendData = [];
  1358. var yAxis = [];
  1359. var series = [];
  1360. var xAxis = jsonData.labels;
  1361. for (var i = 0; i < jsonData.axixs.length; i++) {
  1362. var axis = jsonData.axixs[i];
  1363. yAxis.push({
  1364. type: 'value',
  1365. name: axis.name,
  1366. min: axis.minValue,
  1367. max: axis.maxValue,
  1368. interval: axis.spaceValue,
  1369. position: axis.position,
  1370. offset: axis.offset,
  1371. axisLabel: {
  1372. formatter: axis.formatter,
  1373. color: axis.color,
  1374. fontSize: 12,
  1375. fontFamily: 'TTTGB-Medium',
  1376. },
  1377. axisLine: {
  1378. show: true,
  1379. lineStyle: {
  1380. color: axis.color,
  1381. },
  1382. },
  1383. splitLine: self.splitLine,
  1384. });
  1385. }
  1386. for (var i = 0; i < jsonData.datas.length; i++) {
  1387. var data = jsonData.datas[i];
  1388. legendData.push(data.name);
  1389. series.push({
  1390. name: data.name,
  1391. type: data.type,
  1392. data: data.data,
  1393. yAxisIndex: data.yAxisIndex,
  1394. itemStyle: {
  1395. color: data.color,
  1396. shadowColor: '#5cfbff',
  1397. shadowBlur: 5,
  1398. },
  1399. smooth: true,
  1400. })
  1401. }
  1402. var option = {
  1403. grid: {
  1404. right: jsonData.right,
  1405. left: jsonData.left,
  1406. bottom: jsonData.bottom,
  1407. },
  1408. tooltip: {
  1409. trigger: 'axis',
  1410. axisPointer: {
  1411. type: 'cross',
  1412. crossStyle: {
  1413. color: '#999'
  1414. }
  1415. }
  1416. },
  1417. legend: {
  1418. data: legendData,
  1419. x: 'center',
  1420. y: '0px',
  1421. textStyle: {
  1422. color: '#FFFFFF',
  1423. fontSize: 13,
  1424. },
  1425. },
  1426. xAxis: [{
  1427. type: 'category',
  1428. data: xAxis,
  1429. axisPointer: {
  1430. type: 'shadow',
  1431. },
  1432. axisLabel: self.xAxisLabel,
  1433. axisLine: self.xAxisLine,
  1434. }],
  1435. yAxis: yAxis,
  1436. series: series,
  1437. }
  1438. self.chart.setOption(option);
  1439. },
  1440. /**
  1441. * 创建地图
  1442. * @param {Object} jsonFile
  1443. * @param {Object} valueJson
  1444. * @param {Object} valueLegend
  1445. * @param {Object} showName
  1446. * @param {Object} unit
  1447. */
  1448. createMap: function(jsonFile, valueJson, valueLegend, showName, unit) {
  1449. var self = this;
  1450. var minValue = 5000000000,
  1451. maxValue = 0;
  1452. /* 循环获取最大最小值 */
  1453. for (var i = 0; i < valueJson.length; i++) {
  1454. if (valueJson[i].value < minValue) {
  1455. minValue = valueJson[i].value;
  1456. }
  1457. if (valueJson[i].value > maxValue) {
  1458. maxValue = valueJson[i].value;
  1459. }
  1460. }
  1461. var map = {
  1462. min: minValue,
  1463. max: maxValue,
  1464. realtime: true, //拖拽时是否实时更新
  1465. calculable: false, //是否显示拖拽的手柄
  1466. show: true, //是否显示拖拽手柄
  1467. textStyle: {
  1468. color: 'rgb(255,255,0)',
  1469. fontFamily: 'TTTGB-Medium',
  1470. },
  1471. inRange: {
  1472. color: ['rgb(61,201,61)', 'rgb(5,78,5)']
  1473. },
  1474. pieces: valueLegend,
  1475. left: 5,
  1476. }
  1477. $.get(jsonFile, function(geoJson) {
  1478. echarts.registerMap('village', geoJson);
  1479. var option = {
  1480. visualMap: map,
  1481. tooltip: {
  1482. trigger: 'item',
  1483. formatter: '{b}</br>{c}' + unit,
  1484. },
  1485. series: [{
  1486. name: '人口分布',
  1487. type: 'map',
  1488. mapType: 'village',
  1489. nameProperty: showName,
  1490. roam: true,
  1491. aspectScale: 1.0, //默认显示的地图宽高比
  1492. scaleLimit: { //滚轮缩放的极限控制
  1493. min: 1.2, //缩放最小大小
  1494. max: 6 //缩放最大大小
  1495. },
  1496. label: {
  1497. show: false,
  1498. color: 'rgb(255,255,255)',
  1499. fontFamily: 'TTTGB-Medium',
  1500. },
  1501. borderColor: 'rgb(34,115,141)',
  1502. data: valueJson,
  1503. left: 160
  1504. }],
  1505. }
  1506. self.chart.setOption(option);
  1507. });
  1508. },
  1509. /**
  1510. * 创建指标
  1511. * @param {JSON} jsonData 数据JSON
  1512. * {title: min: max: value: unit:}
  1513. */
  1514. createWatch: function(jsonData) {
  1515. var self = this;
  1516. var option = {
  1517. series: [{
  1518. type: 'gauge',
  1519. min: jsonData.min,
  1520. max: jsonData.max,
  1521. axisLine: {
  1522. lineStyle: {
  1523. width: 15,
  1524. color: [
  1525. [0.3, 'rgb(70,197,69)'],
  1526. [0.7, '#37a2da'],
  1527. [1, '#fd666d']
  1528. ]
  1529. }
  1530. },
  1531. pointer: {
  1532. itemStyle: {
  1533. color: 'auto'
  1534. }
  1535. },
  1536. axisTick: {
  1537. distance: -15,
  1538. length: 8,
  1539. lineStyle: {
  1540. color: '#fff',
  1541. width: 2
  1542. }
  1543. },
  1544. splitLine: {
  1545. distance: -15,
  1546. length: 15,
  1547. lineStyle: {
  1548. color: '#fff',
  1549. width: 2
  1550. }
  1551. },
  1552. axisLabel: {
  1553. color: 'auto',
  1554. distance: -19,
  1555. fontSize: 12,
  1556. fontWeight: 'bold',
  1557. },
  1558. detail: {
  1559. valueAnimation: true,
  1560. formatter: '{value}' + jsonData.unit,
  1561. color: 'auto',
  1562. fontSize: 20,
  1563. fontWeight: 'bold',
  1564. // color: 'rgb(255,255,255)',
  1565. },
  1566. data: [{
  1567. value: jsonData.value,
  1568. name: jsonData.title,
  1569. title: {
  1570. show: true,
  1571. offsetCenter: [0, '120%'],
  1572. color: 'rgb(255,255,0)',
  1573. fontSize: 14,
  1574. fontWeight: 'bold',
  1575. }
  1576. }]
  1577. }]
  1578. };
  1579. self.chart.setOption(option);
  1580. },
  1581. /**
  1582. * 创建雷达图
  1583. * @param {JSON} jsonData 雷达图数据
  1584. */
  1585. createRadar: function(jsonData) {
  1586. var self = this;
  1587. /* 获取最大值 */
  1588. var maxValue = 0;
  1589. for (var i = 0; i < jsonData.datas.length; i++) {
  1590. var dataItem = jsonData.datas[i];
  1591. for (var j = 0; j < dataItem.values.length; j++) {
  1592. if (dataItem.values[j] > maxValue) {
  1593. maxValue = dataItem.values[j];
  1594. }
  1595. }
  1596. }
  1597. /* 构建指示器 */
  1598. var indicator = [];
  1599. for (var i = 0; i < jsonData.labels.length; i++) {
  1600. indicator.push({
  1601. name: jsonData.labels[i],
  1602. max: maxValue,
  1603. })
  1604. }
  1605. /* 构建数据集合 */
  1606. var datas = [];
  1607. for (var i = 0; i < jsonData.datas.length; i++) {
  1608. var dataItem = jsonData.datas[i];
  1609. datas.push({
  1610. itemStyle: {
  1611. color: dataItem.color,
  1612. },
  1613. value: dataItem.values,
  1614. name: dataItem.name,
  1615. })
  1616. }
  1617. /* 构建图例 */
  1618. var ldatas = [];
  1619. for (var i = 0; i < jsonData.datas.length; i++) {
  1620. ldatas.push(jsonData.datas[i].name);
  1621. }
  1622. /* 配置项 */
  1623. var option = {
  1624. legend: {
  1625. data: ldatas,
  1626. show: jsonData.lengend,
  1627. textStyle: {
  1628. color: 'rgb(255,255,0)',
  1629. fontFamily: 'TTTGB-Medium',
  1630. },
  1631. icon: 'roundRect',
  1632. },
  1633. radar: {
  1634. // shape: 'circle',
  1635. name: {
  1636. color: jsonData.lblColor,
  1637. fontSize: jsonData.lblSize == undefined ? 13 : jsonData.lblSize,
  1638. fontFamily: 'TTTGB-Medium',
  1639. formatter: function(value, indicator) {
  1640. var val = value.split(';');
  1641. if (val.length == 1) {
  1642. return value;
  1643. } else {
  1644. return '{a|' + val[0] + '}' + '\n' + '{b|' + val[1] + '}';
  1645. }
  1646. },
  1647. rich: {
  1648. a: {
  1649. color: jsonData.lblColor,
  1650. fontSize: 13,
  1651. fontFamily: 'TTTGB-Medium',
  1652. lineHeight: 20,
  1653. },
  1654. b: {
  1655. color: 'rgb(255,255,255)',
  1656. fontSize: 11,
  1657. fontFamily: 'TTTGB-Medium',
  1658. backgroundColor: '#666',
  1659. fontFamily: 'SimHei',
  1660. padding: 4,
  1661. borderRadius: 2,
  1662. align: 'center',
  1663. }
  1664. }
  1665. },
  1666. indicator: indicator,
  1667. splitLine: {
  1668. lineStyle: {
  1669. color: 'rgb(255,255,255,0.35)',
  1670. type: 'dashed'
  1671. },
  1672. },
  1673. axisLine: {
  1674. lineStyle: {
  1675. color: 'rgb(255,255,255,0.35)',
  1676. type: 'dashed'
  1677. }
  1678. }
  1679. },
  1680. series: [{
  1681. type: 'radar',
  1682. data: datas,
  1683. }]
  1684. };
  1685. if (jsonData.lengend) {
  1686. option.radar.center = ['50%', '55%'];
  1687. option.radar.radius = '55%';
  1688. }
  1689. self.chart.setOption(option);
  1690. },
  1691. /**
  1692. * 重置大小
  1693. */
  1694. resize: function() {
  1695. this.chart.resize();
  1696. },
  1697. });