Beispiel #1
0
    def _draw_labels(self, cur_drawing, left_labels, right_labels):
        """Layout and draw sub-feature labels for the chromosome.

        Tries to place each label at the same vertical position as the
        feature it applies to, but will adjust the positions to avoid or
        at least reduce label overlap.

        Draws the label text and a coloured line linking it to the
        location (i.e. feature) it applies to.
        """
        if not self._sub_components:
            return
        color_label = self._color_labels

        segment_width = (self.end_x_position - self.start_x_position) \
                        * self.chr_percent
        label_sep = (self.end_x_position - self.start_x_position) \
                        * self.label_sep_percent
        segment_x = self.start_x_position \
                  + 0.5 * (self.end_x_position - self.start_x_position - segment_width)

        y_limits = []
        for sub_component in self._sub_components:
            y_limits.extend((sub_component.start_y_position, sub_component.end_y_position))
        y_min = min(y_limits)
        y_max = max(y_limits)
        del y_limits
        #Now do some label placement magic...
        #from reportlab.pdfbase import pdfmetrics
        #font = pdfmetrics.getFont('Helvetica')
        #h = (font.face.ascent + font.face.descent) * 0.90
        h = self.label_size
        left_labels = _place_labels(left_labels, y_min, y_max, h)
        right_labels = _place_labels(right_labels, y_min, y_max, h)
        x1 = segment_x
        x2 = segment_x - label_sep
        for (y1, y2, color, name) in left_labels:
            cur_drawing.add(Line(x1, y1, x2, y2,
                                 strokeColor = color,
                                 strokeWidth = 0.25))
            label_string = String(x2, y2, name,
                                  textAnchor="end")
            label_string.fontName = 'Helvetica'
            label_string.fontSize = self.label_size
            if color_label:
                label_string.fillColor = color
            cur_drawing.add(label_string)
        x1 = segment_x + segment_width
        x2 = segment_x + segment_width + label_sep
        for (y1, y2, color, name) in right_labels:
            cur_drawing.add(Line(x1, y1, x2, y2,
                                 strokeColor = color,
                                 strokeWidth = 0.25))
            label_string = String(x2, y2, name)
            label_string.fontName = 'Helvetica'
            label_string.fontSize = self.label_size
            if color_label:
                label_string.fillColor = color
            cur_drawing.add(label_string)
Beispiel #2
0
    def _draw_title(self, cur_drawing, title, width, height):
        """Write out the title of the organism figure."""
        title_string = String(width / 2, height - inch, title)
        title_string.fontName = 'Helvetica-Bold'
        title_string.fontSize = self.title_size
        title_string.textAnchor = "middle"

        cur_drawing.add(title_string)
Beispiel #3
0
    def _draw_title(self, cur_drawing, title, width, height):
        """Add a title to the page we are outputting (PRIVATE)."""
        title_string = String(width / 2, height - inch, title)
        title_string.fontName = 'Helvetica-Bold'
        title_string.fontSize = self.title_size
        title_string.textAnchor = "middle"

        cur_drawing.add(title_string)
Beispiel #4
0
    def _draw_title(self, cur_drawing, title, width, height):
        """Add the title of the figure to the drawing (PRIVATE)."""
        title_string = String(width / 2, height - inch, title)
        title_string.fontName = "Helvetica-Bold"
        title_string.fontSize = self.title_size
        title_string.textAnchor = "middle"

        cur_drawing.add(title_string)
Beispiel #5
0
    def _draw_title(self, cur_drawing, title, width, height):
        """Add a title to the page we are outputting (PRIVATE)."""
        title_string = String(width / 2, height - inch, title)
        title_string.fontName = "Helvetica-Bold"
        title_string.fontSize = self.title_size
        title_string.textAnchor = "middle"

        cur_drawing.add(title_string)
Beispiel #6
0
    def _draw_title(self, cur_drawing, title, width, height):
        """Write out the title of the organism figure.
        """
        title_string = String(width / 2, height - inch, title)
        title_string.fontName = 'Helvetica-Bold'
        title_string.fontSize = self.title_size
        title_string.textAnchor = "middle"

        cur_drawing.add(title_string)
def string_demo():
    drawing = Drawing(width=400, height=200)

    for size in range(10, 32, 4):
        x = 15 + size * 1.5
        y = 15 + size * 1.5
        my_string = String(x, y, 'Python rocks!')
        my_string.fontName = 'Courier'
        my_string.fontSize = size
        drawing.add(my_string)

    other_string = String(200, 150, 'Centered Text')
    other_string.fontName = 'Times-Roman'
    other_string.fontSize = 40
    other_string.fillColor = colors.red
    other_string.textAnchor = 'middle'
    drawing.add(other_string)

    drawing.save(formats=['pdf'], outDir='.', fnRoot='string_demo')
    def _draw_title(self, cur_drawing, title, start_x, start_y, end_x, end_y):
        """Add the title of the figure to the drawing (PRIVATE)."""
        x_center = start_x + (end_x - start_x) / 2
        y_pos = end_y + (self.padding_percent * (start_y - end_y)) / 2
        title_string = String(x_center, y_pos, title)
        title_string.fontName = 'Helvetica-Bold'
        title_string.fontSize = self.chart_title_size
        title_string.textAnchor = "middle"

        cur_drawing.add(title_string)
Beispiel #9
0
    def _draw_title(self, cur_drawing, title, start_x, start_y, end_x, end_y):
        """Add the title of the figure to the drawing (PRIVATE)."""
        x_center = start_x + (end_x - start_x) / 2
        y_pos = end_y + (self.padding_percent * (start_y - end_y)) / 2
        title_string = String(x_center, y_pos, title)
        title_string.fontName = "Helvetica-Bold"
        title_string.fontSize = self.chart_title_size
        title_string.textAnchor = "middle"

        cur_drawing.add(title_string)
