Changeset 870

Show
Ignore:
Timestamp:
05/14/2007 11:33:29 AM (3 years ago)
Author:
brian
Message:

Made HorizontalGraph? work nicely; made QualityMap? an instance of HorizontalGraph?.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • GraphTool/examples/simple/quality_map.py

    r859 r870  
    1515    return begin_time, end_time, data 
    1616 
     17QM = QualityMap() 
    1718 
    18 # Bar graph stuff. 
    19 QM = QualityMap() 
     19# Data generation 
    2020full_data = {} 
    2121for i in range(10): 
  • GraphTool/src/graphtool/graphs/common_graphs.py

    r865 r870  
    759759      return None 
    760760 
    761 class QualityMap( TimeGraph, PivotGroupGraph ): 
     761class QualityMap( HorizontalGraph, TimeGraph, PivotGroupGraph ): 
    762762 
    763763  sort_keys = Graph.sort_keys 
     
    871871          coords[link][timebin] = p 
    872872 
    873     y_vals =  numpy.arange(.5,len(links)+.5,1) 
    874     fl = FixedLocator( y_vals ) 
    875     if self.kind.lower() == "link": 
    876       links = [ i[0] + ' to ' + i[1] for i in links ] 
    877  
    878873    # Make the colorbar 
    879     cb = self.fig.colorbar( self.mapper, format="%d%%", orientation='horizontal', fraction=0.04, pad=0.13, aspect=40  ) 
     874    # Calculate padding 
     875    pad_pix = self.additional_vertical_padding() 
     876    height_inches = self.fig.get_size_inches()[-1] 
     877    pad_perc = pad_pix / self.fig.get_dpi() / height_inches / 2.0 
     878    print "pad perc", pad_perc 
     879    cb = self.fig.colorbar( self.mapper, format="%d%%", orientation='horizontal', fraction=0.04, pad=pad_perc, aspect=40  ) 
    880880    setp( cb.outline, linewidth=.5 ) 
    881881    setp( cb.ax.get_xticklabels(), size=10 ) 
    882882    setp( cb.ax.get_xticklabels(), family=self.prefs['font_family'] ) 
    883883    setp( cb.ax.get_xticklabels(), fontname = self.prefs['font'] ) 
    884  
    885     # Make the formatter for the y-axis 
    886     ff = FixedFormatter( links ) 
    887     ax.yaxis.set_major_formatter( ff ) 
    888     ax.yaxis.set_major_locator( fl ) 
    889  
    890     # Calculate the spacing of the y-tick labels: 
    891     height_per = ax.get_position()[-1] 
    892     height_inches = self.fig.get_size_inches()[-1] * height_per 
    893     height_pixels = self.fig.get_dpi() * height_inches 
    894     max_height_labels = height_pixels / max( 1, len(links) ) 
    895  
    896     # Adjust the font height to match the maximum available height 
    897     font_height = max_height_labels * 1.7 / 3.0 - 1.0 
    898     font_height = min( font_height, 7 ) 
    899     setp( ax.get_yticklabels(), size=font_height ) 
    900  
    901     ax.yaxis.draw( self.canvas.get_renderer() ) 
    902  
    903     total_xmax = 0 
    904     for label in ax.get_yticklabels(): 
    905       bbox = label.get_window_extent( self.canvas.get_renderer() ) 
    906       total_xmax = max( bbox.xmax()-bbox.xmin(), total_xmax ) 
    907     move_left = (total_xmax+6) / self.prefs['width'] 
    908     pos = ax.get_position() 
    909     pos[0] = move_left 
    910     pos[2] = 1 - pos[0] - .02 
    911     ax.set_position( pos ) 
     884     
     885  def additional_vertical_padding(self): 
     886      return 120 
    912887 
    913888  def get_coords( self ): 
  • GraphTool/src/graphtool/graphs/graph.py

    r869 r870  
    66from matplotlib.backends.backend_svg import FigureCanvasSVG 
    77from matplotlib.figure import Figure 
    8 from matplotlib.ticker import FixedLocator 
     8from matplotlib.ticker import FixedLocator, FixedFormatter 
    99from matplotlib.backends.backend_svg import RendererSVG 
    1010from matplotlib.patches import Wedge, Shadow, Rectangle, Polygon 
     
    2222  'text_padding' : 3, #in pixels 
    2323  'legend_padding' : .01, # In percent of screen space / 100 
    24   'figure_padding' : .10, 
     24  'figure_padding' : 50, #in pixels 
    2525  'width' : 800,  #in pixels 
    2626  'height' : 500, 
     
    3535  'square_axis' : False, 
    3636  'watermark' : '$GRAPHTOOL_ROOT/static_content/CMSbwl3.png', 
     37  'fixed-height' : True 
    3738} 
    3839 
     
    267268    height_inches = prefs['height'] / dpi 
    268269 
     270    # Conversion from pixels to percentage of screen 
     271    figure_padding_perc = prefs['figure_padding'] / float(prefs['height']) 
     272 
    269273    # Calculations for the legend 
    270274    rows = 0.0; column_height = 0.0; bottom = 0.0 
     
    296300    legend_rect = prefs['legend_padding'], prefs['legend_padding'], legend_width, legend_height 
    297301    if prefs['square_axis']: 
    298       min_size = min( 1 - 1.5*prefs['figure_padding'], 1 - bottom - 2*prefs['figure_padding']
     302      min_size = min( 1 - 1.5*figure_padding_perc, 1 - bottom - 2*figure_padding_perc
    299303      ax_rect = (.5 - min_size/2.0*prefs['height']/float(prefs['width']),  
    300                  prefs['figure_padding'] + bottom,  
     304                 figure_padding_perc + bottom,  
    301305                 prefs['height']/float(prefs['width'])*min_size,  
    302306                 min_size ) 
    303307    else: 
    304       ax_rect = (prefs['figure_padding'],  
    305                  prefs['figure_padding'] + bottom,  
    306                  1 - 1.5*prefs['figure_padding']
    307                  1 - bottom - 2*prefs['figure_padding']
     308      ax_rect = (figure_padding_perc,  
     309                 figure_padding_perc + bottom,  
     310                 1 - 1.5*figure_padding_perc
     311                 1 - bottom - 2*figure_padding_perc
    308312 
    309313    # Add a watermark: 
     
    345349    # Creates a subtitle, if necessary  
    346350    title = title.split('\n',1) 
    347     if len(title) == 1: 
    348       ax.set_title( title[0] ) 
    349     else: 
    350       ax.set_title( title[0] + '\n' ) 
     351    subtitle_height_pix = (prefs['subtitle_size'] + 2*prefs['text_padding']) * (len(title) > 1) 
     352    ax_height_pix = ax_rect[-1] * height_inches * dpi  
     353    print "ax_height_pix", ax_height_pix 
     354    ax.title = ax.text( 0.5, 1 + (subtitle_height_pix + prefs['text_padding'])/ax_height_pix, title[0], 
     355                        verticalalignment='bottom', horizontalalignment='center' ) 
     356    if len(title) > 1: 
    351357      ax.title.set_transform( ax.transAxes ) 
    352358      ax.title.set_clip_box( None ) 
    353359      ax._set_artist_props( ax.title ) 
    354       ax.subtitle = ax.text( 0.5, 1.02, title[1], 
     360      ax.subtitle = ax.text( 0.5, 1.0 + prefs['text_padding']/ax_height_pix, title[1], 
    355361          verticalalignment='bottom', 
    356362          horizontalalignment='center' ) 
     
    463469        # First, prepare the canvas to calculate all the necessary parts. 
    464470        super( HorizontalGraph, self ).prepare_canvas() 
    465         if self.prefs['fixed'] == False: 
     471        if self.prefs.get('fixed-height',True) == False: 
    466472            # Then, we re-calculate the heights based on number of labels. 
    467473            labels = getattr( self, 'labels', [] ) 
    468474            height = self.ax.get_position()[3] 
    469             dpi = self.figure.get_dpi() 
    470             fig_width, fig_height = self.figure.get_size_inches() 
     475            dpi = self.fig.get_dpi() 
     476            fig_width, fig_height = self.fig.get_size_inches() 
    471477            height_pix = height * fig_height * dpi 
    472478            num_labels = len(labels) 
    473479            pixels_per_label = 2*self.prefs['text_padding'] + self.prefs['text_size'] 
    474             new_height_pix = max(num_labels * pixels_per_label, height_pix) 
    475             prefs['height'] += new_height_pix - height_pix 
    476              
     480            print pixels_per_label 
     481            new_height_pix = max(num_labels * pixels_per_label + 2*self.prefs['figure_padding'], height_pix) 
     482            self.metadata['height'] = self.prefs['height'] + new_height_pix - height_pix + self.additional_vertical_padding() 
     483            print self.metadata['height'] 
     484            self.metadata['fixed-height'] = True 
    477485            # After we calculate the new height, prepare the canvas again. 
    478486            super( HorizontalGraph, self ).prepare_canvas() 
     
    480488    def y_formatter_cb(self, ax): 
    481489        # y_vals should be the y-location of the labels. 
     490        labels = getattr( self, 'labels', [] ) 
    482491        y_vals =  numpy.arange(.5,len(labels)+.5,1) 
    483492 
     
    489498        ax.yaxis.set_major_locator( fl ) 
    490499         
     500    def additional_vertical_padding(self): return 0 
    491501 
    492502    def write_graph(self): 
    493503            # Calculate the spacing of the y-tick labels: 
    494             height_per = ax.get_position()[-1] 
     504            labels = getattr( self, 'labels', [] ) 
     505            height_per = self.ax.get_position()[-1] 
    495506            height_inches = self.fig.get_size_inches()[-1] * height_per 
    496507            height_pixels = self.fig.get_dpi() * height_inches 
    497             max_height_labels = height_pixels / max( 1, len(links) ) 
     508            print "height_pixels", height_pixels, height_per 
     509            max_height_labels = height_pixels / max( 1, len(labels) ) 
     510            print max_height_labels 
    498511 
    499512            # Adjust the font height to match the maximum available height 
    500513            font_height = max_height_labels * 1.7 / 3.0 - 1.0 
    501514            font_height = min( font_height, 7 ) 
    502             setp( ax.get_yticklabels(), size=font_height ) 
    503  
    504             ax.yaxis.draw( self.canvas.get_renderer() ) 
     515            setp( self.ax.get_yticklabels(), size=font_height ) 
     516 
     517            self.ax.yaxis.draw( self.canvas.get_renderer() ) 
    505518 
    506519            total_xmax = 0 
    507             for label in ax.get_yticklabels(): 
     520            for label in self.ax.get_yticklabels(): 
    508521                bbox = label.get_window_extent( self.canvas.get_renderer() ) 
    509522                total_xmax = max( bbox.xmax()-bbox.xmin(), total_xmax ) 
    510523                move_left = (total_xmax+6) / self.prefs['width'] 
    511524 
    512             pos = ax.get_position() 
     525            pos = self.ax.get_position() 
    513526            pos[0] = move_left 
    514527            pos[2] = 1 - pos[0] - .02 
    515             ax.set_position( pos ) 
     528            self.ax.set_position( pos ) 
     529             
     530            # Finally, call normal writer. 
     531            super( HorizontalGraph, self ).write_graph() 
    516532         
    517533class DBGraph( Graph ):