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

Update label for ColorBarWidget (which relies on ColorBarVisual) #1251

Closed
macrocosme opened this issue Jul 4, 2016 · 1 comment · Fixed by #1271
Closed

Update label for ColorBarWidget (which relies on ColorBarVisual) #1251

macrocosme opened this issue Jul 4, 2016 · 1 comment · Fixed by #1271

Comments

@macrocosme
Copy link
Contributor

macrocosme commented Jul 4, 2016

Hi,

I'll start with that I really like vispy! I wonder if the following would be worth a pull request.

Using vispy 0.5.0.dev0, I was trying to update dynamically the colorbar label from ColorBarWidget.

(1) To set the label from the colorbar widget (ColorBarWidget), I need to pass the label_str directly as the 'label' parameter isn't linked to anything. This works well. This means that the call to ColorBarVisual in ColorBarWidget could be changed to

def __init__(self, cmap, orientation,
                 label="", clim=("", ""),
                 border_width=0.0, border_color="black", **kwargs):

        dummy_size = (1, 1)
        self._colorbar = ColorBarVisual(size=dummy_size, cmap=cmap,
                                        orientation=orientation,
                                        **label_str**=label, clim=clim,
                                        border_width=border_width,
                                        border_color=border_color, **kwargs)

(2) To update it I tried two things. First, I try to simply recreate (self.grid.remove_widget followed by self.grid.add_widget) the whole widget and add it back to the grid. E.g.

cbar = scene.ColorBarWidget(orientation=position,
                                    label_str=label,
                                    cmap=cmap,
                                    clim=clim,
                                    border_width=border_width,
                                    border_color=border_color,
                                    **kwargs)

self.grid.remove_widget(self.cbar)
self.cbar_left = self.grid.add_widget(self.cbar, row=0, col=0)
self.cbar_left.width_max = self.cbar_left.width_min = CBAR_LONG_DIM

Somehow, it doesn't refresh and I end up with the previous text being drawn underneath the new text -- which makes everything unreadable.

The second thing I tried is keep track of cbar and simply update its parameters. E.g.

self.cbar.clim = clim
self.cbar.label = label
self.cbar.cmap = cmap 

It works fine for clim and cmap with no change. However, the setter for label directly calls ColorBarVisual @label.setter which should not accept text directly as in init, it is set via label_str as follow:

self._label_str = label_str
self._label = TextVisual(text=self._label_str)

Adding a setter for label_str can be used to then update it via the label setter. So that in ColorBarWidget, the setter for label would be modifying self.label_str. This work fine. E.g.

# In ColorBarWidget
@label.setter
def label(self, label):
        self._colorbar.label_str = label

# In ColorBarVisual
@property
def label(self):
        """ The vispy.visuals.TextVisual associated with the label
        """
        return self._label

@label.setter
def label(self, label):
        self._label = label
        self._update()

@property
def label_str(self):
        return self._label_str

@label_str.setter
def label_str(self, label_str):
        self._label_str = label_str
        self._update()

As a side note, I also added the following to ColorBarWidget so that I can modify the color and size of the ticks. I guess this could be added to the lot.

@property
def ticks(self):
        return self._colorbar.ticks

@ticks.setter
def ticks(self, color, font_size):
        self._colorbar.ticks[0].font_size = font_size
        self._colorbar.ticks[1].font_size = font_size
        self._colorbar.ticks[0].color = color
        self._colorbar.ticks[1].color = color
@kmuehlbauer
Copy link
Contributor

@macrocosme

Have the same problem in my application. In #1271 I tried to get all needed functionality within ColorBarWidget and ColorBarVisual without changing the calling conventions (just adding to them).

macrocosme added a commit to macrocosme/vispy that referenced this issue Nov 3, 2016
sylm21 pushed a commit to sylm21/vispy that referenced this issue Mar 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants