~ubuntu-branches/ubuntu/utopic/python-chaco/utopic

« back to all changes in this revision

Viewing changes to examples/demo/financial_plot.py

  • Committer: Package Import Robot
  • Author(s): Andrew Starr-Bochicchio
  • Date: 2014-06-01 17:04:08 UTC
  • mfrom: (7.2.5 sid)
  • Revision ID: package-import@ubuntu.com-20140601170408-m86xvdjd83a4qon0
Tags: 4.4.1-1ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
 - Let the binary-predeb target work on the usr/lib/python* directory
   as we don't have usr/share/pyshared anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
Implementation of a standard financial plot visualization using Chaco
3
 
renderers and scales.
4
 
"""
5
 
 
6
 
# Major library imports
7
 
from numpy import abs, arange, cumprod, random
8
 
 
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
13
 
 
14
 
# Chaco imports
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
19
 
 
20
 
#===============================================================================
21
 
# # Create the Chaco plot.
22
 
#===============================================================================
23
 
def _create_plot_component():
24
 
 
25
 
    # Create the data and datasource objects
26
 
    numpoints = 500
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)
31
 
 
32
 
    time_ds = ArrayDataSource(index)
33
 
    vol_ds = ArrayDataSource(volume, sort_order="none")
34
 
    price_ds = ArrayDataSource(price, sort_order="none")
35
 
 
36
 
    xmapper = LinearMapper(range=DataRange1D(time_ds))
37
 
    vol_mapper = LinearMapper(range=DataRange1D(vol_ds))
38
 
    price_mapper = LinearMapper(range=DataRange1D(price_ds))
39
 
 
40
 
    price_plot = FilledLinePlot(index = time_ds, value = price_ds,
41
 
                                index_mapper = xmapper,
42
 
                                value_mapper = price_mapper,
43
 
                                edge_color = "blue",
44
 
                                face_color = "paleturquoise",
45
 
                                alpha = 0.5,
46
 
                                bgcolor = "white",
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",
54
 
                                          always_on=True,
55
 
                                          tool_mode="range",
56
 
                                          axis="index"))
57
 
 
58
 
    vol_plot = BarPlot(index = time_ds, value = vol_ds,
59
 
                       index_mapper = xmapper,
60
 
                       value_mapper = vol_mapper,
61
 
                       line_color = "transparent",
62
 
                       fill_color = "black",
63
 
                       bar_width = 1.0,
64
 
                       bar_width_type = "screen",
65
 
                       antialias = False,
66
 
                       height = 100,
67
 
                       resizable = "h",
68
 
                       bgcolor = "white",
69
 
                       border_visible = True)
70
 
 
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"))
75
 
 
76
 
    container = VPlotContainer(bgcolor = "lightblue",
77
 
                               spacing = 20,
78
 
                               padding = 50,
79
 
                               fill_padding=False)
80
 
    container.add(vol_plot)
81
 
    container.add(price_plot)
82
 
    container.overlays.append(PlotLabel("Financial Plot",
83
 
                                        component=container,
84
 
                                        #font="Times New Roman 24"))
85
 
                                        font="Arial 24"))
86
 
    return container
87
 
 
88
 
#===============================================================================
89
 
# Attributes to use for the plot view.
90
 
size=(800,600)
91
 
title="Financial plot example"
92
 
 
93
 
#===============================================================================
94
 
# # Demo class that is used by the demo.py application.
95
 
#===============================================================================
96
 
class Demo(HasTraits):
97
 
    plot = Instance(Component)
98
 
 
99
 
    traits_view = View(
100
 
                    Group(
101
 
                        Item('plot', editor=ComponentEditor(size=size),
102
 
                             show_label=False),
103
 
                        orientation = "vertical"),
104
 
                    resizable=True, title=title,
105
 
                    width=size[0], height=size[1]
106
 
                    )
107
 
 
108
 
    def _plot_default(self):
109
 
         return _create_plot_component()
110
 
 
111
 
demo = Demo()
112
 
 
113
 
if __name__ == "__main__":
114
 
    demo.configure_traits()
115
 
 
116
 
#--EOF---