Beispiel #10
0
    def draw(self):
        self._calc_labels_size()
        self._adjust_positon()

        self.set_line_color()
        if self.drawLegend is True:
            if self.legendPositionType in ["bottom-left", "bottom-mid", "bottom-right"]:
                row_count = len(self.legendCategoryNames) + 1
                self.height -= row_count * self.legendFontSize
                self.y += row_count * self.legendFontSize

        g = HorizontalLineChart.draw(self)

        if self.drawLegend:
            legend_count = 0
            for i in range(len(self.legendCategoryNames)):
                legend = ChartsLegend()

                legend.positionType = self.legendPositionType
                if self.legendPositionType != "null":
                    if self.legendPositionType in ["bottom-left", "bottom-mid", "bottom-right"]:
                        legend.backgroundRect = \
                            Rect(self.x,
                                 self.y + legend.bottom_gap - self.labels_height - 15 - ((i+1) * legend.fontSize),
                                 self.width, self.height)
                    else:
                        legend.backgroundRect = Rect(self.x, self.y - (i * legend.fontSize * 1.2),
                                                     self.width, self.height)

                legend.adjustX = self.legendAdjustX
                legend.adjustY = self.legendAdjustY

                legend.fontSize = self.legendFontSize

                legend.colorNamePairs = []
                for j in range(len(self.legendCategoryNames[i])):
                    legend.colorNamePairs.append((ALL_COLORS[legend_count + j],
                                                  self.legendCategoryNames[i][j]))
                legend_count += len(self.legendCategoryNames[i])

                g.add(legend)

        if self.titleMain != "":
            title = String(0, 0, self.titleMain)
            title.fontSize = self.titleMainFontSize
            title.fontName = self.titleMainFontName
            title.fillColor = self.titleMainFontColor
            title.textAnchor = 'start'
            # title.x = self.x - 20
            title.x = 0
            title.y = self.y + self.height + 20

            g.add(title)

        return g
Beispiel #11
0
    def _rawDraw(self):
        _text = self._text
        self._text = _text or ""
        self.computeSize()
        self._text = _text
        g = Group()
        g.translate(self.x + self.dx, self.y + self.dy)
        g.rotate(self.angle)

        y = self._top - self._leading * self._baselineRatio
        textAnchor = self._getTextAnchor()
        if textAnchor == "start":
            x = self._left
        elif textAnchor == "middle":
            x = self._left + self._ewidth * 0.5
        else:
            x = self._right

        # paint box behind text just in case they
        # fill it
        if self.boxFillColor or (self.boxStrokeColor and self.boxStrokeWidth):
            g.add(
                Rect(
                    self._left - self.leftPadding,
                    self._bottom - self.bottomPadding,
                    self._width,
                    self._height,
                    strokeColor=self.boxStrokeColor,
                    strokeWidth=self.boxStrokeWidth,
                    fillColor=self.boxFillColor,
                )
            )

        fillColor, fontName, fontSize = self.fillColor, self.fontName, self.fontSize
        strokeColor, strokeWidth, leading = self.strokeColor, self.strokeWidth, self._leading
        svgAttrs = getattr(self, "_svgAttrs", {})
        if strokeColor:
            for line in self._lines:
                s = _text2Path(line, x, y, fontName, fontSize, textAnchor)
                s.fillColor = fillColor
                s.strokeColor = strokeColor
                s.strokeWidth = strokeWidth
                g.add(s)
                y -= leading
        else:
            for line in self._lines:
                s = String(x, y, line, _svgAttrs=svgAttrs)
                s.textAnchor = textAnchor
                s.fontName = fontName
                s.fontSize = fontSize
                s.fillColor = fillColor
                g.add(s)
                y -= leading

        return g
    def _draw_label(self, cur_drawing, label_name):
        """Draw a label for the chromosome."""
        x_position = 0.5 * (self.start_x_position + self.end_x_position)
        y_position = self.end_y_position

        label_string = String(x_position, y_position, label_name)
        label_string.fontName = 'Times-BoldItalic'
        label_string.fontSize = self.title_size
        label_string.textAnchor = 'middle'

        cur_drawing.add(label_string)
Beispiel #13
0
    def draw(self):
        _text = self._text
        self._text = _text or ''
        self.computeSize()
        self._text = _text
        g = Group()
        g.translate(self.x + self.dx, self.y + self.dy)
        g.rotate(self.angle)

        y = self._top - self.fontSize
        textAnchor = self._getTextAnchor()
        if textAnchor == 'start':
            x = self._left
        elif textAnchor == 'middle':
            x = self._left + self._ewidth * 0.5
        else:
            x = self._right

        # paint box behind text just in case they
        # fill it
        if self.boxFillColor or (self.boxStrokeColor and self.boxStrokeWidth):
            g.add(
                Rect(
                    self._left - self.leftPadding,
                    self._bottom - self.bottomPadding,
                    self._width,
                    self._height,
                    strokeColor=self.boxStrokeColor,
                    strokeWidth=self.boxStrokeWidth,
                    fillColor=self.boxFillColor))

        fillColor, fontName, fontSize = self.fillColor, self.fontName, self.fontSize
        strokeColor, strokeWidth, leading = self.strokeColor, self.strokeWidth, (
            self.leading or 1.2 * fontSize)
        if strokeColor:
            for line in self._lines:
                s = _text2Path(line, x, y, fontName, fontSize, textAnchor)
                s.fillColor = fillColor
                s.strokeColor = strokeColor
                s.strokeWidth = strokeWidth
                g.add(s)
                y = y - leading
        else:
            for line in self._lines:
                s = String(x, y, line)
                s.textAnchor = textAnchor
                s.fontName = fontName
                s.fontSize = fontSize
                s.fillColor = fillColor
                g.add(s)
                y = y - leading

        return g
Beispiel #14
0
    def _draw_label(self, cur_drawing, label_name):
        """Draw a label for the chromosome.
        """
        x_position = self.start_x_position
        y_position = self.end_y_position

        label_string = String(x_position, y_position, label_name)
        label_string.fontName = "Times-BoldItalic"
        label_string.fontSize = self.title_size
        label_string.textAnchor = "start"

        cur_drawing.add(label_string)
Beispiel #15
0
    def _rawDraw(self):
        _text = self._text
        self._text = _text or ''
        self.computeSize()
        self._text = _text
        g = Group()
        g.translate(self.x + self.dx, self.y + self.dy)
        g.rotate(self.angle)

        y = self._top - self._leading * self._baselineRatio
        textAnchor = self._getTextAnchor()
        if textAnchor == 'start':
            x = self._left
        elif textAnchor == 'middle':
            x = self._left + self._ewidth * 0.5
        else:
            x = self._right

        # paint box behind text just in case they
        # fill it
        if self.boxFillColor or (self.boxStrokeColor and self.boxStrokeWidth):
            g.add(
                Rect(self._left - self.leftPadding,
                     self._bottom - self.bottomPadding,
                     self._width,
                     self._height,
                     strokeColor=self.boxStrokeColor,
                     strokeWidth=self.boxStrokeWidth,
                     fillColor=self.boxFillColor))

        fillColor, fontName, fontSize = self.fillColor, self.fontName, self.fontSize
        strokeColor, strokeWidth, leading = self.strokeColor, self.strokeWidth, self._leading
        svgAttrs = getattr(self, '_svgAttrs', {})
        if strokeColor:
            for line in self._lines:
                s = _text2Path(line, x, y, fontName, fontSize, textAnchor)
                s.fillColor = fillColor
                s.strokeColor = strokeColor
                s.strokeWidth = strokeWidth
                g.add(s)
                y -= leading
        else:
            for line in self._lines:
                s = String(x, y, line, _svgAttrs=svgAttrs)
                s.textAnchor = textAnchor
                s.fontName = fontName
                s.fontSize = fontSize
                s.fillColor = fillColor
                g.add(s)
                y -= leading

        return g
