Ticket #22: yvals-above-bars.diff

File yvals-above-bars.diff, 2.1 kB (added by aprzywecki@…, 3 years ago)

show yvals above/beside bars (with option to put values inside bars)

  • pycha/chart.py

     
    659659        shadow=True, 
    660660        width=2 
    661661    ), 
     662    showyvals=Option( 
     663        hide=True, 
     664        fontSize=11, 
     665        insideBar=False 
     666    ), 
    662667    fillOpacity=1.0, 
    663668    shouldFill=True, 
    664669    barWidthFillFraction=0.75, 
  • pycha/bar.py

     
    9191                if not self.options.stroke.hide: 
    9292                    cx.set_source_rgb(*hex2rgb(self.options.stroke.color)) 
    9393                    cx.stroke() 
     94                     
     95            # render yvals above/beside bars 
     96            if not self.options.showyvals.hide: 
     97                cx.save() 
     98                cx.set_font_size(self.options.showyvals.fontSize) 
     99                cx.set_source_rgba(0, 0, 0, 1) 
     100                label = unicode(bar.yval) 
     101                extents = cx.text_extents(label) 
     102                labelWidth = extents[2] 
     103                labelHeight = extents[3] 
     104                #check if we have a horizontal bar chart (bar.x == 0) 
     105                if bar.x == 0: 
     106                    if self.options.showyvals.insideBar: 
     107                        cx.move_to(x + w - (3.0 * labelWidth),  
     108                                   y + (h / 2.0) + (labelHeight / 2.0)) 
     109                    else: 
     110                        cx.move_to(x + w+ labelWidth,  
     111                                   y + (h / 2.0) + (labelHeight / 2.0)) 
     112                else: 
     113                    if self.options.showyvals.insideBar: 
     114                        cx.move_to(x + (w / 2.0) - (labelWidth / 2.0),  
     115                                   y + (2.0 * labelHeight)) 
     116                    else: 
     117                        cx.move_to(x + (w / 2.0) - (labelWidth / 2.0),  
     118                                   y - labelHeight) 
     119                cx.show_text(label) 
     120                cx.restore() 
    94121 
    95122        cx.save() 
    96123        for bar in self.bars: