• 基于实现地图弹窗轮播功能及遇到的问题解决


    基本使用

    获取地图 geojson 数据

    链接: 阿里云数据可视化平台

    获取ECharts
    npm install echarts

    或者是使用地址链接

    1. <script src="https://registry.npmmirror.com/echarts/5.4.3/files/dist/echarts.min.js"></script>
    2. <script src="https://registry.npmmirror.com/echarts-gl/2/files/dist/echarts-gl.min.js"></script>
    地图基本创建
    1. // 引入 ECharts 主模块
    2. var echarts = require('echarts/lib/echarts');
    3. // 引入地图
    4. require('echarts/map/js/china');
    5. // 基于准备好的dom,初始化echarts实例
    6. var myChart = echarts.init(document.getElementById('main'));
    7. // 指定图表的配置项和数据
    8. var option = {
    9. title: {
    10. text: 'ECharts 地图示例',
    11. subtext: '中国地图',
    12. left: 'center'
    13. },
    14. tooltip: {
    15. trigger: 'item'
    16. },
    17. legend: {
    18. orient: 'vertical',
    19. left: 'left',
    20. data: ['中国地图']
    21. },
    22. visualMap: {
    23. min: 0,
    24. max: 2500,
    25. left: 'left',
    26. top: 'bottom',
    27. text: ['高','低'],
    28. calculable: true
    29. },
    30. series: [
    31. {
    32. name: '中国地图',
    33. type: 'map',
    34. mapType: 'china',
    35. roam: true,
    36. label: {
    37. normal: {
    38. show: true
    39. },
    40. emphasis: {
    41. show: true
    42. }
    43. },
    44. data: [
    45. {name: '北京', value: Math.round(Math.random() * 1000)},
    46. {name: '天津', value: Math.round(Math.random() * 1000)},
    47. // ... 其他省份数据
    48. ]
    49. }
    50. ]
    51. };
    52. // 使用刚指定的配置项和数据显示图表。
    53. myChart.setOption(option);

    这段代码演示了如何在ECharts中创建一个基本的中国地图,包括如何初始化ECharts实例、设置图表的配置项和数据。

    功能实现
    区域分级
    1. series: [{
    2. tooltip: {
    3. trigger: 'item',
    4. },
    5. name: 'beijing',
    6. type: 'map',
    7. map: 'beijing', // 自定义扩展图表类型
    8. showLegendSymbol: true, // 存在legend时显示
    9. label: { // 文字
    10. show: true,
    11. color: '#fff',
    12. fontSize: 10
    13. },
    14. itemStyle: { // 地图样式
    15. areaColor: '#282C34', //区域颜色
    16. borderColor: '#ffffff', //边框颜色
    17. borderWidth: 1
    18. },
    19. emphasis: { // 鼠标移入时显示的默认样式
    20. itemStyle: {
    21. areaColor: '#4adcf0',
    22. borderColor: '#404a59',
    23. borderWidth: 1
    24. },
    25. label: { // 文字
    26. show: true,
    27. color: '#0D5EFF',
    28. fontSize: 12,
    29. fontWeight: 600
    30. },
    31. },
    32. data: [
    33. {
    34. name: '丰台区',
    35. //自定义区域的颜色
    36. itemStyle: {
    37. areaColor: '#F50508',
    38. borderColor: '#1773c3', // 区域边框
    39. shadowColor: '#1773c3', // 阴影
    40. }
    41. }
    42. ],
    43. }],
    44. }
    • name的属性值必须要对应,比如地图上是丰台区,name值要是丰台区,那么就不会生效。
    • 注册的地图名称,必须与map值一致,比如
    区域轮播高亮

    实现轮播高亮需要借助官方提供的:dispatchActionhighlightdownplay 这3个API来实现

    设置轮播
    1. //设置轮播
    2. myEchart.dispatchAction({
    3. type: 'highlight',
    4. seriesIndex: 0, //指定哪一个系列,就是series里的哪一个
    5. dataIndex: 0 //指定高亮的下标
    6. })

    注意: 必须在设置完属性后,再使用

    接下来无法就是用个定时器,动态改变dataIndex的值就好

    弹窗轮训

    和区域高亮轮播使用的方法相同,用到的属性值不同

    设置轮播
    1. myChart.dispatchAction({
    2. type: 'downplay',
    3. seriesIndex: 0,
    4. })

    弹窗样式写在 echarts 属性中的 tooltip

    1. tooltip: {
    2. backgroundColor: 'none',
    3. trigger: 'item',
    4. textStyle: {
    5. fontSize: 16,
    6. color: '#fff',
    7. fontWeight: 500,
    8. },
    9. position: function (point) {
    10. return [point[0] - 140, point[1] - 140] // 这里的坐标是相对于鼠标位置的偏移量
    11. },
    12. padding: 0,
    13. borderWidth: 0,
    14. shadowBlur: 0, // 去掉阴影
    15. borderColor: 'none',
    16. className: 'custom-tooltip-box',
    17. formatter: function (params) {
    18. },
    19. },
    轮播设定的方法

    在ECharts中实现地图上的标记点轮播显示信息时,可以使用setInterval定时更新数据,并通过setOption方法重绘图表。

    • 准备一系列的地图数据,每个数据都有高亮和正常状态的样式。
    • 在ECharts中,可以通过设置 tooltip.trigger="axis" ,来显示数据轴上的提示框。
    • 实现tooltip的自动轮播显示,结合使用 setInterval()和 dispatchAction()方法,使用setInterval方法设置一个定时器,轮播高亮不同的地区。
    • 获取chart DOM 实例,监听鼠标事件,悬浮时清空定时器,鼠标离开开启定时器,继续轮播。
    1. this.mapSet(myChart, true)
    2. this.initTimer(myChart)
    3. myChart.on('mousemove', function (e) {
    4. clearInterval(_this.hoverTimer)
    5. myChart.dispatchAction({
    6. type: 'downplay',
    7. seriesIndex: 0,
    8. })
    9. myChart.dispatchAction({
    10. type: 'highlight',
    11. seriesIndex: 0,
    12. dataIndex: e.dataIndex,
    13. })
    14. myChart.dispatchAction({
    15. type: 'showTip',
    16. seriesIndex: 0,
    17. dataIndex: e.dataIndex,
    18. })
    19. }) //鼠标移入静止播放
    20. myChart.on('mouseout', function (e) {
    21. clearInterval(_this.hoverTimer)
    22. myChart.dispatchAction({
    23. type: 'downplay',
    24. seriesIndex: 0,
    25. dataIndex: e.dataIndex,
    26. }) //鼠标移出后先把上次的高亮取消
    27. // 把点击过的选中样式去掉
    28. if (this.curSelectName) {
    29. myChart.clear()
    30. myChart.setOption(option)
    31. }
    32. _this.initTimer(myChart)
    33. })
    34. myChart.on('click', function (e) {
    35. this.curSelectName = e.name
    36. let index = _this.mapDate.findIndex(item => item.name === e.name)
    37. _this.curHoverIndex = index
    38. })
    1. // 地图定时轮播动画
    2. initTimer(myChart) {
    3. this.hoverTimer = setInterval(() => {
    4. this.mapSet(myChart)
    5. this.cartoonIndex++
    6. if (this.cartoonIndex >= this.mapDate.length) {
    7. this.cartoonIndex = -1
    8. }
    9. this.curHoverIndex = this.cartoonIndex
    10. }, 5000)
    11. },
    12. mapSet(myChart, isfirst) {
    13. // 隐藏提示框
    14. myChart.dispatchAction({
    15. type: 'hideTip',
    16. seriesIndex: 0,
    17. dataIndex: this.cartoonIndex,
    18. })
    19. // 显示提示框
    20. myChart.dispatchAction({
    21. type: 'showTip',
    22. seriesIndex: 0,
    23. dataIndex: this.cartoonIndex + 1,
    24. })
    25. // 取消高亮指定的数据图形
    26. myChart.dispatchAction({
    27. type: 'downplay',
    28. seriesIndex: 0,
    29. dataIndex: this.cartoonIndex,
    30. })
    31. // 高亮指定的数据图形
    32. myChart.dispatchAction({
    33. type: 'highlight',
    34. seriesIndex: 0,
    35. dataIndex: this.cartoonIndex + 1,
    36. })
    37. if (isfirst) {
    38. this.cartoonIndex++
    39. this.curHoverIndex = this.cartoonIndex
    40. }
    41. },
    去除弹窗阴影
    问题复现

    在使用了弹窗中使用了 formatter 设置自定义的样式背景时,echarts 默认的边框 border 阴影还有明显的显示,在tooltip 中设置了 shadowBlur: 0 依旧显示,查看元素如下图:

    弹窗上未绑定任何 class 样式,在标签中有绑定行内样式 box-shadow 导致弹窗带有默认阴影样式。

    解决方法
    • 在 tooltip 对象中添加自定义样式名 className
    • 在 css 样式中去除阴影
    1. tooltip: {
    2. backgroundColor: 'none',
    3. trigger: 'item',
    4. textStyle: {
    5. fontSize: 16,
    6. color: '#fff',
    7. fontWeight: 500,
    8. },
    9. position: function (point) {
    10. return [point[0] - 140, point[1] - 140] // 这里的坐标是相对于鼠标位置的偏移量
    11. },
    12. padding: 0,
    13. borderWidth: 0,
    14. shadowBlur: 0, // 去掉阴影
    15. borderColor: 'none',
    16. className: 'custom-tooltip-box',
    17. formatter: function (params) {
    18. },
    19. },

    css 样式(需要使用深度样式修改 deep)

    1. :deep(.custom-tooltip-box) {
    2. padding: 0 !important;
    3. border: none !important;
    4. background-color: transparent !important;
    5. box-shadow: none !important;
    6. }
    自定义 tooltip 位置

    实现要求:弹窗显示的位置在地图区域中心点位的正上方

    使用 position 函数表达法

    point: 鼠标位置,如 [20, 40]。
    params: 同 formatter 的参数相同。
    dom: tooltip 的 dom 对象。
    rect: 只有鼠标在图形上时有效,是一个用x, y, width, height四个属性表达的图形包围盒。
    size: 包括 dom 的尺寸和 echarts 容器的当前尺寸,例如:{contentSize: [width, height], viewSize: [width, height]}。

    返回值:
    可以是一个表示 tooltip 位置的数组,数组值可以是绝对的像素值,也可以是相 百分比。

    • point[0] - 140:弹框宽度的一半
    • point[1] - 140]:弹窗的整体高度
    1. position: function (point) {
    2. return [point[0] - 140, point[1] - 140] // 这里的坐标是相对于鼠标位置的偏移量
    3. },


     

  • 相关阅读:
    音频应用编程
    vtk数据集的整合与附加_vtkAppendFilter
    大数据分析案例
    CCF开源发展委员会正式成立,探索开源发展新途径
    机器学习——图片处理应用(人脸补全)
    Java 代码优化29个小技巧
    RabbitMQ安装与简单使用
    java基础 io流 字节流 字符流 节点流 包装流 转换流 缓冲流 对象流 打印流 Properties类
    《安卓逆向》Magisk的编译踩坑记录-安装方法-分享魔改后的Magisk过root检测方法
    获取URL参数的两种方法及location对象的各项获取方式
  • 原文地址:https://blog.csdn.net/qq_43641110/article/details/138149038