当前位置:   article > 正文

vue+echarts 实现地图tooltip点击事件;toolTip数据动态渲染;同时鼠标滑过涟漪点时实现地图多区域联动_vue echarts tooltip

vue echarts tooltip

最终做出来的效果是这样的:

最近做项目时,遇到这样的需求:

        1、toolTip上的数据根据后台动态渲染

        2、鼠标移入地图涟漪点时显示tootTip,点击toolTip上的文字,携带动态数据id进行路由跳转

        3、鼠标移入地图涟漪点,与涟漪点相关的省份多区域联动高亮

   这么多问题,不要慌,办法都是人想出来的,我们先从第一个问题来开整:

一、众所周知,echart的toolTip有自己的显示样式,但是在实际开发中需要自定义html结构才能满足需求,如我遇到的动态渲染,于是我这样做:

  1. let initOption = {
  2. tooltip: {
  3. show: false, // 提示框
  4. triggerOn: "mousemove|click", //必须使用这种方式,因为tooltip需要有点击事件,同时移入effectScatter点区域联动
  5. extraCssText: "border:none;",//清除tooltip自带的边框颜色
  6. // alwaysShowContent: true,//提示框不消失
  7. hideDelay: 2000, //提示框移出或点击2秒后消失
  8. },
  9. //使用这种方式添加tooltip的formatter,便于点击事件的获取
  10. initOption.tooltip.formatter = function (params) {
  11. if (params.seriesType === `effectScatter`) {
  12. this.proviesName = [];
  13. const theData = params.data?.theData;
  14. //省份集合value
  15. let provies = [];
  16. theData.map((item) => {
  17. provies.push(...item.businessArea.split(","));
  18. });
  19. //遍历匹配得到省份合集
  20. provies?.map((item) => {
  21. mapCenterData.map((ite) => {
  22. if (item == ite.value) this.proviesName.push(ite.name);
  23. });
  24. });
  25. //数组去重
  26. this.proviesName = Array.from(new Set(this.proviesName));
  27. let before = "";
  28. let center = "";
  29. // 格式化提示框信息
  30. theData.map((item) => {
  31. item.recruitmentInfoList.map((ite) => {
  32. center += `
  33. <div style="width: 15vw;display: flex;justify-content: space-between;padding: 0 10px; font-size: 14px; margin: 10px 0;">
  34. ${ite.title}
  35. </div>
  36. `;
  37. });
  38. before +=
  39. `
  40. <div style="position: relative;z-index: 100000000000000000000000;">
  41. <div style="font-weight: bold;display: flex;padding: 0 5px;justify-content: space-between;margin-bottom: 20px;">
  42. <div style="font-weight: bold;color: #348df2;font-size: 16px;z-index:100;">${item.shortName}</div>
  43. </div>` +
  44. center +
  45. `
  46. <div style="width: 15vw;display: flex;justify-content: flex-end;padding: 0 10px; font-size: 14px;" >
  47. <div style="align-items: flex-end;display: flex;align-items: center;color:#3da2ff;pointer-events: auto;" class="toJoin-${
  48. item.id
  49. }">
  50. <p οnclick="${this.getMoreJob(item.id)}">更多</p>
  51. <svg style="width:20px;height:20px;margin-left: 10px;" t="1672020296882" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2518" width="200" height="200"><path d="M1024.2048 512c0 6.656-2.4576 13.2608-7.5264 18.2784l-325.8368 325.8368a25.6 25.6 0 1 1-36.1984-36.1984l282.5216-282.5216H25.8048a25.6 25.6 0 1 1 0-51.2h910.9504l-282.112-282.112a25.6 25.6 0 1 1 36.1984-36.1984l325.8368 325.8368c4.608 4.608 7.5264 11.008 7.5264 18.0736V512z" fill="#3ca1fe" p-id="2519"></path></svg>
  52. </div>
  53. </div>
  54. </div>
  55. `;
  56. });
  57. return before;
  58. }
  59. }.bind(this);
  60. }

tooTip对象是放在初始化的echart对象中,相信聪明的你肯定知道。这样写就能实现toolTip中html自定义。

