|
@@ -41,36 +41,6 @@ const formatterTooltip = (params: Array<any>) => {
|
|
|
return result
|
|
|
}
|
|
|
|
|
|
-// 选项
|
|
|
-const options = {
|
|
|
- xAxis: {
|
|
|
- type: 'category',
|
|
|
- data: [],
|
|
|
- axisTick: {
|
|
|
- alignWithLabel: true
|
|
|
- }
|
|
|
- },
|
|
|
- yAxis: {
|
|
|
- type: 'value'
|
|
|
- },
|
|
|
- tooltip: {
|
|
|
- trigger: 'axis',
|
|
|
- formatter: formatterTooltip
|
|
|
- },
|
|
|
- legend: {
|
|
|
- orient: 'horizontal', //图例布局方式:水平 'horizontal' 、垂直 'vertical'
|
|
|
- x: 'right', // 横向放置位置,选项:'center'、'left'、'right'、'number'(横向值 px)
|
|
|
- y: 'top', // 纵向放置位置,选项:'top'、'bottom'、'center'、'number'(纵向值 px)
|
|
|
- data: []
|
|
|
- },
|
|
|
- grid: {
|
|
|
- left: '24px',
|
|
|
- right: '10px',
|
|
|
- containLabel: true
|
|
|
- },
|
|
|
- series: []
|
|
|
-}
|
|
|
-
|
|
|
// 尺寸变化
|
|
|
const changeSize = () => {
|
|
|
nextTick(() => {
|
|
@@ -80,6 +50,36 @@ const changeSize = () => {
|
|
|
|
|
|
// 初始化options
|
|
|
const initOptions = () => {
|
|
|
+ // 选项
|
|
|
+ let options: any = {
|
|
|
+ xAxis: {
|
|
|
+ type: 'category',
|
|
|
+ data: [],
|
|
|
+ axisTick: {
|
|
|
+ alignWithLabel: true
|
|
|
+ }
|
|
|
+ },
|
|
|
+ yAxis: {
|
|
|
+ type: 'value'
|
|
|
+ },
|
|
|
+ tooltip: {
|
|
|
+ trigger: 'axis',
|
|
|
+ formatter: formatterTooltip
|
|
|
+ },
|
|
|
+ legend: {
|
|
|
+ orient: 'horizontal', //图例布局方式:水平 'horizontal' 、垂直 'vertical'
|
|
|
+ x: 'right', // 横向放置位置,选项:'center'、'left'、'right'、'number'(横向值 px)
|
|
|
+ y: 'top', // 纵向放置位置,选项:'top'、'bottom'、'center'、'number'(纵向值 px)
|
|
|
+ data: []
|
|
|
+ },
|
|
|
+ grid: {
|
|
|
+ left: '24px',
|
|
|
+ right: '10px',
|
|
|
+ containLabel: true
|
|
|
+ },
|
|
|
+ series: []
|
|
|
+ }
|
|
|
+
|
|
|
// 基本的series数据配置
|
|
|
let baseSeries = {
|
|
|
symbol: 'circle',
|
|
@@ -101,28 +101,52 @@ const initOptions = () => {
|
|
|
stack: 'x',
|
|
|
smooth: true
|
|
|
}
|
|
|
- // 最终的siries数组
|
|
|
- let finalSeriesList: any = []
|
|
|
- // const finalSeriesList: Array<any> = []
|
|
|
-
|
|
|
- props.legendData.map((item, index) => {
|
|
|
- const seriesClone = JSON.parse(JSON.stringify(baseSeries))
|
|
|
- // 设置克隆的属性
|
|
|
- seriesClone.name = item
|
|
|
- seriesClone.data = props.seriesData[index]
|
|
|
- seriesClone.itemStyle.borderColor = colorList[index]
|
|
|
- seriesClone.lineStyle.color = colorList[index]
|
|
|
-
|
|
|
- // 将克隆后的对象添加到 finalSeriesList 中
|
|
|
- finalSeriesList.push(seriesClone)
|
|
|
- })
|
|
|
|
|
|
- options.series = finalSeriesList
|
|
|
- options.legend.data = props.legendData as any
|
|
|
- options.xAxis.data = props.xAxisData as any
|
|
|
- linechart.value.setOption(options)
|
|
|
+ linechart.value.clear()
|
|
|
+
|
|
|
+ if (props.seriesData.length) {
|
|
|
+ // 最终的siries数组
|
|
|
+ let finalSeriesList: any = []
|
|
|
+ // const finalSeriesList: Array<any> = []
|
|
|
+
|
|
|
+ props.legendData.map((item, index) => {
|
|
|
+ const seriesClone = JSON.parse(JSON.stringify(baseSeries))
|
|
|
+ // 设置克隆的属性
|
|
|
+ seriesClone.name = item
|
|
|
+ seriesClone.data = props.seriesData[index]
|
|
|
+ seriesClone.itemStyle.borderColor = colorList[index]
|
|
|
+ seriesClone.lineStyle.color = colorList[index]
|
|
|
+
|
|
|
+ // 将克隆后的对象添加到 finalSeriesList 中
|
|
|
+ finalSeriesList.push(seriesClone)
|
|
|
+ })
|
|
|
+
|
|
|
+ options.series = finalSeriesList
|
|
|
+ options.legend.data = props.legendData as any
|
|
|
+ options.xAxis.data = props.xAxisData as any
|
|
|
+ } else {
|
|
|
+ options = {
|
|
|
+ title: {
|
|
|
+ text: '暂无数据',
|
|
|
+ x: 'center',
|
|
|
+ y: 'center',
|
|
|
+ textStyle: {
|
|
|
+ fontSize: 16,
|
|
|
+ fontWeight: 'normal'
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ linechart.value.setOption(options, true)
|
|
|
+}
|
|
|
+
|
|
|
+// 改变加载状态
|
|
|
+const changeLoading = (state: boolean) => {
|
|
|
+ if (state) linechart.value.showLoading()
|
|
|
+ else linechart.value.hideLoading()
|
|
|
}
|
|
|
|
|
|
+// 监听options信息的变化
|
|
|
watch(
|
|
|
() => [props.legendData, props.seriesData, props.xAxisData],
|
|
|
() => {
|
|
@@ -130,11 +154,22 @@ watch(
|
|
|
}
|
|
|
)
|
|
|
|
|
|
+// 监听加载状态
|
|
|
+watch(
|
|
|
+ () => props.loadingState,
|
|
|
+ (val) => {
|
|
|
+ changeLoading(val)
|
|
|
+ if (!val) initOptions()
|
|
|
+ },
|
|
|
+ { deep: true }
|
|
|
+)
|
|
|
+
|
|
|
onMounted(() => {
|
|
|
nextTick(() => {
|
|
|
+ // 开始的时候,不要去直接初始化,先展示加载状态,等到数据加载完状态改变,再去设置
|
|
|
linechart.value = echarts.init(chart.value)
|
|
|
-
|
|
|
- initOptions()
|
|
|
+ changeLoading(props.loadingState)
|
|
|
+ if (!props.loadingState) initOptions()
|
|
|
|
|
|
/**
|
|
|
* @description: 只监听window会导致当侧边栏缩放时,dom大小变化但无法resize
|