PureBasic - форум

Информация о пользователе

Привет, Гость! Войдите или зарегистрируйтесь.


Вы здесь » PureBasic - форум » Вопросы по PureBasic » помогите построить график


помогите построить график

Сообщений 1 страница 7 из 7

1

Добрый вечер.
Совсем нигде нет информации про построение графиков в PB...
Помогите или ссылочками, или может кто-нибудь покажет на примере как строить самый элементарный график....
Зараннее спасибо.

0

2

Готовый модуль рисования графиков

0

3

Как-то делал нечто подобное:
http://ompldr.org/vaDV6cw/pb_stats.rar
Запустите скрипт и в директории должен будет появиться простенький график.

0

4

График любой функции

Код:
FontID1 = LoadFont(0, "Verdana", 10)
FontID2 = LoadFont(1, "Verdana", 7)
CreateImage(0,640,480)
Structure sp_
   x.l
   y.l
EndStructure
Global cb.l=RGB(0,0,0)
Global ca.l=RGB(0,192,192)
Global cg.l=RGB(0,0,192)
Global cl.l=RGB(255,255,63)
Global wi=640, he=480
pp.s=Chr(10)+Chr(13)
SetThreadPriority_(GetCurrentThread_(),255)
Global x1.d, x2.d, y1.d, y2.d, x3.d, y3.d
;#E=2.71828182846

Enumeration ; window 1
   #Text_0
   #Input_1
   #Frame3D_0
   #Text_1
   #Input_2
   #Text_3
   #Input_4
   #Text_9
   #Input_10
   #Text_5
   #Input_6
   #Text_6
   #Input_7
   #Text_11
   #Input_12
   #Frame3D_1
   #Button_0
   #Button_1
   #Button_2
   #Button_3
   #Button_4
   #Button_5
   #Button_6
   #img
   #check1
   #check2
EndEnumeration
Procedure.d integer(x.d)
   If x>=0: ProcedureReturn Int(x) : Else: ProcedureReturn Int(x)-1: EndIf
EndProcedure
Procedure.d flo(x.d)
   ProcedureReturn x-integer(x)
