最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解vue文件中使用echarts.js的兩種方式

 更新時(shí)間:2018年10月18日 11:18:33   作者:Nanana  
這篇文章主要介紹了詳解vue文件中使用echarts.js的兩種方式,主要介紹了兩種使用方式,一種是以組件的形式另一種直接引入,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

最近工作中需要用到echarts,由于項(xiàng)目是用的vue-cli開發(fā)的。在網(wǎng)上搜到vue中合成了vue-echarts,但是不想使用vue中規(guī)定好的數(shù)據(jù)格式,于是就自己做了一個(gè)vue項(xiàng)目引用原生echarts的簡(jiǎn)單demo,實(shí)現(xiàn)過程如下:用了兩種實(shí)現(xiàn)方式

準(zhǔn)備工作

1、安裝echarts依賴

控制臺(tái)輸入:npm install echarts --save

2、全局引入

main.js中引入

import echarts from 'echarts'
Vue.prototype.$echarts = echarts

創(chuàng)建圖表

第一種創(chuàng)建方式

在一個(gè).vue文件中引入多張圖表

創(chuàng)建WelcomePage.vue

<template>
 <div>
 <h1>第一種在vue中使用echart的方式</h1>

 <div class="charts">
  <div id="barGraph" style="height: 350px;"></div>
 </div>

 <div class="charts">
  <div id="pieGraph" style="height: 350px;"></div>
 </div>

 </div>
</template>
<script>
// 引入基本模板,按需加載
 let echarts = require('echarts/lib/echarts');
 // 引入柱狀圖
 require('echarts/lib/chart/bar');
 // 引入柱狀圖
 require('echarts/lib/chart/pie');
 require('echarts/lib/component/tooltip');
 require('echarts/lib/component/title');


export default {
 name: "WelcomePage",
 data () {
 return { }
 },
 mounted(){
 this.drawBar();
 this.drawPie();
 },
 methods:{
 drawBar(){
  // 基于dom,初始化echarts實(shí)例
  let barGraph = echarts.init(document.getElementById('barGraph'));
  // 繪制圖表
  barGraph.setOption({
  title: {
   text: '全年產(chǎn)量趨勢(shì)圖',
   left: 'center'
  },
  tooltip: {
   trigger: 'item',
   formatter: '{a} <br/> : {c}'
  },
  legend: {
   left: 'center',
   data: ['本年', '上年'],
   bottom:0
  },
  xAxis: {
   type: 'category',
   name: 'x',
   splitLine: {show: false},
   data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
  },
  grid: {
   left: '1%',
   right: '2%',
   bottom: '8%',
   containLabel: true
  },
  yAxis: {
   type: 'category',
   name: 'y',
   splitLine: {show: true},
   data:['10%','20%','30%','40%','50%','60%','70%','80%','90%','100%']
  },
  series: [
   {
   name: '本年',
   type: 'line',
   data: [0.8, 0.98, 0.96, 0.27, 0.81, 0.47, 0.74, 0.23, .69, 0.25, 0.36, 0.56]
   },
   {
   name: '上年',
   type: 'line',
   data: [1, 0.2, 0.4, 0.8, 0.16, 0.32, 0.64, 1.28, 5.6, 0.25, 0.63, 0.65, 0.12]
   },
  ]
  })
 },
 drawPie(){
  let pieGraph = echarts.init(document.getElementById('pieGraph'));
  pieGraph.setOption({
  title : {
   text: '某站點(diǎn)用戶訪問來源',
   subtext: '純屬虛構(gòu)',
   x:'center'
  },
  tooltip : {
   trigger: 'item',
   formatter: "{a} <br/> : {c} (wppm3vysvbp%)"
  },
  legend: {
   orient: 'vertical',
   left: 'left',
   data: ['直接訪問','郵件營銷','聯(lián)盟廣告','視頻廣告','搜索引擎']
  },
  series : [
   {
   name: '訪問來源',
   type: 'pie',
   radius : '55%',
   center: ['50%', '60%'],
   data:[
    {value:335, name:'直接訪問'},
    {value:310, name:'郵件營銷'},
    {value:234, name:'聯(lián)盟廣告'},
    {value:135, name:'視頻廣告'},
    {value:1548, name:'搜索引擎'}
   ],
   itemStyle: {
    emphasis: {
    shadowBlur: 10,
    shadowOffsetX: 0,
    shadowColor: 'rgba(0, 0, 0, 0.5)'
    }
   }
   }
  ]
  })
 }
 }
}
</script>

實(shí)現(xiàn)效果如下圖:

第二種實(shí)現(xiàn)方式(以組件的形式)

創(chuàng)建父組件 father.vue

 <div>
 <h1>{{ msg }}</h1>
 <p>第二種方式:通過組件的方式進(jìn)行頁面渲染</p>
 <div class="container" >
  <bar-graph></bar-graph>
 </div>

 <div class="container">
  <pie-graph></pie-graph>
 </div>

 </div>
