Skip to content

Commit

Permalink
Merge pull request #363 from enthought/feature/customize_axis_title_a…
Browse files Browse the repository at this point in the history
…ngle

Feature: allow user control over axis title angle
  • Loading branch information
jvkersch authored Jul 11, 2017
2 parents 997ec7e + 25451de commit 7e48209
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
23 changes: 17 additions & 6 deletions chaco/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class PlotAxis(AbstractOverlay):
# The color of the title.
title_color = ColorTrait("black")

# The angle of the title, in degrees, from horizontal line
title_angle = Float(0.)

# The thickness (in pixels) of each tick.
tick_weight = Float(1.0)

Expand Down Expand Up @@ -535,7 +538,6 @@ def _calculate_geometry(self):
self._minor_axis_size = self.bounds[1]
self._major_axis = array([1., 0.])
self._title_orientation = array([0.,1.])
self.title_angle = 0.0
if self.orientation == 'top':
self._origin_point = array(self.position)
self._inside_vector = array([0.,-1.])
Expand All @@ -553,11 +555,9 @@ def _calculate_geometry(self):
if self.orientation == 'left':
self._origin_point = array(self.position) + array([self.bounds[0], 0.])
self._inside_vector = array([1., 0.])
self.title_angle = 90.0
else: #self.orientation == 'right'
self._origin_point = array(self.position)
self._inside_vector = array([-1., 0.])
self.title_angle = 270.0
if "top" in origin:
screenlow, screenhigh = screenhigh, screenlow

Expand All @@ -584,7 +584,6 @@ def _calculate_geometry_overlay(self, overlay_component=None):
self._minor_axis_size = overlay_component.bounds[1]
self._major_axis = array([1., 0.])
self._title_orientation = array([0.,1.])
self.title_angle = 0.0
if self.orientation == 'top':
self._origin_point = array([overlay_component.x, overlay_component.y2])
self._inside_vector = array([0.0, -1.0])
Expand All @@ -602,11 +601,9 @@ def _calculate_geometry_overlay(self, overlay_component=None):
if self.orientation == 'left':
self._origin_point = array([overlay_component.x, overlay_component.y])
self._inside_vector = array([1.0, 0.0])
self.title_angle = 90.0
else:
self._origin_point = array([overlay_component.x2, overlay_component.y])
self._inside_vector = array([-1.0, 0.0])
self.title_angle = 270.0
if "top" in component_origin:
screenlow, screenhigh = screenhigh, screenlow

Expand Down Expand Up @@ -728,6 +725,7 @@ def _anytrait_changed(self, name, old, new):
'title_font',
'title_spacing',
'title_color',
'title_angle',
'tick_weight',
'tick_color',
'tick_label_font',
Expand Down Expand Up @@ -756,6 +754,18 @@ def _anytrait_changed(self, name, old, new):
if name in invalidate_traits:
self._invalidate()

# ------------------------------------------------------------------------
# Initialization-related methods
# ------------------------------------------------------------------------

def _title_angle_default(self):
if self.orientation == 'left':
return 90.0
if self.orientation == 'right':
return 270.0
# Then self.orientation in {'top', 'bottom'}
return 0.0

#------------------------------------------------------------------------
# Persistence-related methods
#------------------------------------------------------------------------
Expand Down Expand Up @@ -795,6 +805,7 @@ def __setstate__(self, state):
self._cache_valid = False
return


class MinorPlotAxis(PlotAxis):
"""
The MinorPlotAxis is a PlotAxis which draws ticks with a smaller interval,
Expand Down
5 changes: 4 additions & 1 deletion docs/source/user_manual/basic_elements/overlays.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,17 @@ These attributes control the appearance of the axis:
:attr:`~chaco.axis.PlotAxis.title_font`,
:attr:`~chaco.axis.PlotAxis.title_color`,
:attr:`~chaco.axis.PlotAxis.title_spacing`
:attr:`~chaco.axis.PlotAxis.title_angle`

Define the axis label. :attr:`title` is a string or unicode object
that is rendered using the given font and color. :attr:`title_font` is
a string describing a font (e.g. '12 pt bold italic',
'swiss family Arial' or 'default 12'; see
:class:`~kiva.kiva_font_trait.TraitKivaFont` for details).
Finally, :attr:`title_spacing` is the space between the axis line and the
title (either the number of pixels or 'auto', default).
title (either the number of pixels or 'auto', default) and
:attr:`title_angle` can be overridden to change the rotation angle (in deg,
wrt horizontal).


:attr:`~chaco.axis.PlotAxis.tick_weight`,
Expand Down
10 changes: 8 additions & 2 deletions examples/demo/basic/line_plot1.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ def _create_plot_component():
pd.set_data("y" + str(i), jn(i,x))

# Create some line plots of some of the data
plot1 = Plot(pd, title="Line Plot", padding=50, border_visible=True)
plot1 = Plot(pd, title="Line Plot", padding=60, border_visible=True)
plot1.legend.visible = True
plot1.plot(("index", "y0", "y1", "y2"), name="j_n, n<3", color="red")
plot1.plot(("index", "y3"), name="j_3", color="blue")
plot1.value_axis.title = "J0, J1, J2, J3"

# Attach some tools to the plot
plot1.tools.append(PanTool(plot1))
Expand All @@ -46,10 +47,15 @@ def _create_plot_component():

# Create a second scatter plot of one of the datasets, linking its
# range to the first plot
plot2 = Plot(pd, range2d=plot1.range2d, title="Scatter plot", padding=50,
plot2 = Plot(pd, range2d=plot1.range2d, title="Scatter plot", padding=60,
border_visible=True)
plot2.plot(('index', 'y3'), type="scatter", color="blue", marker="circle")

# Configure the vertical axis:
plot2.value_axis.title = "J3"
plot2.value_axis.orientation = "right"
plot2.value_axis.title_angle = 0.0 # instead of default 270 deg

# Create a container and add our plots
container = HPlotContainer()
container.add(plot1)
Expand Down

0 comments on commit 7e48209

Please sign in to comment.