二、点击文字,携带参数的路由跳转,由于tooltip中不能使用vue语法,只能使用原生点击事件(onclick),大家可以参考上面代码

<p οnclick="${this.getMoreJob(item.id)}">更多</p> //这里可以获取到定义在vue中的方法

三、移入地图涟漪点省份区域联动,实现思路就是在initOption对象中添加:

  1. let initOption = {
  2. // 设置高亮颜色
  3. dataRange: {
  4. show: false,
  5. x: "left",
  6. y: "bottom",
  7. splitList: [
  8. { start: 5, end: 5, color: "#007aff" }, //当值为5时,区域背景(值随便设置)
  9. ],
  10. },
  11. }

然后在ehcart的移入事件中这么写:

  1. //移入显示区域联动
  2. this.chart.on("mouseover", (params) => {
  3. if (params.seriesType == "effectScatter") {
  4. this.seriesData = [];
  5. this.proviesName.map((item) => {
  6. this.seriesData.push({
  7. name: item + "省",//这里名字得与联动的省份的名字对应,不然没效果
  8. value: 5, //随便写的值,主要是与dataRange中设置的值相对应,这样就能实现区域联动高亮
  9. });
  10. this.chart.setOption({ series: [{}, { data: this.seriesData }] });
  11. });
  12. }
  13. });

鼠标移出还得操作一手,为了除高亮:

  1. //鼠标移出,清除联动高亮
  2. this.chart.on("mouseout", (params) => {
  3. if (params.seriesType == "effectScatter") {
  4. this.seriesData = [];
  5. this.chart.setOption({ series: [{}, { data: this.seriesData }] });
  6. }
  7. });

