map-chart.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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'],
  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. var options=JSON.parse(JSON.stringify(defaults));
  43. if(this.config){
  44. var maxValue =0;
  45. this.config.data.map(function(v){
  46. if(v.value){
  47. maxValue = Math.max(maxValue,parseInt(v.value || 0));
  48. maxValue = Math.ceil(maxValue/5)*5
  49. }
  50. })
  51. var piceArr = this.getPiceArr(maxValue)
  52. options.visualMap = {
  53. min: 0,
  54. max: maxValue,
  55. splitNumber: 5,//分成5等分
  56. color: ['#0046ac', '#005adc', '#277fff','#62a2ff','#c5ddff'],
  57. textStyle: {
  58. color: '#b5e1fc'
  59. },
  60. // pieces: piceArr,
  61. left: '50',
  62. bottom:'20'
  63. }
  64. options.series[0].zoom = this.config.zoom || 1.2;
  65. options.series[0].data = this.config.data;
  66. }
  67. return options
  68. },
  69. getPiceArr(max, num) {
  70. var lastArr = [];
  71. if (max == 0) {
  72. lastArr = [{
  73. max: 0,
  74. min: 0,
  75. label: '0'
  76. }]
  77. } else {
  78. var divisor = max > 10000 ? 10000 : (max > 1000 ? 1000 : 100); //最小区间跨度值
  79. var unitSpan = divisor >= 10000 ? 10000 : 1; //计算单位用
  80. var _num = num || (max / divisor > 5) ? 5 : (max / divisor); //区间取值次数
  81. var _span = (max / _num); //刚好区间跨度,需调整
  82. _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填充等
  83. var company = _span >= 10000 ? '万' : ''; //区间单位
  84. _num = num || Math.ceil(max / _span); //重新计算区间次数
  85. for (var j = 0; j < _num; j++) {
  86. lastArr.push({
  87. min: _span * (_num - j - 1),
  88. max: _span * (_num - j),
  89. label: (_span * (_num - j - 1) / unitSpan) + company + "-" + (_span * (_num - j) / unitSpan) + company
  90. })
  91. }
  92. }
  93. return lastArr;
  94. },
  95. closeLineHeight(index) {
  96. var that = this;
  97. if (index) that.lastIndex = index
  98. if (that.lastIndex === null) return
  99. that.chart.dispatchAction({ //把上一个高亮的取消掉
  100. type: 'downplay',
  101. seriesIndex: 0,
  102. dataIndex: that.lastIndex
  103. })
  104. },
  105. selectLineHeight(index){ //高亮某个区县 传入城市名 获得对应下标 然后高亮
  106. var that =this;
  107. this.closeLineHeight();
  108. var isExit = false;
  109. this.config.data.map(function(v,i){
  110. console.log(v)
  111. console.log(index)
  112. if(v.name == index) {
  113. console.log(i)
  114. that.lastIndex =i;
  115. isExit=true
  116. }
  117. })
  118. // this.lastIndex = index;
  119. if(isExit)
  120. this.chart.dispatchAction({
  121. type: 'highlight',
  122. seriesIndex: 0,
  123. dataIndex: that.lastIndex
  124. })
  125. }
  126. },
  127. watch: {
  128. config: function (data) {
  129. if (data) {
  130. this.chart.setOption(this.getOption());
  131. if(data.index !== null)this.selectLineHeight(data.index)
  132. }
  133. }
  134. }
  135. })
  136. var defaults = {
  137. tooltip: {
  138. trigger: 'item',
  139. formatter: '{b}<br/>{c} (人次)'
  140. },
  141. series: [{
  142. type: 'map',
  143. mapType: '上饶', // 自定义扩展图表类型
  144. itemStyle: {
  145. normal: {
  146. label: {
  147. show: true,
  148. color: '#ffffff'
  149. },
  150. borderColor: '#fff',
  151. shadowColor: '#0355ab',
  152. shadowBlur: 10,
  153. shadowOffsetX: 10,
  154. shadowOffsetY: 10
  155. },
  156. emphasis: {
  157. label: {
  158. show: true,
  159. color: '#002f9c'
  160. },
  161. areaColor: '#00e9fa',
  162. }
  163. },
  164. data: [],
  165. selectedMode: 'single'
  166. }],
  167. }
  168. })()