1

我想使用 chaco 工具ScatterInspector和/或ScatterInspectorOverlayenaml。我已经设置了一个非常简单的控制器和视图(下面的源代码),但无法确定如何进行。我尝试遵循我发现的最小和旧示例,但没有成功。

如果我取消注释覆盖部分ScatterInspectorOverlay,代码将无法运行

文件“.../chaco/scatter_inspector_overlay.py”,第 51 行,如果不是 plot 或不是 plot.index 或不是 getattr(plot,“value”,True):

如果我注释掉覆盖部分,我当然不会得到我想要的覆盖行为,而且在移动鼠标时,得到

文件“.../chaco/tools/scatter_inspector.py”,第 48 行,在 normal_mouse_move index = plot.map_index((event.x, event.y), threshold=self.threshold)

view.enaml 来源:

from enaml.widgets.api import (
        Window, Container, EnableCanvas,
    )

enamldef ScatterView(Window):
    attr controller
    title = "Scatter Inspector Test"
    initial_size = (640,480)

    Container:

        EnableCanvas:
            component = controller.scatter_plot

控制器.py 来源:

import enaml
from enaml.stdlib.sessions import show_simple_view
from traits.api import HasTraits, Instance
from chaco.api import Plot, ArrayPlotData, ScatterInspectorOverlay
from chaco.tools.api import ScatterInspector
from numpy import linspace, sin

class ScatterController(HasTraits):
    scatter_plot = Instance(Plot)

    def _scatter_plot_default(self):
        # data
        x = linspace(-14, 14, 100)
        y = sin(x) * x**3
        plotdata = ArrayPlotData(x = x, y = y)

        # plot
        scatter_plot = Plot(plotdata)
        renderer = scatter_plot.plot(("x", "y"), type="scatter", color="red")

        # inspector
        scatter_plot.tools.append(ScatterInspector(scatter_plot))

        # overlay
        # scatter_plot.overlays.append( ScatterInspectorOverlay(
        #         scatter_plot,
        #         hover_color = 'red',
        #         hover_marker_size = 6,
        #         selection_marker_size = 6,
        #         selection_color = 'yellow',
        #         selection_outline_color='purple',
        #         selection_line_width = 3
        #     ))

        #return
        return scatter_plot

if __name__ == "__main__":
    with enaml.imports():
        from view import ScatterView

    main_controller = ScatterController()
    window = ScatterView(controller=ScatterController())
    show_simple_view(window)
4

1 回答 1

2

我上面的代码的问题是我添加ScatterInspector了 toscatter_plot而不是 torenderer并且我错过了[0]get 的索引renderer

不过,我真正想做的关键是在鼠标悬停在数据点上和/或选择数据点时收到通知。我添加了when_hover_or_selection_changes它显示了如何做到这一点。

工作控制器.py:

...
# plot
scatter_plot = Plot(plotdata)
renderer = scatter_plot.plot(("x", "y"), type="scatter", color="lightblue")[0]

# inspector
renderer.tools.append(ScatterInspector(renderer))

# overlay
renderer.overlays.append(ScatterInspectorOverlay(renderer,
                hover_color="red",
                hover_marker_size=6,
                selection_marker_size=6,
                selection_color="yellow",
                selection_outline_color="purple",
                selection_line_width=3))

...

# get notified when hover or selection changes
@on_trait_change('renderer.index.metadata')
def when_hover_or_selection_changes(self):
    print 'renderer.index.metadata = ', self.renderer.index.metadata    
于 2013-03-15T19:40:29.823 回答