返回 导航

其他

hangge.com

ECharts - 自定义地图散点标记的背景图片(附样例)

作者:hangge | 2024-02-20 09:35
    当使用 ECharts 制作地图散点标记时,我们通常可以使用默认的标记图标,比如圆点或者其他简单形状。然而,有时候我们希望使用自定义的背景图片来代替默认的标记图标,以使地图更符合特定主题或需求。下面我将演示如何在 ECharts 中实现自定义地图散点标记的背景图片。

1,默认样式

(1)ECharts 默认自带了 circle(圆形)、rect(矩形)、roundRect(圆角矩形)、triangle(三角形)、diamond(菱形)、pin(图钉)、arrow(箭头)等标记样式,下面是使用 pin 图钉样式的效果:
(2)下面是相关的样例代码:
<template>
  <!-- 地图容器 -->
  <div ref="mapContainer" style="width:100%; height: 600px;"></div>
</template>

<script>
// 引入echarts库
import echarts from 'echarts'

export default {
  data() {
    return {
      // 导入江苏省地图的JSON数据
      jsMap: require("@/libs/mapJson/320000.json"),
      // ECharts的配置项
      chartOption: {
        geo: [
          {
            // 地图名称,这里为江苏省
            map: '江苏',
            // 地图缩放级别
            zoom: 1,
            // 地图上的标签配置
            label: {
              show: true,           // 是否显示标签
              fontSize: 13,         // 标签字体大小
              color: '#333',        // 标签字体颜色
              position: ['50%', '50%'],  // 标签位置
            },
            // 地图区域的样式配置
            itemStyle: {
              areaColor: '#5FC5C1',  // 地图区域颜色
              borderColor: '#fff',   // 地图区域边界颜色
              fontWeight: 700,       // 地图区域字体粗细
              borderWidth: 1,        // 地图区域边界宽度
              shadowColor: "rgba(0, 0, 0, 0.5)", // 阴影颜色
              shadowBlur: 10, // 阴影模糊度
            },
            // 地图区域在强调(hover)状态下的样式配置
            emphasis: {
              itemStyle: {
                label: {
                  color: '#222',       // 强调状态下的标签颜色
                  fontWeight: 700,     // 强调状态下的标签字体粗细
                },
                areaColor: '#2D8F8A',  // 强调状态下的地图区域颜色
                borderColor: '#fff',   // 强调状态下的地图区域边界颜色
              },
            },
          },
        ],
        series: [
          {
            name: "alarmScatter",
            type: "scatter",
            symbol: "pin",
            symbolSize: 45,
            coordinateSystem: "geo",
            data: [],
            label: {
              show: true,
              color: "#ffebdc",
              fontSize: 13,
              formatter: "{@value}"
            },
            itemStyle: {
              color: "#1F94E7",
              opacity: 0.9
            }
          }
        ]
      },
    }
  },
  mounted() {
    // 在组件挂载完成后初始化地图
    this.initChart()
  },
  methods: {
    // 初始化地图
    initChart() {
      // 注册江苏省地图
      echarts.registerMap("江苏", this.jsMap)
      // 初始化ECharts实例,并将地图挂载到指定的容器上
      this.chart = echarts.init(this.$refs.mapContainer)
      // 设置散点数据
      this.chartOption.series[0].data = [
          { value: [117.184811, 34.261792, 234], name: "徐州市" },
          { value: [120.864608, 32.016212, 123], name: "南通市" },
          { value: [119.021265, 33.597506, 323], name: "淮安市" }
        ]
      // 设置ECharts的配置项
      this.chart.setOption(this.chartOption)
    }
  },
  beforeDestroy() {
    // 在组件销毁前,释放ECharts实例占用的资源
    if (this.chart) {
      this.chart.dispose()
    }
  },
}
</script>

2,自定义散点背景

(1)假设我们需要将散点标记的背景替换成如下效果,将 symbol 配置属性设置为指定的图片即可。具体有三种方式指定图片。

