vue+echarts繪制折線圖、柱狀圖和扇形圖教程
前端時間在前端崗位上要實(shí)現(xiàn)Vue項(xiàng)目中的echarts畫圖,今天來分享下vue+echarts折線圖、柱狀圖和扇形圖的方法。
每種圖的echarts配置差不多一致,但根據(jù)圖種需進(jìn)行指定選項(xiàng)的改動。
一、折線圖
折線圖中,series要包含每條折線的數(shù)據(jù)。
<template>
<div id="myChart"></div>
</template>
<script>
import echarts from 'echarts' // 引入echarts
export default {
name: 'LineChart',
data () {
return {
echartsOption: { // echarts選項(xiàng),所有繪圖數(shù)據(jù)和樣式都在這里設(shè)置
legend: { //圖表上方的圖例
data: ['騰訊', '阿里巴巴', '華為', '字節(jié)跳動']
},
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], // x軸數(shù)據(jù)
name: '日期', // x軸名稱
nameTextStyle: { // x軸名稱樣式
fontWeight: 600,
fontSize: 18
}
},
yAxis: {
type: 'value',
name: '市值', // y軸名稱
nameTextStyle: { // y軸名稱樣式
fontWeight: 600,
fontSize: 18
}
},
tooltip: { //鼠標(biāo)放到圖上的數(shù)據(jù)展示樣式
trigger: 'axis'
},
series: [ //每條折線的數(shù)據(jù)系列
{
name: '騰訊',
data: [820, 932, 901, 934, 1290, 1330, 1320],
type: 'line'
},
{
name: '阿里巴巴',
data: [620, 711, 823, 934, 1445, 1456, 1178],
type: 'line'
},
{
name: '華為',
data: [612, 920, 1140, 1160, 1190, 1234, 1321],
type: 'line'
},
{
name: '字節(jié)跳動',
data: [234, 320, 453, 567, 789, 999, 1200],
type: 'line'
}
],
}
}
},
mounted () {
let myChart = echarts.init(document.getElementById('myChart'), 'light') // 初始化echarts, theme為light
myChart.setOption(this.echartsOption) // echarts設(shè)置選項(xiàng)
}
}
</script>
<style>
#myChart{
width: 100%;
height: 500px;
margin: 0 auto;
margin-top: 5%;
}
</style>
效果圖

二、柱狀圖
柱狀圖中,要在series加入每個bar的數(shù)據(jù)。
<template>
<div id="myChart"></div>
</template>
<script>
import echarts from 'echarts' // 引入echarts
export default {
name: 'LineChart',
data () {
return {
echartsOption: { // echarts選項(xiàng),所有繪圖數(shù)據(jù)和樣式都在這里設(shè)置
xAxis: {
type: 'category',
data: ['騰訊', '阿里巴巴', '華為', '字節(jié)跳動'], // x軸數(shù)據(jù)
name: '日期', // x軸名稱
nameTextStyle: { // x軸名稱樣式
fontWeight: 600,
fontSize: 18
}
},
yAxis: {
type: 'value',
name: '公司盈利率', // y軸名稱
nameTextStyle: { // y軸名稱樣式
fontWeight: 600,
fontSize: 18
}
},
tooltip: { //鼠標(biāo)放到圖上的數(shù)據(jù)展示樣式
trigger: 'axis'
},
series: [{ //series中加入每個bar的數(shù)據(jù)
name: '公司盈利率',
type: 'bar',
barWidth: '60%',
data: [0.35, 0.2, 0.25, 0.15],
}],
}
}
},
mounted () {
let myChart = echarts.init(document.getElementById('myChart'), 'light') // 初始化echarts, theme為light
myChart.setOption(this.echartsOption) // echarts設(shè)置選項(xiàng)
}
}
</script>
<style>
#myChart{
width: 100%;
height: 500px;
margin: 0 auto;
margin-top: 5%;
}
</style>
效果圖

三、扇形圖
扇形圖中不需要xAxis和yAxis,一個series的data加入每個類別的數(shù)據(jù),格式為 {value, name}。
<template>
<div id="myChart"></div>
</template>
<script>
import echarts from 'echarts' // 引入echarts
export default {
name: 'PieChart',
data () {
return {
echartsOption: { // echarts選項(xiàng),所有繪圖數(shù)據(jù)和樣式都在這里設(shè)置
legend: { //圖表上方的圖例
data: ['騰訊', '阿里巴巴', '華為', '字節(jié)跳動']
},
tooltip: { //鼠標(biāo)放到圖上的數(shù)據(jù)展示樣式
trigger: 'axis'
},
series: [{
name: '公司市值占比',
type: 'pie',
barWidth: '60%',
data: [ // 扇形圖數(shù)據(jù)格式: {value, name}
{value: 0.35, name: '騰訊'}, // value不一定是比例,echarts會自動轉(zhuǎn)換
{value: 0.2, name: '阿里巴巴'},
{value: 0.25, name: '華為'},
{value: 0.2, name: '字節(jié)跳動'},
],
}],
}
}
},
mounted () {
let myChart = echarts.init(document.getElementById('myChart'), 'light') // 初始化echarts, theme為light
myChart.setOption(this.echartsOption) // echarts設(shè)置選項(xiàng)
}
}
</script>
<style>
#myChart{
width: 100%;
height: 500px;
margin: 0 auto;
margin-top: 5%;
}
</style>
效果圖

這樣就繪制出簡單的echarts圖啦!
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3如何利用自定義指令實(shí)現(xiàn)下拉框分頁懶加載
下拉框一開始請求第一頁的內(nèi)容,滾動到最后的時候,請求第二頁的內(nèi)容,如此反復(fù),直到所有數(shù)據(jù)加載完成,這篇文章主要介紹了vue3如何利用自定義指令實(shí)現(xiàn)下拉框分頁懶加載,需要的朋友可以參考下2024-07-07
vue-element-admin后臺生成動態(tài)路由及菜單方法詳解
vue-element-admin后臺管理系統(tǒng)模板框架 是vue結(jié)合element-ui一體的管理系統(tǒng)框架,下面這篇文章主要給大家介紹了關(guān)于vue-element-admin后臺生成動態(tài)路由及菜單的相關(guān)資料,需要的朋友可以參考下2023-09-09
利用Vue Native構(gòu)建移動應(yīng)用的全過程記錄
VueNative是一個使用JavaScript構(gòu)建跨平臺原生移動應(yīng)用程序的框架m這篇文章主要給大家介紹了關(guān)于如何利用Vue Native構(gòu)建移動應(yīng)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2021-08-08
Vue創(chuàng)建項(xiàng)目后沒有webpack.config.js(vue.config.js)文件的解決
這篇文章主要介紹了Vue創(chuàng)建項(xiàng)目后沒有webpack.config.js(vue.config.js)文件的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01

