Vue2+Echarts封装组件:专注逻辑,图表生成自动化

通过Vue2封装了一个Echarts图表组件,不需要关注图表的生成和渲染了,只需要关注逻辑开发即可,包含了柱状图、折线图、饼图、柱状叠加图、柱状图折线图组合、柱状叠加图与柱状图的组合等,包含了图表下钻、右键跳转明细功能,无需关注Echarts的实现,提高开发效率

开发者能够将注意力集中在业务逻辑的开发上,而无需担心底层图表的创建和渲染过程。这样的表述既体现了组件的便捷性,也突出了它对提高开发效率的帮助。同时,对于那些寻找简化ECharts集成解决方案的开发者来说,这也是一个很好的案例。Vue2+Echarts封装组件:专注逻辑,图表生成自动化_第1张图片

组件使用
(1调用)

(2data)data() {
    return {
      aotuMainHeight: {
        height: ''
      },
      charDataArray: [],
    }
  },
(3接口回调后)
	// legend 处理:
               const all = ['示例1','实例2','示例3']
	// 下钻的数据
	const idData = [{ name: '', id: '',userId: ''}]
	// 横坐标的数据
            	const xData = ['X1','X2','X3']
	// 图表的数据
               const mbData = [1,2,3] // 柱状图
               const sjData = [4,5,6] // 柱状图
               const lvData = [7,8,9] // 折线图
	// 图表的颜色
	const color = ['#73deb4', '#f6e0b4', '#5b8ff9']
	// Y轴单位
                 const unit = ['个', '']
	// 参数传递 ,
	// 2:  2代表左侧Y轴两个柱状图  1代表右侧Y轴的折线图  'barAndLine', 图的类型(此处为柱状图和折线图的组合,其他的见 SzqCommonChart)
	// true 是否下钻  标志1表示不同页面的不同图表的区分  all 图表实例 color图表颜色 unitY轴单位 true是否显示单位	
               this.charDataArray.push([2, 1, 'barAndLine', true, '标志1', all, color, unit, true])
            // 参数2:下钻参数.
            this.charDataArray.push(idData)
            // 参数3:横坐标
            this.charDataArray.push(xData)
            // 参数4 数据信息
            this.charDataArray.push([mbData, sjData, lvData])
/**
 * 最大值最小值
 */
export function szqMaxAndMin(data) {
  // series 中 所有数据的最大值和最小值
  let isLing = false
  const arrLing = []
  data.forEach(it => {
    if (it.yAxisIndex === 1) {
      arrLing.push(...it.data)
    }
  })
  isLing = arrLing.every(number => Number(number) === 0)
  const mm = maxAndMin(data)
  // Min 左边数值轴, Min1 右侧百分比轴
  const Max = Math.ceil(mm[0].max)
  const Max1 = Math.ceil(mm[1].max)
  let Min = mm[0].min
  let Min1 = mm[1].min
  const rat1 = Min / Max
  const rat2 = Min1 / Max1
  rat1 > rat2 ? Min = rat2 * Max : Min1 = rat1 * Max1
  return { max: Max, min: Math.ceil(Min), max1: Max1, min1: Math.ceil(Min1), isLing }
}
function maxAndMin(allData) {
  const result = {}
  allData.forEach(item => {
    const yAxisIndex = item.yAxisIndex
    const values = item.data.map(value => value && parseInt(value))
    if (!result[yAxisIndex]) {
      result[yAxisIndex] = {
        max: Math.max(...values),
        min: Math.min(...values)
      }
    } else {
      result[yAxisIndex].max = Math.max(result[yAxisIndex].max, ...values)
      result[yAxisIndex].min = Math.min(result[yAxisIndex].min, ...values)
    }
  })
  return result
}

export function autoBottom(h, isTitle) {
  let height = ''
  let left = []
  let right = []
  if (h <= 400) {
    isTitle ? height = '42.5%' : height = '40%'
  } else if (h <= 450) {
    isTitle ? height = '42.5%' : height = '40%'
  } else if (h <= 500) {
    isTitle ? height = '37.5%' : height = '35%'
  } else if (h <= 550) {
    isTitle ? height = '34.5%' : height = '32%'
  } else if (h <= 600) {
    isTitle ? height = '33.5%' : height = '31%'
  } else if (h <= 650) {
    isTitle ? height = '30.5%' : height = '28%'
    left = [0, 10, -10, -20]
    right = [0, -30, -10, 0]
  } else if (h <= 700) {
    isTitle ? height = '27.5%' : height = '25%'
    left = [0, 10, -5, -20]
    right = [0, -30, -5, 0]
  } else if (h <= 750) {
    isTitle ? height = '26.5%' : height = '24%'
  } else if (h <= 800) {
    isTitle ? height = '23.5%' : height = '21%'
  } else {
    isTitle ? height = '22.5%' : height = '20%'
  }
  return { height, left, right }
}



                    
                    

你可能感兴趣的:(软件开发,Vue.js,Echarts,vue.js,echarts,前端)