export const dataSource = (min: string, max: string, step: string) => { const minTemp: number = Number.parseFloat(min); const maxTemp: number = Number.parseFloat(max); const stepTemp: number = Number.parseFloat(step); // 切割后的循环数 let num = 0; // 数组 [{value: 10, text: '低档'}] const dataList: any = []; num = Math.round((maxTemp - minTemp) / stepTemp); // 最大值距离循环后的最大值的差 // 5 % 2 // // console.log(100 % 0.2) === 0.19999999999999446 const lastStep = maxTemp - (num * stepTemp + minTemp); // 生成数组 [{value: 10, text: '低档'}] for (let i = 0; i < (lastStep < 0 ? num : num + 1); i++) { dataList.push({ value: i + 1, // lastStep === 0说明和step值一样 text: ((stepTemp * 10) * i + minTemp * 10) / 10, }) } // lastStep !== 0 说明有余数,不取余数,操作小数不是很精确,新添加最大值到数组的最后一个 if (lastStep !== 0) { dataList.push({ value: dataList.length + 1, text: maxTemp, }) } // console.log(dataList) return dataList } export const dotInfoList = (dotsData: any[]) => { const aInfoList = []; const dotsLength = dotsData.length; if (dotsLength > 0) { const oneStepPercent = 100 / (dotsLength - 1); for (let i = 0; i < dotsLength; i += 1) { let aInfo: any = { icon: '', }; const hasIcon = !!dotsData[i].icon; if (hasIcon) { aInfo = { ...aInfo, icon: dotsData[i].icon,}; } if (i === (dotsLength - 1)) { aInfo = { ...aInfo, left: 100,}; } else { const pos = oneStepPercent * i; aInfo = { ...aInfo, left: pos,}; } aInfo = { ...aInfo, text: dotsData[i].text, value: dotsData[i].value,}; aInfoList.push(aInfo); } } return aInfoList }