首页 > 其他分享 >Echarts 绘制地图(中国、省市、区县)

Echarts 绘制地图(中国、省市、区县)

时间:2024-08-10 12:26:21浏览次数:6  
标签:绘制地图 name random value round 区县 100 Echarts Math

目录

前言

一、Apache Echarts

二、获取地图的GeoJSON

三、项目中引用

四、准备放地图的容器

五、配置地图option信息

六、初始化地图

往期回顾


前言

最近在搞大数据可视化的项目,需要用Echart来做可视化的图表,比如像地图,图表类,今天主要介绍下echart绘制地图如何使用,话不多说,直接上干货!

一、Apache Echarts

官网地址:https://echarts.apache.org/ 

npm install echarts --save

 

二、获取地图的GeoJSON

地址:DataV.地图GeoJSON数据

 左侧是地图,右侧是JSON数据路径,点击你想要生成的地图省市、地级(以中华人名共和国为例为例)

注意:地图点击可以下钻,再点进去是二级,直到你想要的省市地区(点击旁边的空白可以返回上一级);右侧JSON数据的链接地址,可以选择下载下来(放在json文件夹中),也可以使用在线地址!(json API与geoJSON数据地址均可用)

三、项目中引用

import * as echarts from 'echarts';

四、准备放地图的容器

<div style="width:800px;height:600px" id="chartMap"></div>

五、配置地图option信息

let mapOption = {
    tooltip: {
      trigger: 'item',
      formatter: (params: any) => {
        let num
        let showHtml = ''
        if (isNaN(params.value)) {
          num = '0'
        } else {
          num = params.value
        }
        showHtml += `
              <span style="display: flex; font-size: 12px;">
                  ${'装机量'}:${params.name}</br>
              </span>
          `
        return showHtml
      },
    },

    legend: {
      orient: 'vertical',
      left: 'left',
      data: [],
    },
    series: [
      {
        name: '安装信息',
        type: 'map',
        mapType: 'china',
        roam: false,
        label: {
          normal: {
            show: true,
          },
          emphasis: {
            show: true,
          },
        },
        emphasis: {
          scale: true,
          itemStyle: {
            areaColor: '#fff',
          },
        },
        symbolSize: function (val: any) {
          return val[2] / 15
        },
        showEffectOn: 'render',
        itemStyle: {
          areaColor: '#2043AA',
          borderColor: '#111',
          color: '#fff',
        },
        zlevel: 1,
        data: [
          {
            name: '北京',
            value: Math.round(Math.random() * 100),
            warn: 10,
            problem: 12,
          },
          {
            name: '天津',
            value: Math.round(Math.random() * 100),
            warn: 10,
            problem: 12,
          },
          {
            name: '上海',
            value: Math.round(Math.random() * 100),
            warn: 10,
            problem: 12,
          },
          {
            name: '重庆',
            value: Math.round(Math.random() * 100),
            warn: 10,
            problem: 12,
          },
          { name: '河北', value: Math.round(Math.random() * 100) },
          { name: '河南', value: Math.round(Math.random() * 100) },
          { name: '云南', value: Math.round(Math.random() * 100) },
          { name: '辽宁', value: Math.round(Math.random() * 100) },
          { name: '黑龙江', value: Math.round(Math.random() * 100) },
          { name: '湖南', value: Math.round(Math.random() * 100) },
          { name: '安徽', value: Math.round(Math.random() * 100) },
          { name: '山东', value: Math.round(Math.random() * 100) },
          { name: '新疆', value: Math.round(Math.random() * 100) },
          { name: '江苏', value: Math.round(Math.random() * 100) },
          { name: '浙江', value: Math.round(Math.random() * 100) },
          { name: '江西', value: Math.round(Math.random() * 100) },
          { name: '湖北', value: Math.round(Math.random() * 100) },
          { name: '广西', value: Math.round(Math.random() * 100) },
          { name: '甘肃', value: Math.round(Math.random() * 100) },
          { name: '山西', value: Math.round(Math.random() * 100) },
          { name: '内蒙古', value: Math.round(Math.random() * 100) },
          { name: '陕西', value: Math.round(Math.random() * 100) },
          { name: '吉林', value: Math.round(Math.random() * 100) },
          { name: '福建', value: Math.round(Math.random() * 100) },
          { name: '贵州', value: Math.round(Math.random() * 100) },
          { name: '广东', value: Math.round(Math.random() * 100) },
          { name: '青海', value: Math.round(Math.random() * 100) },
          { name: '西藏', value: Math.round(Math.random() * 100) },
          { name: '四川', value: Math.round(Math.random() * 100) },
          { name: '宁夏', value: Math.round(Math.random() * 100) },
          { name: '海南', value: Math.round(Math.random() * 100) },
          // {name: '台湾',value: Math.round(Math.random()*100)},
          { name: '香港', value: Math.round(Math.random() * 100) },
          { name: '澳门', value: Math.round(Math.random() * 100) },
        ],
      },
    ],
  }

