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

使用Taipy绘制含日期时间的甘特图:起止时间异常问题咨询

Taipy甘特图起止时间异常问题排查

问题描述

尝试用Taipy构建甘特图,按官方文档准备了包含start、end、duration、press、label、color字段的DataFrame:

indexstartenddurationpresslabelcolor
02022-10-22 00:00:002022-10-22 01:00:481970-01-01 01:00:48WCINY_161303237XSW#31e374
12022-10-22 01:21:482022-10-22 03:21:001970-01-01 01:59:12WCINY_161735973JET_1736247JET#c8fb49
22022-10-22 03:42:502022-10-22 06:06:341970-01-01 02:23:44WCINY_1654525#434b
32022-10-22 06:28:242022-10-22 09:42:461970-01-01 03:14:22WCINY_161341533XSW#16552c
42022-10-22 10:04:362022-10-22 15:55:001970-01-01 05:50:24WCINY_161303237XSW#8d1032
52022-10-22 16:17:202022-10-22 18:13:031970-01-01 01:55:43WCINY_161212583XSW#db8572
62022-10-22 18:33:512022-10-22 22:57:511970-01-01 04:24:00WCINY_161735973JET_1736247JET#49e61c

使用以下Taipy图表配置:

<|{tp_ganttchart_description}|chart|type=bar|orientation=h|x[1]=duration|base[1]=start|y[1]=press|text[1]=label|color[1]=color|>

但生成的甘特图起止时间不符合逻辑,需排查遗漏的配置或处理步骤。

问题根源与修复步骤

1. 修正duration字段的数据类型

你的duration字段存储的是日期时间格式(如1970-01-01 01:00:48),但Taipy甘特图需要的是时间差数值(以毫秒为单位的时长),而非带日期的时间值。

处理代码:

import pandas as pd

# 确保start和end转为datetime类型
df['start'] = pd.to_datetime(df['start'])
df['end'] = pd.to_datetime(df['end'])
# 计算实际时长(转换为毫秒适配Taipy时间轴)
df['duration'] = (df['end'] - df['start']).dt.total_seconds() * 1000

2. 显式配置时间轴类型与格式

Taipy默认不会自动识别时间轴类型,需显式指定x轴为时间轴并设置显示格式:

<|{tp_ganttchart_description}|chart|type=bar|orientation=h|x[1]=duration|base[1]=start|y[1]=press|text[1]=label|color[1]=color|xaxis_type=datetime|xaxis_tickformat=%Y-%m-%d %H:%M:%S|>

3. 确认start字段的类型正确性

必须保证start字段是datetime类型而非字符串,否则Taipy无法正确解析时间轴坐标,导致任务定位错误。

4. 调整y轴分组逻辑

当前press字段所有值均为WCINY_16,会导致所有任务堆叠在同一行,视觉上易出现时间混乱。若需区分任务,可将label赋值给y[1]:

y[1]=label

内容的提问来源于stack exchange,提问作者Rym Guerbi Michaut

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 13:53:20