最后,废话不多说,上完整代码:

  1. //初始化图表
  2. initChart() {
  3. this.chart = echarts.init(this.$refs.map);
  4. let initOption = {
  5. grid: {
  6. bottom: "0%",
  7. top: "10%",
  8. left: "10%",
  9. right: "10%",
  10. },
  11. // 设置高亮颜色
  12. dataRange: {
  13. show: false,
  14. x: "left",
  15. y: "bottom",
  16. splitList: [
  17. { start: 5, end: 5, color: "#007aff" }, //当值为5时,区域背景
  18. ],
  19. },
  20. tooltip: {
  21. show: false, // 提示框
  22. triggerOn: "mousemove|click", //必须使用这种方式,因为tooltip需要有点击事件,同时移入effectScatter点区域联动
  23. extraCssText: "border:none;",//清除tooltip自带颜色
  24. // alwaysShowContent: true,//提示框不消失
  25. hideDelay: 2000, //提示框2秒后小时
  26. },
  27. geo: {
  28. show: true,
  29. map: "china",
  30. zoom: 1.1, // 地图比例
  31. label: {
  32. normal: {
  33. show: false,
  34. },
  35. emphasis: {
  36. show: false,
  37. },
  38. },
  39. roam: false,
  40. itemStyle: {
  41. normal: {
  42. areaColor: "#01215c",
  43. borderWidth: 5, //设置外层边框
  44. borderColor: "transparent",
  45. shadowColor: "#add7ff",
  46. shadowBlur: 30,
  47. shadowOffsetX: 0,
  48. shadowOffsetY: 15,
  49. },
  50. },
  51. },
  52. series: [
  53. {
  54. type: "effectScatter",
  55. coordinateSystem: "geo",
  56. showEffectOn: "render",
  57. rippleEffect: {
  58. brushType: "stroke",
  59. scale: 5,
  60. period: 2, // 秒数
  61. },
  62. symbolSize: 12,
  63. hoverAnimation: true,
  64. label: {
  65. normal: {
  66. formatter: "{b}",
  67. position: "right",
  68. show: true,
  69. },
  70. },
  71. zlevel: 1,
  72. tooltip: {
  73. show: true, // 提示框
  74. triggerOn: "click",
  75. },
  76. },
  77. {
  78. type: "map",
  79. map: "china",
  80. aspectScale: 0.75,
  81. zoom: 1.1, // 地图比例
  82. itemStyle: {
  83. normal: {
  84. areaColor: "#ebf6fd",
  85. borderColor: "#a1cffb",
  86. shadowColor: "rgba(255, 230, 175,0.5)",
  87. borderWidth: 1,
  88. },
  89. emphasis: {
  90. areaColor: "#52C5F7",
  91. },
  92. },
  93. emphasis: {
  94. label: {
  95. show: true,
  96. },
  97. },
  98. },
  99. ],
  100. };
  101. //添加tooltip对象
  102. initOption.tooltip.formatter = function (params) {
  103. if (params.seriesType === `effectScatter`) {
  104. this.proviesName = [];
  105. const theData = params.data?.theData;
  106. //省份集合value
  107. let provies = [];
  108. theData.map((item) => {
  109. provies.push(...item.businessArea.split(","));
  110. });
  111. //遍历匹配得到省份合集
  112. provies?.map((item) => {
  113. mapCenterData.map((ite) => {
  114. if (item == ite.value) this.proviesName.push(ite.name);
  115. });
  116. });
  117. //数组去重
  118. this.proviesName = Array.from(new Set(this.proviesName));
  119. let before = "";
  120. let center = "";
  121. // 格式化提示框信息
  122. theData.map((item) => {
  123. item.recruitmentInfoList.map((ite) => {
  124. center += `
  125. <div style="width: 15vw;display: flex;justify-content: space-between;padding: 0 10px; font-size: 14px; margin: 10px 0;">
  126. ${ite.title}
  127. </div>
  128. `;
  129. });
  130. before +=
  131. `
  132. <div style="position: relative;z-index: 100000000000000000000000;">
  133. <div style="font-weight: bold;display: flex;padding: 0 5px;justify-content: space-between;margin-bottom: 20px;">
  134. <div style="font-weight: bold;color: #348df2;font-size: 16px;z-index:100;">${item.shortName}</div>
  135. </div>` +
  136. center +
  137. `
  138. <div style="width: 15vw;display: flex;justify-content: flex-end;padding: 0 10px; font-size: 14px;" >
  139. <div style="align-items: flex-end;display: flex;align-items: center;color:#3da2ff;pointer-events: auto;" class="toJoin-${
  140. item.id
  141. }">
  142. <p οnclick="${this.getMoreJob(item.id)}">更多</p>
  143. <svg style="width:20px;height:20px;margin-left: 10px;" t="1672020296882" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2518" width="200" height="200"><path d="M1024.2048 512c0 6.656-2.4576 13.2608-7.5264 18.2784l-325.8368 325.8368a25.6 25.6 0 1 1-36.1984-36.1984l282.5216-282.5216H25.8048a25.6 25.6 0 1 1 0-51.2h910.9504l-282.112-282.112a25.6 25.6 0 1 1 36.1984-36.1984l325.8368 325.8368c4.608 4.608 7.5264 11.008 7.5264 18.0736V512z" fill="#3ca1fe" p-id="2519"></path></svg>
  144. </div>
  145. </div>
  146. </div>
  147. `;
  148. });
  149. return before;
  150. }
  151. }.bind(this);
  152. this.chart.setOption(initOption);
  153. //移入显示区域联动
  154. this.chart.on("mouseover", (params) => {
  155. if (params.seriesType == "effectScatter") {
  156. this.seriesData = [];
  157. this.proviesName.map((item) => {
  158. this.seriesData.push({
  159. name: item + "省",
  160. value: 5,
  161. });
  162. this.chart.setOption({ series: [{}, { data: this.seriesData }] });
  163. });
  164. }
  165. });
  166. //鼠标移出,清除联动高亮
  167. this.chart.on("mouseout", (params) => {
  168. if (params.seriesType == "effectScatter") {
  169. this.seriesData = [];
  170. this.chart.setOption({ series: [{}, { data: this.seriesData }] });
  171. }
  172. });
  173. },

        注意:初始化图表的数据都来原于后台接口请求,涟漪点的数据,与地图每个区域的数据,需要各位根据实际情况渲染到地图上,然后才会有上面的3个问题的出现

        最后:原创不易,转载请注明出处

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/308621
推荐阅读
相关标签
  

闽ICP备14008679号