可视化点击x轴标签实现标签文字切换

function getIndex(x, first, last, diff) {
if (x < first) {
return 0;
}

if (x > last) {
    return Math.floor((last - first) / diff);
}

return Math.floor((x - first) / diff);

}
y轴可以自己定义一范围
// 测试示例
console.log(getIndex(5, 10, 100, 10)); // 输出: 0
console.log(getIndex(20, 10, 100, 10)); // 输出: 1
console.log(getIndex(30, 10, 100, 10)); // 输出: 2
console.log(getIndex(35, 10, 100, 10)); // 输出: 2
console.log(getIndex(105, 10, 100, 10)); // 输出: 9

posted @ 2025-02-09 15:16  jialiangzai  阅读(44)  评论(0)    收藏  举报