@@ -541,39 +541,91 @@ def display_changed_disc(self):
541541
542542 for row , box in enumerate (self .boxes ):
543543 y = (- len (self .boxes ) + row ) * 40 + 10
544+ bars , labels = box [::2 ], box [1 ::2 ]
544545
545- label = self .attr_labels [row ]
546- b = label .boundingRect ()
547- label .setPos (- b .width () - 10 , y - b .height () / 2 )
548- self .box_scene .addItem (label )
546+ self .__draw_group_labels (y , row )
549547 if not self .stretched :
550- label = self .labels [row ]
551- b = label .boundingRect ()
552- if self .group_var :
553- right = self .scale_x * sum (self .conts [row ])
554- else :
555- right = self .scale_x * sum (self .dist )
556- label .setPos (right + 10 , y - b .height () / 2 )
557- self .box_scene .addItem (label )
558-
548+ self .__draw_row_counts (y , row )
559549 if self .show_labels and self .attribute is not self .group_var :
560- for text_item , bar_part in zip (box [1 ::2 ], box [::2 ]):
561- label = QGraphicsSimpleTextItem (
562- text_item .toPlainText ())
563- label .setPos (bar_part .boundingRect ().x (),
564- y - label .boundingRect ().height () - 8 )
565- self .box_scene .addItem (label )
566- for item in box :
567- if isinstance (item , QGraphicsTextItem ):
568- continue
569- self .box_scene .addItem (item )
570- item .setPos (0 , y )
550+ self .__draw_bar_labels (y , bars , labels )
551+ self .__draw_bars (y , bars )
552+
571553 self .box_scene .setSceneRect (- self .label_width - 5 ,
572554 - 30 - len (self .boxes ) * 40 ,
573555 self .scene_width , len (self .boxes * 40 ) + 90 )
574556 self .infot1 .setText ("" )
575557 self .select_box_items ()
576558
559+ def __draw_group_labels (self , y , row ):
560+ """Draw group labels
561+
562+ Parameters
563+ ----------
564+ y: int
565+ vertical offset of bars
566+ row: int
567+ row index
568+ """
569+ label = self .attr_labels [row ]
570+ b = label .boundingRect ()
571+ label .setPos (- b .width () - 10 , y - b .height () / 2 )
572+ self .box_scene .addItem (label )
573+
574+ def __draw_row_counts (self , y , row ):
575+ """Draw row counts
576+
577+ Parameters
578+ ----------
579+ y: int
580+ vertical offset of bars
581+ row: int
582+ row index
583+ """
584+ label = self .labels [row ]
585+ b = label .boundingRect ()
586+ if self .group_var :
587+ right = self .scale_x * sum (self .conts [row ])
588+ else :
589+ right = self .scale_x * sum (self .dist )
590+ label .setPos (right + 10 , y - b .height () / 2 )
591+ self .box_scene .addItem (label )
592+
593+ def __draw_bar_labels (self , y , bars , labels ):
594+ """Draw bar labels
595+
596+ Parameters
597+ ----------
598+ y: int
599+ vertical offset of bars
600+ bars: List[FilterGraphicsRectItem]
601+ list of bars being drawn
602+ labels: List[QGraphicsTextItem]
603+ list of labels for corresponding bars
604+ """
605+ label = bar_part = None
606+ for text_item , bar_part in zip (labels , bars ):
607+ label = self .Label (
608+ text_item .toPlainText ())
609+ label .setPos (bar_part .boundingRect ().x (),
610+ y - label .boundingRect ().height () - 8 )
611+ label .setMaxWidth (bar_part .boundingRect ().width ())
612+ self .box_scene .addItem (label )
613+
614+ def __draw_bars (self , y , bars ):
615+ """Draw bars
616+
617+ Parameters
618+ ----------
619+ y: int
620+ vertical offset of bars
621+
622+ bars: List[FilterGraphicsRectItem]
623+ list of bars to draw
624+ """
625+ for item in bars :
626+ item .setPos (0 , y )
627+ self .box_scene .addItem (item )
628+
577629 # noinspection PyPep8Naming
578630 def compute_tests (self ):
579631 # The t-test and ANOVA are implemented here since they efficiently use
@@ -972,6 +1024,44 @@ def send_report(self):
9721024 if text :
9731025 self .report_caption (text )
9741026
1027+ class Label (QGraphicsSimpleTextItem ):
1028+ """Boxplot Label with settable maxWidth"""
1029+ # Minimum width to display label text
1030+ MIN_LABEL_WIDTH = 25
1031+
1032+ # padding bellow the text
1033+ PADDING = 3
1034+
1035+ __max_width = None
1036+
1037+ def maxWidth (self ):
1038+ return self .__max_width
1039+
1040+ def setMaxWidth (self , max_width ):
1041+ self .__max_width = max_width
1042+
1043+ def paint (self , painter , option , widget ):
1044+ """Overrides QGraphicsSimpleTextItem.paint
1045+
1046+ If label text is too long, it is elided
1047+ to fit into the allowed region
1048+ """
1049+ if self .__max_width is None :
1050+ width = option .rect .width ()
1051+ else :
1052+ width = self .__max_width
1053+
1054+ if width < self .MIN_LABEL_WIDTH :
1055+ # if space is too narrow, no label
1056+ return
1057+
1058+ fm = painter .fontMetrics ()
1059+ text = fm .elidedText (self .text (), Qt .ElideRight , width )
1060+ painter .drawText (
1061+ option .rect .x (),
1062+ option .rect .y () + self .boundingRect ().height () - self .PADDING ,
1063+ text )
1064+
9751065
9761066def main (argv = None ):
9771067 from AnyQt .QtWidgets import QApplication
0 commit comments