欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue+vuex+axios+echarts畫(huà)一個(gè)動(dòng)態(tài)更新的中國(guó)地圖的方法

 更新時(shí)間:2017年12月19日 16:18:24   作者:后除  
本篇文章主要介紹了vue+vuex+axios+echarts畫(huà)一個(gè)動(dòng)態(tài)更新的中國(guó)地圖的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文介紹了vue+vuex+axios+echarts畫(huà)一個(gè)動(dòng)態(tài)更新的中國(guó)地圖的方法,分享給大家,具體如下:

一. 生成項(xiàng)目及安裝插件

# 安裝vue-cli
npm install vue-cli -g
# 初始化項(xiàng)目
vue init webpack china-map
# 切到目錄下
cd china-map
# 安裝項(xiàng)目依賴(lài)
npm install
# 安裝 vuex
npm install vuex --save
# 安裝 axios
npm install axios --save
# 安裝 ECharts
npm install echarts --save

二. 項(xiàng)目結(jié)構(gòu)

├── index.html
├── main.js
├── components
│  └── index.vue
└── store
  ├── index.js     # 組裝模塊及導(dǎo)出store的文件
  └── modules
    └── ChinaMap.js  # 中國(guó)地圖Vuex模塊

三. 引入中國(guó)地圖并繪制基本的圖表

1.按需求引入與中國(guó)地圖相關(guān)的Echarts圖表和組件。

// 主模塊
let echarts = require('echarts/lib/echarts')
// 散點(diǎn)圖
require('echarts/lib/chart/scatter')
// 散點(diǎn)圖放大
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.引入中國(guó)地圖JS文件,會(huì)自動(dòng)注冊(cè)地圖;也可以通過(guò)axios方式引入json文件,需要手動(dòng)注冊(cè)echarts.registerMap('china', chinaJson.data)。

// 中國(guó)地圖JS文件
require('echarts/map/js/china')

3.準(zhǔn)備一個(gè)有固定寬高的DOM容器并在mounted里面初始化一個(gè)echarts實(shí)例。

DOM容器

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

初始化echarts實(shí)例

let chinaMap = echarts.init(document.getElementById('china-map'))

4.設(shè)置初始化的空白地圖,這里需要設(shè)置很多echarts參數(shù),參考ECharts配置項(xiàng)手冊(cè)。

chinaMap.setOption({
  backgroundColor: '#272D3A',
  // 標(biāo)題
  title: {
   text: '中國(guó)地圖閃閃發(fā)光',
   left: 'center',
   textStyle: {
    color: '#fff'
   }
  },
  // 地圖上圓點(diǎn)的提示
  tooltip: {
   trigger: 'item',
   formatter: function (params) {
    return params.name + ' : ' + params.value[2]
   }
  },
  // 圖例按鈕 點(diǎn)擊可選擇哪些不顯示
  legend: {
   orient: 'vertical',
   left: 'left',
   top: 'bottom',
   data: ['地區(qū)熱度', 'top5'],
   textStyle: {
    color: '#fff'
   }
  },
  // 地理坐標(biāo)系組件
  geo: {
   map: 'china',
   label: {
    // true會(huì)顯示城市名
    emphasis: {
     show: false
    }
   },
   itemStyle: {
    // 地圖背景色
    normal: {
     areaColor: '#465471',
     borderColor: '#282F3C'
    },
    // 懸浮時(shí)
    emphasis: {
     areaColor: '#8796B4'
    }
   }
  },
  // 系列列表
  series: [
   {
    name: '地區(qū)熱度',
    // 表的類(lèi)型 這里是散點(diǎn)
    type: 'scatter',
    // 使用地理坐標(biāo)系,通過(guò) geoIndex 指定相應(yīng)的地理坐標(biāo)系組件
    coordinateSystem: 'geo',
    data: [],
    // 標(biāo)記的大小
    symbolSize: 12,
    // 鼠標(biāo)懸浮的時(shí)候在圓點(diǎn)上顯示數(shù)值
    label: {
     normal: {
      show: false
     },
     emphasis: {
      show: false
     }
    },
    itemStyle: {
     normal: {
      color: '#ddb926'
     },
     // 鼠標(biāo)懸浮的時(shí)候圓點(diǎn)樣式變化
     emphasis: {
      borderColor: '#fff',
      borderWidth: 1
     }
    }
   },
   {
    name: 'top5',
    // 表的類(lèi)型 這里是散點(diǎn)
    type: 'effectScatter',
    // 使用地理坐標(biāo)系,通過(guò) geoIndex 指定相應(yīng)的地理坐標(biāo)系組件
    coordinateSystem: 'geo',
    data: [],
    // 標(biāo)記的大小
    symbolSize: 12,
    showEffectOn: 'render',
    rippleEffect: {
     brushType: 'stroke'
    },
    hoverAnimation: true,
    label: {
     normal: {
      show: false
     }
    },
    itemStyle: {
     normal: {
      color: '#f4e925',
      shadowBlur: 10,
      shadowColor: '#333'
     }
    },
    zlevel: 1
   }
  ]
 })

四. 配置Vuex管理和分發(fā)數(shù)據(jù)

1.在ChinaMap.js中引入vuex和axios。

import axios from 'axios'

2.設(shè)置必要的變量。

const state = {
 geoCoordMap: {'香港特別行政區(qū)': [114.08, 22.2], '澳門(mén)特別行政區(qū)': [113.33, 22.13], '臺(tái)北': [121.5, 25.03]/*等等*/},
 // 發(fā)光的城市
 showCityNumber: 5,
 showCount: 0,
 // 是否需要loading
 isLoading: true
}

3.在actions中抓取后臺(tái)數(shù)據(jù)并更新地圖。

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: '地區(qū)熱度',
        data: paleData
       },
       {
        name: 'top5',
        data: lightData
       }
      ]
     })
    }
   )
 }
}

此時(shí)npm run dev已經(jīng)可以看到中國(guó)地圖上閃閃的黃色小點(diǎn)點(diǎn)。

若想改變她使動(dòng)態(tài)展示,可以在index.vue中mounted下面加上:

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

在ChinaMap.js中actions的mutations中fetchHeatChinaRealData修改:

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')
}

五. 其它

1.別忘了在main.js里面引入Vuex。

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.案例代碼

GitHub

3.效果圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論