GnuplotGraphView.st
changeset 5742 5a4a11ee0b1f
child 5750 78ef41988e39
equal deleted inserted replaced
5741:75e825820538 5742:5a4a11ee0b1f
       
     1 "{ Package: 'stx:libwidg2' }"
       
     2 
       
     3 "{ NameSpace: Smalltalk }"
       
     4 
       
     5 ImageView subclass:#GnuplotGraphView
       
     6 	instanceVariableNames:'script data title'
       
     7 	classVariableNames:''
       
     8 	poolDictionaries:''
       
     9 	category:'Views-Misc'
       
    10 !
       
    11 
       
    12 !GnuplotGraphView class methodsFor:'documentation'!
       
    13 
       
    14 documentation
       
    15 "
       
    16     displays the graph of the data (in model) using a gnuplot script
       
    17 
       
    18     [author:]
       
    19         cg
       
    20 
       
    21     [instance variables:]
       
    22 
       
    23     [class variables:]
       
    24 
       
    25     [see also:]
       
    26 
       
    27 "
       
    28 !
       
    29 
       
    30 examples
       
    31 "
       
    32  Notice that everything between [exBegin] and [exEnd] is extracted by the html-doc generator
       
    33  to create nicely formatted and clickable executable examples in the generated html-doc.
       
    34  (see the browsers class-documentation menu items for more)
       
    35 
       
    36  trying the widget as standAlone view:
       
    37                                                         [exBegin]
       
    38     GnuplotGraphView new 
       
    39         script:(GnuplotGraphView defaultScript);
       
    40         model:(ValueHolder with:(Random new next:100));
       
    41         open
       
    42                                                         [exEnd]
       
    43 
       
    44  embedded in another view:
       
    45                                                         [exBegin]
       
    46     |top v|
       
    47 
       
    48     top := StandardSystemView new.
       
    49     top extent:300@300.
       
    50     v := GnuplotGraphView new.
       
    51     v origin:10@10 corner:150@150.
       
    52     top add:v.
       
    53     top open
       
    54                                                         [exEnd]
       
    55 "
       
    56 ! !
       
    57 
       
    58 !GnuplotGraphView class methodsFor:'defaults'!
       
    59 
       
    60 defaultScript
       
    61     "return a default initial gnuplot script"
       
    62 
       
    63     ^ self defaultScriptForHistogram
       
    64 !
       
    65 
       
    66 defaultScriptForHistogram
       
    67     "a default initial gnuplot script to show a histogram"
       
    68 
       
    69     ^ '
       
    70 set term %(outputFormat)
       
    71 set output "%(outputFile)"
       
    72 set title "%(title)"
       
    73 plot [-10:110] ''%(data)'' with histogram 
       
    74 '
       
    75 ! !
       
    76 
       
    77 !GnuplotGraphView methodsFor:'accessing'!
       
    78 
       
    79 data
       
    80     ^ data
       
    81 !
       
    82 
       
    83 data:something
       
    84     data := something.
       
    85 !
       
    86 
       
    87 script
       
    88     ^ script
       
    89 !
       
    90 
       
    91 script:something
       
    92     script := something.
       
    93 !
       
    94 
       
    95 title
       
    96     ^ title
       
    97 !
       
    98 
       
    99 title:something
       
   100     title := something.
       
   101 ! !
       
   102 
       
   103 !GnuplotGraphView methodsFor:'drawing'!
       
   104 
       
   105 generateDataFileIn:aDirectory
       
   106     "the format of data expected by gnuplot depends on the type of plot:
       
   107         regular plot: a number of values (each in a separate line)
       
   108         multiCol plots: a number of lines, each containing a row
       
   109      here, handle common situations;
       
   110      however, if the data does not match, the program should have prepared the 
       
   111      data into a string and present it as such.
       
   112      Strings will be sent as-is to the file"
       
   113 
       
   114     |dataFilename printRow|
       
   115 
       
   116     dataFilename := (Filename newTemporaryIn:aDirectory) pathName.
       
   117     dataFilename := dataFilename asFilename withSuffix:'dat'.
       
   118 
       
   119     (data isString or:[data isByteArray]) ifTrue:[
       
   120         dataFilename contents:data.
       
   121         ^ dataFilename.
       
   122     ].
       
   123 
       
   124     data isCollection ifFalse:[
       
   125         dataFilename contents:data printString.
       
   126         ^ dataFilename.
       
   127     ].
       
   128 
       
   129     printRow := 
       
   130         [:s :eachValueOrRow |
       
   131             eachValueOrRow isString ifTrue:[
       
   132                 s nextPutAll:eachValueOrRow
       
   133             ] ifFalse:[
       
   134                 eachValueOrRow isAssociation ifTrue:[
       
   135                     eachValueOrRow key printOn:s.
       
   136                     s space.
       
   137                     eachValueOrRow value printOn:s.
       
   138                 ] ifFalse:[
       
   139                     eachValueOrRow isCollection ifTrue:[
       
   140                         eachValueOrRow 
       
   141                             do:[:element | element printOn:s]
       
   142                             separatedBy:[ s space]
       
   143                     ] ifFalse:[
       
   144                         eachValueOrRow printOn:s
       
   145                     ].
       
   146                 ].
       
   147             ].
       
   148             s cr.
       
   149     ].
       
   150 
       
   151     dataFilename writingFileDo:[:s |
       
   152         data isSequenceable ifTrue:[
       
   153             data do:[:eachValueOrRow |
       
   154                 printRow value:s value:eachValueOrRow.
       
   155             ].
       
   156         ] ifFalse:[
       
   157             data keysAndValuesDo:[:eachKey :eachValueOrRow |
       
   158                 eachKey printOn:s.
       
   159                 s space.
       
   160                 printRow value:s value:eachValueOrRow.
       
   161             ].
       
   162         ].
       
   163     ].
       
   164     ^ dataFilename
       
   165 !
       
   166 
       
   167 generateImage
       
   168     "
       
   169      self new
       
   170         script:(GnuplotGraphView defaultScript);
       
   171         data:(RandomGenerator new next:50);
       
   172         open
       
   173     "
       
   174     |command tmpDir outStream errorStream statusCode ok
       
   175      dataFilename scriptFilename outFilename argsDict expandedScript
       
   176      stdout stderr|
       
   177 
       
   178     script isNil ifTrue:[^ self].
       
   179 
       
   180     command := 'gnuplot "%1"'.
       
   181     argsDict := Dictionary new.
       
   182 
       
   183     tmpDir := Filename tempDirectory.
       
   184 
       
   185     data notEmptyOrNil ifTrue:[
       
   186         dataFilename := self generateDataFileIn:tmpDir.
       
   187     ] ifFalse:[
       
   188         dataFilename := ''.
       
   189     ].
       
   190 
       
   191     outFilename := (Filename newTemporaryIn:tmpDir) withSuffix:'png'.
       
   192 
       
   193     argsDict at:'data' put:dataFilename.
       
   194     argsDict at:'dataSize' put:(data size).
       
   195     argsDict at:'outputFile' put:(outFilename pathName).
       
   196     argsDict at:'outputFormat' put:'png'.
       
   197     argsDict at:'title' put:(title ? '').
       
   198 
       
   199     expandedScript := script bindWithArguments:argsDict.
       
   200     scriptFilename := (Filename newTemporaryIn:tmpDir).
       
   201     scriptFilename := scriptFilename asFilename withSuffix:'gnuplot'.
       
   202     scriptFilename contents:expandedScript.
       
   203 
       
   204     command := command bindWith:(scriptFilename pathName).
       
   205 
       
   206     outStream := '' writeStream.
       
   207     errorStream := '' writeStream.
       
   208 
       
   209     ok := OperatingSystem 
       
   210         executeCommand:command
       
   211         inputFrom:Stdin
       
   212         outputTo:outStream
       
   213         errorTo:errorStream
       
   214         environment:nil
       
   215         inDirectory:tmpDir
       
   216         lineWise:true
       
   217         showWindow:false
       
   218         onError:[:status | 
       
   219             statusCode := status code.
       
   220             ok := false.
       
   221         ].
       
   222 
       
   223     ok ifFalse:[
       
   224         (stderr := errorStream contents) notEmptyOrNil ifTrue:[
       
   225             Transcript showCR:(stderr withColor:Color red).
       
   226         ].
       
   227     ].
       
   228     (stdout := outStream contents) notEmpty ifTrue:[
       
   229         Transcript showCR:stdout.
       
   230     ].
       
   231 
       
   232     image := Image fromFile:outFilename.
       
   233 !
       
   234 
       
   235 generateMagnifiedImage
       
   236     image isNil ifTrue:[
       
   237         self generateImage  
       
   238     ].
       
   239     super generateMagnifiedImage.
       
   240 ! !
       
   241 
       
   242 !GnuplotGraphView class methodsFor:'documentation'!
       
   243 
       
   244 version
       
   245     ^ '$Header$'
       
   246 !
       
   247 
       
   248 version_CVS
       
   249     ^ '$Header$'
       
   250 ! !
       
   251