vue3+echarts5+ts setup语法糖实现全球1000w GPS点可视化散点图

1. 引入echarts5

 pnpm install echarts --save

2. 在需要展示散点图的页面引入echarts

import * as echarts from 'echarts'

3. 在项目根目录 public 文件夹中引入 geo文件

V5移除了内置的geoJSON(原先在echarts/map文件夹下),这里引用的中国和世界地图的geoJSON是我从老的版本中复制出来的

git clone https://gitee.com/potatocoder/geo_json.git 

4. 在onMounted中调用onload方法渲染echarts

代码如下:
<template>
   <div class="center-body">
       <div id="map" ref="chartRef" style="width: 100%; height: 100%" class="map" />
   </div>
</template>

<script setup lang="ts">
import { nextTick, onMounted, ref } from 'vue'
import * as echarts from 'echarts'
import 'echarts-gl'
import WorldGeo from '../../../../public/monitor/geoJSON_world.json'
// import '../../../../public/monitor/geoJSON_china.json'
import { echarsGet } from '@/api/echars'
defineOptions({
  name: 'Global'
})

// 全球散点地图
const chartRef = ref()
let myChart
function onload() {
  if (myChart != null && myChart != '' && myChart != undefined) {
    myChart.dispose() //销毁
  }
  // 将自定义的全球地理数据注册到 ECharts 中
  echarts.registerMap('world', WorldGeo as any)
  myChart = echarts.init(chartRef.value)

  const option = {
    backgroundColor: 'transparent',
    title: {
      text: '10000000 GPS Points',
      left: 'center',
      textStyle: {
        color: '#fff'
      }
    },
    geo: {
      map: 'world',
      roam: true,
      label: {
        emphasis: {
          show: false
        }
      },
      silent: true,
      itemStyle: {
        normal: {
          areaColor: '#153777',
          borderColor: '#4F8CCD'
        },
        emphasis: {
          areaColor: '#2a333d'
        }
      }
    },
    series: [
      {
        name: '弱',
        type: 'scatterGL',
        progressive: 1e6,
        coordinateSystem: 'geo',
        symbolSize: 2,
        zoomScale: 0.002,
        blendMode: 'lighter',
        large: true,
        itemStyle: {
          color: 'rgb(20, 15, 2)'
        },
        postEffect: {
          enable: true
        },
        silent: true,
        dimensions: ['lng', 'lat'],
        left: 0,
        right: 0,
        data: new Float32Array()
      }
    ],
    grid: {
      top: '0px',
      left: '0px',
      right: '0px',
      bottom: '0px',
      containLabel: true
    }
  }
  // 通过接口请求千万条数据
  fetchData(0, myChart)
  // 放大缩小窗口自适应屏幕,调用前使用nextTick()可在初始化页面就自适应
  window.addEventListener('resize', function () {
    myChart.resize()
  })
  myChart.setOption(option)
}

// 请求echarts接口返回定位数据
function fetchData(idx, myChart) {
  const CHUNK_COUNT = 230
  if (idx >= CHUNK_COUNT) {
    return
  }
  // 调用api获取数据,因为本地调用echars接口请求千万条数据会跨域,自己配置了下
  const promise = echarsGet(idx)
  promise
    .then((res) => {
      const rawData = new Int32Array(res)
      const data = new Float32Array(rawData.length)
      for (let i = 0; i < 10; i += 2) {
        data[i] = rawData[i + 1] / 1e7
        data[i + 1] = rawData[i] / 1e7
      }
      myChart.appendData({
        seriesIndex: 0,
        data
      })
      // 数据太大会一直调用,我这里先请求30次,每次请求会返回40万条数据
      if (idx < 30) {
        fetchData(idx + 1, myChart)
      }
    })
    .finally(() => {})
}

onMounted(() => {
  // 加载中间世界地图
  nextTick(() => {
    onload()
  })
})
</script>

<style lang="less" scoped>
@import '@/styles/style/monitor.css';
</style>

5. 因为数据请求接口调用的外部地址'https://echarts.apache.org/examples',且请求头需要将responseType设置为'arraybuffer',因此在vue项目中需要在请求的时候配置请求头和设置允许跨域:

'@/api/echars'请求页面配置如下:
import { request } from '@/utils/request'

export function echarsGet(idx) {
  return request(
    {
      url: `/echars/data/asset/data/gps/gps_${idx}.bin`,
      method: 'get'
    },
    {
      isGetDataDirectly: false
    }
  )
}
 echarts接口请求前需要将responseType设置为'arraybuffer',请求拦截处理如下: 
 请求地址中以/echars开头的,设置允许跨域请求地址为'https://echarts.apache.org/examples'

注意: 

option中geo的map名称必须与注册时registerMap('world', WorldGeo as any)的名称相对应

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue3是一种流行的JavaScript框架,而Echarts5是一种强大的数据可视化库。结合Vue3和Echarts5,可以很容易地实现世界地图的可视化效果。 首先,你需要在Vue项目中安装Echarts5。可以通过npm或yarn来安装echarts: ``` npm install echarts ``` 或者 ``` yarn add echarts ``` 安装完成后,你可以在Vue组件中引入Echarts,并使用它来绘制世界地图。 在Vue组件中,你可以使用`<template>`标签来定义HTML模板,使用`<script>`标签来编写JavaScript代码,使用`<style>`标签来定义CSS样式。 下面是一个简单的示例代码,展示了如何在Vue3中使用Echarts5绘制世界地图: ```vue <template> <div id="world-map" style="width: 100%; height: 400px;"></div> </template> <script> import * as echarts from 'echarts'; export default { mounted() { this.drawWorldMap(); }, methods: { drawWorldMap() { const chartDom = document.getElementById('world-map'); const myChart = echarts.init(chartDom); // 定义地图数据 const mapData = [ { name: 'China', value: 100 }, { name: 'United States', value: 50 }, // 其他国家... ]; // 配置地图选项 const option = { tooltip: { trigger: 'item', formatter: '{b}: {c}', }, visualMap: { min: 0, max: 100, left: 'left', top: 'bottom', text: ['High', 'Low'], seriesIndex: [1], inRange: { color: ['#e0ffff', '#006edd'], }, }, series: [ { type: 'map', mapType: 'world', roam: true, label: { show: true, }, data: mapData, }, ], }; // 使用配置项绘制地图 myChart.setOption(option); }, }, }; </script> <style> #world-map { width: 100%; height: 400px; } </style> ``` 在上面的代码中,我们首先引入了Echarts库,然后在`mounted`生命周期钩子函数中调用`drawWorldMap`方法来绘制地图。`drawWorldMap`方法中,我们使用`echarts.init`方法初始化一个Echarts实例,并通过配置项`option`来定义地图的样式和数据。最后,使用`myChart.setOption(option)`方法将配置项应用到地图上。 这样,你就可以在Vue项目中使用Vue3和Echarts5来实现世界地图的可视化效果了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值