|
@@ -0,0 +1,178 @@
|
|
|
+<template>
|
|
|
+ <div id="barLine" ref="barLine" style="height: 400px;"></div>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script>
|
|
|
+import { onMounted, onBeforeUnmount, ref, } from 'vue'
|
|
|
+import * as echarts from 'echarts'
|
|
|
+
|
|
|
+export default {
|
|
|
+ props: ['barLineData'],
|
|
|
+
|
|
|
+ setup(props, context) {
|
|
|
+ let lineChart = ref()
|
|
|
+
|
|
|
+ onMounted(() => {
|
|
|
+ lineChart = echarts.init(document.getElementById('barLine'))
|
|
|
+ updateChart()
|
|
|
+ window.addEventListener('resize', resizeChart) // 监听窗口大小变化,调整图表大小
|
|
|
+ setTimeout(() => {
|
|
|
+ resizeChart()
|
|
|
+ },0)
|
|
|
+ })
|
|
|
+ onBeforeUnmount(() => {
|
|
|
+ window.removeEventListener('resize', resizeChart) // 在组件销毁前移除事件监听,避免内存泄漏
|
|
|
+ })
|
|
|
+ // 获取图像数据 lieChartData
|
|
|
+ let chartData = props.barLineData
|
|
|
+
|
|
|
+ function updateChart() {
|
|
|
+ const option = {
|
|
|
+ // title: {
|
|
|
+ // text: chartData.title,
|
|
|
+ // },
|
|
|
+ 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: true,
|
|
|
+ data: chartData.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: '柱状图',
|
|
|
+ type: 'bar',
|
|
|
+ // tooltip: {
|
|
|
+ // valueFormatter: function (value) {
|
|
|
+ // return value + ' ml';
|
|
|
+ // }
|
|
|
+ // },
|
|
|
+ barWidth: '30%',
|
|
|
+ data: chartData.barData,
|
|
|
+ yAxisIndex: 1,
|
|
|
+ itemStyle: {
|
|
|
+ color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
|
|
|
+ { offset: 0, color: 'rgba(108,80,243,0.3)' },
|
|
|
+ { offset: 1, color: 'rgba(108,80,243,0)' },
|
|
|
+ ]),
|
|
|
+ //柱状图圆角
|
|
|
+ borderRadius: [15, 15, 0, 0],
|
|
|
+ },
|
|
|
+ },
|
|
|
+ {
|
|
|
+ name: '数据1',
|
|
|
+ type: 'line',
|
|
|
+ symbolSize: 6,
|
|
|
+ symbol: 'circle',
|
|
|
+ smooth: true,
|
|
|
+ data: chartData.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: chartData.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 {}
|
|
|
+ },
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+</script>
|
|
|
+
|
|
|
+<style>
|
|
|
+</style>
|