search
HomeWeb Front-endJS TutorialHow to implement China map in vue+vuex+axios+echarts

How to implement China map in vue+vuex+axios+echarts

Jun 19, 2018 pm 04:53 PM
echartsvuedynamic

This article mainly introduces the method of using vue vuex axios echarts to draw a dynamically updated map of China. It has certain reference value. Interested friends can refer to it.

This article introduces vue vuex axios I would like to share with you how echarts draws a dynamically updated map of China. The details are as follows:

1. Generate the project and install the plug-in

# 安装vue-cli
npm install vue-cli -g
# 初始化项目
vue init webpack china-map
# 切到目录下
cd china-map
# 安装项目依赖
npm install
# 安装 vuex
npm install vuex --save
# 安装 axios
npm install axios --save
# 安装 ECharts
npm install echarts --save

2. Project Structure

├── index.html
├── main.js
├── components
│  └── index.vue
└── store
  ├── index.js     # 组装模块及导出store的文件
  └── modules
    └── ChinaMap.js  # 中国地图Vuex模块

3. Introduce China map and draw basic charts

1.Introduce Echarts charts and components related to China map as required.

// 主模块
let echarts = require('echarts/lib/echarts')
// 散点图
require('echarts/lib/chart/scatter')
// 散点图放大
require('echarts/lib/chart/effectScatter')
// 地图
require('echarts/lib/chart/map')
// 图例
require('echarts/lib/component/legend')
// 提示框
require('echarts/lib/component/tooltip')
// 地图geo
require('echarts/lib/component/geo')

2. Introducing the China map JS file will automatically register the map; you can also introduce the json file through axios, and you need to manually register echarts.registerMap('china', chinaJson.data).

// 中国地图JS文件
require('echarts/map/js/china')

3. Prepare a DOM container with fixed width and height and initialize an echarts instance in mounted.

DOM container

<template>
 <p id="china-map"></p>
</template>

Initialize echarts instance

