123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <template>
- <div>
- <el-row :gutter="5">
- <el-col :span="14">
- <el-card>
- <div id="main" ref="line" style="height: 400px;"></div>
- </el-card>
- </el-col>
- <el-col :span="10">
- <el-card>
- <div class="grid-content ep-bg-purple-light" style="background-color:#9b9da1; height: 400px;">占个位置</div>
- </el-card>
- </el-col>
- </el-row>
- </div>
- </template>
- <script>
- import { onMounted, onBeforeUnmount, ref, } from 'vue'
- import * as echarts from 'echarts'
- export default {
- props: ['chartData'],
- setup(props, context) {
- let lineChart = ref()
- let line = ref()
- onMounted(() => {
- lineChart = echarts.init(document.getElementById('main'))
- updateChart()
- // updateChart()
- setTimeout(()=>{
- resizeChart()
- }, 0)
- window.addEventListener('resize', resizeChart) // 监听窗口大小变化,调整图表大小
- })
- onBeforeUnmount(() => {
- window.removeEventListener('resize', resizeChart) // 在组件销毁前移除事件监听,避免内存泄漏
- })
- // 获取图像数据 lieChartData
- let lieChartData = props.chartData
- console.log('lieChartData', lieChartData)
- function updateChart() {
- const option = {
- tooltip: {
- trigger: 'axis',
- axisPointer: {
- type: 'cross',
- label: {
- backgroundColor: '#6a7985'
- }
- }
- },
- // legend: {data: ['数据1', '数据2'], right: '50%',left: '50%'},
- toolbox: {
- feature: {
- saveAsImage: { yAxisIndex: 'none' }
- }
- },
- grid: {
- top: 70, right: 60, bottom: 30, left: 55,
- },
- xAxis: [
- {
- type: 'category',
- boundaryGap: false,
- data: lieChartData.xData
- }
- ],
- yAxis: [
- {
- type: 'value',
- // name: yTitle1,
- splitLine: {
- show: true // 设置显示分割线
- },
- axisLabel: {
- formatter: '{value} 单位1'
- }
- },
- {
- type: 'value',
- // name: yTitle2,
- splitLine: {
- show: false
- },
- axisLabel: {
- formatter: '{value} 单位2'
- }
- }
- ],
- series: [
- {
- name: '数据1',
- type: 'line',
- symbolSize: 6,
- symbol: 'circle',
- smooth: true,
- data: lieChartData.yData1,
- yAxisIndex: 0,
- lineStyle: { color: '#fe9a8b' },
- itemStyle: { color: '#fe9a8b', borderColor: '#fe9a8b' },
- areaStyle: {
- color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
- { offset: 0, color: '#fe9a8bb3' },
- { offset: 1, color: '#fe9a8b03' },
- ]),
- },
- },
- {
- name: '数据2',
- type: 'line',
- symbolSize: 6,
- symbol: 'circle',
- smooth: true,
- data: lieChartData.yData2,
- yAxisIndex: 1,
- lineStyle: { color: '#9E87FF' },
- itemStyle: { color: '#9E87FF', borderColor: '#9E87FF' },
- areaStyle: {
- color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
- { offset: 0, color: '#9E87FFb3' },
- { offset: 1, color: '#9E87FF03' },
- ]),
- },
- emphasis: {
- itemStyle: {
- color: {
- type: 'radial',
- x: 0.5,
- y: 0.5,
- r: 0.5,
- colorStops: [
- { offset: 0, color: '#9E87FF' },
- { offset: 0.4, color: '#9E87FF' },
- { offset: 0.5, color: '#fff' },
- { offset: 0.7, color: '#fff' },
- { offset: 0.8, color: '#fff' },
- { offset: 1, color: '#fff' },
- ],
- },
- borderColor: '#9E87FF',
- borderWidth: 2,
- },
- },
- },
- ],
- }
- lineChart.setOption(option)
- }
- // 自适应调整窗口
- function resizeChart() {
- if (lineChart) {
- lineChart.resize()
- }
- }
- return { }
- }
- }
- // let props = defineProps({
- // chartData: {
- // type: Object,
- // required: true,
- // },
- // })
- // let { title, xData, yData1, yData2 } = props.chartData // 将需要传入的数据解构出来方便在option中调用
- </script>
- <style scoped lang="scss">
- </style>
|