Hi cirno,
It’s not too difficult. I just had a problem with setting the pen colors. Also, changed the tick mark labels to reflect the actual numbers in the data to make it more easy. That was kind of easy though and makes it easier to add scaling later on. Found out how to adjust the window size to match the graphic view. Remove the x markers. I really should put the title in the window. Note that I just added the axis labels to see how it was done. You can use this later for different types of graphs. Also, note that this is kind of easy because it’s not that complicated. There are only a few commands. It just takes time reading the documentation and experimenting.
set a to createarray 41 range {0, 400}
set lines_list to {}
repeat 2 times
set xvalues to a as list of real
set yvalues to {}
repeat with x in xvalues
set end of yvalues to random number from 0 to 400
end repeat
set end of lines_list to {xvalues, yvalues}
end repeat
MakeGraph(lines_list)
on MakeGraph(the_data)
tell application "Smile"
launch
activate
set w to make new graphic window with properties ¬
{name:"Example Graph", frame:{-100, -100, 600, 600}}
set {width of w, height of w} to {(pagewidth of w) + 16, (pageheight of w) + 16 + 22}
BeginFigure(w)
-- draw border
RectPath({-80, -80, 560, 560})
-- draw x-axis
-- change later by starting at end point instead of origin
MoveTo({0, 0})
set end_pt to {450, 0}
LineTo(end_pt)
SaveState()
SetArrowSize({0, 4.5, 2.25})
ArrowPath(0) -- 0 radians
-- label x-axis
TextMoveTo(end_pt)
DrawString("[r]X")
-- draw y-axis, arrow, and label
MoveTo({0, 0})
set end_pt to {0, 450}
LineTo(end_pt)
ArrowPath(pi / 2) -- 90 degrees
RestoreState() -- crosssize & arrowsize
TextMoveTo(end_pt)
DrawString("[t]Y")
-- label origin
TextMoveTo({0, 0})
DrawString("[bl]" & "0")
-- label x-values
TextMoveTo({225, -50})
DrawString("[h]hello")
-- set transformation for rotation of y-values label
SaveState()
SetTransformation({0, 1, -1, 0, -50, 225})
DrawString("[h]goodbye")
RestoreState()
-- draw and label tick marks
repeat with i from 10 to 400 by 10
set m to i mod 50
if m = 0 then
set d to 9
TextMoveTo({i, 0})
DrawString("[b]" & i)
TextMoveTo({0, i})
DrawString("[l]" & i)
else
set d to 4.5
end if
MoveTo({i, 0})
DLine({0, d})
MoveTo({0, i})
DLine({d, 0})
end repeat
---- graph the data
DrawPath(2)
set pen_colors to {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}} -- add more colors for more lines
set color_index to 0
SaveState()
repeat with this_line in the_data
set {xvalues, yvalues} to this_line
-- move to the first pair
set c to count xvalues
set color_index to color_index + 1
setpencolor(item color_index of pen_colors)
MoveTo({item 1 of xvalues, item 1 of yvalues})
repeat with i from 2 to c
set p to {item i of xvalues, item i of yvalues}
LineTo(p)
end repeat
-- change color
DrawPath(2)
end repeat
RestoreState()
----
EndFigure()
end tell
end MakeGraph
Edited: just noticed that some of the comments at the end of the script don’t match. I was moving things around trying to figure out how to change pen colors.
gl,