(2)第 1 种是直接通过提供图片的 url 地址(image://url)进行设置:
series: [
  {
    name: "alarmScatter",
    type: "scatter",
    symbol: "image:///hangge-web/image/jsmap-pin.png",
    symbolSize: 100,
    coordinateSystem: "geo",
    data: [],
    label: {
      show: true,
      color: "#2C8E89",
      fontSize: 14,
      formatter: "{@value}"
    }
  }
]

(3)第 2 种方法是使用 base64 编码的图片数据:
提示:要将图片转换 base64 编码可以借助一些在线的工具网站来实现(点击访问
series: [
  {
    name: "alarmScatter",
    type: "scatter",
    symbol: "image://data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHwAAAB8CAYAAACrHtS+AAATcUlEQVR4nO2dX4gjyX3Hv79qqbv1byTNqU9mPGdrD3HhEPghusvDGYweDHmMX7T4YuMQMAcGP2wM8UPWYdv3krfDb7kkl9hkgx92As5TEoiPjLFNDEEkJsyDL8N6MHPLzmmYfy2tuqqlrjxMVW+pRzO7m7u924T6QK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EULXzfnwNIjo6O0sPDQ9FoNMR4PE663W7S7XYF1GCl0+kkyl4cHh6KWq02Pzw8FKVSaXF4eMgPDg7mpny9Xk86nc5ch1mv15PxeJw0Gg0BQFQqlfn6+joHIHZ2dubVajUZj8filVdeSRqNhuh0OvPpdDofj8dJtVrN7BqNhqhUKnMAotfriW63mwyHQ67C13kxH4/H83a7zZVOotFoCM550u12k8PDQ1Gv1xOlcwJgUa/Xk+eff1584QtfSFSezdvt9vyNN95I1XMaw+FQXr9+XW5tbaVhGEoieqzn9SpWfgjJzdKQabrdLtvd3aXNzU1K05QWi4V+PSMiknfv3v2DarX6h3fv3r3x2muv/YfjOJIxJvf392Wn00GhUMie9fq77WAwkOZxGWqmL1Pnzp07uH79OtNyo9Eo1Ys1xuMx6Q8Gw+EQ169fp8FggCAI5Hg8JvW8Wwqz3++TcXJCllD9pVDroLZPy8FgQDq8l156id577z05GAz0WSvQYfT7/aUjP4IgyMIfDodS66pXBpty+lqtScjSjvNXZdy6dUsaI/ALa9U+LFkBG0dEZYfw4eGZYvq4Lv1bBlA6PT392t7e3mswziszj5/C8gF/2cmO+hRCI87LTCZjnlxomnwYpnwuTcw4KXHJ/kmNPqXSTIeOS+/ANc5Wy/wZp2cx42y8/BFqT5cVkZoH8y2d17biID1t8ofMmcdLLh07ZZoVCV35kUfb53TMu18wuQPtsjh1OOZRWPnjRx+hJx5Xhyvizaf3Y4dyrWXpkD5tdAvW57npe/MsVKMV5U8ytjxDLNV4LHejSwfZmgfq5g69XXVkpOUZJt8VLj2DcPHg3UcdCmt51jG74PxB9uaAyDx92ZT72M78tjwd8gOaVYMv084+s//vk2/tF1o+lrtwW+D/37jkf0QsFovFYrFYLBaLxWKxWCwWi8VisVgsFovFYrFYLBaLxWKxWCwWi8VisVgsFovFYrFYLBaLxWKxPCH/AwZB+8b+J0puAAAAAElFTkSuQmCC",
    symbolSize: 100,
    coordinateSystem: "geo",
    data: [],
    label: {
      show: true,
      color: "#2C8E89",
      fontSize: 14,
      formatter: "{@value}"
    }
  }
]

(4)第 3 中方法是如何使用 Vue 开发,我们可以直接通过 require 引入图片使用:
series: [
  {
    name: "alarmScatter",
    type: "scatter",
    symbol: "image://" + require("@/assets/jsmap-pin.png"),
    symbolSize: 100,
    coordinateSystem: "geo",
    data: [],
    label: {
      show: true,
      color: "#2C8E89",
      fontSize: 14,
      formatter: "{@value}"
    }
  }
]
评论

全部评论(0)

回到顶部