You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AnyChart 8.2.0资源甘特图编辑模式下如何判断所处时段?

Fixing Undefined periodIndex in AnyChart 8.2.0 Resource Gantt Tooltip (Edit Mode)

Hey there! I’ve run into similar quirks with AnyChart’s edit mode context before, so let’s break down how to fix your undefined periodIndex issue.

The Root Cause

In non-edit mode, AnyChart’s tooltip callback passes periodIndex directly in the event object because the timeline is static. But when you switch to edit mode, the tooltip context shifts—periodIndex isn’t exposed the same way because the UI is handling interactive elements like drag handles or resizable period edges.

Solution: Get Period Index from the Target Element

Instead of relying on e.periodIndex, we can pull the period index directly from the hovered target element and its associated task. Here’s how to adjust your tooltip code:

// Custom timeLine Tooltip
timeLine.tooltip().useHtml(true);
timeLine.tooltip().format(function (e) {
    let periodIndex;

    // Fallback to direct periodIndex for non-edit mode
    if (e.periodIndex !== undefined) {
        periodIndex = e.periodIndex;
    } 
    // Handle edit mode by fetching from target/period reference
    else if (e.target && e.target.period && e.target.task) {
        const taskPeriods = e.target.task.periods();
        // Find the index of the hovered period in the task's period list
        periodIndex = taskPeriods.indexOf(e.target.period);
    }

    // Now use periodIndex for your custom tooltip logic
    if (periodIndex !== undefined) {
        // Replace this with your actual tooltip content logic
        return `<div class="custom-tooltip">
                    <p>时段 ${periodIndex + 1}</p>
                    <!-- Add your custom content based on periodIndex here -->
                </div>`;
    } else {
        // Fallback content if index can't be retrieved
        return `<div>无法获取时段信息</div>`;
    }
});

Pro Tip for Debugging

If this doesn’t work immediately, add a console.log(e) inside the format function while in edit mode. This will let you inspect the full event object structure—sometimes the exact path to the period/task might vary slightly based on your chart configuration, and debugging will help you tweak the code to match your setup.

内容的提问来源于stack exchange,提问作者migrafik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:35:38