Vue前端柱狀圖實(shí)例(疊狀條形圖)
vue前端柱狀圖(疊狀條形圖)
通過(guò)echarts來(lái)實(shí)現(xiàn)柱狀圖的效果,echarts是針對(duì)數(shù)據(jù)報(bào)表等展現(xiàn)的一個(gè)產(chǎn)品,具體了解看官方網(wǎng)站:Echarts官網(wǎng),在這個(gè)官網(wǎng)中有詳細(xì)教程以及API,很容易入手Echarts,從個(gè)人剛學(xué)習(xí)Echarts圖表來(lái)看,它的難度在于它有自己的一套屬性,這和我們平時(shí)用的css是不同的,因而我們需要什么效果就得去遵守它的屬性,去識(shí)別圖表的各個(gè)部分的專業(yè)名稱,才能從它API中獲取相應(yīng)屬性去實(shí)現(xiàn)你想要效果。
效果圖

代碼:
<template>
<div>
<!-- 柱狀圖 -->
<el-row :gutter="80">
<!-- 柱狀圖兩側(cè)間距 最大24 -->
<el-col :span="24">
<div class="border-card ">
<el-row>
<div class="chart-wrapper">
<bar-chart ref="chartOne" v-if="chartOneShow" style="width:100%;height:400px"
:option="chartOptionOne" />
</div>
</el-row>
</div>
</el-col>
</el-row>
</div>
</template>
<script>
//保存的柱狀圖組件路徑
import BarChart from "../../dashboard/chart.vue";
export default {
//使用的柱狀圖組件
components: {
BarChart,
},
data(){
return{
//表圖選項(xiàng)
chartOptionOne: {},
//表圖顯示
chartOneShow: true,
//前端接收到的數(shù)據(jù)
tableData: [],
}
},
created() {
this.getList();
},
methods: {
getList() {
this.loading = true;
let data=[];
let data1=[];
let data2=[];
let data3=[];
taskStatisticsRwpf(this.queryParams).then(response => {
this.tableData = response.data;
for (var i = 0; i < this.tableData.length; i++) {
data.push(this.tableData[i].字段名)//此data為x坐標(biāo)展示的文字
data1.push(this.tableData[i].字段名)
data2.push(this.tableData[i].字段名)
data3.push(this.tableData[i].字段名)
}
this.getChartsData(data,data1,data2,data3);
this.loading = false;
});
},
getChartsData(data,data1,data2,data3) {
this.chartOneShow = false
this.chartTwoShow = false
let that = this;
taskStatisticsRwpf({
queryType: "chart"
}).then(response => {
that.chartOptionOne = {
title: {
text: "柱狀圖標(biāo)題名",
x:'center',
y:'top',
textAlign:'left', //位置
textStyle:{
//文字顏色
color:'#000000',
//字體風(fēng)格,'normal','italic','oblique'
fontStyle:'normal',
//字體粗細(xì) 'normal','bold','bolder','lighter',100 | 200 | 300 | 400...
fontWeight:'400',
//字體系列
// fontFamily:'sans-serif',
//字體大小
fontSize:20,
}
},
grid: {
left: '1%',
right: '1%',
containLabel: true
},
legend: {
data: data1,data2,data3
},
//鼠標(biāo)懸停顯示數(shù)據(jù)
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
}
},
//x坐標(biāo)
xAxis: {
type: 'category',
//x坐標(biāo)顯示的文字
data: data,
axisLabel: {
//x軸文字間距
interval:0,
//x軸文字傾斜度
rotate:0
}
},
//y坐標(biāo)
yAxis: {
type: 'value'
},
series: [{
name: '文字名',
type: 'bar',
stack: 'total',
label: {
show: true
},
emphasis: {
focus: 'series'
},
data: data1
},
{
name: '文字名',
type: 'bar',
stack: 'total',
label: {
show: true
},
emphasis: {
focus: 'series'
},
data: data2
},
{
name: '文字名',
type: 'bar',
stack: 'total',
label: {
show: true
},
emphasis: {
focus: 'series'
},
data: data3
},
{
stack: 'total',
type: 'bar',
//設(shè)置柱狀圖大小
barWidth: 30,
label: {
// 柱圖頭部顯示值
show: true,
position: "right",
color: "#333",
fontSize: "12px",
formatter: (params) => {
return params.value[params.encode.x[0]];
},
}
}]
};
//表圖顯示
this.chartOneShow = true
})
},
}
}
</script>vue echarts柱狀圖自定義formatter
echarts雙柱狀圖自定義tooltip
<template> <div id="echartsId" style="width:100%;height:200px;"></div> </template>
zhuDouble() {
var myChart = echarts.init(document.getElementById('echartsId'))
window.addEventListener('resize', function () {
myChart.resize()
})
myChart.setOption({
legend: {
left: 'center',
bottom:'3%',
icon: 'circle',
data: ['Forest', 'Steppe'],
textStyle: {
fontSize: 12,
color: '#8C8C8C'
}
},
xAxis: {
type: 'category',
axisTick: {
show: false // 去掉x軸 小細(xì)條
},
data: ['2018', '2019', '2020', '2021', '2022'],
axisLabel: {
width: 30,
overflow: "breakAll",
fontSize: 11,
},
},
grid: {
left: '5%',
right: '8%',
bottom: '12%',
top: '8%',
containLabel: true,
},
color: ['#3372FF', '#21C9E6'],
yAxis: {
type: 'value'
},
tooltip: {
trigger: 'item',
formatter:function(params){
let tip = '';
tip += '<div>總數(shù)' + 23 + '</div><div>'+ params.seriesName + '數(shù)量' + params.value +'所</p></div>'
return tip
},
borderColor: "rgba(255, 255, 255, 1)"
},
series: [
{
name: 'Forest',
type: 'bar',
data: [320, 332, 301, 334, 390]
},
{
name: 'Steppe',
type: 'bar',
data: [220, 182, 191, 234, 290]
},
})
}

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解vue-cli構(gòu)建項(xiàng)目反向代理配置
本篇文章主要介紹了詳解vue-cli構(gòu)建項(xiàng)目反向代理配置,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
vue 國(guó)際化 vue-i18n 雙語(yǔ)言 語(yǔ)言包
這篇文章主要介紹了vue 國(guó)際化 vue-i18n 雙語(yǔ)言 語(yǔ)言包的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
vue input標(biāo)簽通用指令校驗(yàn)的實(shí)現(xiàn)
這篇文章主要介紹了vue input標(biāo)簽通用指令校驗(yàn)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
vue使用Highcharts實(shí)現(xiàn)3D餅圖
這篇文章主要為大家詳細(xì)介紹了vue使用Highcharts實(shí)現(xiàn)3D餅圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
antd+vue實(shí)現(xiàn)動(dòng)態(tài)驗(yàn)證循環(huán)屬性表單的思路
今天通過(guò)本文給大家分享antd+vue實(shí)現(xiàn)動(dòng)態(tài)驗(yàn)證循環(huán)屬性表單的思路,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-09-09
el-form-item中表單項(xiàng)label和表單項(xiàng)內(nèi)容換行實(shí)現(xiàn)方法
這篇文章主要給大家介紹了el-form-item中表單項(xiàng)label和表單項(xiàng)內(nèi)容換行實(shí)現(xiàn)的相關(guān)資料,每個(gè)表單el-form由多個(gè)表單域el-form-item組成,需要的朋友可以參考下2023-09-09
vue2左側(cè)菜單欄收縮展開功能實(shí)現(xiàn)
vue2左側(cè)菜單欄收縮展開目前是非常常見的,我們?cè)谌粘i_發(fā)過(guò)程中經(jīng)常會(huì)碰到,這篇文章給大家介紹vue2左側(cè)菜單欄收縮展開功能,感興趣的朋友跟隨小編一起看看吧2024-04-04

