使用Taipy绘制含日期时间的甘特图:起止时间异常问题咨询
Taipy甘特图起止时间异常问题排查
问题描述
尝试用Taipy构建甘特图,按官方文档准备了包含start、end、duration、press、label、color字段的DataFrame:
| index | start | end | duration | press | label | color |
|---|---|---|---|---|---|---|
| 0 | 2022-10-22 00:00:00 | 2022-10-22 01:00:48 | 1970-01-01 01:00:48 | WCINY_16 | 1303237XSW | #31e374 |
| 1 | 2022-10-22 01:21:48 | 2022-10-22 03:21:00 | 1970-01-01 01:59:12 | WCINY_16 | 1735973JET_1736247JET | #c8fb49 |
| 2 | 2022-10-22 03:42:50 | 2022-10-22 06:06:34 | 1970-01-01 02:23:44 | WCINY_16 | 54525 | #434b |
| 3 | 2022-10-22 06:28:24 | 2022-10-22 09:42:46 | 1970-01-01 03:14:22 | WCINY_16 | 1341533XSW | #16552c |
| 4 | 2022-10-22 10:04:36 | 2022-10-22 15:55:00 | 1970-01-01 05:50:24 | WCINY_16 | 1303237XSW | #8d1032 |
| 5 | 2022-10-22 16:17:20 | 2022-10-22 18:13:03 | 1970-01-01 01:55:43 | WCINY_16 | 1212583XSW | #db8572 |
| 6 | 2022-10-22 18:33:51 | 2022-10-22 22:57:51 | 1970-01-01 04:24:00 | WCINY_16 | 1735973JET_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
相关产品推荐
相关产品推荐

