如何在vue 中使用柱狀圖 并自修改配置
1.在html文件導(dǎo)入echart
<!-- 引入echarts --> <script src="https://cdn.bootcdn.net/ajax/libs/echarts/4.8.0/echarts.min.js"></script>
2.在main.js上掛載echarts對(duì)象
Vue.prototype.$echarts = window.echarts // 使用時(shí)直接使用this.$echarts
3.頁(yè)面結(jié)構(gòu)
<template> <div class="com-container"> <div class="com-chart" ref="sellerRef"></div> </div> </template>
4.data中的數(shù)據(jù)
export default {
data () {
return {
// 初始化的圖表
chartInstance: null,
allDate: null, // 服務(wù)器返回的數(shù)據(jù)
}
},
}
```js
##### 5.methods中的邏輯
```js
methods: {
// 初始化echarts對(duì)象
initEchart(){
// 獲取dom對(duì)象
this.chartInstance = this.$echarts.init(this.$refs.sellerRef)
},
// 獲取服務(wù)器的數(shù)據(jù)
async getData(){
const {data:res} = await this.$http.get('seller')
this.allDate = res
// 返會(huì)的數(shù)據(jù)結(jié)構(gòu)是 name商家 value數(shù)值
// 對(duì)返回的數(shù)據(jù)進(jìn)行從小打到排序 sort方法
this.allDate.sort((a, b) => {
return a.value - b.value
})
// 調(diào)用更新視方法
this.updateChart()
},
// 更新圖表
updateChart(){
// y軸類目軸的數(shù)據(jù)
const sellerNames = this.allDate.map(item=>{
// 根據(jù)你的需求調(diào)整
return item.name
})
// x軸數(shù)值軸的數(shù)據(jù)
const sellerValues = this.allDate.map(item=>{
return item.value
})
const option = {
xAxis: {
type: 'value'
},
yAxis: {
type: 'category',
// y軸坐標(biāo)軸使用遍歷出來(lái)的name
data: sellerNames
},
series: [
{
// 類型為柱狀圖
type: 'bar',
// x軸數(shù)據(jù)需要設(shè)置在series的data類型為遍歷的value
data: sellerValues
}
]
}
// 渲染optio數(shù)據(jù)給dom對(duì)象
this.chartInstance.setOption(option)
},
mounted鉤子函數(shù)調(diào)用
// dom加載完成調(diào)用
mounted () {
this.initChart()
this.getData()
},
更改柱形圖配置
1.在index.html 引入主題配置文件
<!-- 引入主題 --> <script src="./static/lib/theme/chalk.js"></script>
2.在需要使用主題的地方使用 初始化獲取dom傳入chalk
this.chartInstance = this.$echarts.init(this.$refs.sellerRef, 'chalk')
3.option的配置 LinearGradient(x1,x2,y1,y2)線性漸變
const option = {
title: {
text: '| 商家銷售統(tǒng)計(jì)',
textStyle: {
fontSize: 66
},
left: 20,
top: 20
},
// 坐標(biāo)軸配置
grid: {
top: '20%',
left: '3%',
right: '6%',
bottom: '3%',
// 距離包含坐標(biāo)軸文字
containLabel: true
},
xAxis: {
type: 'value'
},
yAxis: {
type: 'category',
// y軸坐標(biāo)軸使用遍歷出來(lái)的name
data: sellerNames
},
series: [
{
// 類型為柱狀圖
type: 'bar',
// x軸數(shù)據(jù)需要設(shè)置在series的data類型為遍歷的value
data: sellerValues,
// 柱的寬度
barWidth: 66,
// 柱文字 默認(rèn)不展示
label: {
show: true,
// 文字靠右顯示
position: 'right',
textStyle: {
// 顏色為白色
color: 'white'
}
},
// 控制柱的每一項(xiàng)
itemStyle: {
// 控制柱的圓角半徑
barBorderRadius: [0, 33, 33, 0],
// 線性漸變
// 指定不同百分比的顏色數(shù)值
color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
{
// 百分之0的樣式
offset: 0,
color: '#5052EE'
},
{
// 百分之百
offset: 1,
color: '#AB6EE5'
}
])
}
}
],
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'line', // 默認(rèn)為直線,可選為:'line' | 'shadow'
z: 0, // 背景層級(jí)
lineStyle: {
width: 66, // 背景寬度
color: '#2D3443' // 背景顏色
}
}
}
}
```
以上就是如何在vue 中使用柱狀圖 并自修改配置的詳細(xì)內(nèi)容,更多關(guān)于vue 中使用柱狀圖 的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Vue?Echarts實(shí)現(xiàn)帶滾動(dòng)效果的柱形圖
- vue+echarts實(shí)現(xiàn)3D柱形圖
- vue基于echarts實(shí)現(xiàn)立體柱形圖
- vue使用echarts實(shí)現(xiàn)水平柱形圖實(shí)例
- 使用D3.js+Vue實(shí)現(xiàn)一個(gè)簡(jiǎn)單的柱形圖
- Vue使用Echarts實(shí)現(xiàn)立體柱狀圖
- vue echarts實(shí)現(xiàn)橫向柱狀圖
- vue echarts實(shí)現(xiàn)柱狀圖動(dòng)態(tài)展示
- vue+echarts實(shí)現(xiàn)堆疊柱狀圖
- vue使用echarts實(shí)現(xiàn)立體柱形圖
相關(guān)文章
vuejs+element-ui+laravel5.4上傳文件的示例代碼
本篇文章主要介紹了vuejs+element-ui+laravel5.4上傳文件的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
詳解vite2.0配置學(xué)習(xí)(typescript版本)
這篇文章主要介紹了詳解vite2.0配置學(xué)習(xí)(typescript版本),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Vue3使用element-plus實(shí)現(xiàn)彈窗效果
本文主要介紹了Vue3使用element-plus實(shí)現(xiàn)彈窗效果,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
淺談Vue.js之初始化el以及數(shù)據(jù)的綁定說(shuō)明
今天小編就為大家分享一篇淺談Vue.js之初始化el以及數(shù)據(jù)的綁定說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
Vue中如何對(duì)ElementUI的Dialog組件封裝
這篇文章主要介紹了Vue中如何對(duì)ElementUI的Dialog組件封裝問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
基于node+vue實(shí)現(xiàn)簡(jiǎn)單的WebSocket聊天功能
最近學(xué)習(xí)了一下websocket的即時(shí)通信,感覺(jué)非常的強(qiáng)大,這里我用node啟動(dòng)了一個(gè)服務(wù)進(jìn)行websocket鏈接,然后再vue的view里面進(jìn)行了鏈接,進(jìn)行通信,廢話不多說(shuō),直接上代碼吧2020-02-02
vue中defineProperty和Proxy的區(qū)別詳解
這篇文章主要介紹了vue中defineProperty和Proxy的區(qū)別詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
vue3實(shí)現(xiàn)alert自定義的plugins方式
這篇文章主要介紹了vue3實(shí)現(xiàn)alert自定義的plugins方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

