Vue+vuex+axios+echarts draw a dynamic updated map of China

Source: Internet
Author: User

I. Build the project and install the plugin
# installation VUE-CLINPM Install vue-cli-g# Initialize Project Vue init webpack china-map# cut to directory CD china-map# Setup project dependent npm install# install VUEXNPM Insta ll Vuex--save# installing AXIOSNPM install Axios--save# installing ECHARTSNPM install Echarts--save
Two. Project structure
├──index.html├──main.js├──components│└──index.vue└──store├──index.js # assembly module and export store file └──module S└──chinamap.js # China Map Vuex Module
Three. Introduce the Chinese map and draw the basic chart

1. Introduce echarts charts and groups related to Chinese maps as required.

Main module Let Echarts = require (' echarts/lib/echarts ')//scatter plot require (' echarts/lib/chart/scatter ')//Scatter plot amplification require (' Echarts /lib/chart/effectscatter ')//Map require (' echarts/lib/chart/map ')//Legend require (' echarts/lib/component/legend ')// Cue box require (' Echarts/lib/component/tooltip ')//Map Georequire (' Echarts/lib/component/geo ')

2. The introduction of the Chinese map JS file will automatically register the map, you can also introduce the JSON file by Axios, you need to register manually echarts.registerMap('china', chinaJson.data) .

China map JS file require (' Echarts/map/js/china ')

3. Prepare a DOM container with a fixed width and height and initialize a Echarts instance inside the mounted.

Dom Container

<template> <div id= "China-map" ></div></template>

Initializing an Echarts instance

Let Chinamap = Echarts.init (document.getElementById (' China-map '))

4. Set a blank map for initialization, where you need to set many echarts parameters, refer to the Echarts configuration item manual.

Chinamap.setoption ({    backgroundcolor:  ' #272D3A ',    //  title     title: {      text:  ' China map Sparkling ',       left:  ' Center ',      textstyle: {         color:  ' #fff '       }     },    //  tips for dots on the map     tooltip: {       trigger:  ' Item ',      formatter: function  ( params)  {        return params.name +  '  :   '  + params.value[2]      }    },     //  Legend button   Click to select which does not show     legend: {       orient:  ' Vertical ',      left:  ' left ',      top:  ' bottom ',       data: [' regional heat ',  ' top5 '],       textstyle: {        color:  ' #fff '        }    },    //  geographic coordinate system components     geo:  {      map:  ' China ',       label: {         // true will show city name          emphasis: {          show: false         }      },       itemStyle: {        //  Map Background Color          normal:&nbsp {          areacolor:  ' #465471 ',           borderColor:  ' #282F3C '          },        //  Suspension          emphasis: {          areaColor:  ' # 8796b4 '         }      }     },    //  Series List     series: [       {        name:  ' Regional Heat ',     Type of     //  table   Here is scatter         type:  ' Scatter ',        //  use a geographic coordinate system to specify the corresponding geographic coordinate system components by  geoIndex          coordinateSystem:  ' Geo ',        data: [],         //  size of the tag          symbolsize: 12,        //  display values on dots when hovering over the mouse          label: {          normal:  {            show: false           },           emphasis: {            show: false           }        },         itemStyle: {           normal:  {            color:  ' #ddb926 '            },             DOT style changes when hovering over the mouse           emphasis: {             borderColor:  ' #fff ',             borderWidth: 1           }        }      },       {        name:  ' TOP5 ',  Type of        //  table   Here is the scatter point          type:  ' Effectscatter ',        //  uses a geographic coordinate system, through   geoindex  specifies the corresponding geographic coordinate system component &NBsp;       coordinatesystem:  ' Geo ',         data: [],        //  size of the tag          symbolsize: 12,        showeffecton :  ' Render ',        rippleeffect: {           brushType:  ' stroke '         },         hoverAnimation: true,         label: {          normal: {             show: false           }        },         itemstyle: {          normal: {             color:  ' #f4e925 ',             shadowBlur: 10,             shadowColor:  ' #333 '           }         },        zlevel:  1      }    ]  })
Four. Configure VUEX to manage and distribute data

1. Introduction of Vuex and Axios in Chinamap.js.

Import Axios from ' Axios '

2. Set the necessary variables.

Const STATE = {geocoordmap: {' Hong Kong SAR ': [114.08, 22.2], ' Macau SAR ': [113.33, 22.13], ' Taipei ': [121.5, 25.03]/*, etc. */},//Glowing City Showcitynumber:5, showcount:0,//Need loading Isloading:true}

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

const actions = {  fetchheatchinarealdata  ({state, commit}, chartsobj)  {    axios.get (' Static/data/heatchinarealdata.json ')        .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:  ' Regional Heat ',                 data: paleData               },               {                 name:  'Top5 ',                 data:  lightData              }             ]           })         }      )   }}

At this time npm run dev can already see the Chinese map of the shiny xxx dot dot. If you want to change her to make a dynamic display, you can add the following in Index.vue mounted:

Chinamap.showloading (Showloadingdefault) this. $store. Commit (' openloading ') this. $store. Dispatch (' Fetchheatchinarealdata ', Chinamap) setinterval (() = {this. $store. Dispatch (' Fetchheatchinarealdata ', Chinamap)}, 1000)

Fetchheatchinarealdata changes 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 (' closeloading ')}
Five. Other

1. Don't forget to introduce Vuex into the main.js.

Import vue from "Vue" import Index from "./components/index.vue ' Import store from './store/index ' let Chinamap = new Vue ({ El: ' #app ', store, Template: ' <Index/> ', components: {Index}}) Vue.use (Chinamap)

2. Case code

GitHub

Vue+vuex+axios+echarts draw a dynamic updated map of China

Vue+vuex+axios+echarts draw a dynamic updated map of China

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.