六、初始化地图

我的地图Json文件是下载放到项目里assets下的,你可以使用在线的也可以引用本地的,如果你框架是vue可以使用axio获取到JSON信息。博主用的是Angular18框架,可以使用HttpClient获取

constructor(private http: HttpClient) {}
  initEcharts() {
    this.http.get('assets/json/china.json').subscribe((res: any) => {
      const echart = echarts.init(this.chartMap.nativeElement) // 获取视图的echarts的DOM节点,并初始化对象
      echarts.registerMap('china', res) // 注册china.json的数据到初始化的echarts对象
      echart.setOption(this.mapOption) // 绑定地图的配置参数对象,参考第二步
      echart.resize()
    })
  }

vue的不难,同理,这里我就不多说了。

完整代码(我的是angular18写法,仅供参考,其他框架大同小异):

import { HttpClient } from '@angular/common/http'
import {
  Component,
  ElementRef,
  Input,
  NgModule,
  OnInit,
  ViewChild,
} from '@angular/core'
import { SharedModule } from '@shared'
import * as echarts from 'echarts/core'
@Component({
  selector: 'china-map',
  template: `<div #chartMap class="china-map" [ngStyle]="styles"></div>`,
  standalone: true,
  imports: [SharedModule],
  styleUrls: ['./china-map.component.less'],
})
export class ChinaMapComponent implements OnInit {
  @Input() styles: any = {
    width: '100%',
    height: '100%',
    left: 0,
    right: 0,
    bottom: 0,
    top: 0,
  }
  @ViewChild('chartMap')
  chartMap!: ElementRef // 获取DOM节点的对象
  constructor(private http: HttpClient) {}
  mapOption = {
    tooltip: {
      trigger: 'item',
      formatter: (params: any) => {
        let num
        let showHtml = ''
        if (isNaN(params.value)) {
          num = '0'
        } else {
          num = params.value
        }
        showHtml += `
              <span style="display: flex; font-size: 12px;">
                  ${'装机量'}:${params.name}</br>
              </span>
          `
        return showHtml
      },
    },

    legend: {
      orient: 'vertical',
      left: 'left',
      data: [],
    },
    series: [
      {
        name: '项目信息',
        type: 'map',
        mapType: 'china',
        roam: false,
        label: {
          normal: {
            show: true,
          },
          emphasis: {
            show: true,
          },
        },
        emphasis: {
          scale: true,
          itemStyle: {
            areaColor: '#fff',
          },
        },
        symbolSize: function (val: any) {
          return val[2] / 15
        },
        showEffectOn: 'render',
        itemStyle: {
          areaColor: '#2043AA',
          borderColor: '#111',
          color: '#fff',
        },
        zlevel: 1,
        data: [
          {
            name: '北京',
            value: Math.round(Math.random() * 100),
            warn: 10,
            problem: 12,
          },
          {
            name: '天津',
            value: Math.round(Math.random() * 100),
            warn: 10,
            problem: 12,
          },
          {
            name: '上海',
            value: Math.round(Math.random() * 100),
            warn: 10,
            problem: 12,
          },
          {
            name: '重庆',
            value: Math.round(Math.random() * 100),
            warn: 10,
            problem: 12,
          },
          { name: '河北', value: Math.round(Math.random() * 100) },
          { name: '河南', value: Math.round(Math.random() * 100) },
          { name: '云南', value: Math.round(Math.random() * 100) },
          { name: '辽宁', value: Math.round(Math.random() * 100) },
          { name: '黑龙江', value: Math.round(Math.random() * 100) },
          { name: '湖南', value: Math.round(Math.random() * 100) },
          { name: '安徽', value: Math.round(Math.random() * 100) },
          { name: '山东', value: Math.round(Math.random() * 100) },
          { name: '新疆', value: Math.round(Math.random() * 100) },
          { name: '江苏', value: Math.round(Math.random() * 100) },
          { name: '浙江', value: Math.round(Math.random() * 100) },
          { name: '江西', value: Math.round(Math.random() * 100) },
          { name: '湖北', value: Math.round(Math.random() * 100) },
          { name: '广西', value: Math.round(Math.random() * 100) },
          { name: '甘肃', value: Math.round(Math.random() * 100) },
          { name: '山西', value: Math.round(Math.random() * 100) },
          { name: '内蒙古', value: Math.round(Math.random() * 100) },
          { name: '陕西', value: Math.round(Math.random() * 100) },
          { name: '吉林', value: Math.round(Math.random() * 100) },
          { name: '福建', value: Math.round(Math.random() * 100) },
          { name: '贵州', value: Math.round(Math.random() * 100) },
          { name: '广东', value: Math.round(Math.random() * 100) },
          { name: '青海', value: Math.round(Math.random() * 100) },
          { name: '西藏', value: Math.round(Math.random() * 100) },
          { name: '四川', value: Math.round(Math.random() * 100) },
          { name: '宁夏', value: Math.round(Math.random() * 100) },
          { name: '海南', value: Math.round(Math.random() * 100) },
          // {name: '台湾',value: Math.round(Math.random()*100)},
          { name: '香港', value: Math.round(Math.random() * 100) },
          { name: '澳门', value: Math.round(Math.random() * 100) },
        ],
      },
    ],
  }
  ngOnInit() {
    this.initEcharts() // 初始化地图
  }
  initEcharts() {
    this.http.get('assets/json/china.json').subscribe((res: any) => {
      const echart = echarts.init(this.chartMap.nativeElement) // 获取视图的echarts的DOM节点,并初始化对象
      echarts.registerMap('china', res) // 注册china.json的数据到初始化的echarts对象
      echart.setOption(this.mapOption) // 绑定地图的配置参数对象,参考第二步
      echart.resize()
    })
  }
}

