<template> <v-chart ref="vChartRef" :option="option" autoresize></v-chart> </template> <script setup> import {ref, defineProps, watch} from 'vue' import VChart from 'vue-echarts' import * as echarts from 'echarts' const props = defineProps({ data: { type: Object } }) const option = ref() watch( () => props.data, () => { //解决警告 There is a chart instance already initialized on the dom. // VChart.dispose(document.getElementById('LineBar')) console.log(props.data); let xLabel = props.data.legend; let L1 =props.data.L1; let L0 = props.data.L0; initChart(xLabel,L1,L0) } ) const initChart=(xLabel,L1,L0)=>{ option.value={ tooltip: { trigger: 'item', }, legend: { data:xLabel }, grid: { top:'15%', left: '3%', right: '4%', bottom: '3%', containLabel: true }, xAxis: { type: 'category', boundaryGap: true, axisLine: { //坐标轴轴线相关设置。数学上的x轴 show: true, lineStyle: { color: '#233653' } }, axisLabel: { //坐标轴刻度标签的相关设置 textStyle: { color: 'rgba(51,51,51,.6)', padding: 5, fontSize: 12 } }, splitLine: { show: false, lineStyle: { color: '#192a44' } }, axisTick: { show: false }, data: xLabel }, yAxis: [ { type: 'value', name: '万元', splitLine: { show: false, lineStyle: { type: 'dashed' } }, axisLine: { show: false, lineStyle: { color: '#233653' } }, axisLabel: { show: true, textStyle: { color: 'rgba(51,51,51,.6)', padding: 10 } }, axisTick: { show: false } }, { type: 'value', name: '%', splitLine: { show: false, lineStyle: { type: 'dashed' } }, axisLine: { show: false, lineStyle: { color: '#233653' } }, axisLabel: { show: true, textStyle: { color: 'rgba(51,51,51,.6)', padding: 10 } }, axisTick: { show: false } } ], series: [ { name: '营业收入', type: 'line', symbolSize: 5, // 设置折线上圆点大小 symbol: 'circle', // 设置拐点为实心圆 yAxisIndex: 0, data: L1, itemStyle: { color: '#669AFF', }, }, { name: '入住率', type: 'line', symbolSize: 5, // 设置折线上圆点大小 symbol: 'circle', // 设置拐点为实心圆 yAxisIndex: 1, data:L0, itemStyle: { color: '#62D158', }, } ] } } </script> <style lang="scss" scoped></style>我想给两根折线添加图例,请问要怎么办
时间: 2024-03-05 17:53:19 浏览: 126
您可以在 option.value 中的 series 数组中分别为两根折线设置 name 属性,例如:
```javascript
series: [
{
name: '营业收入',
type: 'line',
// ...
},
{
name: '入住率',
type: 'line',
// ...
}
]
```
然后,在 option.value 中添加 legend 属性,将 data 属性设置为两根折线的 name 属性组成的数组,例如:
```javascript
legend: {
data: ['营业收入', '入住率']
}
```
最后,将修改后的 option.value 赋值给组件的 option 属性即可:
```html
<v-chart ref="vChartRef" :option="option" autoresize></v-chart>
```
完整的代码如下:
```html
<template>
<v-chart ref="vChartRef" :option="option" autoresize></v-chart>
</template>
<script setup>
import { ref, defineProps, watch } from 'vue'
import VChart from 'vue-echarts'
import * as echarts from 'echarts'
const props = defineProps({
data: {
type: Object
}
})
const option = ref()
watch(
() => props.data,
() => {
//解决警告 There is a chart instance already initialized on the dom.
// VChart.dispose(document.getElementById('LineBar'))
console.log(props.data)
let xLabel = props.data.legend
let L1 = props.data.L1
let L0 = props.data.L0
initChart(xLabel, L1, L0)
}
)
const initChart = (xLabel, L1, L0) => {
option.value = {
tooltip: {
trigger: 'item'
},
legend: {
data: ['营业收入', '入住率']
},
grid: {
top: '15%',
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: {
type: 'category',
boundaryGap: true,
axisLine: {
//坐标轴轴线相关设置。数学上的x轴
show: true,
lineStyle: {
color: '#233653'
}
},
axisLabel: {
//坐标轴刻度标签的相关设置
textStyle: {
color: 'rgba(51,51,51,.6)',
padding: 5,
fontSize: 12
}
},
splitLine: {
show: false,
lineStyle: {
color: '#192a44'
}
},
axisTick: {
show: false
},
data: xLabel
},
yAxis: [
{
type: 'value',
name: '万元',
splitLine: {
show: false,
lineStyle: {
type: 'dashed'
}
},
axisLine: {
show: false,
lineStyle: {
color: '#233653'
}
},
axisLabel: {
show: true,
textStyle: {
color: 'rgba(51,51,51,.6)',
padding: 10
}
},
axisTick: {
show: false
}
},
{
type: 'value',
name: '%',
splitLine: {
show: false,
lineStyle: {
type: 'dashed'
}
},
axisLine: {
show: false,
lineStyle: {
color: '#233653'
}
},
axisLabel: {
show: true,
textStyle: {
color: 'rgba(51,51,51,.6)',
padding: 10
}
},
axisTick: {
show: false
}
}
],
series: [
{
name: '营业收入',
type: 'line',
symbolSize: 5, // 设置折线上圆点大小
symbol: 'circle', // 设置拐点为实心圆
yAxisIndex: 0,
data: L1,
itemStyle: {
color: '#669AFF'
}
},
{
name: '入住率',
type: 'line',
symbolSize: 5, // 设置折线上圆点大小
symbol: 'circle', // 设置拐点为实心圆
yAxisIndex: 1,
data: L0,
itemStyle: {
color: '#62D158'
}
}
]
}
}
</script>
<style lang="scss" scoped></style>
```
阅读全文