Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Fix ImageInspectorTool event key (backward INcompatible) #453

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions chaco/tools/image_inspector_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ImageInspectorTool(BaseTool):

# Stores the value of self.visible when the mouse leaves the tool,
# so that it can be restored when the mouse enters again.
_old_visible = Enum(None, True, False) #Trait(None, Bool(True))
_old_visible = Enum(None, True, False)

def normal_key_pressed(self, event):
if self.inspector_key.match(event):
Expand Down Expand Up @@ -64,19 +64,14 @@ def normal_mouse_move(self, event):

x_index, y_index = ndx
image_data = plot.value
new_value = {"indices": ndx,
"data_value": image_data.data[y_index, x_index]}
if hasattr(plot, "_cached_mapped_image") and \
plot._cached_mapped_image is not None:
self.new_value = \
{"indices": ndx,
"data_value": image_data.data[y_index, x_index],
"color_value": plot._cached_mapped_image[y_index,
x_index]}

else:
self.new_value = \
{"indices": ndx,
"color_value": image_data.data[y_index, x_index]}
new_value["color_value"] = \
plot._cached_mapped_image[y_index, x_index]

self.new_value = new_value
self.last_mouse_position = (event.x, event.y)
return

Expand Down
71 changes: 71 additions & 0 deletions examples/demo/basic/dataframe_scatter_inspector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
""" Overlay to display the data attached to a scatter point hovered over.
"""
import pandas as pd
import numpy as np

from traits.api import Callable, Enum, HasTraits, Instance, on_trait_change, \
Str
from traitsui.api import View, Item
from enable.api import ComponentEditor
from chaco.api import Plot, ArrayPlotData, ScatterInspectorOverlay
from chaco.tools.api import ScatterInspector
from chaco.overlays.api import DataFrameScatterOverlay, TextBoxOverlay


def _create_plot_component():
# Create a fake dataset from which 2 dimensions will be displayed in a
# scatter plot:
x = np.random.uniform(0.0, 10.0, 50)
y = np.random.uniform(0.0, 5.0, 50)
data = pd.DataFrame({"x": x, "y": y,
"dataset": np.random.choice(list("abcdefg"), 50)})
plot_data = ArrayPlotData(x=x, y=y)
plot = Plot(plot_data)
scatter = plot.plot(("x", "y"), type="scatter")[0]

# Attach the inspector and its overlays
inspector = ScatterInspector(component=scatter)
scatter.tools.append(inspector)

text_overlay = DataframeScatterOverlay(component=plot,
inspector=inspector,
source_df=data,
bgcolor="black", alpha=0.6,
text_color="white",
border_color='none')
plot.overlays.append(text_overlay)

# Optional: add an overlay on the point to confirm what is hovered over
# Note that this overlay magically knows about hovered points by
# listening to renderer events rather than inspector events:
point_overlay = ScatterInspectorOverlay(component=scatter,
hover_color="red",
hover_marker_size=6)
scatter.overlays.append(point_overlay)
return plot


# =============================================================================
# Demo class that is used by the demo.py application.
# =============================================================================

size = (900, 500)
title = "Tooltip demo"


class Demo(HasTraits):
plot = Instance(Plot)

traits_view = View(
Item('plot', editor=ComponentEditor(size=size), show_label=False),
resizable=True, title=title
)

def _plot_default(self):
return _create_plot_component()


demo = Demo()

if __name__ == "__main__":
demo.configure_traits()