class TimeSeriesViewer(BaseViewer): """ This class just contains the two data arrays that will be updated by the Controller. The visualization/editor for this class is a Chaco plot. """ class_name = 'TimeSeriesViewer' index = Array data = Array plot_type = Enum("line", "scatter") view = View( ChacoPlotItem( "index", "data", type_trait="plot_type", resizable=True, title='', x_label="Time", y_label="Signal", color="blue", bgcolor="white", border_visible=True, border_width=1, #padding_bg_color="lightgray", width=800, height=380, marker_size=2, show_label=False), HGroup(spring, Item("plot_type", style='custom'), spring), resizable=True, buttons=["OK"], width=800, height=500)
class Plot2dPane(TraitsTaskPane): #### 'ITaskPane' interface ################################################ id = 'example.attractors.plot_2d_pane' name = 'Plot 2D Pane' #### 'Plot2dPane' interface ############################################### active_model = Instance(IPlottable2d) models = List(IPlottable2d) plot_type = Property(Unicode, depends_on='active_model.plot_type') title = Property(Unicode, depends_on='active_model.name') x_data = Property(depends_on='active_model.x_data') y_data = Property(depends_on='active_model.y_data') x_label = Property(Unicode, depends_on='active_model.x_label') y_label = Property(Unicode, depends_on='active_model.y_label') view = View( HGroup( Label('Model: '), Item( 'active_model', editor=EnumEditor(name='_enum_map')), show_labels=False), ChacoPlotItem( 'x_data', 'y_data', show_label=False, resizable=True, orientation='h', marker='pixel', marker_size=1, type_trait='plot_type', title='', x_label_trait='x_label', y_label_trait='y_label', color='blue', bgcolor='white', border_visible=False, border_width=1), resizable=True) #### Private traits ####################################################### _enum_map = Dict(IPlottable2d, Unicode) ########################################################################### # Protected interface. ########################################################################### #### Trait property getters/setters ####################################### def _get_plot_type(self): return self.active_model.plot_type if self.active_model else 'line' def _get_title(self): return self.active_model.name if self.active_model else '' def _get_x_data(self): return self.active_model.x_data if self.active_model else [] def _get_y_data(self): return self.active_model.y_data if self.active_model else [] def _get_x_label(self): return self.active_model.x_label if self.active_model else '' def _get_y_label(self): return self.active_model.y_label if self.active_model else '' #### Trait change handlers ################################################ @on_trait_change('models[]') def _update_models(self): # Make sure that the active model is valid with the new model list. if self.active_model not in self.models: self.active_model = self.models[0] if self.models else None # Refresh the EnumEditor map. self._enum_map = dict((model, model.name) for model in self.models)
class Plotter(HasTraits): """ Cette classe contient uniquement les points a afficher, et la methode qui permet de les mettre a jour. """ # definition d'objets avec Traits x_data = Array y_data = Array length = Int(20) tick = Int(0) printed_points = Int(0) # pour positionner le grapher (le conteneur est la classe d'en dessous ( # Viewer) view = View(ChacoPlotItem("x_data", "y_data", resizable=True, x_label="Time", y_label="Signal", color="blue", bgcolor="white", border_visible=True, border_width=1, padding_bg_color="lightgray", width=800, height=380, marker_size=2, show_label=False), Item('length'), buttons=['OK'], resizable=True, width=800, height=500) def __init__(self): print('(toototo') def update_graph(self): """Methode appelee par le timer.""" recv = queue.get() self.nb_points = len(recv['time(sec)']) if self.tick == self.length: # pour creer un graph dynamique. cur_index = self.x_data[-self.length * self.nb_points:] cur_data = self.y_data[-self.length * self.nb_points:] else: cur_index = self.x_data cur_data = self.y_data self.tick += 1 new_y_data = np.hstack((cur_data, recv['signal'])) new_x_data = np.hstack((cur_index, recv['time(sec)'])) self.printed_points = len(new_x_data) self.x_data = new_x_data self.y_data = new_y_data return def _length_changed(self): print 'NEW LENGTH!', self.length if self.length < self.printed_points: self.tick = 0 self.x_data = self.x_data[-self.length * self.nb_points:] self.y_data = self.y_data[-self.length * self.nb_points:] else: pass
class Data(HasTraits): """ Traits class and elements required to model the equation. Volume and pressure hold lists for X,Y coords. attraction and total volume are provided by the user. Plot type will be shown as a drop down list. """ volume = Array attraction = Range(low=-50.0, high=50.0, value=0.0) totalVolume = Range(low=0.01, high=100.0, value=0.01) temperature = Range(low=-50.0, high=50.0, value=50.0) r_constant = Float(8.314472) plot_type = Enum('line', 'scatter') pressure = Property( Array, depends_on=['temperature', 'attraction', 'totalVolume']) def _volume_default(self): return arange(0.1, 100) def _get_pressure(self): return ((self.r_constant * self.temperature) / (self.volume - self.totalVolume) - (self.attraction / (self.volume * self.volume))) # Main GUI window defined by View instance # Contains all GUI elements including the plot # Link gui element by using an Item instance with the same name traits_view = View(ChacoPlotItem("volume", "pressure", type_trait="plot_type", resizable=True, x_label="Volume", y_label="Pressure", x_bounds=(-10, 120), x_auto=False, y_bounds=(-2000, 4000), y_auto=False, color="blue", bgcolor="white", border_visible=True, border_width=1, title='Pressure vs. Volume', padding_bg_color="lightgray"), Item(name='attraction'), Item(name='totalVolume'), Item(name='temperature'), Item(name='r_constant', style='readonly'), Item(name='plot_type'), resizable=True, buttons=["OK"], title='Van der Waal Equation', width=900, height=800) # Re-calculate when attraction, totVolume, or temperature are changed. @on_trait_change('attraction, totVolume, temperature') def calc(self): """ Update the data based on the numbers specified by the user. """ self.volume = arange(.1, 100) self.pressure = ((self.r_constant * self.temperature) / (self.volume - self.totalVolume) - (self.attraction / (self.volume * self.volume)))
class Plot2dPane(TraitsTaskPane): #### 'ITaskPane' interface ################################################ id = "example.attractors.plot_2d_pane" name = "Plot 2D Pane" #### 'Plot2dPane' interface ############################################### active_model = Instance(IPlottable2d) models = List(IPlottable2d) plot_type = Property(Str, depends_on="active_model.plot_type") title = Property(Str, depends_on="active_model.name") x_data = Property(depends_on="active_model.x_data") y_data = Property(depends_on="active_model.y_data") x_label = Property(Str, depends_on="active_model.x_label") y_label = Property(Str, depends_on="active_model.y_label") view = View( HGroup( Label("Model: "), Item("active_model", editor=EnumEditor(name="_enum_map")), show_labels=False, ), ChacoPlotItem( "x_data", "y_data", show_label=False, resizable=True, orientation="h", marker="pixel", marker_size=1, type_trait="plot_type", title="", x_label_trait="x_label", y_label_trait="y_label", color="blue", bgcolor="white", border_visible=False, border_width=1, ), resizable=True, ) #### Private traits ####################################################### _enum_map = Dict(IPlottable2d, Str) ########################################################################### # Protected interface. ########################################################################### #### Trait property getters/setters ####################################### def _get_plot_type(self): return self.active_model.plot_type if self.active_model else "line" def _get_title(self): return self.active_model.name if self.active_model else "" def _get_x_data(self): return self.active_model.x_data if self.active_model else [] def _get_y_data(self): return self.active_model.y_data if self.active_model else [] def _get_x_label(self): return self.active_model.x_label if self.active_model else "" def _get_y_label(self): return self.active_model.y_label if self.active_model else "" #### Trait change handlers ################################################ @on_trait_change("models[]") def _update_models(self): # Make sure that the active model is valid with the new model list. if self.active_model not in self.models: self.active_model = self.models[0] if self.models else None # Refresh the EnumEditor map. self._enum_map = dict((model, model.name) for model in self.models)