基于vue、vuex、vue-router、echarts搭建的数据展现平台

真的很久没有更新博客了,可是我最近并无偷懒哦,一直在学习vue这个框架,而且用它作了一个小项目,如今就给你们分享一下个人这个还比较有意思的小项目咯,本项目是基于vue2.0开发的。html

灵感来源

这是一个数据可视化相关的项目,做为一个学生班主任,须要对班上同窗的各方面的状况都有所了解,因而我便作了一个问卷调查来了解学生各方面的状况。而后我又想到能够分析咱们班的群聊记录呀,根据群聊记录能够获得班上同窗之间的亲密度和班级群聊活跃度等信息。天然而然,我就想着能够搭建一个平台来展现这些数据,既然是数据固然是以图表的方式来展现更加直观,而后折中选择了echarts这个图标库。至于为何要选择用vue这个插件,以前只是以为学了vue能够练练手,作完以后发现vue真的很轻量也很好上手,结合vuex和vue-router基本能完成我项目中的全部需求。vue

在线展现:http://119.29.57.165:8080/family
源码:https://github.com/hieeyh/tong2-family
本教程是基于你已经有必定vue基础之上的,若是你还不了解什么是vue建议先去学习一下node

项目初始构建

首先全局安装vue-cli,vue-cli是vue本身的项目构建工具,几个简单的步骤就能够帮助你快速构建一个vue项目。webpack

npm install -g vue-cli

而后,利用vue-cli构建一个vue项目git

# 建立一个基于 "webpack" 模板的新项目
$ vue init webpack family
# 安装依赖
$ cd family
$ npm install

项目文件解释

  • build中是webpack基本配置文件,开发环境配置文件,生产环节配置文件github

  • node_modules是各类依赖模块web

  • src中是vue组件及入口文件vue-router

  • static中放置静态文件vuex

  • index.html是页面入口文件vue-cli

基本页面实现

项目建立好以后,就开始实现本身想要的页面了,修改src文件夹下的App.vue文件,以下:

<template>
<div id="#app">
<!-- 导航栏 -->
  <my-head></my-head>
  <my-nav></my-nav>
  <transition>
    <router-view></router-view>
  </transition>
  <my-foot></my-foot>
</div>
</template>

<script>
import myHead from './components/header'
import myNav from './components/nav'
import myFoot from './components/foot'

export default {
  name: 'app',
  components: {
    myHead,
    myNav,
    myFoot
  }
}
</script>

myHead组件是页面头部,myNav组件是页面左侧导航栏,myFoot是页面底部,router-view组件是vue-router中渲染路径匹配到的视图组件。每一个组件的具体实现能够去github项目地址去看源码。

建立配置路由

显然,我要作的是一个单页面应用,要用到vue-router,先安装vue-router,输入以下命令:

npm install --save vue-router

而后,在src文件夹下面的main.js文件中添加路由相关的代码,以下:

import Vue from 'vue'
import App from './App'
import VueRouter from 'vue-router'

Vue.use(VueRouter) 
// 定义路由组件
const Worldcloud = require('./components/cloud.vue')
const Building = require('./components/building.vue')
const Canteen = require('./components/canteen.vue')
const Selfstudy = require('./components/selfstudy.vue')
const Difficult = require('./components/difficult.vue')
const Interest = require('./components/interest.vue')
const Bedroom = require('./components/bedroom.vue')
const Graduate = require('./components/graduate.vue')
const Getup = require('./components/getup.vue')
const Gotobed = require('./components/gotobed.vue')
const Eat = require('./components/eat.vue')
const Amuse = require('./components/amuse.vue')
const Single = require('./components/single.vue')
const Chat = require('./components/chat.vue')
const Onlyme = require('./components/onlyme.vue')

// 定义路由,配置路由映射
const routes = [
  { path: '/', redirect: '/wordcloud' },
  { path: '/wordcloud', component: Worldcloud },
  { path: '/building', component: Building },
  { path: '/canteen', component: Canteen },
  { path: '/selfstudy', component: Selfstudy },
  { path: '/difficult', component: Difficult },
  { path: '/interest', component: Interest },
  { path: '/bedroom', component: Bedroom },
  { path: '/graduate', component: Graduate },
  { path: '/getup', component: Getup },
  { path: '/gotobed', component: Gotobed },
  { path: '/eat', component: Eat },
  { path: '/amuse', component: Amuse },
  { path: '/single', component: Single },
  { path: '/chat', component: Chat },
  { path: '/onlyme', component: Onlyme }
]

new Vue({
  el: '#app',
  template: '<App/>',
  components: { App },
  router
})

从路由映射的配置中能够看出,访问网站的根路由会直接跳转到/wordcloud。路由映射的组件中用到了百度的echarts库,这是一个很好用的图表库。

