.vue
import multiBarLineChart from '@/utils/echarts.js'
drawChart (xData, datas) {
this.myEchart = echarts.init(this.$refs.myChart)
const countLimit = 8 // 可视范围展示个数
this.myEchart.clear()
this.myEchart.setOption(multiBarLineChart(xData, datas, countLimit))
window.onresize = _.debounce(this.myEchart.resize, 500)
}
echarts.js
xData的数据格式为[‘2021Q1’,‘2021Q2’,…]
datas的数据格式如下图
import * as echarts from 'echarts'
const axisLineTxTColor = '#666' // 轴和文字颜色
const splitLineColor = '#d9d9d9' // 分割线颜色
const gradientList = [ // 渐变色字典(最多是4个)
{
light: '#a4cbfd', dark: '#5c97ca' },
{
light: '#b4ecd0', dark: '#73deb3' },
{
light: '#ffe3a4', dark: '#fac858' },
{
light: '#ffa1a1', dark: '#ee6666' }
]
/** 动态柱状折线图
* @xData 横坐标数据 ['','',...]
* @datas 传过来的原始数据(用于处理legend,yAxis,series等)
* @showCount 可视区域内展示的个数,超过则滑动(默认8)
*/
const multiBarLineChart = (xData, datas, showCount = 8) => {
const obj = handleDatas(datas) // 处理数据获取legend等信息
const {
legend, yAxis, series } = obj
// 赋值option
const option = {
title: {
text: xData.length > showCount ? '(滑动查看更多)' : '',
left: 48,
textStyle: {
fontSize: '12',
color: '#999',
fontWeight: 500
}
}