enthought / chaco

Chaco is a Python package for building interactive and custom 2-D plots.
http://docs.enthought.com/chaco/
Other
292 stars 99 forks source link

changing TextPlot color & font #729

Closed fred4ets closed 1 year ago

fred4ets commented 3 years ago

Hello,

I would like to change TextPlot color & font.

Initialization is ok, but when the color trait is changed for instance, the color is not changed in the display.

There is the same issue with the font trait.

Here's the CME.

#! /usr/bin/env python3

import numpy as np

from chaco.api import ArrayPlotData, Plot
from enable.api import ComponentEditor
from traits.api import (
    Array,
    Color,
    Font,
    HasTraits,
    Instance,
    List
)
from traitsui.api import Item, UItem, View

class Data(HasTraits):

    data = Array
    labels = List
    color = Color
    font = Font
    plot = Instance(Plot)

    def _data_default(self):
        data = np.array([[0, 0], [0, 1], [1, 1], [1, 0]])
        return (data)

    def _labels_default(self):
        labels = ['A', 'B', 'C', 'D']
        return (labels)

    def _plot_default(self):
        self.plotdata = ArrayPlotData()
        self.plotdata.set_data('x', self.data[:, 0])
        self.plotdata.set_data('y', self.data[:, 1])
        self.plotdata.set_data('labels', self.labels)

        plot = Plot(self.plotdata)
        plot.range2d.set_bounds((-1, -1), (2, 2))
        plot.plot(("x", "y"),
                  type='scatter',
                  marker='dot',
                  color=self.color)
        plot.plot(("x", "y", "labels"),
                  type='text',
                  text_margin=4,
                  h_position='right',
                  text_offset=(4, 4),
                  text_color=self.color)

        return plot

    traits_view = View(
        UItem(
            "plot",
            editor=ComponentEditor(),
            resizable=True
        ),
        UItem('color',
              style='simple'),
        UItem('font'),
        resizable=True,
        buttons=["OK"],
        width=900,
        height=800,
    )

    def _color_changed(self):
        self.plot.plots['plot0'][0].color = self.color
        self.plot.plots['plot1'][0].text_color = self.color

    def _font_changed(self):
        name = self.font.family()
        size = self.font.pointSize()
        self.plot.plots['plot1'][0].text_font = '%s %d' % (name, size)

if __name__ == '__main__':
    viewer = Data(color=(255, 128, 64))
    viewer.configure_traits()

Thanks in advance for any help.

Regards

Debian 10 x86_64, Python 3.7.3, ETS source code from git repo

corranwebster commented 2 years ago

As a work-around, you can manually call invalidate_and_redraw() on the appropriate plot components in each of those change methods.

However the TextPlot should probably do that automatically.

corranwebster commented 2 years ago

This may just be a matter of adding requires_redraw metadata to the traits in TextPlot (see #595)

fred4ets commented 2 years ago

As a work-around, you can manually call invalidate_and_redraw() on the appropriate plot components in each of those change methods.

I tried your hint, but unhappily, it does not fix the issue, nothing changes.