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

« back to all changes in this revision

Viewing changes to examples/demo/financial_plot_dates.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.  This differs from the financial_plot.py example
4
 
in that it uses a date-oriented axis.
5
 
"""
6
 
 
7
 
# Major library imports
8
 
from numpy import abs, cumprod, linspace, random
9
 
import time
10
 
 
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
15
 
 
16
 
# Chaco imports
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
21
 
 
22
 
 
23
 
from chaco.scales.api import CalendarScaleSystem
24
 
from chaco.scales_tick_generator import ScalesTickGenerator
25
 
 
26
 
 
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".
31
 
    """
32
 
    units_map = { "weeks" : 7*24*3600,
33
 
                  "days" : 24*3600,
34
 
                  "hours" : 3600,
35
 
                  "minutes" : 60,
36
 
                  "seconds" : 1 }
37
 
    now = time.time()
38
 
    dt = units_map[units]
39
 
    dates = linspace(now, now+numpoints*dt, numpoints)
40
 
    return dates
41
 
 
42
 
#===============================================================================
43
 
# # Create the Chaco plot.
44
 
#===============================================================================
45
 
def _create_plot_component():
46
 
 
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.
52
 
    numpoints = 500
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)
57
 
 
58
 
    time_ds = ArrayDataSource(index)
59
 
    vol_ds = ArrayDataSource(volume, sort_order="none")
60
 
    price_ds = ArrayDataSource(price, sort_order="none")
61
 
 
62
 
    xmapper = LinearMapper(range=DataRange1D(time_ds))
63
 
    vol_mapper = LinearMapper(range=DataRange1D(vol_ds))
64
 
    price_mapper = LinearMapper(range=DataRange1D(price_ds))
65
 
 
66
 
    price_plot = FilledLinePlot(index = time_ds, value = price_ds,
67
 
                                index_mapper = xmapper,
68
 
                                value_mapper = price_mapper,
69
 
                                edge_color = "blue",
70
 
                                face_color = "paleturquoise",
71
 
                                bgcolor = "white",
72
 
                                border_visible = True)
73
 
    price_plot.overlays.append(PlotAxis(price_plot, orientation='left')),
74
 
 
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
81
 
 
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",
85
 
                                          always_on=True,
86
 
                                          tool_mode="range",
87
 
                                          axis="index",
88
 
                                          max_zoom_out_factor=10.0,
89
 
                                         ))
90
 
 
91
 
    vol_plot = BarPlot(index = time_ds, value = vol_ds,
92
 
                       index_mapper = xmapper,
93
 
                       value_mapper = vol_mapper,
94
 
                       line_color = "transparent",
95
 
                       fill_color = "black",
96
 
                       bar_width = 1.0,
97
 
                       bar_width_type = "screen",
98
 
                       antialias = False,
99
 
                       height = 100,
100
 
                       resizable = "h",
101
 
                       bgcolor = "white",
102
 
                       border_visible = True)
103
 
 
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"))
110
 
 
111
 
    container = VPlotContainer(bgcolor = "lightblue",
112
 
                               spacing = 40,
113
 
                               padding = 50,
114
 
                               fill_padding=False)
115
 
    container.add(vol_plot)
116
 
    container.add(price_plot)
117
 
    container.overlays.append(PlotLabel("Financial Plot with Date Axis",
118
 
                                        component=container,
119
 
                                        #font="Times New Roman 24"))
120
 
                                        font="Arial 24"))
121
 
 
122
 
    return container
123
 
 
124
 
#===============================================================================
125
 
# Attributes to use for the plot view.
126
 
size=(800,600)
127
 
title="Financial plot example"
128
 
 
129
 
#===============================================================================
130
 
# # Demo class that is used by the demo.py application.
131
 
#===============================================================================
132
 
class Demo(HasTraits):
133
 
    plot = Instance(Component)
134
 
 
135
 
    traits_view = View(
136
 
                    Group(
137
 
                        Item('plot', editor=ComponentEditor(size=size),
138
 
                             show_label=False),
139
 
                        orientation = "vertical"),
140
 
                    resizable=True, title=title,
141
 
                    width=size[0], height=size[1]
142
 
                    )
143
 
 
144
 
    def _plot_default(self):
145
 
         return _create_plot_component()
146
 
 
147
 
demo = Demo()
148
 
 
149
 
if __name__ == "__main__":
150
 
    demo.configure_traits()
151
 
 
152
 
#--EOF---