Beispiel #16
0
    def _draw_label(self, cur_drawing):
        """Add a label to the chromosome segment.
        """
        # the label will be applied to the right of the segment
        if self.label is not None:

            label_x = self.start_x_position + (self.chr_percent + 0.05) * (self.end_x_position - self.start_x_position)
            label_y = (self.start_y_position - self.end_y_position) / 2 + self.end_y_position

            label_string = String(label_x, label_y, self.label)
            label_string.fontName = "Helvetica"
            label_string.fontSize = self.label_size

            cur_drawing.add(label_string)
    def _draw_label(self, cur_drawing):
        """Add a label to the chromosome segment.
        """
        # the label will be applied to the right of the segment
        if self.label is not None:

            label_x = self.start_x_position + \
                      (self.chr_percent + 0.05) * (self.end_x_position -
                                                   self.start_x_position)
            label_y = ((self.start_y_position - self.end_y_position) / 2 +
                       self.end_y_position)

            label_string = String(label_x, label_y, self.label)
            label_string.fontName = 'Helvetica'
            label_string.fontSize = self.label_size

            cur_drawing.add(label_string)
Beispiel #18
0
    def _draw_label(self, cur_drawing):
        """Add a label to the chromosome segment (PRIVATE).

        The label will be applied to the right of the segment.

        This may be overlapped by any sub-feature labels on other segments!
        """
        if self.label is not None:
            label_x = 0.5 * (self.start_x_position + self.end_x_position) + \
                      (self.chr_percent + 0.05) * (self.end_x_position -
                                                   self.start_x_position)
            label_y = ((self.start_y_position - self.end_y_position) / 2 +
                       self.end_y_position)

            label_string = String(label_x, label_y, self.label)
            label_string.fontName = 'Helvetica'
            label_string.fontSize = self.label_size

            cur_drawing.add(label_string)
Beispiel #19
0
    def _draw_label(self, cur_drawing):
        """Add a label to the chromosome segment.

        The label will be applied to the right of the segment.

        This may be overlapped by any sub-feature labels on other segments!
        """
        if self.label is not None:

            label_x = 0.5 * (self.start_x_position + self.end_x_position) + \
                      (self.chr_percent + 0.05) * (self.end_x_position -
                                                   self.start_x_position)
            label_y = ((self.start_y_position - self.end_y_position) / 2 +
                       self.end_y_position)

            label_string = String(label_x, label_y, self.label)
            label_string.fontName = 'Helvetica'
            label_string.fontSize = self.label_size

            cur_drawing.add(label_string)
Beispiel #20
0
def _addWedgeLabel(self,text,angle,labelX,labelY,wedgeStyle,labelClass=WedgeLabel):
    # now draw a label
    if self.simpleLabels:
        theLabel = String(labelX, labelY, text)
        theLabel.textAnchor = "middle"
        theLabel._pmv = angle
        theLabel._simple_pointer = 0
    else:
        theLabel = labelClass()
        theLabel._pmv = angle
        theLabel.x = labelX
        theLabel.y = labelY
        theLabel.dx = wedgeStyle.label_dx
        theLabel.dy = wedgeStyle.label_dy
        theLabel.angle = wedgeStyle.label_angle
        theLabel.boxAnchor = wedgeStyle.label_boxAnchor
        theLabel.boxStrokeColor = wedgeStyle.label_boxStrokeColor
        theLabel.boxStrokeWidth = wedgeStyle.label_boxStrokeWidth
        theLabel.boxFillColor = wedgeStyle.label_boxFillColor
        theLabel.strokeColor = wedgeStyle.label_strokeColor
        theLabel.strokeWidth = wedgeStyle.label_strokeWidth
        _text = wedgeStyle.label_text
        if _text is None: _text = text
        theLabel._text = _text
        theLabel.leading = wedgeStyle.label_leading
        theLabel.width = wedgeStyle.label_width
        theLabel.maxWidth = wedgeStyle.label_maxWidth
        theLabel.height = wedgeStyle.label_height
        theLabel.textAnchor = wedgeStyle.label_textAnchor
        theLabel.visible = wedgeStyle.label_visible
        theLabel.topPadding = wedgeStyle.label_topPadding
        theLabel.leftPadding = wedgeStyle.label_leftPadding
        theLabel.rightPadding = wedgeStyle.label_rightPadding
        theLabel.bottomPadding = wedgeStyle.label_bottomPadding
        theLabel._simple_pointer = wedgeStyle.label_simple_pointer
    theLabel.fontSize = wedgeStyle.fontSize
    theLabel.fontName = wedgeStyle.fontName
    theLabel.fillColor = wedgeStyle.fontColor
    return theLabel
Beispiel #21
0
def _addWedgeLabel(self,text,angle,labelX,labelY,wedgeStyle,labelClass=WedgeLabel):
    # now draw a label
    if self.simpleLabels:
        theLabel = String(labelX, labelY, text)
        theLabel.textAnchor = "middle"
        theLabel._pmv = angle
        theLabel._simple_pointer = 0
    else:
        theLabel = labelClass()
        theLabel._pmv = angle
        theLabel.x = labelX
        theLabel.y = labelY
        theLabel.dx = wedgeStyle.label_dx
        theLabel.dy = wedgeStyle.label_dy
        theLabel.angle = wedgeStyle.label_angle
        theLabel.boxAnchor = wedgeStyle.label_boxAnchor
        theLabel.boxStrokeColor = wedgeStyle.label_boxStrokeColor
        theLabel.boxStrokeWidth = wedgeStyle.label_boxStrokeWidth
        theLabel.boxFillColor = wedgeStyle.label_boxFillColor
        theLabel.strokeColor = wedgeStyle.label_strokeColor
        theLabel.strokeWidth = wedgeStyle.label_strokeWidth
        _text = wedgeStyle.label_text
        if _text is None: _text = text
        theLabel._text = _text
        theLabel.leading = wedgeStyle.label_leading
        theLabel.width = wedgeStyle.label_width
        theLabel.maxWidth = wedgeStyle.label_maxWidth
        theLabel.height = wedgeStyle.label_height
        theLabel.textAnchor = wedgeStyle.label_textAnchor
        theLabel.visible = wedgeStyle.label_visible
        theLabel.topPadding = wedgeStyle.label_topPadding
        theLabel.leftPadding = wedgeStyle.label_leftPadding
        theLabel.rightPadding = wedgeStyle.label_rightPadding
        theLabel.bottomPadding = wedgeStyle.label_bottomPadding
        theLabel._simple_pointer = wedgeStyle.label_simple_pointer
    theLabel.fontSize = wedgeStyle.fontSize
    theLabel.fontName = wedgeStyle.fontName
    theLabel.fillColor = wedgeStyle.fontColor
    return theLabel
