| 1 |
# (C) Copyright 2007-2025 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
# Local imports. |
| 12 |
from attractors.model.i_plottable_2d import IPlottable2d |
| 13 |
|
| 14 |
# Enthought library imports. |
| 15 |
from chaco.api import ArrayPlotData, Plot |
| 16 |
from enable.api import ComponentEditor |
| 17 |
|
| 18 |
from pyface.tasks.api import TraitsTaskPane |
| 19 |
from traits.api import ( |
| 20 |
Dict, |
| 21 |
Instance, |
| 22 |
List, |
| 23 |
observe, |
| 24 |
on_trait_change, |
| 25 |
Property, |
| 26 |
Str, |
| 27 |
) |
| 28 |
from traitsui.api import EnumEditor, HGroup, Item, Label, UItem, View |
| 29 |
|
| 30 |
|
| 31 |
class Plot2dPane(TraitsTaskPane): |
| 32 |
#### 'ITaskPane' interface ################################################ |
| 33 |
|
| 34 |
id = "example.attractors.plot_2d_pane" |
| 35 |
name = "Plot 2D Pane" |
| 36 |
|
| 37 |
#### 'Plot2dPane' interface ############################################### |
| 38 |
|
| 39 |
active_model = Instance(IPlottable2d) |
| 40 |
models = List(IPlottable2d) |
| 41 |
|
| 42 |
plot_type = Property(Str, observe="active_model.plot_type") |
| 43 |
title = Property(Str, observe="active_model.name") |
| 44 |
x_data = Property(observe="active_model.x_data") |
| 45 |
y_data = Property(observe="active_model.y_data") |
| 46 |
x_label = Property(Str, observe="active_model.x_label") |
| 47 |
y_label = Property(Str, observe="active_model.y_label") |
| 48 |
plot = Instance(Plot) |
| 49 |
|
| 50 |
def _plot_default(self): |
| 51 |
plot = Plot(ArrayPlotData(x=self.x_data, y=self.y_data)) |
| 52 |
plot.x_axis.title = self.x_label |
| 53 |
plot.y_axis.title = self.y_label |
| 54 |
|
| 55 |
plot.plot( |
| 56 |
("x", "y"), |
| 57 |
type=self.plot_type, |
| 58 |
name=self.title, |
| 59 |
marker="pixel", |
| 60 |
color="blue", |
| 61 |
) |
| 62 |
|
| 63 |
return plot |
| 64 |
|
| 65 |
@observe("x_data,y_data") |
| 66 |
def _update_plot_data(self, event): |
| 67 |
if event.name == "x_data": |
| 68 |
self.plot.data.set_data("x", event.new) |
| 69 |
else: |
| 70 |
self.plot.data.set_data("y", event.new) |
| 71 |
self.plot.invalidate_and_redraw() |
| 72 |
|
| 73 |
@observe("x_label,y_label") |
| 74 |
def _update_axis_label(self, event): |
| 75 |
if event.name == "x_label": |
| 76 |
self.plot.x_axis.title = event.new |
| 77 |
else: |
| 78 |
self.plot.y_axis.title = event.new |
| 79 |
self.plot.invalidate_and_redraw() |
| 80 |
|
| 81 |
@observe("active_model") |
| 82 |
def _update_plot_new_model(self, event): |
| 83 |
if event.old: |
| 84 |
self.plot.delplot(event.old.name) |
| 85 |
|
| 86 |
self.plot.data.set_data("x", event.new.x_data) |
| 87 |
self.plot.data.set_data("y", event.new.y_data) |
| 88 |
self.plot.plot( |
| 89 |
("x", "y"), |
| 90 |
type=self.plot_type, |
| 91 |
name=self.title, |
| 92 |
marker="pixel", |
| 93 |
color="blue", |
| 94 |
) |
| 95 |
self.plot.invalidate_and_redraw() |
| 96 |
|
| 97 |
view = View( |
| 98 |
HGroup( |
| 99 |
Label("Model: "), |
| 100 |
Item("active_model", editor=EnumEditor(name="_enum_map")), |
| 101 |
show_labels=False, |
| 102 |
), |
| 103 |
UItem("plot", editor=ComponentEditor()), |
| 104 |
resizable=True, |
| 105 |
) |
| 106 |
|
| 107 |
#### Private traits ####################################################### |
| 108 |
|
| 109 |
_enum_map = Dict(IPlottable2d, Str) |
| 110 |
|
| 111 |
########################################################################### |
| 112 |
# Protected interface. |
| 113 |
########################################################################### |
| 114 |
|
| 115 |
#### Trait property getters/setters ####################################### |
| 116 |
|
| 117 |
def _get_plot_type(self): |
| 118 |
return self.active_model.plot_type if self.active_model else "line" |
| 119 |
|
| 120 |
def _get_title(self): |
| 121 |
return self.active_model.name if self.active_model else "" |
| 122 |
|
| 123 |
def _get_x_data(self): |
| 124 |
return self.active_model.x_data if self.active_model else [] |
| 125 |
|
| 126 |
def _get_y_data(self): |
| 127 |
return self.active_model.y_data if self.active_model else [] |
| 128 |
|
| 129 |
def _get_x_label(self): |
| 130 |
return self.active_model.x_label if self.active_model else "" |
| 131 |
|
| 132 |
def _get_y_label(self): |
| 133 |
return self.active_model.y_label if self.active_model else "" |
| 134 |
|
| 135 |
#### Trait change handlers ################################################ |
| 136 |
|
| 137 |
@on_trait_change("models[]") |
| 138 |
def _update_models(self): |
| 139 |
# Make sure that the active model is valid with the new model list. |
| 140 |
if self.active_model not in self.models: |
| 141 |
self.active_model = self.models[0] if self.models else None |
| 142 |
|
| 143 |
# Refresh the EnumEditor map. |
| 144 |
self._enum_map = dict((model, model.name) for model in self.models) |