Last active
August 29, 2015 14:13
-
-
Save kjordahl/169048a12f3f03dff2c3 to your computer and use it in GitHub Desktop.
Simple traits/chaco example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from traits.api import * | |
from traitsui.api import * | |
from chaco.api import Plot, ArrayPlotData | |
from enable.component_editor import ComponentEditor | |
class Rectangle(HasTraits): | |
width = Range(0.0, 10.0) | |
height = Range(0.0, 10.0) | |
x0 = Float(0.0) | |
y0 = Float(0.0) | |
perimeter = Property(Float, depends_on='height, width') | |
area = Property(Float, depends_on='height, width') | |
x = Property(Array, depends_on='x0, width') | |
y = Property(Array, depends_on='y0, height') | |
plot = Instance(Plot) | |
plot_data = Instance(ArrayPlotData) | |
def _get_perimeter(self): | |
return 2 * self.height + 2 * self.width | |
def _get_area(self): | |
return self.height * self.width | |
def _get_x(self): | |
""" Calculate x coordinates of corners """ | |
x1 = self.x0 + self.width | |
return np.array([self.x0, x1, x1, self.x0, self.x0]) | |
def _get_y(self): | |
""" Calculate y coordinates of corners """ | |
y1 = self.y0 + self.height | |
return np.array([self.y0, self.y0, y1, y1, self.y0]) | |
def _plot_default(self): | |
self.plot_data = ArrayPlotData(x=self.x, y=self.y) | |
plot = Plot(self.plot_data) | |
plot.plot(('x', 'y'), type='line', color='blue', line_width=2.0) | |
plot.x_axis.mapper.range.set(low=0.0, high=10.0) | |
plot.y_axis.mapper.range.set(low=0.0, high=10.0) | |
return plot | |
def _x_changed(self): | |
if self.plot_data is not None: | |
self.plot_data.set_data('x', self.x) | |
def _y_changed(self): | |
if self.plot_data is not None: | |
self.plot_data.set_data('y', self.y) | |
view = View(VGroup(Item('height'), | |
Item('width'), | |
Item('area', style='readonly'), | |
Item('perimeter', style='readonly'), | |
Item('plot', editor=ComponentEditor(), show_label=False, | |
width=500, height=500, resizable=True), | |
) | |
) | |
if __name__ == '__main__': | |
r = Rectangle(width=4.0, height=3.0) | |
r.configure_traits() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment