예제 #1
0
def scatter_plot(final_dis_angle, xname, yname):
    drawing = Drawing(400, 300)

    chart = ScatterPlot()

    chart.width = 450
    chart.height = 350

    chart.x = 32
    chart.y = 26

    chart.data = [final_dis_angle]

    chart.joinedLines = 0
    chart.fillColor = color03
    chart.lineLabelFormat = None
    chart.lineLabels.fontName = 'Helvetica'

    lab = Label()
    lab.setOrigin(130, 260)

    chart.xValueAxis.avoidBoundFrac = 1
    chart.xValueAxis.visibleGrid = 1
    chart.xValueAxis.tickDown = 2
    chart.xValueAxis.labels.fontName = 'Helvetica'
    chart.xValueAxis.labels.fontSize = 10
    chart.xValueAxis.labelTextFormat = '%d'
    chart.leftPadding = -32

    chart.xLabel = xname
    chart.xValueAxis.forceZero = 1

    chart.yValueAxis.avoidBoundFrac = 1
    chart.yValueAxis.visibleGrid = 1
    chart.yValueAxis.tickLeft = 2
    chart.yValueAxis.labels.fontName = 'Helvetica'
    chart.yValueAxis.labels.fontSize = 10
    chart.yValueAxis.labelTextFormat = '%d'

    chart.yLabel = yname
    chart.yLabel.center(10)
    chart.yValueAxis.forceZero = 0

    # Title = Label()
    # Title.fontName = 'Helvetica-Bold'
    # Title.fontSize = 10
    # Title.x = 100
    # Title.y = 550
    # Title._text = 'This is just a test chart'
    # Title.maxWidth = 20
    # Title.height = 100
    # Title.textAnchor = 'middle'

    # legend = Legend()
    # legend.colorNamePairs = [(color01, 'Widgets'), (color02, 'Sprockets')]
    # legend.fontName = 'Helvetica'
    # legend.fontSize = 8
    # legend.x = 470
    # legend.y = 470
    # legend.dxTextSpace = 4
    # legend.dy = 7
    # legend.dx = 7
    # legend.deltay = 4
    # legend.alignment = 'right'

    drawing.add(chart)
    return drawing
예제 #2
0
def line_chart(data, labels, **kw):
    """
    :param data:    contains a three dimensional array of values (list of lists of points)
                    or just a list of datapoint lists (it will be auto-transposed to start at 0)
    :type data:     list
    :param labels:  can contain, but must not ["xlabel", "ylabel", ["data label0", ...]]
                    third item can also be an interger stating the iteration start as label
                    when of same size as data, then a legend is added instead
    :type lables:   ???
    :param xlim:    limit the x axis to these values, e.g. (0, 100)
    :type xlim:     Tuple(Number, Number)
    :param ylim:    limit the y axis to these values, e.g. (0, 50)
    :type ylim:     Tuple(Number, Number)
    :param size:    size in pixels, e.g. (18*cm, 9*cm)
    :type size:     Tuple(Number, Number)
    :param title:   title of bar chart
    :type title:    string
    :param lines:   list of colors we should use to paint lines
    :type lines:    list[???]
    :param markers: list of markers we should use to draw markers
    :type markers:  list[`PDF_LINE_MARKERS`,...]
    :param scatter: weather to do a scatter plot or line chart
    :type scatter:  boolean
    """
    # Get all arguments from the keywordargs
    title = kw.pop('title', None)
    scatter = kw.pop('scatter', False)
    size = kw.pop('plotSize', (18 * cm, 9 * cm))
    lines = kw.pop('lines', PDF_CHART_COLORS)
    markers = kw.pop('markers', PDF_LINE_MARKERS)
    xlim = kw.pop('xlim', None)
    ylim = kw.pop('ylim', None)

    drawing = Drawing(size[0], size[1])

    chart = None

    if(scatter):
        chart = ScatterPlot()
    else:
        chart = LinePlot()

    for key, val in list(kw.items()):
        setattr(chart, key, val)

    if title is not None:
        drawing.add(String(20, size[1] - 10, title), name='title')
        chart.y -= 10

    chart.width = drawing.width - 20
    chart.height = drawing.height - 40
    chart.x = 10
    chart.y = 10

    chart.data = data if type(data[0][0]) in (tuple, list) else [list(zip(list(range(len(i))), i)) for i in data]

    max_y = 0
    min_y = maxsize
    for i in range(len(data)):
        chart.lines[i].strokeColor = HexColor(lines[i % len(lines)])
        if markers is not None:
            chart.lines[i].symbol = makeMarker(markers[i % len(markers)])
            chart.lines[i].symbol.size = 3
        max_y = max([k[1] for k in chart.data[i]] + [max_y])
        min_y = min([k[1] for k in chart.data[i]] + [min_y])
    chart.yValueAxis.valueMax = max_y * 1.1
    chart.yValueAxis.valueMin = min_y * 0.9

    chart.xValueAxis.visibleGrid = True
    chart.yValueAxis.visibleGrid = True

    if xlim is not None:
        chart.xValueAxis.valueMin = xlim[0]
        chart.xValueAxis.valueMax = xlim[1]
    if ylim is not None:
        chart.yValueAxis.valueMin = ylim[0]
        chart.yValueAxis.valueMax = ylim[1]

    if scatter:
        chart.xLabel = ''
        chart.yLabel = ''
        chart.y -= 10
        chart.lineLabelFormat = None

    if labels is not None:
        if len(labels) > 0:
            xlabel = Label()
            xlabel._text = labels[0]  # pylint: disable=W0212
            xlabel.textAnchor = 'middle'
            xlabel.x = drawing.width / 2
            xlabel.y = 5
            chart.y += 15
            drawing.add(xlabel, name="xlabel")

        if len(labels) > 1:
            ylabel = Label()
            ylabel._text = labels[1]  # pylint: disable=W0212
            xlabel.textAnchor = 'middle'
            ylabel.angle = 90
            ylabel.x = 0
            ylabel.y = drawing.height / 2
            chart.x += 12
            drawing.add(ylabel, name="ylabel")

        if len(labels) > 2:
            # when labels are of same size as max nr of data point, use as x axis labels
            if len(labels[2]) == max([len(x) for x in data]):
                chart.categoryAxis.categoryNames = labels[2]  # pylint: disable=E1101
                chart.xValueAxis.labels.angle = 30  # pylint: disable=E1101
            # otherwise when integer use the counter
            elif type(labels[2]) == int:
                temp = range(labels[2], max([len(x) for x in data]) + labels[2])
                chart.categoryAxis.categoryNames = temp  # pylint: disable=E1101
            # or we could add a legend when of same size as data
            elif len(labels[2]) == len(data):
                legend = Legend()
                chart.height -= 8
                chart.y += 8
                xlabel.y += 8
                legend.boxAnchor = 'sw'
                legend.x = chart.x + 8
                legend.y = -2
                legend.columnMaximum = 1
                legend.deltax = 50
                legend.deltay = 0
                legend.dx = 10
                legend.dy = 1.5
                legend.fontSize = 7
                legend.alignment = 'right'
                legend.dxTextSpace = 5
                legend.colorNamePairs = [(HexColor(lines[i]), labels[2][i]) for i in range(len(chart.data))]
                legend.strokeWidth = 0
                drawing.add(legend, name='legend')

    drawing.add(chart, name='chart')

    return drawing