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

Ag Charts K线系列问题:蜡烛中心与X轴时间点不对应

解决AG Charts Enterprise中K线与时间轴偏移的问题

问题分析

自定义时间轴配置后,K线(蜡烛)整体向右偏移,无法在对应时间点居中,导致高低引线无法对齐高低价线节点。这是因为自定义的时间轴刻度间隔与K线系列的位置计算逻辑不匹配:默认时间轴会自动适配数据点位置,而手动指定interval和min/max后,刻度的放置位置与数据点时间错位。

解决方案

1. 调整时间轴刻度的放置方式

在时间轴配置中添加tick.placement: 'on',让刻度直接对齐数据点的时间位置,确保K线的x坐标与数据点时间完全匹配:

axes: [
    {
        type: "time",
        position: "bottom",
        nice: false,
        gridLine: {
            enabled: true,
        },
        max: new Date(),
        min: new Date(2024, 11, 1),
        keys: ["date"],
        interval: { step: time.hour.every(6) },
        // 添加刻度对齐配置
        tick: {
            placement: 'on'
        }
    },
    // ... 右侧数值轴配置
]

2. 适配K线宽度(可选)

如果调整刻度后仍有轻微偏移,可以给K线系列设置barWidth,让蜡烛宽度适配时间轴的间隔,避免超出时间点范围:

series: [
    {
        type: "candlestick",
        xKey: "date",
        lowKey: "low",
        highKey: "high",
        openKey: "open",
        closeKey: "close",
        // 根据数据间隔调整宽度(取值0-1)
        barWidth: 0.7
    },
    // ... 高低价线系列配置
]

3. 验证数据时间戳

确保你的date字段是对应时间区间的起始点(比如每6小时的数据,date设置为整点时刻),而非区间中点或结束点,否则会导致K线位置与刻度错位。

修改后的完整配置示例

const options = {
    container: document.getElementById("agChart"),
    dataSource: {
        getData: this.fetchData.bind(this),
    },
    series: [
        {
            type: "candlestick",
            xKey: "date",
            lowKey: "low",
            highKey: "high",
            openKey: "open",
            closeKey: "close",
            barWidth: 0.7
        },
        {
            type: "line",
            xKey: "date",
            yKey: "high",
        },
        {
            type: "line",
            xKey: "date",
            yKey: "low",
        }
    ],
    axes: [
        {
            type: "time",
            position: "bottom",
            nice: false,
            gridLine: {
                enabled: true,
            },
            max: new Date(),
            min: new Date(2024, 11, 1),
            keys: ["date"],
            interval: { step: time.hour.every(6) },
            tick: {
                placement: 'on'
            }
        },
        {
            type: "number",
            position: "right",
            gridLine: {
                enabled: true,
            },
            keys: ["low", "high", "open", "close"],
        }
    ],
    legend: {
        position: "right",
    },
    zoom: {
        enabled: true
    },
    initialState: {
        zoom: {
            rangeX: {
                start: {
                    __type: 'date',
                    value: new Date('2024-12-20').getTime(),
                },
                end: {
                    __type: 'date',
                    value: new Date('2024-12-25').getTime(),
                },
            },
        }
    }
};

效果验证

修改后,蜡烛会在对应时间点居中显示,高低引线将准确对齐高低价线的节点,同时保留你需要的自定义时间轴配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 06:24:54