[reportlab-users] Computing chart size

Johan Paul Glutting reportlab-users@reportlab.com
Mon, 04 Oct 2004 16:34:12 +0200


Vladimir,

You can try this function I am using to generate graphs. It just uses a 
fixed size (which is probably what you want, since your page is always 
going to be the same size), but you can pass in the size you want 
specifically. It has some settings that are what I want for all my 
charts, and these cannot be changed (like the filled diamond, but you 
could pass that in as well).

It has a nice function (well, I like it, anyway ;-) that calculates a 
"ceiling" for the data, so that the data points are not plotted right on 
the top of the y axis, no matter what your scale is (at least it has 
worked for the data that I have thrown at it).

This is specific to horizontal line charts, but it should not be too 
hard to tweak:


    def hlinechart(self, data, cats, llabels=[], dw=400, dh=200, lh=30, 
vmax=99, bgcolor='lemonchiffon', sep=0.5):
        D = Drawing(dw, dh)
        lc = HorizontalLineChart()
        lc.data = data
        lc.categoryAxis.categoryNames = cats
        lc.width = dw
        lc.height = dh - lh
        lc.fillColor = colors.toColor(bgcolor)
        lc.strokeColor = colors.black
        lc.strokeWidth = 0.5
        # Put in a function to calculate the vmax directly from the data ...
        if vmax==0:
            pass
        elif vmax==99:
            import math
            ltmp = []
            for lst in data: ltmp += lst
            ltmp.sort()
            sn = "%e" %(ltmp.pop())
            smant, sexp = sn.split('e+')
            mant, exp = float(smant), float(sexp)
            vmax = int(math.ceil(mant)*(10**exp))
            lc.valueAxis.valueMax = vmax
        else:
            lc.valueAxis.valueMax = vmax
        # --------------------------------
        lc.valueAxis.valueMin = 0
        lc.valueAxis.strokeWidth = 1
        lc.valueAxis.gridStrokeWidth = 0.1
        lc.valueAxis.visibleGrid = 1
        lc.valueAxis.gridStrokeColor = colors.cornflower
        # --------------------------------
        lc.categoryAxis.strokeWidth = 1
        #lc.categoryAxis.gridStrokeWidth = 0.25
        #lc.categoryAxis.visibleGrid = 1
        #lc.categoryAxis.gridStrokeColor = colors.black
        # --------------------------------
        lc.lines.symbol = makeMarker('FilledDiamond')
        lc.lineLabelFormat = '%s'
        lc.x = 0
        lc.y = lh
        legend = LineLegend()
        legend.alignment = 'right'
        legend.x = 0
        legend.y = 0
        legend.dxTextSpace = 3
        leglabels = []
        for i, label in enumerate(llabels):
            leglabels.append((lc.lines[i].strokeColor, label))
        legend.colorNamePairs = leglabels
        D.add(lc)
        D.add(legend)
        s = Spacer(0.5*cm, sep*cm)
        self.Elements.append(s)
        self.Elements.append(D)



En/na Vladimir Prus ha escrit:

>Hello,
>I'm trying to use reportlab to generate very basic charts, and run into a 
>trouble. The code suggested in the docs is:
>
>   drawing = Drawing(....., ........)
>
>   lc = HorizontalLineChart()
>
>   drawing.add(lc)
>
>I'm required to provide the size of the Drawing object. However, I don't know 
>how to compute the right size. I want the drawing object to be just as large 
>as the chart is and the size of the chart depends on the number of data 
>points.
>
>Is it possible to get some "bounding box" of the chart, which can be then used 
>as size of the Drawing object?
>
>Thanks in advance,
>Volodya.
>_______________________________________________
>reportlab-users mailing list
>reportlab-users@reportlab.com
>http://two.pairlist.net/mailman/listinfo/reportlab-users
>  
>


-- 
JP Glutting
Oficina de Qualitat		Plaša VÝctor Siurana, 1
Universitat de Lleida		25003 Lleida
-------------------------------------------------------
Tel: 973 703 103		Fax: 973 702 036