2
Implementation of a standard financial plot visualization using Chaco
6
# Major library imports
7
from numpy import abs, arange, cumprod, random
9
# Enthought library imports
10
from enable.api import Component, ComponentEditor
11
from traits.api import HasTraits, Instance
12
from traitsui.api import Item, Group, View
15
from chaco.api import ArrayDataSource, BarPlot, DataRange1D, \
16
LinearMapper, VPlotContainer, PlotAxis, FilledLinePlot, \
17
add_default_grids, PlotLabel
18
from chaco.tools.api import PanTool, ZoomTool
20
#===============================================================================
21
# # Create the Chaco plot.
22
#===============================================================================
23
def _create_plot_component():
25
# Create the data and datasource objects
27
index = arange(numpoints)
28
returns = random.lognormal(0.01, 0.1, size=numpoints)
29
price = 100.0 * cumprod(returns)
30
volume = abs(random.normal(1000.0, 1500.0, size=numpoints) + 2000.0)
32
time_ds = ArrayDataSource(index)
33
vol_ds = ArrayDataSource(volume, sort_order="none")
34
price_ds = ArrayDataSource(price, sort_order="none")
36
xmapper = LinearMapper(range=DataRange1D(time_ds))
37
vol_mapper = LinearMapper(range=DataRange1D(vol_ds))
38
price_mapper = LinearMapper(range=DataRange1D(price_ds))
40
price_plot = FilledLinePlot(index = time_ds, value = price_ds,
41
index_mapper = xmapper,
42
value_mapper = price_mapper,
44
face_color = "paleturquoise",
47
border_visible = True)
48
add_default_grids(price_plot)
49
price_plot.overlays.append(PlotAxis(price_plot, orientation='left'))
50
price_plot.overlays.append(PlotAxis(price_plot, orientation='bottom'))
51
price_plot.tools.append(PanTool(price_plot, constrain=True,
52
constrain_direction="x"))
53
price_plot.overlays.append(ZoomTool(price_plot, drag_button="right",
58
vol_plot = BarPlot(index = time_ds, value = vol_ds,
59
index_mapper = xmapper,
60
value_mapper = vol_mapper,
61
line_color = "transparent",
64
bar_width_type = "screen",
69
border_visible = True)
71
add_default_grids(vol_plot)
72
vol_plot.underlays.append(PlotAxis(vol_plot, orientation='left'))
73
vol_plot.tools.append(PanTool(vol_plot, constrain=True,
74
constrain_direction="x"))
76
container = VPlotContainer(bgcolor = "lightblue",
80
container.add(vol_plot)
81
container.add(price_plot)
82
container.overlays.append(PlotLabel("Financial Plot",
84
#font="Times New Roman 24"))
88
#===============================================================================
89
# Attributes to use for the plot view.
91
title="Financial plot example"
93
#===============================================================================
94
# # Demo class that is used by the demo.py application.
95
#===============================================================================
96
class Demo(HasTraits):
97
plot = Instance(Component)
101
Item('plot', editor=ComponentEditor(size=size),
103
orientation = "vertical"),
104
resizable=True, title=title,
105
width=size[0], height=size[1]
108
def _plot_default(self):
109
return _create_plot_component()
113
if __name__ == "__main__":
114
demo.configure_traits()