采用封装的写法
lineChart
<template>
<div :id="id" :style="{height:height,width:width}" />
</template>
<script>
export default {
props: {
id: {
type: String,
default: 'chart'
},
width: {
type: String,
default: '400px'
},
height: {
type: String,
default: '200px'
},
color: {
type: Array,
default: () => {
return ['#00B17D', 'red']
}
},
showArea: {
type: Boolean,
default: false
},
series: {
type: Array,
default: () => {
return [{
name: 'chart',
data: []
}]
}
},
xAxis: {
type: Object,
default: () => {
return {
showSplitLine: true,
data: ['6:00', '8:00', '10:00', '12:00', '14:00', '16:00', '18:00', '20:00', '22:00', '24:00']
}
}
},
yAxis: {
type: Object,
default: () => {
return {
showSplitLine: true,
name: '(%)'
}
}
},
interval: {
type: Number,
default: 0
}
},
data() {
return {
chart: null,
option: {
color: this.color,
title: {
top: 20,
text: this.id,
textStyle: {
fontWeight: 'normal',
fontSize: 15,
color: '#000'
},
left: 0
},
grid: {
top: 80,
width: '60%',
bottom: '2%',
containLabel: true
},
legend: {
tooltip: {
show: true
},
icon: 'rect',
itemWidth: 14,
itemHeight: 5,
itemGap: 13,
data: [],
top: '50',
right: '4%',
textStyle: {
fontSize: 12,
color: this.color
},
formatter: (name) => {
let clientValue = 0
if (name === 'GPU占用') return name
this.series.forEach(element => {
if (element.name === name) {
clientValue = element.data[element.data.length - 1] ? element.data[element.data.length - 1] : 0
}
})
return name + ':' + clientValue + this.yAxis.name
}
},
backgroundColor: '#fff',
tooltip: {
show: true,
trigger: 'axis'
},
xAxis: {
interval: this.interval,
type: 'category',
name: this.xAxis.name,
nameGap: '',
boundaryGap: true,
axisLine: {
lineStyle: {
color: '#57617B'
}
},
axisLabel: {
interval: this.interval
},
splitLine: {
show: this.xAxis.showSplitLine,
interval: this.interval
},
axisTick: { length: 5 },
data: this.xAxis.data
},
yAxis: {
type: 'value',
name: this.yAxis.name,
nameGap: 15,
splitNumber: 4,
boundaryGap: true,
axisTick: {
show: false
},
axisLine: {
lineStyle: {
color: '#57617B'
}
},
axisLabel: {
margin: 10,
textStyle: {
color: '#000',
fontSize: 14,
fontFamily: 'Microsoft YaHe'
}
},
splitLine: {
show: this.yAxis.showSplitLine,
lineStyle: {
color: '#000'
}
}
},
series: []
}
}
},
watch: {
xAxis: {
deep: true,
handler: function(newVal) {
const length = newVal.data.length
let interval = Math.floor(length / 6)
interval = length % 6 === 0 ? interval - 1 : interval
this.chart.setOption({
xAxis: {
...newVal,
axisLabel: {
interval
},
splitLine: {
interval
}
}
})
}
},
series: {
deep: true,
handler(newVal) {
this.chart.setOption({
series: newVal
})
}
}
},
mounted() {
this.series.forEach(elemet => {
this.option.legend.data.push(elemet.name)
})
this.$nextTick(() => {
this.series.forEach(element => {
this.option.series.push({
name: element.name,
type: 'line',
smooth: true,
symbol: 'none',
data: element.data,
areaStyle: this.showArea ? {
color: this.color
} : null,
lineStyle: {
normal: {
width: 1
}
},
itemStyle: {
normal: {
}
}
})
})
this.initChart()
})
window.addEventListener('resize', () => {
if (this.chart) {
this.chart.resize()
}
})
},
beforeDestroy() {
if (!this.chart) {
return
}
this.chart.dispose()
this.chart = null
},
methods: {
initChart() {
this.chart = this.$echarts.init(this.$el)
this.chart.setOption(this.option)
}
}
}
</script>
pieChart
<template>
<div />
</template>
<script>
export default {
props: {
name: {
type: String,
default: ''
},
title: {
type: [String, Number],
default: ''
},
subTitle: {
type: String,
default: ''
},
color: {
type: Array,
default: () => {
return ['#00B17D', '#D2D2D2']
}
},
chartData: {
type: Number,
default: 0
},
unit: {
type: String,
default: '%'
}
},
data() {
return {
chart: null,
data: [
{
value: Number(this.chartData).toFixed(4) * 100,
name: '',
label: {
show: true
},
itemStyle: {
color: this.color[0]
}
},
{
value: 100 - Number(this.chartData) * 100,
name: '',
itemStyle: {
color: this.color[1]
}
}
]
}
},
watch: {
chartData(newVal) {
if (newVal !== '') {
this.data[0].value = Number(newVal).toFixed(4) * 100
this.data[1].value = 100 - Number(newVal) * 100
this.chart.setOption({
series: {
data: this.data
}
})
}
},
title(newVal) {
if (newVal !== '') {
this.chart.setOption({
title: {
text: newVal
}
})
}
}
},
mounted() {
this.initChart()
window.addEventListener('resize', () => {
if (this.chart) {
this.chart.resize()
}
})
},
beforeDestroy() {
if (!this.chart) {
return
}
this.chart.dispose()
this.chart = null
},
methods: {
initChart() {
const _this = this
this.chart = this.$echarts.init(this.$el)
this.chart.setOption({
title: {
text: _this.title,
left: 'center',
bottom: -5,
textStyle: {
fontSize: 13,
color: _this.color[0],
fontWeight: 'bold'
},
subtext: _this.subTitle,
subtextStyle: {
fontWeight: 'bold',
fontSize: 15,
color: '#000'
}
},
color: _this.color,
tooltip: {
show: false
},
label: {
},
series: [{
type: 'pie',
radius: ['30%', '50%'],
avoidLableOverLap: false,
label: {
normal: {
show: false,
position: 'center',
formatter: `{d} ${this.unit}`,
fontSize: 12,
fontWeight: 'bold'
}
},
labelLine: {
normal: {
show: false
}
},
clockwise: false,
legendHoverLinK: false,
name: _this.name + '占比',
data: _this.data
}]
})
}
}
}
</script>
不采用封装的写法
<!-- 负载路数统计/负载速率统计 -->
<div class="loadControl" style="margin-top:10px">
<el-row>
<el-col class="loadControl-way" :xs="{span: 24}" :sm="{span: 24}" :lg="{span: 12}">
<div id="loadControl-way-chart" :style="{height:'200px'}" style="margin:10px 0 0 10px" />
</el-col>
<el-col class="loadControl-speed" :xs="{span: 24}" :sm="{span: 24}" :lg="{span: 11,offset:1}">
<div id="loadControl-speed-chart" :style="{height:'200px'}" style="margin:10px 0 0 10px" />
</el-col>
</el-row>
</div>
mounted() {
const init = this.$echarts.init
this.loadControlWay = init(document.getElementById('loadControl-way-chart'))
this.loadControlSpeed = init(document.getElementById('loadControl-speed-chart'))
this.getLoadInfo()
this.timer1 = setInterval(() => {
this.getLoadInfo()
}, 2000)
window.addEventListener('resize', () => {
this.loadControlWay.resize()
this.loadControlSpeed.resize()
})
},
beforeDestroy() {
this.clearTimer()
},
methods: {
clearTimer() {
if (this.timer1) {
clearInterval(this.timer1)
}
this.timer1 = null
},
getLoadInfo() {
if (this.loadControlXAxis.length >= 30) {
this.loadControlXAxis = [0, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]
this.loadControlWayUp.shift()
this.loadControlWayDown.shift()
this.loadControlSpeedUp.shift()
this.loadControlSpeedDown.shift()
}
this.loadControlXAxis = [0, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]
this.loadControlWayUp.push(25 + Math.floor(Math.random() * 10))
this.loadControlWayDown.push(15 + Math.floor(Math.random() * 10))
this.loadControlSpeedUp.push(45 + Math.floor(Math.random() * 10))
this.loadControlSpeedDown.push(35 + Math.floor(Math.random() * 10))
this.charLoadControlWay()
this.charLoadControlSpeed()
},
charLoadControlWay() {
const loadControlWayOption = {
title: {
text: '负载路数统计'
},
grid: {
...commonEchartStyle.grid
},
legend: {
formatter: function() {
var index = 0
var clientlabels = ['上行 :', '下行 :']
var clientcounts = [9, 1]
clientlabels.forEach(function(value, i) {
if (value === name) {
index = i
}
})
return name + ' : ' + clientcounts[index]
},
...commonEchartStyle.legendCommon
},
xAxis: {
interval: 5,
type: 'category',
name: '时间',
nameGap: '',
boundaryGap: true,
axisLine: {
lineStyle: {
color: '#57617B'
}
},
axisLabel: {
interval: 15
},
splitLine: {
show: false,
interval: 5
},
axisTick: { length: 5 },
data: this.loadControlXAxis,
...commonEchartStyle.xAxisOption
},
yAxis: {
name: '(路)',
axisLine: {
lineStyle: {
color: '#57617B'
}
},
...commonEchartStyle.yAxisOption
},
tooltip: {
show: true,
trigger: 'axis'
},
series: [
{
...commonEchartStyle.seriesUpOption,
areaStyle: {
color: '#1496D0'
},
data: this.loadControlWayUp,
lineStyle: {
width: 1
}
},
{
...commonEchartStyle.seriesDownOption,
areaStyle: {
color: '#00B17D'
},
data: this.loadControlWayDown,
lineStyle: {
width: 1
}
}
],
color: ['#1496D0', '#00B17D']
}
this.loadControlWay.setOption(loadControlWayOption)
},
charLoadControlSpeed() {
const loadControlSpeedOption = {
title: {
text: '负载速率统计'
},
grid: {
...commonEchartStyle.grid
},
legend: {
formatter: function(name) {
var index = 0
var clientlabels = ['上行 :', '下行 :']
var clientcounts = []
clientcounts.push(1, 2)
clientlabels.forEach(function(value, i) {
if (value === name) {
index = i
}
})
return name + ' : ' + clientcounts[index]
},
...commonEchartStyle.legendCommon
},
xAxis: {
interval: 5,
type: 'category',
name: '时间',
nameGap: '',
boundaryGap: true,
axisLine: {
lineStyle: {
color: '#57617B'
}
},
axisLabel: {
interval: 15
},
splitLine: {
show: false,
interval: 5
},
axisTick: { length: 5 },
data: this.loadControlXAxis,
...commonEchartStyle.xAxisOption
},
yAxis: {
name: '(MB/S)',
...commonEchartStyle.yAxisOption
},
tooltip: {
show: true,
trigger: 'axis'
},
series: [
{
...commonEchartStyle.seriesUpOption,
areaStyle: {
color: '#1496D0'
},
data: this.loadControlSpeedUp,
lineStyle: {
width: 1
}
},
{
...commonEchartStyle.seriesDownOption,
areaStyle: {
color: '#00B17D'
},
data: this.loadControlSpeedDown,
lineStyle: {
width: 1
}
}
],
color: ['#1496D0', '#00B17D']
}
this.loadControlSpeed.setOption(loadControlSpeedOption)
},
}