如何在vue 中使用柱狀圖 并自修改配置
更新時間:2021年01月21日 09:56:21 作者:canlan在掘金
這篇文章主要介紹了如何在vue 中使用柱狀圖 并自修改配置,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
1.在html文件導入echart
<!-- 引入echarts --> <script src="https://cdn.bootcdn.net/ajax/libs/echarts/4.8.0/echarts.min.js"></script>
2.在main.js上掛載echarts對象
Vue.prototype.$echarts = window.echarts // 使用時直接使用this.$echarts
3.頁面結構
<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, // 服務器返回的數(shù)據(jù)
}
},
}
```js
##### 5.methods中的邏輯
```js
methods: {
// 初始化echarts對象
initEchart(){
// 獲取dom對象
this.chartInstance = this.$echarts.init(this.$refs.sellerRef)
},
// 獲取服務器的數(shù)據(jù)
async getData(){
const {data:res} = await this.$http.get('seller')
this.allDate = res
// 返會的數(shù)據(jù)結構是 name商家 value數(shù)值
// 對返回的數(shù)據(jù)進行從小打到排序 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軸坐標軸使用遍歷出來的name
data: sellerNames
},
series: [
{
// 類型為柱狀圖
type: 'bar',
// x軸數(shù)據(jù)需要設置在series的data類型為遍歷的value
data: sellerValues
}
]
}
// 渲染optio數(shù)據(jù)給dom對象
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)計',
textStyle: {
fontSize: 66
},
left: 20,
top: 20
},
// 坐標軸配置
grid: {
top: '20%',
left: '3%',
right: '6%',
bottom: '3%',
// 距離包含坐標軸文字
containLabel: true
},
xAxis: {
type: 'value'
},
yAxis: {
type: 'category',
// y軸坐標軸使用遍歷出來的name
data: sellerNames
},
series: [
{
// 類型為柱狀圖
type: 'bar',
// x軸數(shù)據(jù)需要設置在series的data類型為遍歷的value
data: sellerValues,
// 柱的寬度
barWidth: 66,
// 柱文字 默認不展示
label: {
show: true,
// 文字靠右顯示
position: 'right',
textStyle: {
// 顏色為白色
color: 'white'
}
},
// 控制柱的每一項
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', // 默認為直線,可選為:'line' | 'shadow'
z: 0, // 背景層級
lineStyle: {
width: 66, // 背景寬度
color: '#2D3443' // 背景顏色
}
}
}
}
```
以上就是如何在vue 中使用柱狀圖 并自修改配置的詳細內(nèi)容,更多關于vue 中使用柱狀圖 的資料請關注腳本之家其它相關文章!
相關文章
vue使用elementui的el-menu的折疊菜單collapse示例詳解
這篇文章主要介紹了vue使用elementui的el-menu的折疊菜單collapse示例詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-12-12
Vue中使用create-keyframe-animation與動畫鉤子完成復雜動畫
這篇文章主要介紹了Vue中使用create-keyframe-animation與動畫鉤子完成復雜動畫,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
element?ui動態(tài)側(cè)邊菜單欄及頁面布局實現(xiàn)方法
后臺管理系統(tǒng)經(jīng)常會使用到一個左側(cè)菜單欄,右側(cè)Tab頁的頁面顯示結構,這篇文章主要給大家介紹了關于element?ui動態(tài)側(cè)邊菜單欄及頁面布局實現(xiàn)的相關資料,需要的朋友可以參考下2023-09-09

