Creating a Graph, Part 2

This page will start with the script graph.py, and create a new graph2.py to add some additional features.

graph.py makes a simple graph with X and Y axes, labels for the axes, and a title. With graph2.py we will add the ability to assign a scale to the axes, make user-defined tick marks, and then give some feed back about the typographic features of this graph.

New requestors
xrange = int(scribus.valueDialog('Maximum X Scale', 'Enter Top X Value\n(Scale)')) xmark = int(scribus.valueDialog('Tick Mark Interval', 'Enter X Interval for Tick Marks')) The first asks for the value for the total X axis in the units of the graph (no need to name the units here), which we need to convert to an integer. The second asks for the number of units between tick marks on the X axis scale.

From this we can calculate a typographic scale for our graph, using the fact that we have set the width of the graph with the variable xaxis: xscale = xaxis/xrange # this gives you typographic points per data unit Now we can generate the tick marks, using a while loop: while (xtick < (xorigin + xaxis)): xt = scribus.createLine(xtick, yorigin, xtick, yorigin - 10) scribus.setLineWidth(t, xt) scribus.setLineColor(color, xt) scribus.setFillColor(color, xt) xtick = xtick + xmark * xscale These tick marks will appear above the axis line, but could as easily appear below -- change -10 to +10.

Now, as an aid to the Scribus user, let's add a text box that will help with the page measurements for adding other features with Scribus. Also, we will have a reminder about the settings we supplied: caption = "Scale for this graph\n\n" "X: " + str(xmark) + "units/mark\n" + "Y: " caption = caption + str(ymark) + "units/mark\n" + str(xscale) + " pts/unit (X)\n" caption = caption + str(yscale) + " pts/unit (Y)" S = scribus.createText(200, 120, 150, 80) scribus.setTextColor("Blue", S)   scribus.setText(caption, S)    scribus.setTextAlignment(1, S)    scribus.setFont("Nimbus Roman No9 L Bold", S)    scribus.setFontSize(8, S)
 * 1) These caption statements can be combined to one line