def _draw_legend(self): """ Draw the color scale legend. """ if not self._color_mode: return legend_height = self.height - self.padding_top - self.padding_bottom #draw each legend block block_height = float(legend_height) / LEGEND_NUM_BLOCKS x = self.width - self.padding_right + LEGEND_LEFT_PAD for i in range(LEGEND_NUM_BLOCKS): color = unpack_color(COLORS[self._color_mode][int( 255 * i / float(LEGEND_NUM_BLOCKS - 1))]) GL.glColor4f(*numpy.array(color) / 255.0) y = self.height - (i + 1) * block_height - self.padding_bottom self._draw_rect(x, y, LEGEND_WIDTH, block_height) #draw rectangle around color scale border GL.glColor3f(*LEGEND_BORDER_COLOR_SPEC) self._draw_rect(x, self.padding_top, LEGEND_WIDTH, legend_height, fill=False) #draw each legend label label_spacing = float(legend_height) / (LEGEND_NUM_LABELS - 1) x = self.width - (self.padding_right - LEGEND_LEFT_PAD - LEGEND_WIDTH) / 2 for i in range(LEGEND_NUM_LABELS): proportion = i / float(LEGEND_NUM_LABELS - 1) dB = proportion * (self._maximum - self._minimum) + self._minimum y = self.height - i * label_spacing - self.padding_bottom txt = gltext.Text('%ddB' % int(dB), font_size=LEGEND_FONT_SIZE, centered=True) txt.draw_text(wx.Point(x, y))
def _draw_point_label(self): """ Draw the point label for the last mouse motion coordinate. The mouse coordinate must be an X, Y tuple. The label will be drawn at the X, Y coordinate. The values of the X, Y coordinate will be scaled to the current X, Y bounds. """ if not self.enable_point_label(): return if not self._point_label_coordinate: return x, y = self._point_label_coordinate if x < self.padding_left or x > self.width-self.padding_right: return if y < self.padding_top or y > self.height-self.padding_bottom: return #scale to window bounds x_win_scalar = float(x - self.padding_left)/(self.width-self.padding_left-self.padding_right) y_win_scalar = float((self.height - y) - self.padding_bottom)/(self.height-self.padding_top-self.padding_bottom) #scale to grid bounds x_val = x_win_scalar*(self.x_max-self.x_min) + self.x_min y_val = y_win_scalar*(self.y_max-self.y_min) + self.y_min #create text label_str = self._populate_point_label(x_val, y_val) if not label_str: return txt = gltext.Text(label_str, font_size=POINT_LABEL_FONT_SIZE) w, h = txt.get_size() #enable transparency GL.glEnable(GL.GL_BLEND) GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA) #draw rect + text GL.glColor4f(*POINT_LABEL_COLOR_SPEC) if x > self.width/2: x -= w+2*POINT_LABEL_PADDING + POINT_LABEL_OFFSET else: x += POINT_LABEL_OFFSET self._draw_rect(x, y-h-2*POINT_LABEL_PADDING, w+2*POINT_LABEL_PADDING, h+2*POINT_LABEL_PADDING) txt.draw_text(wx.Point(x+POINT_LABEL_PADDING, y-h-POINT_LABEL_PADDING))
def _get_tick_label(self, tick, unit): """ Format the tick value and create a gl text. @param tick the floating point tick value @param unit the axis unit @return the tick label text """ if unit: tick_str = common.sci_format(tick) else: tick_str = common.eng_format(tick) return gltext.Text(tick_str, font_size=TICK_TEXT_FONT_SIZE)
def _draw_legend(self): """ Draw the legend in the upper right corner. For each channel, draw a rectangle out of the channel color, and overlay the channel text on top of the rectangle. """ if not self.enable_legend(): return x_off = self.width - self.padding_right - LEGEND_BOX_PADDING for i, channel in enumerate(reversed(sorted(self._channels.keys()))): samples = self._channels[channel][SAMPLES_KEY] if not len(samples): continue color_spec = self._channels[channel][COLOR_SPEC_KEY] txt = gltext.Text(channel, font_size=LEGEND_TEXT_FONT_SIZE) w, h = txt.get_size() #draw rect + text GL.glColor3f(*color_spec) self._draw_rect( x_off - w - LEGEND_BOX_PADDING, self.padding_top / 2 - h / 2 - LEGEND_BOX_PADDING, w + 2 * LEGEND_BOX_PADDING, h + 2 * LEGEND_BOX_PADDING, ) txt.draw_text(wx.Point(x_off - w, self.padding_top / 2 - h / 2)) x_off -= w + 4 * LEGEND_BOX_PADDING
def _draw_grid(self): """ Create the x, y, tick, and title labels. Resize the padding for the labels. Draw the border, grid, title, and labels. """ ################################################## # Create GL text labels ################################################## #create x tick labels x_tick_labels = [ (tick, self._get_tick_label(tick, self.x_units)) for tick in self._get_ticks(self.x_min, self.x_max, self.x_step, self.x_scalar) ] #create x tick labels y_tick_labels = [ (tick, self._get_tick_label(tick, self.y_units)) for tick in self._get_ticks(self.y_min, self.y_max, self.y_step, self.y_scalar) ] #create x label x_label_str = self.x_units and "%s (%s%s)" % ( self.x_label, self.x_prefix, self.x_units) or self.x_label x_label = gltext.Text(x_label_str, bold=True, font_size=UNITS_TEXT_FONT_SIZE, centered=True) #create y label y_label_str = self.y_units and "%s (%s%s)" % ( self.y_label, self.y_prefix, self.y_units) or self.y_label y_label = gltext.Text(y_label_str, bold=True, font_size=UNITS_TEXT_FONT_SIZE, centered=True) #create title title_label = gltext.Text(self.title, bold=True, font_size=TITLE_TEXT_FONT_SIZE, centered=True) ################################################## # Resize the padding ################################################## self.padding_top = max( 2 * TITLE_LABEL_PADDING + title_label.get_size()[1], self.padding_top_min) self.padding_right = max(2 * TICK_LABEL_PADDING, self.padding_right_min) self.padding_bottom = max( 2 * AXIS_LABEL_PADDING + TICK_LABEL_PADDING + x_label.get_size()[1] + max([label.get_size()[1] for tick, label in x_tick_labels]), self.padding_bottom_min) self.padding_left = max( 2 * AXIS_LABEL_PADDING + TICK_LABEL_PADDING + y_label.get_size()[1] + max([label.get_size()[0] for tick, label in y_tick_labels]), self.padding_left_min) #enforce padding aspect ratio if enabled if self.enable_grid_aspect_ratio(): w_over_h_ratio = float(self.width) / float(self.height) horizontal_padding = float(self.padding_right + self.padding_left) veritical_padding = float(self.padding_top + self.padding_bottom) if w_over_h_ratio > horizontal_padding / veritical_padding: #increase the horizontal padding new_padding = veritical_padding * w_over_h_ratio - horizontal_padding #distribute the padding to left and right self.padding_left += int(round(new_padding / 2)) self.padding_right += int(round(new_padding / 2)) else: #increase the vertical padding new_padding = horizontal_padding / w_over_h_ratio - veritical_padding #distribute the padding to top and bottom self.padding_top += int(round(new_padding / 2)) self.padding_bottom += int(round(new_padding / 2)) ################################################## # Draw Grid X ################################################## for tick, label in x_tick_labels: scaled_tick = (self.width-self.padding_left-self.padding_right)*\ (tick/self.x_scalar-self.x_min)/(self.x_max-self.x_min) + self.padding_left self._draw_grid_line( (scaled_tick, self.padding_top), (scaled_tick, self.height - self.padding_bottom), ) w, h = label.get_size() label.draw_text( wx.Point( scaled_tick - w / 2, self.height - self.padding_bottom + TICK_LABEL_PADDING)) ################################################## # Draw Grid Y ################################################## for tick, label in y_tick_labels: scaled_tick = (self.height-self.padding_top-self.padding_bottom)*\ (1 - (tick/self.y_scalar-self.y_min)/(self.y_max-self.y_min)) + self.padding_top self._draw_grid_line( (self.padding_left, scaled_tick), (self.width - self.padding_right, scaled_tick), ) w, h = label.get_size() label.draw_text( wx.Point(self.padding_left - w - TICK_LABEL_PADDING, scaled_tick - h / 2)) ################################################## # Draw Border ################################################## GL.glColor3f(*GRID_BORDER_COLOR_SPEC) self._draw_rect( self.padding_left, self.padding_top, self.width - self.padding_right - self.padding_left, self.height - self.padding_top - self.padding_bottom, fill=False, ) ################################################## # Draw Labels ################################################## #draw title label title_label.draw_text( wx.Point(self.width / 2.0, TITLE_LABEL_PADDING + title_label.get_size()[1] / 2)) #draw x labels x_label.draw_text( wx.Point( (self.width - self.padding_left - self.padding_right) / 2.0 + self.padding_left, self.height - (AXIS_LABEL_PADDING + x_label.get_size()[1] / 2), )) #draw y labels y_label.draw_text( wx.Point( AXIS_LABEL_PADDING + y_label.get_size()[1] / 2, (self.height - self.padding_top - self.padding_bottom) / 2.0 + self.padding_top, ), rotation=90, )