Echarts自定义地图和添加图标

这篇具有很好参考价值的文章主要介绍了Echarts自定义地图和添加图标。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

  • 介绍:由于我司在项目中使用到了Echarts,所以我开了Echarts的相关栏目来分享在使用上的经验。这是本栏目的第一篇文章,需要Echarts相关基础,之后会介绍Echarts的基础,需要的朋友可以关注一下。废话不多说,先看看例子:

echarts地图添加图片,# Echarts,echarts,前端,javascript

  • 先到阿里云的地理小工具获取对应的地图json数据并保存到本地,通过registerMap方法加载。地理小工具

  • 主要代码:文章来源地址https://www.toymoban.com/news/detail-697391.html

<template>
  <!-- 中心地图 -->
  <div ref="gemEchart" style="width: 100%; height: 100%" />
</template>

<script>
import * as echarts from 'echarts'
import quanZhouJSON from '@/utils/quanzhou.json' // 地图json
import * as echartsOption from '../echartsOption'

export default {
  data: () => ({
    myEcharts: null,
    myData: []
  }),
  mounted() {
    this.init()
    // 适应屏幕大小
    window.addEventListener('resize', () => {
      if (this.myEcharts && this.myEcharts.resize) {
        this.myEcharts.resize()
      }
    })
    // 获取数据
    this.getData()
  },
  methods: {
    init() {
      this.myEcharts = echarts.init(this.$refs['gemEchart'])
      echarts.registerMap('泉州', quanZhouJSON)
      const option = {
        geo: {
          map: '泉州',
          label: {
            show: true,
            // 自定义label
            formatter: (params) => {
              const find = this.myData.find(
                (item) => item.name === params.name
              )
              if (find) {
                return `{name|${find.name}}\n{data|${find.value * 100}%}`
              } else {
                return `{name|${params.name}}`
              }
            },
            // 富文本
            rich: {
              name: {
                height: 24,
                color: 'white',
                padding: [-80, 0, 0, 0],
                fontSize: 18
              },
              data: {
                height: 24,
                color: 'white',
                padding: [-40, 0, 0, 0],
                fontSize: 18
              }
            }
          },
          // 地图区域的样式设置
          itemStyle: {
            areaColor: 'gray' // 区域颜色
          },
          top: 10,
          bottom: 10,
          left: 20,
          right: 20
        },
        series: [
          {
            type: 'map',
            map: '泉州',
            // 默认情况下,map series 会自己生成内部专用的 geo 组件。用 geoIndex 指定一个 geo 组件,
            // 这样的话,map 和 其他 series(例如散点图)就可以共享一个 geo 组件了。
            // 当设定了 geoIndex 后,series-map.map 属性,以及 series-map.itemStyle 等样式配置不再起作用,
            // 而是采用 geo 中的相应属性。
            geoIndex: 0,
            data: []
          },
          // 用于地图中的图标
          {
            type: 'scatter',
            coordinateSystem: 'geo',
            data: echartsOption.icon,
            symbol: echartsOption.img,
            symbolSize: [50, 50],
            symbolOffset: [0, 8]
          }
        ],
        visualMap: {
          // 设置地图范围值显示的颜色
          selectedMode: false, // 不能选择
          hoverLink: false, // 取消鼠标移入效果
          textStyle: {
            color: '#fff'
          },
          align: 'left',
          itemGap: 20,
          pieces: [
            {
              gt: 0.9,
              lte: 1,
              label: '可靠性较高(90-100)',
              color: '#7FB6FF'
            },
            {
              gte: 0.8,
              lte: 0.9,
              label: '可靠性中等(80-90)',
              color: '#5993FF'
            },
            {
              lt: 0.8,
              label: '可靠性较差(80以下)',
              color: '#2458F1'
            }
          ],
          right: 20,
          bottom: 20
        }
      }
      this.myEcharts.setOption(option)
    },
    getData() {
      this.myData = quanZhouJSON.features.map((item) => {
        const {
          name
        } = item.properties
        return {
          name,
          value: (Math.random()).toFixed(2) // 等数据接入
        }
      })
      this.myEcharts.setOption({
        series: [
          {
            type: 'map',
            data: this.myData
          }
        ]
      })
    }
  }
}
</script>
  • echartsOption:地图的相关配置
// 中间地图的一些配置
import quanZhouJSON from '@/utils/quanzhou.json'

// 获取图标的数据
export const icon = quanZhouJSON.features.map((item) => {
  const {
    name,
    centroid
  } = item.properties
  return {
    name,
    value: centroid
  }
})

// 图标 base64
export const img = `image://data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEkAAABBCAYAAACdIImxAAAAAXNSR0IArs4c6QAAAARzQklUCAgICHwIZIgAAAtSSURBVHic7Vrfb2RJdf5OVd3b7W7/nu1ZG5ksIGu09PAU8xBFETNCSPkLLASIvyCBhxUPIASOSfLIMwgWpOwqi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K0n6vlvR4a3Q+163O8xLX0DE4CAUQ9ksGsyGt1bMmaQeGkydGGunpChYBbdWIZuYUTwBjvZg8qw0WJcgmUp5tpwQNz0O9kUzeioBzsyQA6JtEtzoD6mYKLq9L+UFbgncSCf08WxuLiyN2jyIOaqF3jyIOwrwPFuc49PMcvJNI+UFbcHld0s3rxRHU6/58MXzzpGNRd0BYKtGHSo+Mn88Q1j4IOXjY08csPauu4dXVvO6uxIqDYupipxMEnMM1wC4ugCTgVKJKN9Kj8coMAcD68uC9pV0AqMbHCf1KbXDj2tP+/CyoiwsiqW9+AgbvMnXJOl5sd3Ar/dVPDoCLJgg43zmpHwoiDM5RpXTgt/cVSxuCJQyVjbTc3u8QtP2+ENQTd4E4/foggNSyhlDa7IXHXpi/YIJ6Ii8eevIEeOAKcwf914+B94Wgntj3CXQiIOlg7VDzCydniimmmGKKKaaYYooppphiiin+rPBHu222sxUl4c4AAAAASUVORK5CYII=`


到了这里,关于Echarts自定义地图和添加图标的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包