Topic
6 replies Latest Post - ‏2013-09-09T21:21:25Z by JonPeck
Nihonisumitai
Nihonisumitai
4 Posts
ACCEPTED ANSWER

Pinned topic TEXT.spe in function / PDF pagebreaks

‏2013-09-05T14:27:46Z |

Hi,

I am trying to produce an array of charts and tables in the following way:
Using: GGRAPH,CTABLES, Python, and TEXT.spe.

I had some problems with GGRAPH because I superimposed one graph on another, resulting in 2 legends/explanations.
I had to build a template and edit it with an XML-editor, to make sure the second legend would be shown at the right place.

To use the graph repreatedly I build a small function library called lib.py which is placed in the extensions folder.
The function I am working on looks like this:



def graphAndTables(var, label):
    """Graph and tables"""
    labeld = label.decode('utf8')
    import spssaux, SpssClient
    cmd = """


     TEXT "<html><B><font size=5><font face='Arial'>{0}</B></font></html>".

                GGRAPH
                    /GRAPHDATASET NAME="graphdataset" VARIABLES=F1_REF COUNT()[name="COUNT"] {0}
                    casesPerProgram MISSING=LISTWISE REPORTMISSING=no
                    /GRAPHSPEC SOURCE=INLINE
                 TEMPLATE ='C:/V/templateGaT.sgt'.
                BEGIN GPL
                     SOURCE: s=userSource(id("graphdataset"))
                     DATA: cases=col(source(s), name("casesPerProgram"), unit.category())
                     DATA: COUNT=col(source(s), name("COUNT"))
                     DATA: program=col(source(s), name("F1_REF"), unit.category())
                     DATA: rating_cat=col(source(s), name("{0}"), unit.category())
                     DATA: rating_stet=col(source(s), name("{0}"))
                        PAGE: begin(scale(800px, 600px))
                           GRAPH: begin(origin(33%, 10%), scale(50%, 80%))
                                GUIDE: axis(dim(1), label(""))
                                 GUIDE: axis(dim(2), label("{1}"))
                                 GUIDE: legend(aesthetic(aesthetic.color.interior), label("Rating"))
                                 SCALE: linear(dim(2), include(0))
                                 SCALE: cat(aesthetic(aesthetic.color.interior), include("5", "4", "3", "2", "1"),
                                 reverse(), map(("1", color."008000f7"), ("2", color."00c900f7"),
                                 ("3", color."ffff00e9"), ("4", color."fe6d01ff"), ("5", color."ff0000ff")))
                                 COORD: transpose()
                                 ELEMENT: interval.stack(position(summary.percent((("All programs"+Studiengang)
                                 *COUNT), base.coordinate(dim(1)))) , color.interior(rating_cat), shape.interior(shape.square)))
                            GRAPH: end()
                            GRAPH: begin(origin(33%, 10%), scale(50%, 80%))
                                 SCALE: linear(dim(2), include(1, 2, 3, 4, 5), min(1))
                                 GUIDE: text.subtitle(label("Mean"))
                                 GUIDE: text.footnote(label("Percent"))
                                 GUIDE: axis(dim(2), label(""), opposite())
                                 GUIDE: axis(dim(1), null())
                                 GUIDE: legend(aesthetic(aesthetic.color.saturation), label("Cases per program"))
                                 COORD: transpose()
                                 ELEMENT: line(position(summary.mean((" "+program)*rating_stet)), color(color."d40000ff"))
                                 ELEMENT: point(position(summary.mean((" "+program)*rating_stet)),
                                 shape(shape.circle), color.saturation(cases), color(color.red))
                           GRAPH: end()
                        PAGE: end()
                END GPL.

                               
                           CTABLES
                              /VLABELS VARIABLES=F1_REF {0} DISPLAY=NONE
                              /TABLE F1_REF [C] BY {0} [C][COUNT 'n' F40.0, ROWPCT.COUNT '%' PCT40.1]
                              /CATEGORIES VARIABLES=F1_REF ORDER=D KEY=VALUE  TOTAL=YES LABEL='All programs' POSITION=AFTER MISSING=EXCLUDE EMPTY=EXCLUDE
                              /CATEGORIES VARIABLES={0} ORDER=A KEY=VALUE EMPTY=INCLUDE TOTAL=YES POSITION=AFTER
                                MISSING=EXCLUDE
                              /TITLES
                                TITLE=')TABLE'.

                                            CTABLES
                                                  /VLABELS VARIABLES=F1_REF {0} DISPLAY=NONE
                                                      /TABLE F1_REF [C] BY {0} [S][MEAN F40.2, STDDEV F40.2, MEDIAN, PTILE 25 F40.2, PTILE 75
                                                        F40.2]
                                                          /CATEGORIES VARIABLES=F1_REF ORDER=D KEY=VALUE TOTAL=YES LABEL='All programs' POSITION=AFTER MISSING=EXCLUDE EMPTY=EXCLUDE
                                                          /TITLES
                                                            TITLE=')TABLE'.
                                                       
    """.format(var, labeld)
    spss.Submit(cmd)



