Changeset 149 for trunk

Show
Ignore:
Timestamp:
02/07/09 03:38:31 (3 years ago)
Author:
lgs
Message:

Add an option to draw the y vals on the bars. See #22. Patch contributed by Adam and polished by Lorenzo

Location:
trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/chavier/app.py

    r148 r149  
    101101            width=int, 
    102102            ), 
     103        yvals=dict( 
     104            show=bool, 
     105            inside=bool, 
     106            fontSize=int, 
     107            fontColor=str 
     108            ), 
    103109        fillOpacity=float, 
    104110        shouldFill=bool, 
  • trunk/pycha/bar.py

    r140 r149  
    9393                    cx.stroke() 
    9494 
     95            # render yvals above/beside bars 
     96            if self.options.yvals.show: 
     97                cx.save() 
     98                cx.set_font_size(self.options.yvals.fontSize) 
     99                cx.set_source_rgb(*hex2rgb(self.options.yvals.fontColor)) 
     100 
     101                label = unicode(bar.yval) 
     102                extents = cx.text_extents(label) 
     103                labelW = extents[2] 
     104                labelH = extents[3] 
     105 
     106                self._renderYVal(cx, label, labelW, labelH, x, y, w, h) 
     107 
     108                cx.restore() 
     109 
    95110        cx.save() 
    96111        for bar in self.bars: 
    97112            drawBar(bar) 
    98113        cx.restore() 
     114 
     115    def _renderYVal(self, cx, label, width, height, x, y, w, h): 
     116        raise NotImplementedError 
    99117 
    100118 
     
    129147        return (x-2, y-2, w+4, h+2) 
    130148 
     149    def _renderYVal(self, cx, label, labelW, labelH, barX, barY, barW, barH): 
     150        x = barX + (barW / 2.0) - (labelW / 2.0) 
     151        if self.options.yvals.inside: 
     152            y = barY + (1.5 * labelH) 
     153        else: 
     154            y = barY - 0.5 * labelH 
     155 
     156        # if the label doesn't fit below the bar, put it above the bar 
     157        if y > (barY + barH): 
     158            y = barY - 0.5 * labelH 
     159 
     160        cx.move_to(x, y) 
     161        cx.show_text(label) 
     162 
    131163 
    132164class HorizontalBarChart(BarChart): 
     
    187219        cx.stroke() 
    188220 
     221    def _renderYVal(self, cx, label, labelW, labelH, barX, barY, barW, barH): 
     222        y = barY + (barH / 2.0) + (labelH / 2.0) 
     223        if self.options.yvals.inside: 
     224            x = barX + barW - (1.2 * labelW) 
     225        else: 
     226            x = barX + barW + 0.2 * labelW 
     227 
     228        # if the label doesn't fit to the left of the bar, put it to the right 
     229        if x < barX: 
     230            x = barX + barW + 0.2 * labelW 
     231 
     232        cx.move_to(x, y) 
     233        cx.show_text(label) 
     234 
    189235 
    190236class Rect(object): 
  • trunk/pycha/chart.py

    r142 r149  
    660660        width=2 
    661661    ), 
     662    yvals=Option( 
     663        show=False, 
     664        inside=False, 
     665        fontSize=11, 
     666        fontColor='#000000', 
     667    ), 
    662668    fillOpacity=1.0, 
    663669    shouldFill=True,