Beispiel #22
0
    def __init__(self, barcode_value, width=200, height=320, fontSize=30):
        barcode = BarcodeGen(barcode_value)
        barcode.translate((width-barcode.width)/2, 0)

        Drawing.__init__(self, width, height)
        self.add(barcode, name='barcode')

        box = Box.objects.get(barcode=barcode_value)

        box_category = String(10, 275, box.box_category.letter)
        box_category.fontSize = 55

        box_id = String(10, 230, box.box_id)
        box_id.fontSize = 55

        expiration = String(12, 195, "Exp: " + box.get_printable_expiration())
        expiration.fontSize = 30

        category = String(12, 165, box.box_category.name)
        category.fontSize = 15

        weight = String(12, 150, "Weight: " + "%.1f" % box.weight + ' lbs.')
        weight.fontSize = 15 

        contents = String(12, 135, "Contents:")
        contents.fontSize = 15

        count = 120

        for content in box.contents_set.all():
            item = String(17, count, content.item.name)
            item.fontSize = 15
            self.add(item, "item")
            count = count - 15

        self.add(box_category, "box_category")
        self.add(box_id, "boxId")
        self.add(category, "category")
        self.add(expiration, "expiration")
        self.add(weight, "weight")
        self.add(contents, name="contents_of_box")
Beispiel #23
0
    def _draw_text(format_json, auto_calc=True):
        width = format_json['rect'][2]
        height = format_json['rect'][3]

        d = Drawing(width, height, vAlign="TOP")

        content = format_json['content']
        position = format_json['position']
        x = 0  # format_json['x']
        y = 0  # format_json['y']

        font_size = STATE_DEFAULTS['fontSize']
        if "font_size" in format_json:
            font_size = format_json['font_size']

        if auto_calc:
            if position == "middle":
                x = int(width / 2)
                y = int(height / 2) - int(font_size / 2)
            elif position == "start":
                y = height
        else:
            x = format_json['rect'][0]
            y = format_json['rect'][1]

        text = String(x, y, content)

        text.fontName = DefaultFontName
        if "font_name" in format_json:
            text.fontName = format_json['font_name']
        text.fontSize = font_size
        if "font_color" in format_json:
            text.fillColor = format_json['font_color']
        text.textAnchor = position

        d.add(text)

        return d
