Dynamic graph using canvas

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Post Reply
Message
Author
JackZ
Posts: 2267
Joined: Mon Feb 22, 2016 1:02 pm

Dynamic graph using canvas

#1 Post by JackZ »

Hello

Starting to investigate more seriously the use of canvas() functions (@Keith Baxter and @Sling are having a stroke right now :lol: ), I have a newbie question:

I want to create a curve graph that updates constantly.
Capture.PNG
Capture.PNG (43.25 KiB) Viewed 2643 times
For this, i want to use canvas draw, where I have to create a new segment of the curve every iteration, while retaining the previous display until the screen is filled, then scroll the graph.
What I understood is that canvas is drawn at once, and cannot really be updated, ie the previous content kept, while adding new content. So i figured that i would have to create a canvas every iteration, and draw my new pixels on top of each other.

Here's the test code I wrote for this, which is not working, I get a "cannot initiate canvas outside of initial logic.lua execution" error.

Code: Select all

function line(x1,y1,x2,y2)
  _move_to(x1,y1)
  _line_to(x2,y2)
end

x_oldvalue=10
y_oldvalue=390
x_newvalue=x_old_value
y_newvalue=0

function draw()
    x_newvalue=x_oldvalue+5
    y_newvalue=math.random(0,380)
    print(x_newvalue.."/"..y_newvalue)
    canvas_add(0,0,400,400,function()
         line(10,0,10,390)
         line(10,390,400,390)
         _fstroke("red",1)
           line(x_oldvalue,y_oldvalue,x_newvalue,y_newvalue)
        _stroke("black",1)
        end)
x_oldvalue=x_newvalue
y_oldvalue=y_newvalue
end

timer_start(0,1000,draw)
When i repetedly call the draw() funstion using a for...do loop, i get the wanted result, but using the same call in a timer gives the error message
Do I need to first create a table of values in memory (like a screen buffer), then draw the content of the table all at once every 500msec?

Thanks

Jacques
My YouTube Chanel on the A320 (Real SOPs by an Airline Pilot IRL):
https://www.youtube.com/playlist?list=P ... 0Q6SBASRqJ

User avatar
Keith Baxter
Posts: 4685
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Dynamic graph using canvas

#2 Post by Keith Baxter »

Hi Jacques,

WOW You moving into a powerful part of AM and never to return. :lol: :lol:

Your error. You cannot add a canvas within a function. Just like txt_add(). A canvas has to initiated outside the function and can be redrawn anywhere thereafter.

Yes canvas_draw() on each refresh of data.
For graph's that are timer based "Timed" like frequencies, use a separate timer to determine. Do not rely on the the sim callback fire.

In other words. Time stamp your data. Do not rely on sim data timing.

Only canvas_draw() when necessary.


You need to use the old safety net.

Code: Select all

if my_data ~= nil then

canvas_draw(my_canvas, function()
 	 _txt(" I love you Jacques"..":)", "font:arimo_bold.ttf; size:40px; color: #05fcfe; halign:center; valign:center;",20,20)
end)
end
Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

User avatar
Ralph
Posts: 7924
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: Dynamic graph using canvas

#3 Post by Ralph »

I see that Valentines day is getting closer!

User avatar
Keith Baxter
Posts: 4685
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Dynamic graph using canvas

#4 Post by Keith Baxter »

Ralph wrote: Wed Feb 10, 2021 7:25 pm I see that Valentines day is getting closer!
I thought you skim read posts. :D

Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

JackZ
Posts: 2267
Joined: Mon Feb 22, 2016 1:02 pm

Re: Dynamic graph using canvas

#5 Post by JackZ »

Sorry Keith, but I still love png images, I'm kinda fond of polygamy when talking about AM :lol:
My YouTube Chanel on the A320 (Real SOPs by an Airline Pilot IRL):
https://www.youtube.com/playlist?list=P ... 0Q6SBASRqJ

User avatar
Keith Baxter
Posts: 4685
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Dynamic graph using canvas

#6 Post by Keith Baxter »

JackZ wrote: Wed Feb 10, 2021 8:20 pm Sorry Keith, but I still love png images, I'm kinda fond of polygamy when talking about AM :lol:
I would love to see the all the .png files when simulating that heart rate .


Why is it that as of to-date you get 47 hits on your image when I battle to get 5 on any I post?
You got a program to do that?

Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

JackZ
Posts: 2267
Joined: Mon Feb 22, 2016 1:02 pm

Re: Dynamic graph using canvas

#7 Post by JackZ »

Of course, there was no way (if I disregard the graphic library I designed as ça a workaround at the time canvas function were not even in the works) to do such thing as a dynamic curve like this using images, but images are still the best solution for plenty of things IMHO.

As for the rest, people just love me, can’t help it 8-)
My YouTube Chanel on the A320 (Real SOPs by an Airline Pilot IRL):
https://www.youtube.com/playlist?list=P ... 0Q6SBASRqJ

User avatar
jph
Posts: 2856
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: Dynamic graph using canvas

#8 Post by jph »

I would love to see the all the .png files when simulating that heart rate .
Keith
Keith :D
If that's his cardiac monitor output then you had better get a Defibrillator as he is in VF (Ventricular Fibrillation) :lol:
Also, some Adrenaline and Lignocaine on standby.........
'RIP' otherwise ............... ;)
Joe. CISSP, MSc.

Post Reply