map-chart.js 6.9 KB

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