Beispiel #24
0
    def makeSectors(self):
        # normalize slice data
        if type(self.data) in (ListType, TupleType) and type(
                self.data[0]) in (ListType, TupleType):
            #it's a nested list, more than one sequence
            normData = []
            n = []
            for l in self.data:
                t = self.normalizeData(l)
                normData.append(t)
                n.append(len(t))
            self._seriesCount = max(n)
        else:
            normData = self.normalizeData(self.data)
            n = len(normData)
            self._seriesCount = n

        #labels
        if self.labels is None:
            labels = []
            if type(n) not in (ListType, TupleType):
                labels = [''] * n
            else:
                for m in n:
                    labels = list(labels) + [''] * m
        else:
            labels = self.labels
            #there's no point in raising errors for less than enough labels if
            #we silently create all for the extreme case of no labels.
            if type(n) not in (ListType, TupleType):
                i = n - len(labels)
                if i > 0:
                    labels = list(labels) + [''] * i
            else:
                tlab = 0
                for m in n:
                    tlab = tlab + m
                i = tlab - len(labels)
                if i > 0:
                    labels = list(labels) + [''] * i

        xradius = self.width / 2.0
        yradius = self.height / 2.0
        centerx = self.x + xradius
        centery = self.y + yradius

        if self.direction == "anticlockwise":
            whichWay = 1
        else:
            whichWay = -1

        g = Group()
        sn = 0

        startAngle = self.startAngle  #% 360
        styleCount = len(self.slices)
        if type(self.data[0]) in (ListType, TupleType):
            #multi-series doughnut
            iradius = (self.height / 5.0) / len(self.data)
            for series in normData:
                i = 0
                for angle in series:
                    endAngle = (startAngle + (angle * whichWay))  #% 360
                    if abs(startAngle - endAngle) >= 1e-5:
                        if startAngle < endAngle:
                            a1 = startAngle
                            a2 = endAngle
                        else:
                            a1 = endAngle
                            a2 = startAngle

                    #if we didn't use %stylecount here we'd end up with the later sectors
                    #all having the default style
                    sectorStyle = self.slices[i % styleCount]

                    # is it a popout?
                    cx, cy = centerx, centery
                    if sectorStyle.popout != 0:
                        # pop out the sector
                        averageAngle = (a1 + a2) / 2.0
                        aveAngleRadians = averageAngle * pi / 180.0
                        popdistance = sectorStyle.popout
                        cx = centerx + popdistance * cos(aveAngleRadians)
                        cy = centery + popdistance * sin(aveAngleRadians)

                    if type(n) in (ListType, TupleType):
                        theSector = Wedge(
                            cx,
                            cy,
                            xradius + (sn * iradius) - iradius,
                            a1,
                            a2,
                            yradius=yradius + (sn * iradius) - iradius,
                            radius1=yradius + (sn * iradius) - (2 * iradius))
                    else:
                        theSector = Wedge(cx,
                                          cy,
                                          xradius,
                                          a1,
                                          a2,
                                          yradius=yradius,
                                          radius1=iradius)

                    theSector.fillColor = sectorStyle.fillColor
                    theSector.strokeColor = sectorStyle.strokeColor
                    theSector.strokeWidth = sectorStyle.strokeWidth
                    theSector.strokeDashArray = sectorStyle.strokeDashArray

                    g.add(theSector)
                    startAngle = endAngle

                    text = self.getSeriesName(i, '')
                    if text:
                        averageAngle = (a1 + a2) / 2.0
                        aveAngleRadians = averageAngle * pi / 180.0
                        labelRadius = sectorStyle.labelRadius
                        labelX = centerx + (0.5 * self.width *
                                            cos(aveAngleRadians) * labelRadius)
                        labelY = centery + (0.5 * self.height *
                                            sin(aveAngleRadians) * labelRadius)
                        _addWedgeLabel(self, text, g.add, averageAngle, labelX,
                                       labelY, sectorStyle)
                    i = i + 1
                sn = sn + 1

        else:
            i = 0
            #single series doughnut
            iradius = self.height / 5.0
            for angle in normData:
                endAngle = (startAngle + (angle * whichWay))  #% 360
                if abs(startAngle - endAngle) >= 1e-5:
                    if startAngle < endAngle:
                        a1 = startAngle
                        a2 = endAngle
                    else:
                        a1 = endAngle
                        a2 = startAngle

                #if we didn't use %stylecount here we'd end up with the later sectors
                #all having the default style
                sectorStyle = self.slices[i % styleCount]

                # is it a popout?
                cx, cy = centerx, centery
                if sectorStyle.popout != 0:
                    # pop out the sector
                    averageAngle = (a1 + a2) / 2.0
                    aveAngleRadians = averageAngle * pi / 180.0
                    popdistance = sectorStyle.popout
                    cx = centerx + popdistance * cos(aveAngleRadians)
                    cy = centery + popdistance * sin(aveAngleRadians)

                if n > 1:
                    theSector = Wedge(cx,
                                      cy,
                                      xradius,
                                      a1,
                                      a2,
                                      yradius=yradius,
                                      radius1=iradius)
                elif n == 1:
                    theSector = Wedge(cx,
                                      cy,
                                      xradius,
                                      a1,
                                      a2,
                                      yradius=yradius,
                                      iradius=iradius)

                theSector.fillColor = sectorStyle.fillColor
                theSector.strokeColor = sectorStyle.strokeColor
                theSector.strokeWidth = sectorStyle.strokeWidth
                theSector.strokeDashArray = sectorStyle.strokeDashArray

                g.add(theSector)

                # now draw a label
                if labels[i] != "":
                    averageAngle = (a1 + a2) / 2.0
                    aveAngleRadians = averageAngle * pi / 180.0
                    labelRadius = sectorStyle.labelRadius
                    labelX = centerx + (0.5 * self.width *
                                        cos(aveAngleRadians) * labelRadius)
                    labelY = centery + (0.5 * self.height *
                                        sin(aveAngleRadians) * labelRadius)

                    theLabel = String(labelX, labelY, labels[i])
                    theLabel.textAnchor = "middle"
                    theLabel.fontSize = sectorStyle.fontSize
                    theLabel.fontName = sectorStyle.fontName
                    theLabel.fillColor = sectorStyle.fontColor

                    g.add(theLabel)

                startAngle = endAngle
                i = i + 1

        return g
    def _draw_labels(self, cur_drawing, left_labels, right_labels):
        """Layout and draw sub-feature labels for the chromosome.

        Tries to place each label at the same vertical position as the
        feature it applies to, but will adjust the positions to avoid or
        at least reduce label overlap.

        Draws the label text and a coloured line linking it to the
        location (i.e. feature) it applies to.
        """
        if not self._sub_components:
            return
        color_label = self._color_labels

        segment_width = (self.end_x_position - self.start_x_position) \
            * self.chr_percent
        label_sep = (self.end_x_position - self.start_x_position) \
            * self.label_sep_percent
        segment_x = self.start_x_position \
            + 0.5 * (self.end_x_position - self.start_x_position - segment_width)

        y_limits = []
        for sub_component in self._sub_components:
            y_limits.extend((sub_component.start_y_position,
                             sub_component.end_y_position))
        y_min = min(y_limits)
        y_max = max(y_limits)
        del y_limits
        # Now do some label placement magic...
        # from reportlab.pdfbase import pdfmetrics
        # font = pdfmetrics.getFont('Helvetica')
        # h = (font.face.ascent + font.face.descent) * 0.90
        h = self.label_size
        for x1, x2, labels, anchor in [
                (segment_x,
                 segment_x - label_sep,
                 _place_labels(left_labels, y_min, y_max, h),
                 "end"),
                (segment_x + segment_width,
                 segment_x + segment_width + label_sep,
                 _place_labels(right_labels, y_min, y_max, h),
                 "start"),
                ]:
            for (y1, y2, color, back_color, name) in labels:
                cur_drawing.add(Line(x1, y1, x2, y2,
                                     strokeColor=color,
                                     strokeWidth=0.25))
                label_string = String(x2, y2, name,
                                      textAnchor=anchor)
                label_string.fontName = 'Helvetica'
                label_string.fontSize = h
                if color_label:
                    label_string.fillColor = color
                if back_color:
                    w = stringWidth(name,
                                    label_string.fontName,
                                    label_string.fontSize)
                    if x1 > x2:
                        w = w * -1.0
                    cur_drawing.add(Rect(x2, y2 - 0.1 * h, w, h,
                                         strokeColor=back_color,
                                         fillColor=back_color))
                cur_drawing.add(label_string)
