2
Implementation of a standard financial plot visualization using Chaco
3
renderers and scales. This differs from the financial_plot.py example
4
in that it uses a date-oriented axis.
7
# Major library imports
8
from numpy import abs, cumprod, linspace, random
11
# Enthought library imports
12
from enable.api import Component, ComponentEditor
13
from traits.api import HasTraits, Instance
14
from traitsui.api import Item, Group, View
17
from chaco.api import ArrayDataSource, BarPlot, DataRange1D, \
18
LinearMapper, VPlotContainer, PlotAxis, \
19
FilledLinePlot, add_default_grids, PlotLabel
20
from chaco.tools.api import PanTool, ZoomTool
23
from chaco.scales.api import CalendarScaleSystem
24
from chaco.scales_tick_generator import ScalesTickGenerator
27
def create_dates(numpoints, units="days"):
28
""" Returns **numpoints** number of dates that evenly bracket the current
29
date and time. **units** should be one of "weeks", "days", "hours"
30
"minutes", or "seconds".
32
units_map = { "weeks" : 7*24*3600,
39
dates = linspace(now, now+numpoints*dt, numpoints)
42
#===============================================================================
43
# # Create the Chaco plot.
44
#===============================================================================
45
def _create_plot_component():
47
# Create the data and datasource objects
48
# In order for the date axis to work, the index data points need to
49
# be in units of seconds since the epoch. This is because we are using
50
# the CalendarScaleSystem, whose formatters interpret the numerical values
51
# as seconds since the epoch.
53
index = create_dates(numpoints)
54
returns = random.lognormal(0.01, 0.1, size=numpoints)
55
price = 100.0 * cumprod(returns)
56
volume = abs(random.normal(1000.0, 1500.0, size=numpoints) + 2000.0)
58
time_ds = ArrayDataSource(index)
59
vol_ds = ArrayDataSource(volume, sort_order="none")
60
price_ds = ArrayDataSource(price, sort_order="none")
62
xmapper = LinearMapper(range=DataRange1D(time_ds))
63
vol_mapper = LinearMapper(range=DataRange1D(vol_ds))
64
price_mapper = LinearMapper(range=DataRange1D(price_ds))
66
price_plot = FilledLinePlot(index = time_ds, value = price_ds,
67
index_mapper = xmapper,
68
value_mapper = price_mapper,
70
face_color = "paleturquoise",
72
border_visible = True)
73
price_plot.overlays.append(PlotAxis(price_plot, orientation='left')),
75
# Set the plot's bottom axis to use the Scales ticking system
76
bottom_axis = PlotAxis(price_plot, orientation="bottom",# mapper=xmapper,
77
tick_generator=ScalesTickGenerator(scale=CalendarScaleSystem()))
78
price_plot.overlays.append(bottom_axis)
79
hgrid, vgrid = add_default_grids(price_plot)
80
vgrid.tick_generator = bottom_axis.tick_generator
82
price_plot.tools.append(PanTool(price_plot, constrain=True,
83
constrain_direction="x"))
84
price_plot.overlays.append(ZoomTool(price_plot, drag_button="right",
88
max_zoom_out_factor=10.0,
91
vol_plot = BarPlot(index = time_ds, value = vol_ds,
92
index_mapper = xmapper,
93
value_mapper = vol_mapper,
94
line_color = "transparent",
97
bar_width_type = "screen",
102
border_visible = True)
104
hgrid, vgrid = add_default_grids(vol_plot)
105
# Use the same tick generator as the x-axis on the price plot
106
vgrid.tick_generator = bottom_axis.tick_generator
107
vol_plot.underlays.append(PlotAxis(vol_plot, orientation='left'))
108
vol_plot.tools.append(PanTool(vol_plot, constrain=True,
109
constrain_direction="x"))
111
container = VPlotContainer(bgcolor = "lightblue",
115
container.add(vol_plot)
116
container.add(price_plot)
117
container.overlays.append(PlotLabel("Financial Plot with Date Axis",
119
#font="Times New Roman 24"))
124
#===============================================================================
125
# Attributes to use for the plot view.
127
title="Financial plot example"
129
#===============================================================================
130
# # Demo class that is used by the demo.py application.
131
#===============================================================================
132
class Demo(HasTraits):
133
plot = Instance(Component)
137
Item('plot', editor=ComponentEditor(size=size),
139
orientation = "vertical"),
140
resizable=True, title=title,
141
width=size[0], height=size[1]
144
def _plot_default(self):
145
return _create_plot_component()
149
if __name__ == "__main__":
150
demo.configure_traits()