怎么画图

怎么用echarts画图呢?其实官网上有不少实例,下面以bedroom.vue组件为例来简单说明,bedroom.vue代码以下:

<template>
  <div class="main_content">
    <div id="bedroom"></div>
  </div>
</template>
<script>
  import echarts from 'echarts'
  export default {
    data() {
      return {
        chart: null,
        opinion: ['学习氛围差', '学习氛围通常', '学习氛围很好'],
        opinionData: [
          {value:26, name:'学习氛围差'},
          {value:31, name:'学习氛围通常'},
          {value:8, name:'学习氛围很好'}
        ]
      }
    },
    methods: {
      drawPie (id) {
        this.chart = echarts.init(document.getElementById(id))
        this.chart.setOption({
          title: {
            text: '寝室学习氛围状况调查',
            left: 'center',
            top: 10,
            textStyle: {
              fontSize: 24,
              fontFamily: 'Helvetica',
              fontWeight: 400
            }
          },
          tooltip: {
            trigger: 'item',
            formatte: "{b}: {c} ({d}%)"
          },
          toolbox: {
            feature: {
              saveAsImage: {},
              dataView: {}
            },
            right: 15,
            top: 10
          },
          legend: {
              orient: 'vertical',
              left: 5,
              top: 10,
              data: this.opinion,
          },
          series: [
            {
              name: '寝室学习氛围',
              type: 'pie',
              radius: ['50%', '70%'],
              center: ['50%', '60%'],
              avoidLabelOverlap: false,
              label: {
                emphasis: {
                  show: true,
                  textStyle: {
                    fontSize: '24',
                    fontWeight: 'bold'
                  }
                }
              },
              labelLine: {
                normal: {
                  show: false
                }
              },
              data: this.opinionData,
              itemStyle: {
                emphasis: {
                  shadowBlur: 10,
                  shadowOffset: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              }
            }
          ]
        })
      }
    },
    mounted() {
      this.$nextTick(function() {
        this.drawPie('bedroom')
      })
    }
  }
</script>
<style scoped>
#bedroom {
  position: relative;
  left: 50%;
  margin-left: -400px;
  margin-bottom: 70px;
  width: 800px;
  height: 600px;
  border: solid #D01257 1px;
  box-shadow: 0 0 8px #FB90B7;
  border-radius: 10px;
}   
</style>

这是一个vue的单文件组件,script中,首先导入echarts库,前提是已经安装了echarts库,输入如下命令安装:

npm install --save echarts

data对象中是画图要用到的一些数据,drawpie方法用来画图,接收一个DOM对象,而后在mounted构子函数中调用drawpie便可。

两点说明

  1. drawpie方法接收的DOM对象须要有肯定的宽高,不然图像不显示

  2. mounted中要包含vm.$nextTick才能保证该实例已经插入文档

实现登陆功能

登陆功能基于vuex(vue状态管理)和浏览器的sessionStorage实现的。首先在src文件夹下新建store文件夹,存放vuex的store(仓库),新建三个文件store.js、login.js、user.js。login.js中存储登陆状态,user.js中存储用户登陆信息,store.js加载login和user模块。

注意:在store.js中要引入babel-polyfill(先安装),不然会报错,报错缘由是Babel默认只转换新的JavaScript句法,而不转换新的API,好比Iterator、Generator、Set、Maps、Proxy、Reflect、Symbol、Promise等全局对象,以及一些定义在全局对象上的方法都不会转码。因此必须使用babel-polyfill,为当前环境提供一个垫片。

而后修改main.js文件,引入store:

import store from './store/store'
...
...
new Vue({
  el: '#app',
  template: '<App/>',
  components: { App },
  router,
  store
})

修改App.vue文件,以下:

<template>
<div id="#app">
<!-- 导航栏 -->
  <my-head></my-head>
  <my-nav></my-nav>
  <transition>
    <router-view></router-view>
  </transition>
  <my-mask v-if="canlogin"></my-mask>
  <my-login v-if="canlogin"></my-login>
  <my-foot></my-foot>
</div>
</template>
<script>
...
import myMask from './components/mask'
import myLogin from './components/login'

export default {
  ...
  data() {
    return {
      canlogin: false
    }
  },
  computed: {
    canlogin() {
      return this.$store.state.login.islogin
    }
  }
}
</script>

到此,就基本上大功告成了,在命令行中输入 npm run dev预览一下。

项目发布

项目能够在本地预览了,可是要怎么发布到网上呢?首先,在命令行中输入

npm run build

会生成一个dist文件夹,该文件夹中就是咱们能够用来发布的代码,直接将代码上传到你的服务器上就能够啦。

相关文章
相关标签/搜索