Beispiel #26
0
    def make_graphs(self, canvas=None, left_margin=None):  #text=None):
        from reportlab.graphics import renderPDF
        from reportlab.lib.pagesizes import letter
        from reportlab.graphics.shapes import Drawing, String
        from reportlab.graphics.charts.legends import Legend
        from reportlab.graphics.charts.lineplots import LinePlot
        from reportlab.graphics.widgets.markers import makeMarker
        from reportlab.lib import colors
        from reportlab.lib.units import inch
        #help(colors)

        self.framework = {
            4: dict(status=SpotClass.SPINDLE, color=colors.black),
            5: dict(status=SpotClass.OVERLAP, color=colors.limegreen),
            6: dict(status=SpotClass.OUTLIER, color=colors.greenyellow),
            7: dict(status=SpotClass.ICE, color=colors.skyblue),
        }

        # set size and position
        width, height = letter
        #letter_landscape = (width,height)
        plot_dim = 3.0 * inch

        # construct scatter plot
        plot_dxdy_pos = (left_margin * inch, height - plot_dim - 0.5 * inch)
        plot_dxdy = LinePlot()
        plot_dxdy.data = self.plot_dxdy_data

        std_colors = {0: colors.darkred, 1: colors.red, 2: colors.salmon}
        for key in std_colors.keys():
            plot_dxdy.lines[key].strokeColor = None
            plot_dxdy.lines[key].symbol = makeMarker('Circle')
            plot_dxdy.lines[key].symbol.strokeColor = None
            plot_dxdy.lines[key].symbol.fillColor = std_colors[key]
            plot_dxdy.lines[key].symbol.size = 1.2

        for key in self.framework.keys():
            plot_dxdy.lines[key].strokeColor = None
            plot_dxdy.lines[key].symbol = makeMarker('Circle')
            plot_dxdy.lines[key].symbol.strokeColor = None
            plot_dxdy.lines[key].symbol.fillColor = self.framework[key][
                "color"]
            plot_dxdy.lines[key].symbol.size = 1.2

        plot_dxdy.lines[3].strokeColor = None
        plot_dxdy.lines[3].symbol = makeMarker('Circle')
        plot_dxdy.lines[3].symbol.strokeColor = colors.blue
        plot_dxdy.lines[3].symbol.fillColor = None
        plot_dxdy.lines[3].symbol.strokeWidth = 0.6
        plot_dxdy.lines[3].symbol.size = plot_dim * (self.sqrtr2)
        #print plot_dxdy.lines[3].symbol.getProperties()
        plot_dxdy.width = plot_dim
        plot_dxdy.height = plot_dim
        plot_dxdy.xValueAxis.valueMax = 1.0
        plot_dxdy.xValueAxis.valueMin = -1.0
        plot_dxdy.xValueAxis.joinAxis = plot_dxdy.yValueAxis
        plot_dxdy.xValueAxis.joinAxisMode = 'value'
        plot_dxdy.xValueAxis.joinAxisPos = -1.0
        plot_dxdy.yValueAxis.valueMax = 1.0
        plot_dxdy.yValueAxis.valueMin = -1.0
        d_dxdy = Drawing(plot_dim, plot_dim)
        d_dxdy.add(plot_dxdy)

        # construct cdf plot
        plot_cdf_pos = (left_margin * inch,
                        height - 2.0 * (plot_dim + 0.5 * inch))
        plot_cdf = LinePlot()
        plot_cdf.data = self.plot_cdf_data
        plot_cdf.lines[0].strokeColor = colors.blue

        for key in std_colors.keys():
            plot_cdf.lines[key + 1].strokeColor = None
            plot_cdf.lines[key + 1].symbol = makeMarker('Circle')
            plot_cdf.lines[key + 1].symbol.strokeColor = None
            plot_cdf.lines[key + 1].symbol.fillColor = std_colors[key]
            plot_cdf.lines[key + 1].symbol.size = 1.2

        if (len(self.plot_cdf_data) == 5):
            plot_cdf.lines[4].strokeColor = colors.green
        plot_cdf.width = plot_dim
        plot_cdf.height = plot_dim
        plot_cdf.xValueAxis.valueMax = 1.0
        plot_cdf.xValueAxis.valueMin = 0.0
        plot_cdf.yValueAxis.valueMax = 1.0
        plot_cdf.yValueAxis.valueMin = 0.0
        d_cdf = Drawing(plot_dim, plot_dim)
        d_cdf.add(plot_cdf)

        # construct pdf plot
        plot_pdf_pos = (left_margin * inch,
                        height - 3.0 * (plot_dim + 0.5 * inch))
        plot_pdf = LinePlot()
        plot_pdf.data = self.plot_pdf_data
        plot_pdf.lines[1].strokeColor = colors.blue
        plot_pdf.lines[0].strokeColor = None
        plot_pdf.lines[0].symbol = makeMarker('Circle')
        plot_pdf.lines[0].symbol.strokeColor = colors.red
        plot_pdf.lines[0].symbol.size = 1
        plot_pdf.width = plot_dim
        plot_pdf.height = plot_dim
        plot_pdf.xValueAxis.valueMax = 1.0
        plot_pdf.xValueAxis.valueMin = 0.0
        d_pdf = Drawing(2 * plot_dim, plot_dim)
        d_pdf.add(plot_pdf)

        # add legend
        legend = Legend()
        legend.alignment = 'right'
        legend.colorNamePairs = [
            (std_colors[0],
             'Inliers (%d' % int(self.fraction * 100.0) + '% used for fit)'),
            (std_colors[1], 'Other inliers'),
            (std_colors[2], 'Outliers, reject next round'),
        ]
        for key in self.framework.keys():
            legend.colorNamePairs.append(
                (self.framework[key]["color"],
                 "%s" % self.framework[key]["status"]))

        legend.x = plot_dim - 1.0 * inch
        legend.y = plot_dim
        legend.columnMaximum = 8
        d_pdf.add(legend)

        # add titles
        title_pos = (plot_dim / 2.0, plot_dim + 0.25 * inch)
        title_dxdy = String(title_pos[0], title_pos[1], 'dx vs. dy (all)')
        title_dxdy.fontSize = 15
        title_dxdy.textAnchor = 'middle'
        d_dxdy.add(title_dxdy)
        title_cdf = String(title_pos[0], title_pos[1], 'cdf (good)')
        title_cdf.fontSize = 15
        title_cdf.textAnchor = 'middle'
        d_cdf.add(title_cdf)
        title_pdf = String(title_pos[0], title_pos[1], 'pdf (good)')
        title_pdf.fontSize = 15
        title_pdf.textAnchor = 'middle'
        d_pdf.add(title_pdf)

        # draw everything
        renderPDF.draw(d_dxdy, canvas, plot_dxdy_pos[0], plot_dxdy_pos[1])
        renderPDF.draw(d_cdf, canvas, plot_cdf_pos[0], plot_cdf_pos[1])
        renderPDF.draw(d_pdf, canvas, plot_pdf_pos[0], plot_pdf_pos[1])
  def make_graphs(self,canvas=None,left_margin=None):#text=None):
    from reportlab.graphics import renderPDF
    from reportlab.lib.pagesizes import letter
    from reportlab.graphics.shapes import Drawing,String
    from reportlab.graphics.charts.legends import Legend
    from reportlab.graphics.charts.lineplots import LinePlot
    from reportlab.graphics.widgets.markers import makeMarker
    from reportlab.lib import colors
    from reportlab.lib.units import inch
    #help(colors)

    self.framework = {4:dict(status=SpotClass.SPINDLE,color=colors.black),
                      5:dict(status=SpotClass.OVERLAP,color=colors.limegreen),
                      6:dict(status=SpotClass.OUTLIER,color=colors.greenyellow),
                      7:dict(status=SpotClass.ICE,color=colors.skyblue),
    }


    # set size and position
    width,height = letter
    #letter_landscape = (width,height)
    plot_dim = 3.0*inch

    # construct scatter plot
    plot_dxdy_pos = (left_margin*inch,height - plot_dim - 0.5*inch)
    plot_dxdy = LinePlot()
    plot_dxdy.data = self.plot_dxdy_data

    std_colors = {0:colors.darkred, 1:colors.red, 2:colors.salmon}
    for key in std_colors.keys():
      plot_dxdy.lines[key].strokeColor = None
      plot_dxdy.lines[key].symbol = makeMarker('Circle')
      plot_dxdy.lines[key].symbol.strokeColor = None
      plot_dxdy.lines[key].symbol.fillColor = std_colors[key]
      plot_dxdy.lines[key].symbol.size = 1.2

    for key in self.framework.keys():
      plot_dxdy.lines[key].strokeColor = None
      plot_dxdy.lines[key].symbol = makeMarker('Circle')
      plot_dxdy.lines[key].symbol.strokeColor = None
      plot_dxdy.lines[key].symbol.fillColor = self.framework[key]["color"]
      plot_dxdy.lines[key].symbol.size = 1.2

    plot_dxdy.lines[3].strokeColor = None
    plot_dxdy.lines[3].symbol = makeMarker('Circle')
    plot_dxdy.lines[3].symbol.strokeColor = colors.blue
    plot_dxdy.lines[3].symbol.fillColor = None
    plot_dxdy.lines[3].symbol.strokeWidth = 0.6
    plot_dxdy.lines[3].symbol.size = plot_dim*(self.sqrtr2)
    #print plot_dxdy.lines[3].symbol.getProperties()
    plot_dxdy.width = plot_dim
    plot_dxdy.height = plot_dim
    plot_dxdy.xValueAxis.valueMax = 1.0
    plot_dxdy.xValueAxis.valueMin = -1.0
    plot_dxdy.xValueAxis.joinAxis = plot_dxdy.yValueAxis
    plot_dxdy.xValueAxis.joinAxisMode = 'value'
    plot_dxdy.xValueAxis.joinAxisPos = -1.0
    plot_dxdy.yValueAxis.valueMax = 1.0
    plot_dxdy.yValueAxis.valueMin = -1.0
    d_dxdy = Drawing(plot_dim,plot_dim)
    d_dxdy.add(plot_dxdy)

    # construct cdf plot
    plot_cdf_pos = (left_margin*inch, height - 2.0*(plot_dim + 0.5*inch))
    plot_cdf = LinePlot()
    plot_cdf.data = self.plot_cdf_data
    plot_cdf.lines[0].strokeColor = colors.blue

    for key in std_colors.keys():
      plot_cdf.lines[key+1].strokeColor = None
      plot_cdf.lines[key+1].symbol = makeMarker('Circle')
      plot_cdf.lines[key+1].symbol.strokeColor = None
      plot_cdf.lines[key+1].symbol.fillColor = std_colors[key]
      plot_cdf.lines[key+1].symbol.size = 1.2

    if (len(self.plot_cdf_data) == 5):
      plot_cdf.lines[4].strokeColor = colors.green
    plot_cdf.width = plot_dim
    plot_cdf.height = plot_dim
    plot_cdf.xValueAxis.valueMax = 1.0
    plot_cdf.xValueAxis.valueMin = 0.0
    plot_cdf.yValueAxis.valueMax = 1.0
    plot_cdf.yValueAxis.valueMin = 0.0
    d_cdf = Drawing(plot_dim,plot_dim)
    d_cdf.add(plot_cdf)

    # construct pdf plot
    plot_pdf_pos = (left_margin*inch, height - 3.0*(plot_dim + 0.5*inch))
    plot_pdf = LinePlot()
    plot_pdf.data = self.plot_pdf_data
    plot_pdf.lines[1].strokeColor = colors.blue
    plot_pdf.lines[0].strokeColor = None
    plot_pdf.lines[0].symbol = makeMarker('Circle')
    plot_pdf.lines[0].symbol.strokeColor = colors.red
    plot_pdf.lines[0].symbol.size = 1
    plot_pdf.width = plot_dim
    plot_pdf.height = plot_dim
    plot_pdf.xValueAxis.valueMax = 1.0
    plot_pdf.xValueAxis.valueMin = 0.0
    d_pdf = Drawing(2*plot_dim,plot_dim)
    d_pdf.add(plot_pdf)

    # add legend
    legend = Legend()
    legend.alignment = 'right'
    legend.colorNamePairs = [(std_colors[0],'Inliers (%d'%int(self.fraction*100.0) + '% used for fit)'),
                             (std_colors[1],'Other inliers'),
                             (std_colors[2],'Outliers, reject next round'),]
    for key in self.framework.keys():
      legend.colorNamePairs.append(  (self.framework[key]["color"], "%s"%self.framework[key]["status"]  )  )

    legend.x = plot_dim - 1.0*inch
    legend.y = plot_dim
    legend.columnMaximum = 8
    d_pdf.add(legend)

    # add titles
    title_pos = (plot_dim/2.0,plot_dim + 0.25*inch)
    title_dxdy = String(title_pos[0],title_pos[1],'dx vs. dy (all)')
    title_dxdy.fontSize = 15
    title_dxdy.textAnchor = 'middle'
    d_dxdy.add(title_dxdy)
    title_cdf = String(title_pos[0],title_pos[1],'cdf (good)')
    title_cdf.fontSize = 15
    title_cdf.textAnchor = 'middle'
    d_cdf.add(title_cdf)
    title_pdf = String(title_pos[0],title_pos[1],'pdf (good)')
    title_pdf.fontSize = 15
    title_pdf.textAnchor = 'middle'
    d_pdf.add(title_pdf)

    # draw everything
    renderPDF.draw(d_dxdy,canvas,plot_dxdy_pos[0],plot_dxdy_pos[1])
    renderPDF.draw(d_cdf,canvas,plot_cdf_pos[0],plot_cdf_pos[1])
    renderPDF.draw(d_pdf,canvas,plot_pdf_pos[0],plot_pdf_pos[1])
