bar.js 3.41 KB
import * as echarts from '../ec-canvas/echarts';

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    chartData: {
      type: Array,
      value: () => []
    },
    keys: {
      type: Array,
      value: ['year', 'sales']
    },
    tickCount: {
      type: Number,
      value: 4
    },
    color: {
      type: String,
      value: '#3975C6'
    }
  },
  observers: {
    'chartData': function (data) {
      console.log(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,
        });
        this.setOption(chart, xData, yData);
        return chart;
      })

    },
    setOption(chart, xData, yData) {
      var option = {
        grid: {
          left: 8,
          right: 10,
          top: 30,
          bottom: 0,
          containLabel: true
        },
        // tooltip: {
        //   trigger: 'axis',
        //   triggerOn: "mousemove",
        //   axisPointer: {
        //     type: "none",
        //     lineStyle: {
        //       type: 'solid'
        //     }
        //   },
        //   padding: [0, 12],
        //   borderWidth: 2,
        //   borderColor: this.data.color,
        //   borderRadius: 10,
        //   textStyle: {
        //     color: this.data.color,
        //     fontSize: 10
        //   },
        //   position: function(pt) {
        //     console.log(pt)
        //     return [pt[0], pt[1]];
        //   },
        //   formatter: function (item) {
        //     return `${item[0].value}` 
        //   }
        // },
        xAxis: {
          type: 'category',
          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'
            }
          }
        },
        series: [{
          data: yData,
          type: 'bar',
          label: {
            show: true,
            position: 'top',
            color: '#3582EE',
          },
          color: '#3582EE',
          itemStyle: {
            // borderRadius: [50, 50, 0, 0],
            padding: [50, 50, 0, 0]
          },
          barWidth: 24
        }]
      };
      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-bar');
  }
})