~ubuntu-branches/ubuntu/utopic/python-chaco/utopic

« back to all changes in this revision

Viewing changes to chaco/barplot.py

  • Committer: Package Import Robot
  • Author(s): Andrew Starr-Bochicchio
  • Date: 2014-06-01 17:04:08 UTC
  • mfrom: (7.2.5 sid)
  • Revision ID: package-import@ubuntu.com-20140601170408-m86xvdjd83a4qon0
Tags: 4.4.1-1ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
 - Let the binary-predeb target work on the usr/lib/python* directory
   as we don't have usr/share/pyshared anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
 
8
8
from numpy import array, compress, column_stack, invert, isnan, transpose, zeros
9
9
from traits.api import Any, Bool, Enum, Float, Instance, Property, \
10
 
        Range
 
10
        Range, Tuple, cached_property, on_trait_change
11
11
from enable.api import black_color_trait
12
12
from kiva.constants import FILL_STROKE
13
13
 
21
21
logger = logging.getLogger(__name__)
22
22
 
23
23
 
 
24
# TODO: make child of BaseXYPlot
 
25
 
24
26
class BarPlot(AbstractPlotRenderer):
25
27
    """
26
28
    A renderer for bar charts.
32
34
    value = Instance(ArrayDataSource)
33
35
 
34
36
    # The data source to use as "starting" values for the bars.
 
37
    # For instance, if the values are [10, 20] and starting_value
 
38
    # is [3, 7], BarPlot will plot two bars, one  between 3 and 10, and
 
39
    # one between 7 and 20
35
40
    starting_value = Instance(ArrayDataSource)
36
41
 
37
42
    # Labels for the indices.
73
78
    # Color to fill the bars.
74
79
    fill_color = black_color_trait
75
80
 
 
81
    # The RGBA tuple for rendering lines.  It is always a tuple of length 4.
 
82
    # It has the same RGB values as line_color_, and its alpha value is the
 
83
    # alpha value of self.line_color multiplied by self.alpha. 
 
84
    effective_line_color = Property(Tuple, depends_on=['line_color', 'alpha'])
 
85
    
 
86
    # The RGBA tuple for rendering the fill.  It is always a tuple of length 4.
 
87
    # It has the same RGB values as fill_color_, and its alpha value is the
 
88
    # alpha value of self.fill_color multiplied by self.alpha.   
 
89
    effective_fill_color = Property(Tuple, depends_on=['fill_color', 'alpha'])
 
90
 
76
91
    # Overall alpha value of the image. Ranges from 0.0 for transparent to 1.0
77
92
    alpha = Range(0.0, 1.0, 1.0)
78
93
 
134
149
        # Set any keyword Traits that were postponed.
135
150
        self.set(**postponed)
136
151
 
137
 
        # update colors to use the correct alpha channel
138
 
        self.line_color_ = self.line_color_[0:3] + (self.alpha,)
139
 
        self.fill_color_ = self.fill_color_[0:3] + (self.alpha,)
140
152
 
141
153
    def map_screen(self, data_array):
142
154
        """ Maps an array of data points into screen space and returns it as
266
278
        with gc:
267
279
            gc.clip_to_rect(self.x, self.y, self.width, self.height)
268
280
            gc.set_antialias(self.antialias)
269
 
            gc.set_stroke_color(self.line_color_)
270
 
            gc.set_fill_color(self.fill_color_)
 
281
            gc.set_stroke_color(self.effective_line_color)
 
282
            gc.set_fill_color(self.effective_fill_color)
271
283
            gc.set_line_width(self.line_width)
272
284
 
273
285
            if self.bar_width_type == "data":
314
326
 
315
327
    def _render_icon(self, gc, x, y, width, height):
316
328
        with gc:
317
 
            gc.set_fill_color(self.fill_color_)
318
 
            gc.set_stroke_color(self.line_color_)
 
329
            gc.set_fill_color(self.effective_fill_color)
 
330
            gc.set_stroke_color(self.effective_line_color)
319
331
            gc.rect(x+width/4, y+height/4, width/2, height/2)
320
332
            gc.draw_path(FILL_STROKE)
321
333
 
405
417
        self.invalidate_draw()
406
418
        self._cache_valid = False
407
419
 
408
 
    def _alpha_changed(self):
409
 
        self.line_color_ = self.line_color_[0:3] + (self.alpha,)
410
 
        self.fill_color_ = self.fill_color_[0:3] + (self.alpha,)
 
420
    @on_trait_change('line_color, line_width, fill_color, alpha')
 
421
    def _attributes_changed(self):
411
422
        self.invalidate_draw()
412
423
        self.request_redraw()
413
424
 
479
490
        self.invalidate_draw()
480
491
        self.request_redraw()
481
492
 
 
493
    #------------------------------------------------------------------------
 
494
    # Property getters
 
495
    #------------------------------------------------------------------------
 
496
 
 
497
    @cached_property
 
498
    def _get_effective_line_color(self):
 
499
        if len(self.line_color_) == 4:
 
500
            line_alpha = self.line_color_[-1]
 
501
        else:
 
502
            line_alpha = 1.0
 
503
        c = self.line_color_[:3] + (line_alpha * self.alpha,)
 
504
        return c
 
505
 
 
506
    @cached_property
 
507
    def _get_effective_fill_color(self):
 
508
        if len(self.fill_color_) == 4:
 
509
            fill_alpha = self.fill_color_[-1]
 
510
        else:
 
511
            fill_alpha = 1.0
 
512
        c = self.fill_color_[:3] + (fill_alpha * self.alpha,)
 
513
        return c
482
514
 
483
515
 
484
516
### EOF ####################################################################