Beispiel #28
0
    def makeSectors(self):
        # normalize slice data
        if type(self.data) in (ListType, TupleType) and type(self.data[0]) in (ListType, TupleType):
            # it's a nested list, more than one sequence
            normData = []
            n = []
            for l in self.data:
                t = self.normalizeData(l)
                normData.append(t)
                n.append(len(t))
        else:
            normData = self.normalizeData(self.data)
            n = len(normData)

        # labels
        if self.labels is None:
            labels = []
            if type(n) not in (ListType, TupleType):
                labels = [""] * n
            else:
                for m in n:
                    labels = list(labels) + [""] * m
        else:
            labels = self.labels
            # there's no point in raising errors for less than enough errors if
            # we silently create all for the extreme case of no labels.
            if type(n) not in (ListType, TupleType):
                i = n - len(labels)
                if i > 0:
                    labels = list(labels) + [""] * i
            else:
                tlab = 0
                for m in n:
                    tlab = tlab + m
                i = tlab - len(labels)
                if i > 0:
                    labels = list(labels) + [""] * i

        xradius = self.width / 2.0
        yradius = self.height / 2.0
        centerx = self.x + xradius
        centery = self.y + yradius

        if self.direction == "anticlockwise":
            whichWay = 1
        else:
            whichWay = -1

        g = Group()
        i = 0
        sn = 0

        startAngle = self.startAngle  #% 360
        if type(self.data[0]) in (ListType, TupleType):
            # multi-series doughnut
            styleCount = len(self.slices)
            iradius = (self.height / 5.0) / len(self.data)
            for series in normData:
                for angle in series:
                    endAngle = startAngle + (angle * whichWay)  #% 360
                    if abs(startAngle - endAngle) >= 1e-5:
                        if startAngle < endAngle:
                            a1 = startAngle
                            a2 = endAngle
                        else:
                            a1 = endAngle
                            a2 = startAngle

                    # if we didn't use %stylecount here we'd end up with the later sectors
                    # all having the default style
                    sectorStyle = self.slices[i % styleCount]

                    # is it a popout?
                    cx, cy = centerx, centery
                    if sectorStyle.popout != 0:
                        # pop out the sector
                        averageAngle = (a1 + a2) / 2.0
                        aveAngleRadians = averageAngle * pi / 180.0
                        popdistance = sectorStyle.popout
                        cx = centerx + popdistance * cos(aveAngleRadians)
                        cy = centery + popdistance * sin(aveAngleRadians)

                    if type(n) in (ListType, TupleType):
                        theSector = Wedge(
                            cx,
                            cy,
                            xradius + (sn * iradius) - iradius,
                            a1,
                            a2,
                            yradius=yradius + (sn * iradius) - iradius,
                            radius1=yradius + (sn * iradius) - (2 * iradius),
                        )
                    else:
                        theSector = Wedge(cx, cy, xradius, a1, a2, yradius=yradius, radius1=iradius)

                    theSector.fillColor = sectorStyle.fillColor
                    theSector.strokeColor = sectorStyle.strokeColor
                    theSector.strokeWidth = sectorStyle.strokeWidth
                    theSector.strokeDashArray = sectorStyle.strokeDashArray

                    g.add(theSector)
                    startAngle = endAngle

                    if labels[i] != "":
                        averageAngle = (a1 + a2) / 2.0
                        aveAngleRadians = averageAngle * pi / 180.0
                        labelRadius = sectorStyle.labelRadius
                        labelX = centerx + (0.5 * self.width * cos(aveAngleRadians) * labelRadius)
                        labelY = centery + (0.5 * self.height * sin(aveAngleRadians) * labelRadius)

                        theLabel = String(labelX, labelY, labels[i])
                        theLabel.textAnchor = "middle"
                        theLabel.fontSize = sectorStyle.fontSize
                        theLabel.fontName = sectorStyle.fontName
                        theLabel.fillColor = sectorStyle.fontColor
                        g.add(theLabel)
                    i = i + 1
                sn = sn + 1

        else:
            # single series doughnut
            styleCount = len(self.slices)
            iradius = self.height / 5.0
            for angle in normData:
                endAngle = startAngle + (angle * whichWay)  #% 360
                if abs(startAngle - endAngle) >= 1e-5:
                    if startAngle < endAngle:
                        a1 = startAngle
                        a2 = endAngle
                    else:
                        a1 = endAngle
                        a2 = startAngle

                # if we didn't use %stylecount here we'd end up with the later sectors
                # all having the default style
                sectorStyle = self.slices[i % styleCount]

                # is it a popout?
                cx, cy = centerx, centery
                if sectorStyle.popout != 0:
                    # pop out the sector
                    averageAngle = (a1 + a2) / 2.0
                    aveAngleRadians = averageAngle * pi / 180.0
                    popdistance = sectorStyle.popout
                    cx = centerx + popdistance * cos(aveAngleRadians)
                    cy = centery + popdistance * sin(aveAngleRadians)

                if n > 1:
                    theSector = Wedge(cx, cy, xradius, a1, a2, yradius=yradius, radius1=iradius)
                elif n == 1:
                    theSector = Wedge(cx, cy, xradius, a1, a2, yradius=yradius, iradius=iradius)

                theSector.fillColor = sectorStyle.fillColor
                theSector.strokeColor = sectorStyle.strokeColor
                theSector.strokeWidth = sectorStyle.strokeWidth
                theSector.strokeDashArray = sectorStyle.strokeDashArray

                g.add(theSector)

                # now draw a label
                if labels[i] != "":
                    averageAngle = (a1 + a2) / 2.0
                    aveAngleRadians = averageAngle * pi / 180.0
                    labelRadius = sectorStyle.labelRadius
                    labelX = centerx + (0.5 * self.width * cos(aveAngleRadians) * labelRadius)
                    labelY = centery + (0.5 * self.height * sin(aveAngleRadians) * labelRadius)

                    theLabel = String(labelX, labelY, labels[i])
                    theLabel.textAnchor = "middle"
                    theLabel.fontSize = sectorStyle.fontSize
                    theLabel.fontName = sectorStyle.fontName
                    theLabel.fillColor = sectorStyle.fontColor

                    g.add(theLabel)

                startAngle = endAngle
                i = i + 1

        return g
