123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <template>
- <div class="echarts-line" :id="ids"></div>
- </template>
- <script>
- import * as echarts from 'echarts'
- export default {
- props: {
- legendChecked: {
- // 图例
- default: true
- },
- ids: {
- // id
- type: String,
- default: 'echartsline'
- },
- fontColor: {
- // 字体颜色
- type: String,
- default: '#89949F'
- },
- yLine: {
- // x轴分割线
- default: false
- },
- chartData: {
- // echarts·数据
- default: function () {
- return []
- }
- },
- // Y轴单位
- Yunit: {
- type: Boolean,
- default: true
- },
- theme: {
- default: function () {
- return ['#3da4fe', '#23cef2', '#65b664', '#00FDCB']
- }
- }
- },
- watch: {
- chartData: {
- // 深度监听,可监听到对象、数组的变化
- handler() {
- this.$nextTick(() => {
- if (this.chartData && this.chartData.length > 0) {
- this.dataValue(this.chartData)
- }
- })
- },
- deep: true, // 深度监听,可监听到对象、数组的变化
- immediate: true // 立刻执行
- }
- },
- data() {
- return {}
- },
- mounted() {},
- methods: {
- dataValue(newVal) {
- if (newVal && newVal.length > 0) {
- const seriesConfig = []
- const Yconfig = []
- const unit = []
- for (let i = 0; i < newVal.length; i++) {
- if (newVal[i].areaStyle) {
- const obj = {
- type: 'line',
- name: newVal[i].name,
- data: newVal[i].yData,
- smooth: newVal[i].smooth ? newVal[i].smooth : false,
- showSymbol: newVal[i].showSymbol ? newVal[i].showSymbol : false,
- lineStyle: {
- color: newVal[i].lineColor
- },
- areaStyle: {
- color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
- {
- offset: 0,
- color: newVal[i].areaColor
- },
- {
- offset: 1,
- color: 'rgba(0,0,0,0)'
- }
- ])
- }
- }
- if (i === 1) {
- obj.yAxisIndex = 1
- }
- seriesConfig.push(obj)
- if (this.Yunit) {
- Yconfig.push({
- type: 'value',
- name:
- (newVal.length > 1 ? newVal[i].name : '单位') +
- ':' +
- newVal[i].unit,
- nameTextStyle: {
- fontSize: 12,
- color: this.fontColor
- },
- splitNumber: 5,
- axisTick: {
- show: false
- },
- splitLine: {
- show: this.yLine,
- lineStyle: {
- color: 'rgba(153, 153, 153, 0.3)'
- }
- },
- axisLine: {
- show: false
- },
- axisLabel: {
- color: this.fontColor,
- fontSize: 12
- }
- })
- } else {
- Yconfig.push({
- type: 'value',
- nameTextStyle: {
- fontSize: 12,
- color: this.fontColor
- },
- splitNumber: 5,
- axisTick: {
- show: false
- },
- splitLine: {
- show: this.yLine,
- lineStyle: {
- color: 'rgba(153, 153, 153, 0.3)'
- }
- },
- axisLine: {
- show: false
- },
- axisLabel: {
- color: this.fontColor,
- fontSize: 12
- }
- })
- Yconfig[0].name = '(m³/s)'
- }
- unit.push(newVal[i].unit)
- } else {
- seriesConfig.push({
- type: 'line',
- name: newVal[i].name,
- data: newVal[i].yData,
- smooth: newVal[i].smooth ? newVal[i].smooth : false,
- symbolSize: newVal[i].showSymbol ? newVal[i].showSymbol : false,
- lineStyle: {
- color: newVal[i].lineColor
- }
- })
- Yconfig.push({
- type: 'value',
- name: newVal[i].name + ':' + newVal[i].unit,
- nameTextStyle: {
- fontSize: 12,
- color: this.fontColor
- },
- splitNumber: 5,
- axisTick: {
- show: false
- },
- splitLine: {
- show: this.yLine,
- lineStyle: {
- color: '#e5e5e5'
- }
- },
- axisLine: {
- show: false
- },
- axisLabel: {
- color: this.fontColor,
- fontSize: 12
- }
- })
- unit.push(newVal[i].unit)
- }
- }
- this.setEcharts(seriesConfig, Yconfig, newVal[0].xData, unit)
- }
- },
- setEcharts(series, Yconfig, dataAxis, unit) {
- let legend = []
- if (this.legendChecked) {
- legend = this.chartData.map((data, i) => {
- return {
- name: data.name,
- icon: 'roundRect',
- itemStyle: series[i].lineStyle
- }
- })
- } else {
- legend = []
- }
- const myChart = echarts.init(document.getElementById(this.ids))
- const option = {
- color: this.theme,
- tooltip: {
- trigger: 'axis',
- formatter: function (data) {
- if (!data.length) return
- if (data.length > 1) {
- let html = ''
- html = `<div class="toolTips"><div style="font-size: 12px;color: #fff">${data[0].axisValue}</div>`
- for (let i = 0; i < data.length; i++) {
- html += `<div style="font-size: 12px;color: #fff">${series[i].name}: ${data[i].value} ${unit[i]}</div>`
- }
- html += '</div>'
- return html
- } else {
- return `<div class="toolTips"><div style="font-size: 12px;color: #fff">${data[0].axisValue}: ${data[0].value} ${unit[0]}</div></div>`
- }
- },
- backgroundColor: '#487892',
- borderWidth: 0,
- padding: [3, 5],
- axisPointer: {
- lineStyle: {
- color: '#299acb'
- }
- }
- },
- legend: {
- top: 0,
- right: 'center',
- width: 240,
- data: legend,
- itemWidth: 15,
- itemHeight: 5,
- show: true,
- selectedMode: false,
- textStyle: {
- color: this.fontColor
- }
- },
- xAxis: {
- type: 'category',
- data: dataAxis,
- boundaryGap: false,
- axisTick: {
- show: true,
- inside: true
- },
- splitLine: {
- show: false
- },
- axisLine: {
- lineStyle: {
- color: 'rgba(135,142,148,0.5)'
- }
- },
- axisLabel: {
- color: this.fontColor,
- fontSize: 12
- }
- },
- yAxis: Yconfig,
- grid: {
- left: 20,
- right: 35,
- top: this.legendChecked ? 30 : 30,
- bottom: 0,
- containLabel: true
- },
- series: series
- }
- myChart.clear()
- myChart.setOption(option, true)
- }
- }
- }
- </script>
- <style scoped lang="scss">
- .echarts-line {
- width: 80%;
- height: 80%;
- }
- </style>
|