Line charts again

Sorry about this, but i managed accidentally delete earlier line charts topic. I tried to delete second post in topic since my 1st post was twice???, but it deleted entire topic.

So i need to show line chart using AppleScript. I need to show simple line chart in display. I’m not trying to use excel etc, just show data from list.

Hi cirno,

I think you can use Smile to create charts.

http://www.satimage.fr/software/en/smile_doc.html

I haven’t done it before, but if you need help, then I can try. In fact, I think I’ll try it now.

Wish me luck,

Here’s what I got so far:


set d to {{100, 100}, {200, 100}, {300, 300}}
tell application "Smile"
	launch
	activate
	set w to make new graphic window with properties {name:"Line Graph", pagewidth:500, pageheight:500}
	set width of w to 600
	set height of w to 600
	BeginFigure(w)
	MoveTo({0, 0})
	repeat with p in d
		LineTo(p)
		DrawPath(2)
	end repeat
	EndFigure()
end tell

Working on getting the size of the window right.

New toy!

set list_a to {1.1, 1.5, 2.2, 2.5, 5.5}
set list_b to {1.4, 2.4, 3.2, 2.2, 2.3}
set list_c to {1.5, 2.2, 4.3, 4.2, 5.2}
set list_4 to {1.5, 4.2, 3.9, 4.6, 6.6}

I found this sample script in Smile. I try to visualise these 4 lists in one graph, but since i’m AppleScript newbie i dont know what to do. Does Smile need to be running when running these scripts.

–This example displays four curves in a plot view. The legend is displayed on the curves (legend kind 3).

–define the data
set x to createarray 101 range {0, 10}

set v to “four curves automatically computed”

–loop to create four curves
repeat with i from 1 to 4
–compute a formula with the data
set y to evalformula “x/n+cos(x)” with {x:x, n:i}
–create a new curve
set c to QuickCurve(x, y, v)
set name of c to “\Omega_” & i
set v to container of c
end repeat
set properties of v to {name:“Several curves”, legend abscissa:8, legend kind:3, xlabel:“x”, ylabel:“y”}
draw window of v

Hi cirno,

I couldn’t find your example, but it doesn’t look like a simple line graph. It will take me a while to learn this from the documentation. So far, I got this:


set d to {{100, 100}, {200, 100}, {300, 300}}
tell application "Smile"
	launch
	activate
	set w to make new graphic window with properties {name:"Line Graph", frame:{-100, -100, 600, 600}}
	set width of w to 650
	set height of w to 650
	BeginFigure(w)
	MoveTo({-50, 0})
	LineTo({500, 0})
	MoveTo({0, -50})
	LineTo({0, 500})
	MoveTo({0, 0})
	repeat with p in d
		LineTo(p)
		DrawPath(2)
	end repeat
	EndFigure()
end tell

There must be an easier way to make the chart. :slight_smile: Still need to add ticks, labels, key, etc.

You might want to post to the AppleScript users mailing list if nobody answers your post here.

gl,

I found that example from Smile/SmileLab sample scripts/curves/02-several curves-folder or from startup splash screen>SmileLab samples>Curves…>Several curves.

I also noticed nice way to import data in splash screen. This also creates script and graph automatically, but my skills still arent good enough to make it work and i dont understand all those preferences in import feature.

Thanks

Since none of these work for me (I do have Smile), I’m assuming they’re only possible with the full-blown (i.e. not-free) version of Smile; or am I missing something?

Hi,

I downloaded SmileLab and the example is there. All you need to make your line graph is two arrays for x and y. Like this:


set y to {0, 100, 100, 200, 300}
set x to {0, 100, 200, 300, 400}

QuickCurve(x, y, 0)

cirno,

You need to read the documents.

I wonder what they mean by you can print a pdf in the demo version.

gl,

Too bad SmileLab prices are so high - I’d really love to have it, but no longer have any affiliation with a University. :frowning:

Yeah, It’s a nice program. I guess to print and save graphs you need to make your own charts. It doesn’t look to hard though. It just takes time to make a template script. Then just stick in the data.

I was just playing with Intaglio which is much cheaper (US$ 89) and fully scriptable with nice effects as well. Seems like learning to script it would be fairly straight-forward too - it’s fully recordable. As you mention - you’d make some nice handler templates and pass in the data. :confused:

Hi Adam,

I just figured out how to make the chart in Smile. It’s kind of easy if you know how. Making it modular now.

Edited: darn, you can’t save it though! Can’t use plot views. :frowning: Shouldn’t have installed this version. I don’t know what you can use and what you can’t for printing.

Later,

Been there, done that, Kel :rolleyes:, their stuff is pretty well limited until you buy - reasonable, I suppose, when the price is so high.:confused:

Hi cirno and Adam,

Here’s an example of my graph if you want to look at it


set w to make new graphic window with properties ¬
	{name:"Example Graph", frame:{-100, -100, 600, 600}}
tell w
	set {width, height} to {650, 650}
end tell

BeginFigure(w)

SaveState()
-- temporary markers; remove later
SetCrossSize(9)
CrossPath({-50, 0})
CrossPath({0, -50})
-- draw border
RectPath({-80, -80, 560, 560})
-- draw x-axis
-- change later by starting ad end point instead of origin
MoveTo({0, 0})
set end_pt to {450, 0}
LineTo(end_pt)
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 40
	if m = 0 then
		set d to 9
		set n to i div 4
		TextMoveTo({i, 0})
		DrawString("[b]" & n)
		TextMoveTo({0, i})
		DrawString("[l]" & n)
	else
		set d to 4.5
	end if
	MoveTo({i, 0})
	DLine({0, d})
	MoveTo({0, i})
	DLine({d, 0})
end repeat
-- graph some data
MoveTo({0, 0})
repeat with x from 10 to 400 by 10
	set y to random number from 10 to 400
	LineTo({x, y})
end repeat
-- show the drawrings
DrawPath(2)

EndFigure()

It stll needs fixing up and modularity when I get the time. Had to do some review on transformations.

BTW, I think there might be a way to print the demos using the pdf print options or something. Anyway, it’s more fun and effective learning wise starting from scratch. Think I gott most of the basics down.

gl,

How difficult it is to modify this to support more than one line in one chart?

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,