EndProcedure
Procedure.d Compute(line.s)
   Protected w.b=0, u.s=" ", q.l
   Restore again
   For q=1 To 7
      Read.s u.s
      If u="-" Or u="+"
         If FindString(line, u, 2)>0
            If Mid(line,FindString(line, u, 2)-1,1)<>"e"
               w=1
            EndIf
         EndIf
      Else
         If FindString(line, u, 1)>0: w=1: EndIf
      EndIf
   Next
   If w=0 ; it's just a number
      ProcedureReturn ValD(line)
   Else ; we'll compute it
      w=0 ; now it will be number of brackets
      For q=Len(line) To 1 Step -1 ; FROM RIGHT TO LEFT! For correct recursive calculation.
         u=Mid(line,q,1) ; it is a current symbol
         If u=")": w+1: EndIf
         If u="(": w-1: EndIf
         If w=0 ; if we are between two blocks
            If u="+" And Mid(line,q-1,1)<>"e"
               ProcedureReturn Compute(Left(line,q-1))+compute(Right(line,Len(line)-q))
            EndIf
            If u="-" And Mid(line,q-1,1)<>"e"
               ProcedureReturn Compute(Left(line,q-1))-compute(Right(line,Len(line)-q))
            EndIf
         EndIf
      Next
      For q=Len(line) To 1 Step -1
         u=Mid(line,q,1) ; it is a current symbol
         If u=")": w+1: EndIf
         If u="(": w-1: EndIf
         If w=0 ; if we are between two blocks
            If u="*"
               ProcedureReturn Compute(Left(line,q-1))*compute(Right(line,Len(line)-q))
            EndIf
            If u="/"
               ProcedureReturn Compute(Left(line,q-1))/compute(Right(line,Len(line)-q))
            EndIf
         EndIf
      Next
      For q=1 To Len(line)
         u=Mid(line,q,1) ; it is a current symbol
         If u="(": w+1: EndIf
         If u=")": w-1: EndIf
         If w=0 ; if we are between two blocks
            If u="^"
               ProcedureReturn Pow(Compute(Left(line,q-1)),compute(Right(line,Len(line)-q)))
            EndIf
         EndIf
      Next
      ; nothing found
      If Left(line,1)="(" And Right(line,1)=")": ProcedureReturn compute(Mid(line,2,Len(line)-2)): EndIf
      If Left(line,2)="-(" And Right(line,1)=")": ProcedureReturn compute(Mid(line,3,Len(line)-3)): EndIf
     
      If Left(line,4)="sin(" And Right(line,1)=")": ProcedureReturn Sin(compute(Mid(line,5,Len(line)-5))): EndIf
      If Left(line,4)="cos(" And Right(line,1)=")": ProcedureReturn Cos(compute(Mid(line,5,Len(line)-5))): EndIf
      If Left(line,3)="tg(" And Right(line,1)=")": ProcedureReturn Tan(compute(Mid(line,4,Len(line)-4))): EndIf
      If Left(line,4)="ctg(" And Right(line,1)=")": ProcedureReturn 1/Tan(compute(Mid(line,5,Len(line)-5))): EndIf

      If Left(line,7)="arcsin(" And Right(line,1)=")": ProcedureReturn ASin(compute(Mid(line,8,Len(line)-8))): EndIf
      If Left(line,7)="arccos(" And Right(line,1)=")": ProcedureReturn ACos(compute(Mid(line,8,Len(line)-8))): EndIf
      If Left(line,6)="arctg(" And Right(line,1)=")": ProcedureReturn ATan(compute(Mid(line,7,Len(line)-7))): EndIf
      If Left(line,7)="arcctg(" And Right(line,1)=")": ProcedureReturn #PI/2-ATan(compute(Mid(line,8,Len(line)-8))): EndIf

      If Left(line,4)="sqr(" And Right(line,1)=")": ProcedureReturn Sqr(compute(Mid(line,5,Len(line)-5))): EndIf
      If Left(line,4)="abs(" And Right(line,1)=")": ProcedureReturn Abs(compute(Mid(line,5,Len(line)-5))): EndIf
      If Left(line,3)="ln(" And Right(line,1)=")": ProcedureReturn Log(compute(Mid(line,4,Len(line)-4))): EndIf
      If Left(line,3)="lg(" And Right(line,1)=")": ProcedureReturn Log10(compute(Mid(line,4,Len(line)-4))): EndIf
      If Left(line,4)="int(" And Right(line,1)=")": ProcedureReturn integer(compute(Mid(line,5,Len(line)-5))): EndIf
      If Left(line,6)="float(" And Right(line,1)=")": ProcedureReturn flo(compute(Mid(line,7,Len(line)-7))): EndIf
      ;MessageRequester("Ошибка!","Проверьте корректность выражения:"+Chr(10)+Chr(13)+line,#MB_ICONEXCLAMATION)
   EndIf
EndProcedure
DataSection ; symbols
   again:
   Data.s "+", "-", "*", "/", "(", ")", "^"
EndDataSection
Procedure paintwin()
   SetGadgetColor(#button_0,#PB_Gadget_BackColor,cb)
   SetGadgetColor(#button_1,#PB_Gadget_BackColor,ca)
   SetGadgetColor(#button_2,#PB_Gadget_BackColor,cg)
   SetGadgetColor(#button_3,#PB_Gadget_BackColor,cl)
EndProcedure
Procedure.d scrx(x.d)
   ProcedureReturn (x-x1)*(wi-20)/(x2-x1)+10
EndProcedure
Procedure.d scry(y.d)
   ProcedureReturn (y-y2)*(he-20)/(y1-y2)+10
EndProcedure
Procedure.s tostr(s.d)
   Protected l.s
   l=StrD(s)
   While Right(l,1)="0"
      l=Left(l,Len(l)-1)
   Wend
   If l="":l="0":EndIf
   If Right(l,1)=".": l=Left(l,Len(l)-1): EndIf
   ;Debug l
   ProcedureReturn l
EndProcedure
Procedure trace()
   Protected q.d, f.s, x.d, y.d, p1.sp_, p2.sp_, w.b
   x1=ValD(ReplaceString(GetGadgetText(#input_2),",","."))
   x2=ValD(ReplaceString(GetGadgetText(#input_4),",","."))
   y1=ValD(ReplaceString(GetGadgetText(#input_6),",","."))
   y2=ValD(ReplaceString(GetGadgetText(#input_7),",","."))
   x3=ValD(ReplaceString(GetGadgetText(#input_10),",","."))
   y3=ValD(ReplaceString(GetGadgetText(#input_12),",","."))
   f=GetGadgetText(#input_1)
   f=ReplaceString(f,"eu","2.71828182846")
   f=ReplaceString(f,"pi","3.141592653589793")
   
   If GetGadgetState(#check2)=0: w=1: Else: w=3: EndIf
   
   StartDrawing(ImageOutput(0))
      If GetGadgetState(#check1)=0
         Box(0,0,wi,he,cb)
         FrontColor(cg)
         If x3>0
            q=0
            Repeat
               q+x3
               Line(scrx(q),0,0,he)
            Until q>x2
            q=0
            Repeat
               q-x3
               Line(scrx(q),0,0,he)
            Until q<x1
         EndIf
         If y3>0
            q=0
            Repeat
               q+y3
               Line(0,scry(q),wi,0)
            Until q>y2
            q=0
            Repeat
               q-y3
               Line(0,scry(q),wi,0)
            Until q<y1
         EndIf
         FrontColor(ca)
         Line(scrx(0),0,0,he)
         Line(0,scry(0),wi,0)
         DrawingMode(#PB_2DDrawing_Transparent)
         If x3>0
            q=0
            Repeat
               DrawText(scrx(q)+1,scry(0),tostr(q))
               q+x3
            Until q>x2
            q=0
            Repeat
               q-x3
               DrawText(scrx(q)+1,scry(0),tostr(q))
            Until q<x1
         EndIf
         If y3>0
            q=0
            Repeat
               DrawText(scrx(0)+1,scry(q),tostr(q))
               q+y3
            Until q>y2
            q=0
            Repeat
               q-y3
               DrawText(scrx(0)+1,scry(q),tostr(q))
            Until q<y1
         EndIf
      EndIf
      FrontColor(cl)
      q=x1
      While q<=x2 And f<>""
         p2\x=scrx(q)
         p2\y=scry(compute(ReplaceString(f,"x","("+StrD(q)+")")))
         If (p1\x>0 And p1\x<wi And p1\y>0 And p1\y<he) Or (p2\x>0 And p2\x<wi And p2\y>0 And p2\y<he)
            Select w
            Case 1
               If q=x1: Plot(p2\x,p2\y): Else: LineXY(p1\x,p1\y,p2\x,p2\y): EndIf
            Case 3
               If q=x1: Plot(p2\x,p2\y): Else: LineXY(p1\x,p1\y,p2\x,p2\y): EndIf
               If q=x1: Plot(p2\x+1,p2\y): Else: LineXY(p1\x+1,p1\y,p2\x+1,p2\y): EndIf
               If q=x1: Plot(p2\x=1,p2\y): Else: LineXY(p1\x=1,p1\y,p2\x=1,p2\y): EndIf
               If q=x1: Plot(p2\x,p2\y+1): Else: LineXY(p1\x,p1\y+1,p2\x,p2\y+1): EndIf
               If q=x1: Plot(p2\x,p2\y-1): Else: LineXY(p1\x,p1\y-1,p2\x,p2\y-1): EndIf
            EndSelect                 
         EndIf
         p1\x=p2\x
         p1\y=p2\y
         q+1/wi*(x2-x1)
      Wend
      ;Debug 3/we*(x2-x1)
   StopDrawing()
   SetGadgetState(#img,ImageID(0))   
EndProcedure


OpenWindow(0,100,100,wi,he,"График",#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget)
CreateGadgetList(WindowID(0))
ImageGadget(#img,0,0,wi,he,ImageID(0))
OpenWindow(1,354,359,635,241,"Построение графиков",#PB_Window_MinimizeGadget)
;{ ; its gadgets
   CreateGadgetList(WindowID(1))
   TextGadget(#Text_0, 10, 10, 130, 20, "&Выражение:  f(x) =")
   SetGadgetFont(#Text_0, FontID1)
   StringGadget(#Input_1, 150, 7, 365, 24, "")
   SetGadgetFont(#Input_1, FontID1)
   Frame3DGadget(#Frame3D_0, 10, 40, 505, 100, "Область графика")
   SetGadgetFont(#Frame3D_0, FontID1)
   TextGadget(#Text_1, 25, 68, 40, 20, "X: &от")
   SetGadgetFont(#Text_1, FontID1)
   StringGadget(#Input_2, 70, 65, 110, 24, "-12")
   SetGadgetFont(#Input_2, FontID1)
   TextGadget(#Text_3, 188, 68, 20, 20, "&до")
   SetGadgetFont(#Text_3, FontID1)
   StringGadget(#Input_4, 212, 65, 110, 24, "12")
   SetGadgetFont(#Input_4, FontID1)
   TextGadget(#Text_9, 330, 68, 75, 20, "&шаг сетки")
   SetGadgetFont(#Text_9, FontID1)
   StringGadget(#Input_10, 407, 65, 95, 24, "1")
   SetGadgetFont(#Input_10, FontID1)
   TextGadget(#Text_5, 25, 103, 40, 20, "Y: &от")
   SetGadgetFont(#Text_5, FontID1)
   StringGadget(#Input_6, 70, 100, 110, 24, "-9")
   SetGadgetFont(#Input_6, FontID1)
   TextGadget(#Text_6, 188, 103, 20, 20, "&до")
   SetGadgetFont(#Text_6, FontID1)
   StringGadget(#Input_7, 212, 100, 110, 24, "9")
   SetGadgetFont(#Input_7, FontID1)
   TextGadget(#Text_11, 330, 103, 70, 20, "&шаг сетки")
   SetGadgetFont(#Text_11, FontID1)
   StringGadget(#Input_12, 407, 100, 95, 24, "1")
   SetGadgetFont(#Input_12, FontID1)
   Frame3DGadget(#Frame3D_1, 10, 150, 505, 75, "Вид")
   SetGadgetFont(#Frame3D_1, FontID1)
   ButtonGadget(#Button_0, 30, 175, 95, 30, "Цвет &фона")
   SetGadgetFont(#Button_0, FontID1)
   ButtonGadget(#Button_1, 145, 175, 95, 30, "Цвет о&сей")
   SetGadgetFont(#Button_1, FontID1)
   ButtonGadget(#Button_2, 260, 175, 100, 30, "Цвет се&тки")
   SetGadgetFont(#Button_2, FontID1)
   ButtonGadget(#Button_3, 380, 175, 115, 25, "Цвет &графика")
   SetGadgetFont(#Button_3, FontID1)
   CheckBoxGadget(#check2,390,200,100,20,"Толстая &линия")
   SetGadgetFont(#check2, FontID2)
   ButtonGadget(#Button_4, 530, 155, 95, 30, "Спр&авка")
   SetGadgetFont(#Button_4, FontID1)
   ButtonGadget(#Button_5, 530, 15, 95, 30, "&Построить")
   SetGadgetFont(#Button_5, FontID1)
   CheckBoxGadget(#check1,527,45,100,30,"Пов&ерх старого")
   SetGadgetFont(#check1, FontID2)
   ButtonGadget(#Button_6, 530, 77, 95, 30, "Вы&ход")
   SetGadgetFont(#Button_6, FontID1)
;}
paintwin()
;StickyWindow(1,1)
;ShowWindow_(WindowID(1), #SW_HIDE)
;SetWindowLong_(WindowID(1), #GWL_EXSTYLE, #WS_EX_TOOLWINDOW)
;ShowWindow_(WindowID(1), #SW_SHOW)


Repeat
   e=WindowEvent()
   If e=#PB_Event_SizeWindow
      SetGadgetState(#check1,0)
      wi=WindowWidth(0)
      he=WindowHeight(0)
      If he<10: he=25: ResizeWindow(0,#PB_Ignore,#PB_Ignore,#PB_Ignore,he): EndIf
      If WindowHeight(0)>24
         CreateImage(0,wi,he)
         trace()
      EndIf
   ElseIf e=#PB_Event_Gadget
      ew=EventWindow()
      eg=EventGadget()
      et=EventType()
     
      If et=#PB_EventType_LeftClick
         If eg=#button_6: End: EndIf
         If eg=#button_0
            c=ColorRequester(cb)
            If c>=0: cb=c: EndIf
            paintwin()
         EndIf
         If eg=#button_1
            c=ColorRequester(ca)
            If c>=0: ca=c: EndIf
            paintwin()
         EndIf
         If eg=#button_2
            c=ColorRequester(cg)
            If c>=0: cg=c: EndIf
            paintwin()
         EndIf
         If eg=#button_3
            c=ColorRequester(cl)
            If c>=0: cl=c: EndIf
            paintwin()
         EndIf
         If eg=#button_4
            MessageRequester("Синтаксис","Поддерживаются алгебраические выражения практически любой сложности."+pp+"Дробные числа можно писать через точку или запятую."+pp+"Ввод порядка через латинскую «e»: 6.02e23;  7,412e-64"+pp+"Отрицательные числа желательно писать в скобках."+pp+pp+"Арифметические действия:"+pp+"+ — сложение"+pp+Chr(150)+" — вычитание"+pp+"* — умножение"+pp+"/ — деление"+pp+"^ — возведение в степень"+pp+pp+"Математические функции:"+pp+"(аргумент обязательно в скобках)"+pp+"sin(x)"+pp+"cos(x)"+pp+"tg(x)"+pp+"ctg(x)"+pp+"arcsin(x)"+pp+"arccos(x)"+pp+"arctg(x)"+pp+"arcctg(x)"+pp+"sqr(x) — квадратный корень"+pp+"abs(x) — модуль"+pp+"ln(x)"+pp+"lg(x)"+pp+"int(x) — целая часть"+pp+"float(x) — дробная часть"+pp+pp+"Можно использовать число «пи» (pi) и число «e» (eu)")
         EndIf
         If eg=#button_5
            trace()
         EndIf
      EndIf
   EndIf
   ;If GetActiveWindow_()=WindowID(0) Or GetActiveWindow_()=WindowID(1): act.b=1: Else: act=0: EndIf
   ;If act=0
   ;   ShowWindow_(WindowID(1), #SW_HIDE)
   ;Else
   ;   ShowWindow_(WindowID(1), #SW_SHOW)
   ;EndIf
   If GetWindowState(0)=#PB_Window_Minimize
      ;HideWindow(1,1)
      SetWindowState(1,#PB_Window_Minimize)
   Else
      ;If GetWindowState(1)<>#PB_Window_Minimize
      ;   HideWindow(1,0)
      ;EndIf
   EndIf
Until e=#PB_Event_CloseWindow

0

5

спасибо

0

6

но это все сложно для 2 курса

0

7

Мой пример. Создает png-файл для показа на веб-странице.
Имеем
Structure SpeedPoint
    nTime.l ;1-1440 номер минуты в сутках
    nSpeed.l ; скорость м/сек*10
EndStructure
...
NewList SpeedPoints.SpeedPoint()
....

Код:
            ;создать график
            Ymax=1460
            Xmax=350
            hImage1 = CreateImage(#PB_Any , Xmax, Ymax)
            X0=50
            Y0=20
            If StartDrawing(ImageOutput(hImage1)) 
            	BackColor(RGB(255, 255, 255))
            
            	FillArea(10, 10, -1 , RGB(255, 255, 255))
            	DrawText(0, 0, "м/сек>>" , RGB(0,0,255) )
            	;горизонтальные линии часов
            	LineXY(0, Y0, Xmax-1, Y0, RGB(0, 0,255))
            	For i=1 To 24
                n=i*60-1
                LineXY(0, Y0+n, Xmax-1, Y0+n, RGB(0, 0, 255))
                DrawText(0, Y0+n-16, Str(i)+":00" , RGB(0,0,255) )
            	Next
            	
            	;вертикальные линии скорости
            	LineXY(X0+0, 0, X0+0, Ymax-1 , RGB(0, 0, 255)) 
            	DrawText(X0+1, 0, "0" , RGB(0,0,255) )	
            	LineXY(X0+50, 0, X0+50, Ymax-1 , RGB(125, 125, 0)) 
            	DrawText(X0+51, 0, "5" , RGB(0,0,255) )
            	LineXY(X0+100, 0, X0+100, Ymax-1 , RGB(125, 125, 0)) 
            	DrawText(X0+101, 0, "10" , RGB(0,0,255) )
            	LineXY(X0+150, 0, X0+150, Ymax-1 , RGB(255, 0, 0)) 
            	DrawText(X0+151, 0, "15" , RGB(0,0,255) )
            	LineXY(X0+200, 0, X0+200, Ymax-1 , RGB(125, 125, 0)) 
            	DrawText(X0+201, 0, "20" , RGB(0,0,255) )
            	LineXY(X0+250, 0, X0+250, Ymax-1 , RGB(125, 125, 0)) 
            	DrawText(X0+251, 0, "25" , RGB(0,0,255) )
            	LineXY(X0+300, 0, X0+300, Ymax-1 , RGB(125, 125, 0)) 
            	DrawText(X0+301, 0, "30" , RGB(0,0,255) )	
            	;
            	WindSpeed=0
            	WindTime=0
            	ResetList(SpeedPoints())
            	While NextElement(SpeedPoints())
                LineXY(X0+WindSpeed, Y0+WindTime, X0+SpeedPoints()\nSpeed, Y0+SpeedPoints()\nTime, RGB(0, 125, 0))
                WindSpeed=SpeedPoints()\nSpeed
                WindTime=SpeedPoints()\nTime
            	Wend
            
            	StopDrawing()
            	SaveImage(hImage1, GraphFile$ , #PB_ImagePlugin_PNG )
            	FreeImage(hImage1)
            EndIf

0


Вы здесь » PureBasic - форум » Вопросы по PureBasic » помогите построить график