方法
const numberFix = (value, scale, isUpper) => {const moreThanZero = value >= 0value = Math.abs(value)if (scale == 0) {return value}if (scale > 0) {scale -= 1}let resultif (moreThanZero) {result =numberFloorOrCeil(value / Math.pow(10, scale), isUpper) *Math.pow(10, scale)} else {result =-1 *numberFloorOrCeil(value / Math.pow(10, scale), !isUpper) *Math.pow(10, scale)}if (scale < 0) {result =numberFloorOrCeil(result * Math.pow(10, -1 * scale), isUpper) /Math.pow(10, -1 * scale)}return result}const numberFloorOrCeil = (value, isUpper) => {if (isUpper) {return Math.ceil(value)} else {return Math.floor(value)}}const getNumberMaxScale = value => {const moreThanZero = value >= 0let absValue = Math.abs(value)if (absValue >= 1) {const valueStr = absValue + ''return valueStr.split('.')[0].length} else if (absValue > 0) {let scale = 0while (absValue < 1) {absValue *= 10scale -= 1}return scale} else {return 1}}const echartsSplit = (valueArr,splitNum = 5,fromZero = true,splitSmallerScale = 0,splitMinScale = null) => {valueArr = [...valueArr]if (fromZero) {valueArr.push(0)}let maxValue = Math.max(...valueArr)let minValue = Math.min(...valueArr)const maxMoreThanZero = maxValue >= 0const minMoreThanZero = minValue >= 0let totalDiff = maxValue - minValuelet totalDiffScale = getNumberMaxScale(totalDiff / splitNum)let splitScale = totalDiffScale - splitSmallerScaleif (splitMinScale != null && splitScale < splitMinScale) {splitScale = splitMinScale}splitScale = splitScale == 0 ? -1 : splitScalelet minResult = numberFix(minValue, splitScale, false)let splitValue = (maxValue - minResult) / splitNumlet splitResult = numberFix(splitValue, splitScale, true)if (splitResult == 0) {minResult = minResult == 0 ? 0 : minResult - 1splitResult = 1}let maxResult = minResult + splitResult * splitNumreturn {min: Math.round(minResult * 100000000) / 100000000,max: Math.round(maxResult * 100000000) / 100000000,interval: Math.round(splitResult * 100000000) / 100000000}}export default echartsSplit
使用
import echartsSplit from '@/utils/echartsSplit'setOptions(list) {const wData = []const qData = []const drpData = []series.forEach((item) => {if (item.name.indexOf('流量') > -1) {qData.push(...item.data.filter((value) => value))} else if (item.name.indexOf('水位') > -1) {wData.push(...item.data.filter((value) => value))} else {drpData.push(...item.data.filter((value) => value))}})this.options.yAxis = yAxis.map((item, index) => {const data = index === 0 ? drpData : index === 1 ? wData : qDataconst obj = echartsSplit(data, 5, false)return {...item,min: obj.min,max: obj.max,interval: obj.interval}})this.options = { ...this.options }
}
效果