Beispiel #29
0
from reportlab.graphics.shapes import Drawing, String, colors
from reportlab.graphics import renderPDF
from reportlab.pdfbase.ttfonts import *
'''--- 添加中文支持 ---'''
pdfmetrics.registerFont(TTFont('mysh',
                               'C:\WINDOWS\FONTS\ARIAL.TTF'))  # 注册要使用的字体
pdfmetrics.registerFont(TTFont('g', 'C:\WINDOWS\FONTS\ARIAL.TTF'))
'''--- 创建画布 ---'''
d = Drawing(300, 200)  # 创建画布并设置画布尺寸
'''--- 创建文本内容并设置样式与位置 ---'''
s1 = String(150, 100, '这一行使用字体的字体是微软雅黑!')  # 创建字符串并设置坐标、内容
s1.fontName = 'mysh'  # 设置字体
s1.fontSize = 14  # 设置字号
s1.fillColor = colors.red  # 设置字体颜色
s1.textAnchor = 'middle'  # 设置锚点为中心(即位置坐标为文本中心点坐标)

s2 = String(150,
            120,
            '陈磊你是最帅的!',
            fontName='g',
            fontSize=100,
            fillColor=colors.yellow,
            textAnchor='middle')
# 另一种设置方式
'''--- 添加内容到画布并生成PDF文件 ---'''
d.add(s1)  # 将字符串添加到画布
d.add(s2)

renderPDF.drawToFile(d, 'myPDF.pdf', '我的第一个PDF文件。')  # 生成PDF文件并设置文件名称与文档描述