Saturday, August 23, 2008

Grapher

'If you copy or distribute this script without any change except the copyright
'information, please let me know. If you are interested and find a lot of fun or
'bugs in it, please contact with me. It's my pleasure to make friends with
'everyone.
'Copyright: http://scriptdemo.blogspot.com
Sub Main
'Open the application and get it visible
Dim GrapherApp As Object
On Error Resume Next 'Turn off error reporting.
Set SurferApp = GetObject(,"Grapher.Application")
If Err.Number<>0 Then
Set GrapherApp = CreateObject("Grapher.Application")
End If
On Error GoTo 0 'Turn on error reporting.
GrapherApp.Visible = True

' set up the canvas
Dim Docs As Object
Set Docs = GrapherApp.Documents
Dim Plot As Object
Set Plot = Docs.Add(grfPlotDoc)
Plot.PageSetup.pageSize=grfA4 'grfA4Small for small A4
Plot.NewWindow.PageUnits = grfCentimeters
Dim Shapes As Object
Set Shapes = Plot.Shapes

' Control of the x-limit and y-limit of the axes, change it if needed,
' here all are the same for the figures in one page
xxMax=0.8
xxMin=0
yyMax=60
yyMin=0
' %%%%%%% DO NOT USE RELATIVE PATH HERE %%%%%%
DataPath="D:\Program Files\Golden Software\Grapher5\Scripter\"

FigXlen=8 ' cm
FigYlen=4.5 ' cm

' Open files and draw lines
For Nfig=1 To 10 Step 1

'#############################################
' Change it if needed, Assumption:
' Data file is "data01.txt, data02.txt ... data10.txt"
' Add text here is "101, 102, .... 110"
'#############################################

fn = Str$(Nfig+100)
Filename = DataPath + "data" + Right(fn,2) +".txt"
AddText=fn

Set Graph1 = Shapes.AddLinePlotGraph(FileName,2,1)
Nhalf=Int((Nfig+1)/2)
With Graph1
.top=29.7-1-(Nhalf-1)*FigYlen*1.15
.height=FigYlen
.Left=FigXlen*( (Nfig+1) Mod 2 )* 1.2
.width=FigYlen
End With

Set LinePlot1 =Graph1.Plots.Item(1)
Set LinePlot1.line.foreColor=grfColorRed
Set Axesplot1 =Graph1.Axes
Set xAX1=Axesplot1(1)
Set yAx1=Axesplot1(2)

'Set the limit of x-axis
Set xAx1.AutoMax = False
Set xAx1.AutoMin = False
Set xAx1.Max=xxMax
Set xAX1.Min=xxMin

' set the mod of y-axis
Set yAX1.Descending=True

Set Graph2 = Shapes.AddLinePlotGraph(FileName,3,1)
With Graph2
.top = 29.7-1-(Nhalf-1)*FigYlen*1.15
.height=FigYlen
.Left =1+ FigXlen*( (Nfig+1) Mod 2 )* 1.2
.width=FigYlen
End With

Set LinePlot2 =Graph2.Plots.Item(1)
Set LinePlot2.line.foreColor=grfColorBlue
Set Axesplot2 =Graph2.Axes

Set xAX2=Axesplot2(1)
Set yAX2=Axesplot2(2)
Set xAX2.AutoMax = False
Set xAx2.Max=xxMax
Set xAX2.Min=xxMin
Set Axesplot2(2).Descending=True
Set xAx1.length=Figxlen 'xxleng
Set xAx2.length=Figxlen 'xxleng
Set yAx1.length=Figylen 'yyleng
Set yAx2.length=FigYlen

yypos=yAx2.yPos
yxpos=yAx2.xPos
yyleng=yAx2.length
xxpos=xAx2.xPos
xypos=xAx2.yPos
xxleng=xAx2.length
Set xAx1.length=xxleng
Set xAx2.length=xxleng
Set yAx1.length=yyleng
Set yAx1.yPos=yypos
Set yAx1.xPos=yxpos
Set xAx1.xPos=xxpos
Set xAx1.yPos=xypos

' Add x-label and y-label
If Nfig<=8 Then Set xAX1.TickLabels.MajorOn = False Set xAX2.TickLabels.MajorOn=False Else Set xAX1.title.text="Attenuation Coefficient ( /m )" End If If Nfig Mod 2 <>0 Then
Set yAx1.title.text="Depth ( m )"
Else
Set yAx1.TickLabels.MajorOn = False
Set yAx2.TickLabels.MajorOn = False
End If

Set yAxAdd=Graph1.AddAxis(grfYAxis)
Set yAxAdd.xPos= yxpos + xxleng
Set yAxAdd.yPos= yypos
Set yAxAdd.length=yyleng
Set yAxAdd.Descending=True
Set yAxAdd.AutoMax=False
Set yAxAdd.Max=yyMax
Set yAxAdd.TickLabels.MajorOn = False

Set xAxAdd=Graph1.AddAxis(grfXAxis)
Set xAxAdd.xPos=xxpos
Set xAxAdd.yPos= yypos + yyleng
Set xAxAdd.length=xxleng
Set xAxAdd.Descending=True
Set xAxAdd.AutoMax=False
Set xAxAdd.Max=xxMax
Set xAxAdd.Min=xxMin
Set xAxAdd.TickLabels.MajorOn = False

'add text
Set text_data = Shapes.AddText(yxpos+xxleng*0.8,xypos+yyleng*0.2,AddText)
Next Nfig
''quit the application
'GrapherApp.Quit()
End Sub

No comments:

ShowCalendar