It works fine - but there are two problems:

1)
The TEXT-command produces the message:

Traceback (most recent call last):
  File "<string>", line 23, in <module>
  File "", line 17, in <module>
  File "D:\Users\User\AppData\Local\IBM\SPSS\Statistics\21\extensions\TEXT.py", line 89, in Run
    createText(**oobj.parsedparams)
  File "D:\Users\User\AppData\Local\IBM\SPSS\Statistics\21\extensions\TEXT.py", line 137, in createText
    specificitem.SetTextContents("".join(strings))
AttributeError: 'SpssClient.SpssChartItem' object has no attribute 'SetTextContents'

But it works just fine if used from syntax window.


2)

After exporting the content to pdf every image (GGRAPH) is placed on one page - something like an implicit pagebreak.


Is there a way to make the TEXT-command work in that setting? (Everything else I tried was unstable two, e.g. spss.TextBlock and SetTextContents)
Is there a way to export to pdf without unwanted pagebreaks?


Many thanks in advance!
Dirk

PS: Images in PDF look very good. But: if exported as svg the images are still fine, but the texts in the images are messed up. Is there a way to control this?



 

  • JonPeck
    JonPeck
    192 Posts
    ACCEPTED ANSWER

    Re: TEXT.spe in function / PDF pagebreaks

    ‏2013-09-05T17:27:47Z  in response to Nihonisumitai

    On the first point, the TEXT command expects the text block to be the last item in the Viewer as soon as it is executed.  I think the graph is starting to get created before the text command actually completes.  TEXT creates the text block object using backend apis but completes by using some scripting apis.

    Two things to try: first, use Submit with just the text command and then a second Submit with the rest.  Second, if that doesn't work, after submitting TEXT, add a 1 second sleep call before submitting the ggraph command.

    The pagination issue may be due to a calculation or miscalculation of the required space.  Try making the charts a little smaller to see if that helps.

    As for svg, I'm not aware of that problem.  If you can post a complete job that shows this, we can investigate.

    • Nihonisumitai
      Nihonisumitai
      4 Posts
      ACCEPTED ANSWER

      PDF and pagebreaks

      ‏2013-09-06T08:33:03Z  in response to JonPeck

      Thank you! The two submits worked very well!

      To test the pdf-issue I used a TEXT and a GGRAPH-element.

      After exporting:

      in Word -> TEXT appaers and the Graph follows.

      in PDF -> TEXT-element has its own page and the Graph as well.

       

       

      Attachments

    • Nihonisumitai
      Nihonisumitai
      4 Posts
      ACCEPTED ANSWER

      Export to Word/ppt with wmf/emf

      ‏2013-09-06T09:09:29Z  in response to JonPeck

      as for the SVG - issue. I was wrong.

      Althoug the exported wmf/emf looks awful in irfanview it looks great after placing it into word or ppt.

      Is there a way to tell SPSS to use wmf/emf directly when exporting to word or ppt?

      This would be like christmas and easter in one day.

      • JonPeck
        JonPeck
        192 Posts
        ACCEPTED ANSWER

        Re: Export to Word/ppt with wmf/emf

        ‏2013-09-06T13:04:24Z  in response to Nihonisumitai

        I am surprised that you are getting svg graphics. I thought the Word exports would be emf or maybe png.  Are you using OUTPUT EXPORT or OMS?  If you use OMS you can specify the graphics image type, although I don't recall whether that is honored for exports to Office formats.

        • Nihonisumitai
          Nihonisumitai
          4 Posts
          ACCEPTED ANSWER

          Re: Export to Word/ppt with wmf/emf

          ‏2013-09-09T10:19:39Z  in response to JonPeck

          Hi, thank you so much for your help!

          Sorry, I confused "SVG" with an abbreviation and did not think of the file extension. I wanted to refer to (scalable) vector graphics.

          The WORD exports are PNGs. The PPT exports as well. Resulting in images (graphs) looking slightly blurred.

          That's the reason why my colleagues export all tables from SPSS to excel and do the graphics completely there - it's the way they were taught to do it at university, by the way.

          I think the possibilities with GGRAPH are great - although sometimes a bit buggy. And with EMF-exports it would be just fine. But I would have to export one graph at a time.

          So far I used OUTPUT EXPORT and some OMS. Now I tried OMS only - hoped it would do the job and I would have a DOC with EMFs.

          Problem: IMAGEFORMAT applies only to FORMAT=OXML, HTML, SPV, and SPW. Tried it nonetheless but it's just ignored. So the problem persists.

          As well as the problem with PDF export: Even in the case of a very small GGRAPH with PAGE: begin(scale(187px,150px)) the export to PDF would result in a large image filling half the page - compared to word where two graphics would cover the same space. And unwanted page breaks: The exported document starts with a blank page and the first graph follows with the next graph following on page three. In PDF every item starts on a new page even if it is just one line of text. I tried both methods OMS and OUTPUT EXPORT.

          And another problem: orientation / landscape.

          Using the following code (because by using the GUI it want work at all. I could switch to landscape but if I close and open the dialog it would already be switched back):

          begin program.
          import SpssClient
          SpssClient.StartClient( )
          doc=SpssClient.GetDesignatedOutputDoc( )
          doc.SetPrintOptions(SpssClient.PrintOptions.Orientation, '2') # landscape
          SpssClient.StopClient( )
          end program.

          It only works with OUTPUT EXPORT and PDF. Not with OUTPUT EXPORT and DOC and not at all with OMS (tried PDF and DOC).

          I am using SPSS version 21.

           

          Thank you!

          Dirk

          • JonPeck
            JonPeck
            192 Posts
            ACCEPTED ANSWER

            Re: Export to Word/ppt with wmf/emf

            ‏2013-09-09T21:21:25Z  in response to Nihonisumitai

            Not sure I can answer all these questions, but here's a start.

            -The "slightly blurred" appearance you describe is actually anti-aliasing done to prevent jaggies in the charts.  I don't think it depends on the format.  You might try TIFF to see if that improves things.

            You can export sets of graphs using OUTPUT EXPORT (but separate from Word).  If you want to apply type-based selection, you can try the SPSSINC MODIFY OUTPUT extension command to set the visibility of the items you want to export.  Similarly, you can use OMS to generate the images but ignore the html or whatever.

            Going beyond this, you can write a custom script that calls the output export methods to get the desired charts.  You would still need to merge them into the Word or whatever document.

            Statistics 22 has the ability to set output options such as landscape in a persistent way via preferences.  However, the STATS OUTPUT ATTRS extension command can set these in syntax in earlier versions as well as 22 so you don't have to keep setting these manually.