line.js 4.84 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: '#3975C6'
        }
    },
    observers: {
        'chartData': function (data) {
            this.initData(data)
        },
    },
    /**
     * 组件的初始数据
     */
    data: {
        ec: {
            lazyLoad: true
        }

    },

    /**
     * 组件的方法列表
     */
    methods: {
        onInitChart(xData, yData) {
            this.ecComponent && this.ecComponent.init((canvas, width, height, dpr) => {
                const chart = echarts.init(canvas, null, {
                    width: width,
                    height: height,
                    devicePixelRatio: dpr // new
                });
                this.setOption(chart, xData, yData);
                return chart;
            })
        },
        setOption(chart, xData, yData) {
            let maxSpan = xData.length < 10 ? 80 : 20
            // let start= xData.length < 10 ? 50 : 80
            var option = {
                grid: {
                    left: 8,
                    right: 30,
                    top: 30,
                    bottom: 0,
                    containLabel: true
                },
                tooltip: {
                    trigger: 'axis',
                    triggerOn: "mousemove",
                    axisPointer: {
                        type: "shadow",
                        shadowStyle: {
                            shadowColor: 'rgba(53, 195, 118, .1)',
                            color: 'rgba(53, 195, 118, .1)',
                        },
                        lineStyle: {
                            color: '#35C376',
                            type: 'shadow'
                        }
                    },
                    position: 'top',
                    padding: [0, 12],
                    borderWidth: 2,
                    borderColor: '#6ED0BE',
                    borderRadius: 10,
                    textStyle: {
                        color: '#6ED0BE',
                        fontSize: 10
                    },
                    position: function (point, params, dom, rect, size) {
                        // 固定在顶部
                        return [point[0]-20, '10%'];
                    },
                    formatter: function (item) {
                        return `${item[0].value}`
                    }
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    // min:1,
                    // max: 5,

                    axisLabel: {
                        fontSize: 11,
                        color: '#A6A6A6'
                    },
                    axisLine: {
                        show: false //坐标轴线线
                    },
                    axisTick: {
                        show: false //坐标轴刻度
                    },
                    data: xData
                },
                yAxis: {
                    x: 'center',
                    type: 'value',
                    axisLabel: {
                        fontSize: 11,
                        color: '#A6A6A6'
                    },
                    axisLine: {
                        show: false //坐标轴线线
                    },
                    axisTick: {
                        show: false //坐标轴刻度
                    },
                    splitLine: {
                        lineStyle: {
                            type: 'dashed'
                        }
                    }
                },
                dataZoom: {
                    type: "inside",
                    maxSpan: maxSpan,
                    // end: 100,
                    // // start: start,
                },
                series: [{
                    data: yData,
                    type: 'line',
                    //   smooth: true,
                    color: this.data.color
                }]
            }
            chart.setOption(option)
        },
        //初始化数据
        initData(data) {
            const xData = [],
                yData = [];
            const [x, y] = this.data.keys
            data.forEach(item => {
                xData.push(item[x])
                yData.push(item[y])
            })
            this.setData({
                xData,
                yData
            })
            this.onInitChart(xData, yData)
        },
    },
    ready() {
        // 获取组件
        this.ecComponent = this.selectComponent('#mychart-dom-line');
    }
})