5

在 Enthought 的 Chaco 中,TimeFormatter该类用于格式化刻度标签的时间字符串。有没有办法指定时间格式(类似time.strftime())。

源代码现在将显示月份和日期时的格式硬编码为美式 (MMDD)。我想增加一些灵活性,以便以某种方式将时间/日期格式提示传递给TimeFormatter

我不知道有什么好方法可以做到这一点(除了更改源代码本身(TimeFormatter._formats字典))

4

1 回答 1

4

老实说,最简单的方法是对 TimeFormatter 的 _formats 字典进行猴子补丁:

from enthought.chaco.scales.formatters import TimeFormatter
TimeFormatter._formats['days'] = ('%d/%m', '%d%a',)

如果您不想这样做,那么您需要继承 TimeFormatter。这很容易。更麻烦的是使 chaco.scales 包创建的所有现有比例系统都使用您的新子类,而不是内置的 TimeFormatter。如果您查看 scales.time_scale.TimeScale,它在构造函数中接受“格式化程序”关键字参数。因此,在 time_scale.py 的底部,当构建 MDYScales 列表时,您必须创建自己的:

EuroMDYScales = [TimeScale(day_of_month=range(1,31,3), formatter=MyFormatter()),
             TimeScale(day_of_month=(1,8,15,22), formatter=MyFormatter()),
             TimeScale(day_of_month=(1,15), formatter=MyFormatter()),
             TimeScale(month_of_year=range(1,13), formatter=MyFormatter()),
             TimeScale(month_of_year=range(1,13,3), formatter=MyFormatter()),
             TimeScale(month_of_year=(1,7), formatter=MyFormatter()),
             TimeScale(month_of_year=(1,), formatter=MyFormatter())]

然后,当您创建 ScalesTickGenerator 时,您需要将这些比例传递给 ScaleSystem:

euro_scale_system = CalendarScaleSystem(*(HMSScales + EuroMDYScales))
tick_gen = ScalesTickGenerator(scale=euro_scale_system)

然后你可以创建轴,给它这个刻度生成器:

axis = PlotAxis(tick_generator = tick_gen)

HTH,对不起,这大约滞后一个月。我并没有真正检查 StackOverflow。如果您还有其他 chaco 问题,我建议您注册 chaco-users 邮件列表...

于 2010-02-11T17:47:23.253 回答