map-chart.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. (function () {
  2. Vue.component('map-chart', {
  3. template: '<div class="chart_box" >\
  4. <div :id="chartid" class="chart_box"></div>\
  5. </div>',
  6. props: ['config','tooltip','danwei'],
  7. data: function () {
  8. return {
  9. chartid: _.uniqueId("chart_"),
  10. chart: null,
  11. lastIndex: null
  12. }
  13. },
  14. mounted: function () {
  15. var that = this;
  16. this.$nextTick(function () {
  17. that.chart = echarts.init(document.getElementById(that.chartid));
  18. // that.chart.setOption(that.getOption()); //试着一下
  19. this.chart.on('click', function (params) { //点击事件
  20. that.$emit('clickMap', params) //传给外面点击事件
  21. if (params.componentType === 'series') {
  22. if (that.lastIndex !== null) {
  23. that.chart.dispatchAction({ //把上一个高亮的取消掉
  24. type: 'downplay',
  25. seriesIndex: 0,
  26. dataIndex: that.lastIndex
  27. })
  28. }
  29. that.$emit('map-click', params)
  30. that.lastIndex = params.dataIndex //高亮现在这个
  31. that.chart.dispatchAction({
  32. type: 'highlight',
  33. seriesIndex: 0,
  34. dataIndex: params.dataIndex
  35. })
  36. }
  37. })
  38. })
  39. },
  40. methods: {
  41. getOption: function () {
  42. if(this.tooltip == "元"){
  43. defaults.tooltip.formatter = '{b}<br/>{c} (元)'
  44. }
  45. var options=JSON.parse(JSON.stringify(defaults));
  46. if(this.config){
  47. var maxValue =0;
  48. this.config.data.map(function(v){
  49. if(v.value){
  50. maxValue = Math.max(maxValue,parseInt(v.value || 0));
  51. maxValue = Math.ceil(maxValue/5)*5
  52. }
  53. })
  54. var piceArr = this.getPiceArr(maxValue)
  55. options.visualMap = {
  56. min: 0,
  57. max: maxValue,
  58. splitNumber: 5,//分成5等分
  59. color: ['#0046ac', '#005adc', '#277fff','#62a2ff','#c5ddff'],
  60. textStyle: {
  61. color: '#b5e1fc'
  62. },
  63. //pieces: piceArr,
  64. left: '50',
  65. bottom:'20'
  66. }
  67. if(this.danwei == true){
  68. options.visualMap.pieces = piceArr;
  69. }
  70. options.series[0].zoom = this.config.zoom || 1.2;
  71. options.series[0].data = this.config.data;
  72. }
  73. return options
  74. },
  75. getPiceArr(max, num) {
  76. var lastArr = [];
  77. if (max == 0) {
  78. lastArr = [{
  79. max: 0,
  80. min: 0,
  81. label: '0'
  82. }]
  83. } else {
  84. var divisor = max > 10000 ? 10000 : (max > 1000 ? 1000 : 100); //最小区间跨度值
  85. var unitSpan = divisor >= 10000 ? 10000 : 1; //计算单位用
  86. var _num = num || (max / divisor > 5) ? 5 : (max / divisor); //区间取值次数
  87. var _span = (max / _num); //刚好区间跨度,需调整
  88. _span = ((_span + '').match(/0/ig) && (_span + '').match(/0/ig).length) == (((_span + '').split('.'))[0].length - 1) ? _span : (((_span + '').substr(0, 1) | 0) + 1) + Array(((_span + '').split('.'))[0].length).join(0); //区间跨度,可能需要进位 0填充等
  89. var company = _span >= 10000 ? '万' : ''; //区间单位
  90. _num = num || Math.ceil(max / _span); //重新计算区间次数
  91. for (var j = 0; j < _num; j++) {
  92. lastArr.push({
  93. min: _span * (_num - j - 1),
  94. max: _span * (_num - j),
  95. label: (_span * (_num - j - 1) / unitSpan) + company + "-" + (_span * (_num - j) / unitSpan) + company
  96. })
  97. }
  98. }
  99. return lastArr;
  100. },
  101. closeLineHeight(index) {
  102. var that = this;
  103. if (index) that.lastIndex = index
  104. if (that.lastIndex === null) return
  105. that.chart.dispatchAction({ //把上一个高亮的取消掉
  106. type: 'downplay',
  107. seriesIndex: 0,
  108. dataIndex: that.lastIndex
  109. })
  110. },
  111. selectLineHeight(index){ //高亮某个区县 传入城市名 获得对应下标 然后高亮
  112. var that =this;
  113. this.closeLineHeight();
  114. var isExit = false;
  115. this.config.data.map(function(v,i){
  116. // console.log(v)
  117. // console.log(index)
  118. if(v.name == index) {
  119. console.log(i)
  120. that.lastIndex =i;
  121. isExit=true
  122. }
  123. })
  124. // this.lastIndex = index;
  125. if(isExit)
  126. this.chart.dispatchAction({
  127. type: 'highlight',
  128. seriesIndex: 0,
  129. dataIndex: that.lastIndex
  130. })
  131. }
  132. },
  133. watch: {
  134. config: function (data) {
  135. if (data) {
  136. this.chart.setOption(this.getOption());
  137. if(data.index !== null)this.selectLineHeight(data.index)
  138. }
  139. }
  140. }
  141. })
  142. var defaults = {
  143. tooltip: {
  144. trigger: 'item',
  145. formatter: '{b}<br/>{c} (人次)'
  146. },
  147. series: [{
  148. type: 'map',
  149. mapType: '上饶', // 自定义扩展图表类型
  150. itemStyle: {
  151. normal: {
  152. label: {
  153. show: true,
  154. color: '#ffffff'
  155. },
  156. borderColor: '#fff',
  157. shadowColor: '#0355ab',
  158. shadowBlur: 10,
  159. shadowOffsetX: 10,
  160. shadowOffsetY: 10
  161. },
  162. emphasis: {
  163. label: {
  164. show: true,
  165. color: '#002f9c'
  166. },
  167. areaColor: '#00e9fa',
  168. }
  169. },
  170. data: [],
  171. selectedMode: 'single'
  172. }],
  173. }
  174. })()