TAChart: Add 'script' demo

git-svn-id: trunk@41813 -
This commit is contained in:
ask 2013-06-23 11:18:43 +00:00
parent e6ce69ca47
commit c1ad3c17bd
5 changed files with 303 additions and 0 deletions

4
.gitattributes vendored
View File

@ -3244,6 +3244,10 @@ components/tachart/demo/save/main.lfm svneol=native#text/plain
components/tachart/demo/save/main.pas svneol=native#text/plain
components/tachart/demo/save/savedemo.lpi svneol=native#text/plain
components/tachart/demo/save/savedemo.lpr svneol=native#text/plain
components/tachart/demo/script/Main.lfm svneol=native#text/plain
components/tachart/demo/script/Main.pas svneol=native#text/pascal
components/tachart/demo/script/scriptdemo.lpi svneol=native#text/plain
components/tachart/demo/script/scriptdemo.lpr svneol=native#text/pascal
components/tachart/demo/tools/main.lfm svneol=native#text/plain
components/tachart/demo/tools/main.pas svneol=native#text/pascal
components/tachart/demo/tools/toolsdemo.lpi svneol=native#text/plain

View File

@ -0,0 +1,72 @@
object Form1: TForm1
Left = 354
Height = 388
Top = 145
Width = 550
Caption = 'Form1'
ClientHeight = 388
ClientWidth = 550
LCLVersion = '1.1'
object Chart1: TChart
Left = 0
Height = 338
Top = 0
Width = 550
AxisList = <
item
Minors = <>
Title.LabelFont.Orientation = 900
end
item
Alignment = calBottom
Minors = <>
end>
Foot.Brush.Color = clBtnFace
Foot.Font.Color = clBlue
Title.Brush.Color = clBtnFace
Title.Font.Color = clBlue
Title.Text.Strings = (
'TAChart'
)
Align = alClient
ParentColor = False
object Chart1FuncSeries1: TFuncSeries
Active = False
Extent.UseXMax = True
Extent.UseXMin = True
Extent.XMax = 1
Extent.XMin = -1
OnCalculate = Chart1FuncSeries1Calculate
ExtentAutoY = True
Pen.Color = clGreen
Pen.Width = 2
end
end
object Panel1: TPanel
Left = 0
Height = 50
Top = 338
Width = 550
Align = alBottom
ClientHeight = 50
ClientWidth = 550
TabOrder = 1
object edFormula: TEdit
Left = 18
Height = 27
Top = 12
Width = 414
TabOrder = 0
Text = 'sin(x*5) + x'
end
object btnRefresh: TButton
Left = 448
Height = 27
Top = 12
Width = 75
Caption = 'Refresh'
OnClick = btnRefreshClick
TabOrder = 1
end
end
end

View File

@ -0,0 +1,113 @@
unit Main;
{$mode objfpc}{$H+}
interface
uses
Classes, ExtCtrls, StdCtrls, SysUtils, FileUtil, Forms, Controls, Graphics,
Dialogs, TAFuncSeries, TAGraph;
type
TForm1 = class(TForm)
btnRefresh: TButton;
Chart1: TChart;
Chart1FuncSeries1: TFuncSeries;
edFormula: TEdit;
Panel1: TPanel;
procedure btnRefreshClick(Sender: TObject);
procedure Chart1FuncSeries1Calculate(const AX: Double; out AY: Double);
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
uses
uPSCompiler, uPSRuntime, uPSUtils;
type
TUserFunction = function (AX: Double): Double of object;
var
exec: TPSExec;
userFunc: TUserFunction;
function ScriptOnExportCheck(
ASender: TPSPascalCompiler; AProc: TPSInternalProcedure;
const AProcDecl: string): Boolean;
begin
if AProc.Name = 'FN' then
if not ExportCheck(ASender, AProc, [btDouble, btDouble], [pmIn]) then begin
ASender.MakeError('', ecTypeMismatch, '');
exit(false);
end;
Result := true;
end;
function ScriptOnUses(ASender: TPSPascalCompiler; const AName: string): Boolean;
begin
Result := AName = 'SYSTEM';
end;
procedure CompileScript(const AScript: string);
var
data: string;
begin
FreeAndNil(exec);
with TPSPascalCompiler.Create do
try
OnUses := @ScriptOnUses;
OnExportCheck := @ScriptOnExportCheck;
AllowNoBegin := true;
AllowNoEnd := true;
if not Compile(AScript) then exit;
GetOutput(data);
finally
Free;
end;
exec := TPSExec.Create;
if not exec.LoadData(data) then begin
FreeAndNil(exec);
exit;
end;
userFunc := TUserFunction(exec.GetProcAsMethodN('FN'));
if userFunc = nil then
FreeAndNil(exec);
end;
{ TForm1 }
procedure TForm1.btnRefreshClick(Sender: TObject);
begin
CompileScript(
'function fn(x: Double): Double; begin Result := ' + edFormula.Text +
'; end;');
with edFormula do
if exec = nil then
Color := clRed
else
Color := clDefault;
Chart1FuncSeries1.Active := true;
Chart1.Invalidate;
end;
procedure TForm1.Chart1FuncSeries1Calculate(const AX: Double; out AY: Double);
begin
if exec = nil then
AY := AX
else
AY := userFunc(AX);
end;
finalization
FreeAndNil(exec);
end.

View File

@ -0,0 +1,93 @@
<?xml version="1.0"?>
<CONFIG>
<ProjectOptions>
<Version Value="9"/>
<General>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<Title Value="scriptdemo"/>
<ResourceType Value="res"/>
<UseXPManifest Value="True"/>
<Icon Value="0"/>
</General>
<i18n>
<EnableI18N LFM="False"/>
</i18n>
<VersionInfo>
<StringTable ProductVersion=""/>
</VersionInfo>
<BuildModes Count="1">
<Item1 Name="Default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
</PublishOptions>
<RunParams>
<local>
<FormatVersion Value="1"/>
</local>
</RunParams>
<RequiredPackages Count="3">
<Item1>
<PackageName Value="pascalscript"/>
</Item1>
<Item2>
<PackageName Value="TAChartLazarusPkg"/>
</Item2>
<Item3>
<PackageName Value="LCL"/>
</Item3>
</RequiredPackages>
<Units Count="2">
<Unit0>
<Filename Value="scriptdemo.lpr"/>
<IsPartOfProject Value="True"/>
<UnitName Value="scriptdemo"/>
</Unit0>
<Unit1>
<Filename Value="Main.pas"/>
<IsPartOfProject Value="True"/>
<ComponentName Value="Form1"/>
<HasResources Value="True"/>
<ResourceBaseClass Value="Form"/>
<UnitName Value="Main"/>
</Unit1>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<Target>
<Filename Value="scriptdemo"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Linking>
<Options>
<Win32>
<GraphicApplication Value="True"/>
</Win32>
</Options>
</Linking>
<Other>
<CompilerMessages>
<MsgFileName Value=""/>
</CompilerMessages>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>
<Debugging>
<Exceptions Count="3">
<Item1>
<Name Value="EAbort"/>
</Item1>
<Item2>
<Name Value="ECodetoolError"/>
</Item2>
<Item3>
<Name Value="EFOpenError"/>
</Item3>
</Exceptions>
</Debugging>
</CONFIG>

View File

@ -0,0 +1,21 @@
program scriptdemo;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Interfaces, // this includes the LCL widgetset
Forms, Main, pascalscript, tachartlazaruspkg
{ you can add units after this };
{$R *.res}
begin
RequireDerivedFormResource := True;
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.