Skip to content

Commit

Permalink
Convert text plot to use a value set instead of a formatter.
Browse files Browse the repository at this point in the history
  • Loading branch information
corranwebster committed Aug 20, 2014
1 parent 06aebf6 commit d055805
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
26 changes: 24 additions & 2 deletions chaco/plot.py
Expand Up @@ -990,7 +990,11 @@ def plot_1d(self, data, type='scatter_1d', name=None, orientation=None,
index_range = self.index_range
index_mapper = self.index_mapper
self.index_scale = scale
else:
raise ValueError("Unknown plot type: " + plot_type)

if plot_type in ("scatter_1d", "line_scatter_1d", "jitterplot"):
# simple 1d positional plots with no associated value
for source in data:
index = self._get_or_create_datasource(source)
index_range.add(index)
Expand All @@ -1010,8 +1014,26 @@ def plot_1d(self, data, type='scatter_1d', name=None, orientation=None,
**styles)
plots.append(plot)
self.add(plot)
else:
raise ValueError("Unknown plot type: " + plot_type)
elif plot_type in ("textplot_1d",):
# simple positional plots with a single associated value
for source in data[1:]:
value = self._get_or_create_datasource(source)

if scale == "linear":
imap = LinearMapper(range=index_range,
stretch_data=index_mapper.stretch_data)
else:
imap = LogMapper(range=index_range,
stretch_data=index_mapper.stretch_data)
cls = self.renderer_map[plot_type]
plot = cls(index=index,
index_mapper=imap,
value=value,
orientation=orientation,
direction=direction,
**styles)
plots.append(plot)
self.add(plot)

self.plots[name] = plots
return plots
Expand Down
37 changes: 16 additions & 21 deletions chaco/text_plot_1d.py
Expand Up @@ -11,25 +11,20 @@
from numpy import array, empty

# Enthought library imports
from chaco.api import Label
from chaco.api import ArrayDataSource, Label
from enable.api import black_color_trait
from kiva.trait_defs.kiva_font_trait import KivaFont
from traits.api import Bool, Callable, Enum, Float, Int, List, on_trait_change
from traits.api import Bool, Enum, Float, Int, Instance, List, on_trait_change

# local imports
from .base_1d_plot import Base1DPlot


def default_formatter(val):
""" Format a index value as a string """
return ("%f" % val).rstrip("0").rstrip(".")


class TextPlot1D(Base1DPlot):
""" A plot that positions textual labels in 1D """

#: label formatter: callable that given an index, returns appropriate text
formatter = Callable(default_formatter)
#: text values corresponding to indices
value = Instance(ArrayDataSource)

#: The font of the tick labels.
text_font = KivaFont('modern 10')
Expand Down Expand Up @@ -74,18 +69,14 @@ class TextPlot1D(Base1DPlot):

def _compute_labels(self, gc):
"""Generate the Label instances for the plot. """
formatter = self.formatter

def build_label(val):
text = formatter(val) if formatter is not None else str(val)
return Label(text=text,
font=self.text_font,
color=self.text_color,
rotate_angle=self.text_rotate_angle,
margin=self.text_margin)


self._label_cache = [build_label(val) for val in self.index.get_data()]
self._label_cache = [
Label(
text=text,
font=self.text_font,
color=self.text_color,
rotate_angle=self.text_rotate_angle,
margin=self.text_margin
) for text in self.value.get_data()]
self._label_box_cache = [array(label.get_bounding_box(gc), float)
for label in self._label_cache]
self._label_cache_valid = True
Expand Down Expand Up @@ -153,6 +144,10 @@ def _invalidate(self):
self._screen_cache_valid = False
self._label_cache_valid = False

@on_trait_change("value.data_changed")
def _invalidate_labels(self):
self._label_cache_valid = False

def _bounds_changed(self, old, new):
super(TextPlot1D, self)._bounds_changed(old, new)
self._text_position = self._get_text_position()
Expand Down

0 comments on commit d055805

Please sign in to comment.