欢迎在评论区交流。

如果文章对你有所帮助,❤️关注+点赞❤️鼓励一下!博主会持续更新。。。。

往期回顾

 CSS多栏布局-两栏布局和三栏布局

 border边框影响布局解决方案

 css 设置字体渐变色和阴影

css 重置样式表(Normalize.css)

 css实现元素居中的6种方法 

Angular8升级至Angular13遇到的问题

前端vscode必备插件(强烈推荐)

Webpack性能优化

vite构建如何兼容低版本浏览器

前端性能优化9大策略(面试一网打尽)!

vue3.x使用prerender-spa-plugin预渲染达到SEO优化

 vite构建打包性能优化

 vue3.x使用prerender-spa-plugin预渲染达到SEO优化

 ES6实用的技巧和方法有哪些?

 css超出部分显示省略号

vue3使用i18n 实现国际化

vue3中使用prismjs或者highlight.js实现代码高亮

什么是 XSS 攻击?什么是 CSRF?什么是点击劫持?如何防御

标签:绘制地图,name,random,value,round,区县,100,Echarts,Math
From: https://blog.csdn.net/chaoPerson/article/details/140611460

相关文章

  • 用echarts绘制图表时,横坐标数据太多显示不全/堆叠的解决方法
    一、横坐标数据过多可能出现的问题1、横坐标文字堆叠2、横坐标显示不全(数据2,数据4,数据6,数据8,数据10,数据12,数据14均没有显示出来)二、解决方法1、使用rotate属性给echarts的横坐标标签设置旋转角度xAxis:{type:'category',data:xAxis,......
  • react中封装Echarts
    下载npxcreate-react-appmy-echarts创建公共组件importReact,{useState,useEffect,useMemo}from'react';import*asechartsfrom"echarts";constEChartsComponent=({option})=>{const[echartsInstance,setEchartsInstance]=u......
  • 【日常开发】 java返回ECharts数据结构封装
    java返回ECharts数据结构封装一、前端页面示例图如下:二、准备测试数据:三、后端格式封装代码:四、最终结果:......
  • echarts图标如何自适应宽度
    echarts图标直接设置宽度100%会默认魏100px,需要自适应方法如下图标html<el-col:span="8"><divid="userNianling_three"style="width:100%;height:500px;"/>......
  • echarts 关系图(graph)里的links的起点和终点设置无效
    问题描述,data里面数据也设置了id({id:1})这样设置的,links里面设置了source和target({source:0,target:1}),但是运行发现只显示了node没显示连线(edge),去看了文档描述 1、source  stringnumber 边的源节点名称的字符串,也支持使用数字表示源节点的索引。2、target stringn......
  • Echarts 实现圆角环形图
     第一种方式:使用bar实现想要的效果:代码实现:constchartData={title:{text:'97',//圆环中间数字textStyle:{color:'#222222',fontSize:20,},subtext:'成功数',subtextS......
  • echarts自定义x轴和tooltip数据格式
    echarts自定义x轴和tooltip数据格式x轴和y轴数据格式如下x:[0,1,2,3,4,5,6,.....,23],y:[2.5,3.1,3.2,2.2,2.3,3.1,3.1,null,null,null,....,null]//接口返回0-23点的数据,每一个小时一个间隔,没有的话则为null 修改后xy轴数据格式如下//每五分钟一......
  • echarts折线组件
    echarts折线组件echarts折线组件<template><divclass="lineChartsTemplate":id="chartsId"></div></template><script>exportdefault{name:"lineChartsTemplate",components:{},props:{......
  • echarts设置tooltip遇到值为0不展示的问题(已解决)
    echarts设置tooltip遇到值为0不展示的问题(已解决)遇到值为0时tooltip:{trigger:"axis",extraCssText:"z-index:3",axisPointer:{type:"shadow",//默认为直线,可选为:'line'|'shadow'......
  • echarts设置tooltip的层级
    echarts设置tooltip的层级tooltip:{trigger:"axis",extraCssText:'z-index:3',//修改层级borderColor:"rgba(0,170,255)",},完整的option示例如下:option={tooltip:{trig......