let chinaMap = echarts.init(document.getElementById(&#39;china-map&#39;))

4. Set the initialized blank map. You need to set many echarts parameters here. Please refer to the ECharts configuration item manual.

chinaMap.setOption({
  backgroundColor: &#39;#272D3A&#39;,
  // 标题
  title: {
   text: &#39;中国地图闪闪发光&#39;,
   left: &#39;center&#39;,
   textStyle: {
    color: &#39;#fff&#39;
   }
  },
  // 地图上圆点的提示
  tooltip: {
   trigger: &#39;item&#39;,
   formatter: function (params) {
    return params.name + &#39; : &#39; + params.value[2]
   }
  },
  // 图例按钮 点击可选择哪些不显示
  legend: {
   orient: &#39;vertical&#39;,
   left: &#39;left&#39;,
   top: &#39;bottom&#39;,
   data: [&#39;地区热度&#39;, &#39;top5&#39;],
   textStyle: {
    color: &#39;#fff&#39;
   }
  },
  // 地理坐标系组件
  geo: {
   map: &#39;china&#39;,
   label: {
    // true会显示城市名
    emphasis: {
     show: false
    }
   },
   itemStyle: {
    // 地图背景色
    normal: {
     areaColor: &#39;#465471&#39;,
     borderColor: &#39;#282F3C&#39;
    },
    // 悬浮时
    emphasis: {
     areaColor: &#39;#8796B4&#39;
    }
   }
  },
  // 系列列表
  series: [
   {
    name: &#39;地区热度&#39;,
    // 表的类型 这里是散点
    type: &#39;scatter&#39;,
    // 使用地理坐标系,通过 geoIndex 指定相应的地理坐标系组件
    coordinateSystem: &#39;geo&#39;,
    data: [],
    // 标记的大小
    symbolSize: 12,
    // 鼠标悬浮的时候在圆点上显示数值
    label: {
     normal: {
      show: false
     },
     emphasis: {
      show: false
     }
    },
    itemStyle: {
     normal: {
      color: &#39;#ddb926&#39;
     },
     // 鼠标悬浮的时候圆点样式变化
     emphasis: {
      borderColor: &#39;#fff&#39;,
      borderWidth: 1
     }
    }
   },
   {
    name: &#39;top5&#39;,
    // 表的类型 这里是散点
    type: &#39;effectScatter&#39;,
    // 使用地理坐标系,通过 geoIndex 指定相应的地理坐标系组件
    coordinateSystem: &#39;geo&#39;,
    data: [],
    // 标记的大小
    symbolSize: 12,
    showEffectOn: &#39;render&#39;,
    rippleEffect: {
     brushType: &#39;stroke&#39;
    },
    hoverAnimation: true,
    label: {
     normal: {
      show: false
     }
    },
    itemStyle: {
     normal: {
      color: &#39;#f4e925&#39;,
      shadowBlur: 10,
      shadowColor: &#39;#333&#39;
     }
    },
    zlevel: 1
   }
  ]
 })

4. Configure Vuex management and distribution data

1. Introduce vuex and axios into ChinaMap.js.

import axios from &#39;axios&#39;

2. Set the necessary variables.

const state = {
 geoCoordMap: {&#39;香港特别行政区&#39;: [114.08, 22.2], &#39;澳门特别行政区&#39;: [113.33, 22.13], &#39;台北&#39;: [121.5, 25.03]/*等等*/},
 // 发光的城市
 showCityNumber: 5,
 showCount: 0,
 // 是否需要loading
 isLoading: true
}

3. Capture background data in actions and update the map.

const actions = {
 fetchHeatChinaRealData ({state, commit}, chartsObj) {
  axios.get(&#39;static/data/heatChinaRealData.json&#39;)
   .then(
    (res) => {
     let data = res.data
     let paleData = ((state, data) => {
      let arr = []
      let len = data.length
      while (len--) {
       let geoCoord = state.geoCoordMap[data[len].name]
       if (geoCoord) {
        arr.push({
         name: data[len].name,
         value: geoCoord.concat(data[len].value)
        })
       }
      }
      return arr
     })(state, data)
     let lightData = paleData.sort((a, b) => {
      return b.value - a.value
     }).slice(0, state.showCityNumber)
     chartsObj.setOption({
      series: [
       {
        name: &#39;地区热度&#39;,
        data: paleData
       },
       {
        name: &#39;top5&#39;,
        data: lightData
       }
      ]
     })
    }
   )
 }
}

At this time npm run dev can already see the flashing yellow dots on the map of China.

If you want to change it to display dynamically, you can add under mounted in index.vue:

chinaMap.showLoading(showLoadingDefault)
this.$store.commit(&#39;openLoading&#39;)
this.$store.dispatch(&#39;fetchHeatChinaRealData&#39;, chinaMap)
setInterval(() => {
  this.$store.dispatch(&#39;fetchHeatChinaRealData&#39;, chinaMap)
}, 1000)

Modify fetchHeatChinaRealData in the mutations of actions in ChinaMap.js:

let lightData = paleData.sort((a, b) => {
  return b.value - a.value
}).slice(0 + state.showCount, state.showCityNumber + state.showCount)
if (state.isLoading) {
  chartsObj.hideLoading()
  commit(&#39;closeLoading&#39;)
}

5. Others

1. Don’t forget to introduce Vuex into main.js.

import Vue from &#39;vue&#39;
import Index from &#39;./components/index.vue&#39;
import store from &#39;./store/index&#39;

let ChinaMap = new Vue({
 el: &#39;#app&#39;,
 store,
 template: &#39;<Index/>&#39;,
 components: {Index}
})

Vue.use(ChinaMap)

2. Case code

GitHub

3. Effect picture

The above is what I compiled for everyone , I hope it will be helpful to everyone in the future.

Related articles:

How to control the mouse to refuse to click the button in JS

How to implement floating collision in JS

About using bootstrap-table.js to implement extended paging toolbar function

About the difference between substring() and substr() (detailed tutorial)

How to determine whether a variable name exists in an array in JavaScript

The above is the detailed content of How to implement China map in vue+vuex+axios+echarts. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C   and JavaScript: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.