<script>
// 引入兩個(gè)子組件
 import BarGraph from "./bargraph";
 import PieGraph from "./piegraph";
 export default {
 name: "father",
 components:{
  BarGraph,
  PieGraph,
 },
 data(){
  return{
  msg: '我是爸爸,想看我的兒子,眼睛請(qǐng)往下移',
  }
 }
 }
</script>

創(chuàng)建子組件barGraph.vue

 <div>
 <p>{{ msg }}</p>
 <div class="charts">
  <div :id="id" style="min-height: 350px;"></div>
 </div>
 </div>
<script>
 let echarts = require('echarts/lib/echarts');
 // 引入柱狀圖
 require('echarts/lib/chart/bar');
 require('echarts/lib/component/tooltip');
 require('echarts/lib/component/title');

 // import echarts from 'echarts'

 export default {
  name: "bargraph",
  // props:['id'], // 第一種接收父親傳過來的值的方式
  props: {
  id: {
   type: String,
   default: 'chart'
  }
  },
  data(){
   return {
   msg: "我是第一個(gè)子組件--bar",
   chart: null,
   }
  },
  mounted(){
  this.drawBar();
  },
  methods:{
  drawBar(){
   this.chart = echarts.init(document.getElementById(this.id));
   let colors = ['#5793f3', '#d14a61', '#675bba'];
   this.chart.setOption(
   {
    color: colors,

    tooltip: {
    trigger: 'axis',
    axisPointer: {
     type: 'cross'
    }
    },
    grid: {
    right: '20%'
    },
    toolbox: {
    feature: {
     dataView: {show: true, readOnly: false},
     restore: {show: true},
     saveAsImage: {show: true}
    }
    },
    legend: {
    data:['蒸發(fā)量','降水量','平均溫度']
    },
    xAxis: [
    {
     type: 'category',
     axisTick: {
     alignWithLabel: true
     },
     data: ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']
    }
    ],
    yAxis: [
    {
     type: 'value',
     name: '蒸發(fā)量',
     min: 0,
     max: 250,
     position: 'right',
     axisLine: {
     lineStyle: {
      color: colors[0]
     }
     },
     axisLabel: {
     formatter: '{value} ml'
     }
    },
    {
     type: 'value',
     name: '降水量',
     min: 0,
     max: 250,
     position: 'right',
     offset: 80,
     axisLine: {
     lineStyle: {
      color: colors[1]
     }
     },
     axisLabel: {
     formatter: '{value} ml'
     }
    },
    {
     type: 'value',
     name: '溫度',
     min: 0,
     max: 25,
     position: 'left',
     axisLine: {
     lineStyle: {
      color: colors[2]
     }
     },
     axisLabel: {
     formatter: '{value} °C'
     }
    }
    ],
    series: [
    {
     name:'蒸發(fā)量',
     type:'bar',
     data:[2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
    },
    {
     name:'降水量',
     type:'bar',
     yAxisIndex: 1,
     data:[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
    },
    {
     name:'平均溫度',
     type:'line',
     yAxisIndex: 2,
     data:[2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]
    }
    ]
   }
   )
  }
  }
 }
</script>

創(chuàng)建pieGraph.vue

<template>
 <div>
  <p>{{ msg }}</p>

  <div class="charts">
  <div :id="id" style="min-height: 350px;"></div>
  </div>
 </div>
</template>
<script>
 import echarts from 'echarts'

 export default {
  name: "piegraph",
  props:{
  id: {
   type: String,
   default: 'pieChart'
  }
  },
  data(){
   return{
   msg: '我是第二個(gè)子組件--pie',
   pieChart: null
   }
  },
  mounted(){
   this.drawPie();
  },
  methods: {
  drawPie () {
   this.pieChart = echarts.init(document.getElementById(this.id));
   this.pieChart.setOption(
   {
    title : {
    text: '某站點(diǎn)用戶訪問來源',
    subtext: '純屬虛構(gòu)',
    x:'center'
    },
    tooltip : {
    trigger: 'item',
    formatter: "{a} <br/> : {c} (wppm3vysvbp%)"
    },
    legend: {
    orient: 'vertical',
    left: 'left',
    data: ['直接訪問','郵件營銷','聯(lián)盟廣告','視頻廣告','搜索引擎']
    },
    series : [
    {
     name: '訪問來源',
     type: 'pie',
     radius : '55%',
     center: ['50%', '60%'],
     data:[
     {value:335, name:'直接訪問'},
     {value:310, name:'郵件營銷'},
     {value:234, name:'聯(lián)盟廣告'},
     {value:135, name:'視頻廣告'},
     {value:1548, name:'搜索引擎'}
     ],
     itemStyle: {
     emphasis: {
      shadowBlur: 10,
      shadowOffsetX: 0,
      shadowColor: 'rgba(0, 0, 0, 0.5)'
     }
     }
    }
    ]
   }
   )
  }
  }
 }
</script>

效果實(shí)現(xiàn)如下:

路由文件如下:

import WelcomePage from '@/components/WelcomePage'
import Father from '@/components/father'

import BarGraph from '@/components/bargraph'
import PieGraph from '@/components/piegraph'

export default new Router({
 routes: [
 {
  path: '/',
  name: 'WelcomePage',
  component: WelcomePage
 },
 {
  path: '/father',
  name: 'father',
  component: Father,
  children:[
  {
   path: '/bargraph',
   name: 'bargraph',
   component: BarGraph
  },
  {
   path: '/piegraph',
   name: 'piegraph',
   component: PieGraph
  }
  ]
 },
 ]
})

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

相關(guān)文章

  • VUE中的自定義指令鉤子函數(shù)講解

    VUE中的自定義指令鉤子函數(shù)講解

    這篇文章主要介紹了VUE中的自定義指令鉤子函數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • vue實(shí)現(xiàn)頁面添加水印

    vue實(shí)現(xiàn)頁面添加水印

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)頁面添加水印功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • Vue?props傳入function時(shí)的this指向問題解讀

    Vue?props傳入function時(shí)的this指向問題解讀

    這篇文章主要介紹了Vue?props傳入function時(shí)的this指向問題解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 解決Mint-ui 框架Popup和Datetime Picker組件滾動(dòng)穿透的問題

    解決Mint-ui 框架Popup和Datetime Picker組件滾動(dòng)穿透的問題

    這篇文章主要介紹了解決Mint-ui 框架Popup和Datetime Picker組件滾動(dòng)穿透的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • 手寫Vue源碼之?dāng)?shù)據(jù)劫持示例詳解

    手寫Vue源碼之?dāng)?shù)據(jù)劫持示例詳解

    這篇文章主要給大家介紹了手寫Vue源碼之?dāng)?shù)據(jù)劫持的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Vue3中使用fetch實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求的過程詳解

    Vue3中使用fetch實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求的過程詳解

    在現(xiàn)代前端開發(fā)中,數(shù)據(jù)請(qǐng)求是一個(gè)不可或缺的環(huán)節(jié),而在Vue3中,我們有許多方法可以進(jìn)行數(shù)據(jù)請(qǐng)求,其中使用fetch方法是一個(gè)非常常見的選擇,本文將詳細(xì)講解如何在Vue3中使用fetch來實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求,需要的朋友可以參考下
    2024-09-09
  • 在vue-cli腳手架中配置一個(gè)vue-router前端路由

    在vue-cli腳手架中配置一個(gè)vue-router前端路由

    這篇文章主要給大家介紹了在vue-cli腳手架中配置一個(gè)vue-router前端路由的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-07-07
  • 詳解如何使用Vuex實(shí)現(xiàn)Vue后臺(tái)管理中的角色鑒權(quán)

    詳解如何使用Vuex實(shí)現(xiàn)Vue后臺(tái)管理中的角色鑒權(quán)

    最近參與了公司一個(gè)新的B端項(xiàng)目的研發(fā),從無到有搭建項(xiàng)目的過程中,遇到了關(guān)于項(xiàng)目鑒權(quán)的問題,這篇文章主要給大家介紹了關(guān)于如何使用Vuex實(shí)現(xiàn)Vue后臺(tái)管理中的角色鑒權(quán)的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • vue鼠標(biāo)移入添加class樣式,鼠標(biāo)移出去除樣式(active)實(shí)現(xiàn)方法

    vue鼠標(biāo)移入添加class樣式,鼠標(biāo)移出去除樣式(active)實(shí)現(xiàn)方法

    今天小編就為大家分享一篇vue鼠標(biāo)移入添加class樣式,鼠標(biāo)移出去除樣式(active)實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • vue3實(shí)現(xiàn)無縫滾動(dòng)列表效果(大屏數(shù)據(jù)輪播場(chǎng)景)

    vue3實(shí)現(xiàn)無縫滾動(dòng)列表效果(大屏數(shù)據(jù)輪播場(chǎng)景)

    vue3-scroll-seamless 是一個(gè)用于 Vue 3 的插件,用于實(shí)現(xiàn)無縫滾動(dòng)的組件,它可以讓內(nèi)容在水平或垂直方向上無縫滾動(dòng),適用于展示輪播圖、新聞滾動(dòng)、圖片展示等場(chǎng)景,本文就給大家介紹了vue3實(shí)現(xiàn)無縫滾動(dòng)列表效果,需要的朋友可以參考下
    2024-07-07

最新評(píng)論

蛟河市| 河东区| 伊宁市| 平果县| 岱山县| 齐河县| 康保县| 平果县| 托克逊县| 徐水县| 碌曲县| 万载县| 金门县| 仲巴县| 资兴市| 武功县| 泾川县| 遵化市| 开鲁县| 孙吴县| 怀柔区| 永康市| 肥东县| 临沂市| 县级市| 岗巴县| 修水县| 榆树市| 慈利县| 申扎县| 江口县| 阿图什市| 普定县| 嵊州市| 南澳县| 武平县| 元朗区| 云安县| 鄱阳县| 栖霞市| 乳源|