line.js 7.15 KB
import * as echarts from '../ec-canvas/echarts';
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        chartData: {
            type: Array,
            value: () => []
        },
        keys: {
            type: Array,
            value: ['year', 'sales']
        },
        tickCount: {
            type: Number,
            value: 5
        },
        color: {
            type: String,
            value: '#2C66F7'
        }
    },
    observers: {
        'chartData': function (data) {
					this.initData(data)
        },
    },
    /**
     * 组件的初始数据
     */
    data: {
        ec: {
            lazyLoad: true
        },

    },
		lifetimes: {
			detached() {
				if (this.chart) {
					try {
						this.chart.dispose()
					} catch (e) {
					}
					this.chart = null
				}
				if (this._initTimer) {
					clearTimeout(this._initTimer)
					this._initTimer = null
				}
			}
		},

    /**
     * 组件的方法列表
     */
    methods: {
        onInitChart(xData, yData, yData2) {
						if (this.chart) {
							this.setOption(this.chart, xData, yData, yData2)
							return
						}
						this.ecComponent && this.ecComponent.init((canvas, width, height, dpr) => {
								const chart = echarts.init(canvas, null, {
										width: width,
										height: height,
										devicePixelRatio: dpr
								});
								this.chart = chart
								this.setOption(chart, xData, yData, yData2);
								return chart;
						})
					},
        setOption(chart, xData, yData, yData2) {
            let maxSpan = xData.length < 10 ? 80 : 20
            // let start= xData.length < 10 ? 50 : 80
            var option = {
                grid: {
                    left: 8,
                    right: 18,
                    top: 30,
                    bottom: 0,
                    containLabel: true
                },
                tooltip: {
                    trigger: 'axis',
                    triggerOn: "mousemove",
                    axisPointer: {
                        type: "line",
                        shadowStyle: {
                            shadowColor: '#E5EAFC',
                            color: '#E5EAFC',
                        },
                        lineStyle: {
                            color: '#2A2B32',
                            type: 'dashed'
                        }
                    },
                    position: 'top',
                    padding: [0, 12],
                    borderRadius: 8,
                    backgroundColor: '#292D33',
                    textStyle: {
                        color: '#FFFFFF',
                        fontSize: 11
                    },
                    formatter: function (item) {
                      return `${item[0].value}`
                  }
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    // min:1,
                    // max: 5,

                    axisLabel: {
                        interval: 0,
                        fontSize: 11,
                        color: '#999EA8'
                    },
                    axisLine: {
                      show: true,
                      lineStyle: {
                        color: "rgba(153, 158, 168, 0.5)",
                      },
                    },
                    axisTick: {
                        show: false //坐标轴刻度
                    },
                    data: xData
                },
                yAxis: {
                    x: 'center',
                    type: 'value',
                    labelLine: {
                      show: true,
                    },
                    axisLabel: {
                      fontSize: 11,
                      color: '#999EA8',
                      formatter(value) {
                        if (value >= 10000) {
                          return value / 10000 + "w";
                        } else {
                          return value;
                        }
                      },
                    },
                    axisLine: {
                      show: true,
                      lineStyle: {
                        color: "rgba(153, 158, 168, 0.5)",
                      },
                    },
                    axisTick: {
                        show: false //坐标轴刻度
                    },
                    splitLine: {
                      show: true,
                        lineStyle: {
                            type: 'dashed'
                        }
                    }
                },
                series: [
                  {
                    showSymbol: false,
                    data: yData,
                    type: 'line',
										smooth: true,
                    color: this.data.color,
                    areaStyle: {
                      color: {
                        type: 'linear',
                        x: 0,
                        y: 0,
                        x2: 0,
                        y2: 1,
                        colorStops: [{
                          offset: 0, 
                          color: 'rgba(147, 170, 253, 1)'  // 顶部颜色
                        }, {
                          offset: 1, 
                          color: 'rgba(229, 234, 252, 0.31)'  // 底部颜色
                        }]
                      }
                    }
                  },
                  {
                    showSymbol: false,
                    data: yData2,
                    type: 'line',
                    lineStyle: {
                      type: 'dashed'
                    },
                    itemStyle: {
                      color: "rgba(52, 123, 255, 1)",
                    },
										smooth: true,
                    color: this.data.color,
                    areaStyle: {
                      color: {
                        type: 'linear',
                        x: 0,
                        y: 0,
                        x2: 0,
                        y2: 1,
                        colorStops: [{
                          offset: 0, 
                          color: 'rgba(147, 170, 253, 1)'  // 顶部颜色
                        }, {
                          offset: 1, 
                          color: 'rgba(229, 234, 252, 0.31)'  // 底部颜色
                        }]
                      }
                    }
                  }
                ]
            }
            chart.setOption(option)
        },
        //初始化数据
        initData(data) {
            const xData = [],
                yData = [],
                yData2 = []
            const [x, y, y2] = this.data.keys
            data.forEach(item => {
                xData.push(item[x])
                yData.push(item[y])
                yData2.push(item[y2])
            })
            this.setData({
                xData,
                yData,
                yData2
            })
						if (this._initTimer) clearTimeout(this._initTimer)
						this._initTimer = setTimeout(() => {
							this.onInitChart(xData, yData, yData2)
						}, 0)
        },
    },
    ready() {
        // 获取组件
        this.ecComponent = this.selectComponent('#mychart-dom-line');
    }
})