unit ReadScriptFile;
{$mode objfpc}{$H+}
interface
uses
LazUtils, StrUtils, FileUtil, TAGraph, TASeries, TAStyles, TATools, ]"]>BlockedltiSeries, Classes,
SysUtils, Forms, Controls, Graphics, Dialogs, Grids, StdCtrls, ComCtrls, ValEdit, TACustomSeries, TASources,
Types, TATypes, TACustomSource, TADrawUtils, TAChartUtils, TAChartAxisUtils;
type
{ TForm1 }
TForm1 = class(TForm)
chtPlot: TChart;
cmdClose: TButton;
cmdUpdate: TButton;
cmbFiles: TComboBox;
Label1: TLabel;
grdScript: TStringGrid;
tabDisplay: TPageControl;
tabScript: TTabSheet;
tabChart: TTabSheet;
procedure chtPlotBeforeDrawBackWall(ASender: TChart; ACanvas: TCanvas;
const ARect: TRect; var ADoDefaultDrawing: Boolean);
procedure cmbFilesClick(Sender: TObject);
procedure cmdCloseClick(Sender: TObject);
procedure cmdUpdateClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure LoadFileBox(aFName: String);
procedure SetAxisAttributes();
procedure LoadScript();
procedure ProcessScript();
procedure PlotSeries();
function ColorStrToColor(WebColor: string): TColor;
function RGB(r, g, b: Byte): TColor;
private
FBackImage: TPicture;
public
end;
var
Form1: TForm1; sChartTYpe: string; pPointerSize: integer;
pMinX: double; pMaxX: double; pMinY: double; pMaxY: double; pStopsX: integer; pStopsY: integer;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
var
pParam: string;
pFile: string;
begin
LoadFileBox('D:\Lazarus');
If paramcount > 0 then
cmbFiles.Text := paramstr(1);
LoadScript;
ProcessScript;
If paramcount =2 then
if lowercase(paramstr(2)) = '/s' then
begin
beep;
close;
halt(0);
end
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FBackImage.Free;
end;
procedure TForm1.chtPlotBeforeDrawBackWall(ASender: TChart; ACanvas: TCanvas;
const ARect: TRect; var ADoDefaultDrawing: Boolean);
begin
ACanvas.StretchDraw(ARect, FBackImage.Graphic);
ADoDefaultDrawing := false;
end;
procedure TForm1.cmbFilesClick(Sender: TObject);
begin
LoadScript;
end;
procedure TForm1.cmdCloseClick(Sender: TObject);
var
pSelected: Integer;
begin
// Show a confirmation dialog
pSelected := MessageDlg('Are you sure?',mtConfirmation, mbOKCancel, 0);
// Show the button type selected
if pSelected = mrOK then Close;
end;
procedure TForm1.cmdUpdateClick(Sender: TObject);
begin
ProcessScript;
end;
procedure TForm1.LoadFileBox(aFName: String);
var
list: TStringList;
begin
list := FindAllFiles(aFName, '*.txt' , False {don't search in subdirectory});
cmbFiles.Items := list;
list.Free;
end;
procedure Tform1.LoadScript();
var
pFile: TextFile;
pPath: string;
pLine: string;
pSection: string;
pFunction: TStringArray;
pRow: integer;
begin
while grdScript.RowCount > 1 do
begin
grdScript.DeleteRow(1);
end;
//showmessage(pPath);
pPath:= cmbFiles.Text;
AssignFile(pFile, pPath);
try
// Open the file for reading
reset(pFile);
// Keep reading lines until the end of the file is reached
//lstscript.Items.clear;
while not eof(pFile) do
begin
readln(pFile, pLine);
if Trim(pLine) <> '' then
begin
if pos(':',pLine)=0 then
begin
pSection := pLine;
// pLine := pSection + #9 + '' + #9 + '';
// lstScript.Items.Add(pLine);
end;
if pos(':',pLine)>0 then
begin
pFunction := pLine.Split(':');
pRow := grdScript.RowCount;
grdScript.RowCount := pRow + 1;
grdScript.Cells[0, pRow] := trim(pSection);
grdScript.Cells[1, pRow] := trim(pFunction[0]);
grdScript.Cells[2, pRow] := trim(pFunction[1]);
if high(pFunction)>1 then
grdScript.Cells[2, pRow] := trim(pFunction[1]) + ':' + trim(pFunction[2]);
end;
end;
// lstScript.Items.Add(pLine);
end;
// Done so close the file
CloseFile(pFile);
except
on E: EInOutError do
ShowMessage('File not found.');
end;
end;
procedure Tform1.ProcessScript();
var
pRow: integer;
pRowCount: integer;
pCol: integer;
pSection: string;
pProperty: string;
pSetting: string;
pAxis: integer;
pFile: string;
pFormat: string;
begin
pRowCount := grdScript.RowCount - 1;
for pRow := 1 to pRowCount do
begin
pSection := grdScript.Cells[0, pRow];
pProperty := grdScript.Cells[1, pRow];
pSetting := grdScript.Cells[2, pRow];
//Chart...
If lowercase(pSection) = 'chart' then
begin
If LowerCase(pProperty) = 'type' then sChartType := lowercase(pSetting);
if LowerCase(pProperty) = 'save' then pFile := pSetting;
if LowerCase(pProperty) = 'format' then pFormat := lowercase(pSetting);
if LowerCase(pProperty) = 'pointer size' then pPointerSize := lowercase(pSetting).ToInteger;
if LowerCase(pProperty) = 'background' then
begin
FBackImage := TPicture.Create;
FBackImage.LoadFromFile(pSetting);
end;
end;
//Title...
If lowercase(pSection) = 'title' then
begin
if lowercase(pProperty) = 'text' then chtplot.Title.Text.Text := pSetting;
if lowercase(pProperty) = 'size' then chtplot.Title.Font.Size := pSetting.ToInteger;
if lowercase(pProperty) = 'bold' then
If lowercase(pSetting) = 'true' then
chtplot.Title.Font.Bold := true
else
chtplot.Title.Font.Bold := false;
if LowerCase(pProperty) = 'color' then
begin
pSetting := UpperCase(pSetting);
chtPlot.Title.Font.Color := ColorStrToColor(pSetting);
end;
end;
//Set Axis...
If Pos('axis',lowercase(pSection)) > 0 then
case lowercase(pSection) of
'xaxis': pAxis := 1;
'yaxis': pAxis := 0;
end
else
pAxis := -1;
If pAxis >= 0 then
begin
chtPlot.Axislist[pAxis].Title.Distance := chtplot.Title.Font.Size + 30;
if LowerCase(pProperty) = 'text' then chtPlot.Axislist[pAxis].Title.Caption := pSetting;
if LowerCase(pProperty) = 'size' then chtPlot.Axislist[pAxis].LabelSize := pSetting.ToInteger;
if LowerCase(pProperty) = 'color' then
begin
pSetting := UpperCase(pSetting);
chtPlot.AxisList[pAxis].Title.LabelFont.Color := ColorStrToColor(pSetting);
end;
if LowerCase(pProperty) = 'min' then
If pAxis = 0 then pMinY := pSetting.ToInteger else pMinX :=pSetting.ToInteger;
if lowercase(pProperty) = 'max' then
If pAxis = 0 then pMaxY := pSetting.ToInteger else pMaxX :=pSetting.ToInteger;
if LowerCase(pProperty) = 'stops' then
If pAxis = 0 then pStopsY := pSetting.ToInteger else pStopsX :=pSetting.ToInteger;
if LowerCase(pProperty) = 'margin' then chtPlot.Axislist[pAxis].Title.Distance := chtplot.Title.Font.Size + psetting.ToInteger ;
if LowerCase(pProperty) = 'bold' then
If lowercase(pSetting) = 'true' then
chtPlot.Axislist[pAxis].Title.LabelFont.Bold := true
else
chtPlot.Axislist[pAxis].Title.LabelFont.Bold := false;
end;
end;
//Plot data...
PlotSeries;
SetAxisAttributes;
//Save graphic file.
if pFile<>'' then
if (pFormat = 'jpeg') or (pFormat = 'jpg') then
chtPlot.SaveToFile(TJPEGImage, pFile);
if (pformat = 'bmp') or (pformat = 'bitmap') then
chtPlot.SaveToFile(TBitmap, pFile);
if (pformat = 'png') or (pformat = 'portable network graphic') then
chtPlot.SaveToFile(TPortableNetworkGraphic, pFile);
end;
procedure Tform1.PlotSeries();
var
pRow: integer;
pRowCount: integer;
pSeriesBP: TBubbleSeries;
pSeriesXY: TLineSeries;
pSeriesPtr: integer;
pSection: string;
pProperty: string;
pSetting: string;
pData: array of string;
pX: Double;
pY: Double;
pR: Double;
pPointer: string;
pShape: integer;
pColor: Tcolor;
// pSeriesMarks: TseriesMarksStyle;
begin
pRowCount := grdScript.RowCount - 1;
pSeriesPtr :=1;
chtPlot.ClearSeries;
for pRow := 1 to pRowCount do
begin
pSection := LowerCase(grdScript.Cells[0, pRow]);
pProperty := grdScript.Cells[1, pRow];
pSetting := grdScript.Cells[2, pRow];
//Bubble chart...
If pSection = 'bubble series' then
begin
pSeriesBP := TBubbleSeries.Create(chtPlot);
chtPlot.AddSeries(pSeriesBP);
pData := pSetting.Split(',');
pX := pData[0].ToDouble;
pY := pData[1].ToDouble;
pR := pData[2].ToDouble;
If lowercase(pData[3]) = 'random' then
pColor := (Random(256) * Random(256)) + Random(256)
else
pColor := ColorStrToColor(pData[3]);
pSeriesBP.AddXY(pX, pY, pR,pProperty, pColor);
pSeriesBP.BubbleBrush.Color:= pColor;
pSeriesBP.Transparency:=60;
If lowercase(pData[4]) = 'data_labels=true' then
begin
pSeriesBP.Marks.Format:= pProperty + ',' + pData[0] + ',' + pData[1] + ',' + pData[2];
pSeriesBP.Marks.Visible := true;
end;
pSeriesBP.Name := pProperty;
pSeriesBP.Title := pProperty;
pSeriesBP.ShowInLegend:=true;
end;
//Scatter series...
If pSection = 'scatter series' then
begin
pSeriesXY := TLineSeries.Create(chtPlot);
chtPlot.AddSeries(pSeriesXY);
pData := pSetting.Split(',');
pX := pData[0].ToDouble;
pY := pData[1].ToDouble;
pPointer := lowercase(pData[2]);
//Process colour...
If lowercase(pData[3]) = 'random' then
pColor := (Random(256) * Random(256)) + Random(256)
else
pColor := ColorStrToColor(pData[3]);
// Process point data...
pSeriesXY.AddXY(pX, pY, pProperty, pColor);
pSeriesXY.ShowLines:= false;
pSeriesXY.ShowPoints:=true;
pSeriesXY.LinePen.Color := pColor;
pSeriesXY.SeriesColor := pColor;
If pPointerSize=0 then
pPointerSize := 15;
pSeriesXY.Pointer.HorizSize:=pPointerSize;
pSeriesXY.Pointer.VertSize:=pPointerSize;
// Process point shape...
case pPointer of
'cross': pSeriesXY.Pointer.Style := psCross;
'circle': pSeriesXY.Pointer.Style:= psCircle;
'diagcross': pSeriesXY.Pointer.Style := psDiagCross;
'downtriangle': pSeriesXY.Pointer.Style := psDownTriangle;
'triange': pSeriesXY.Pointer.Style := psTriangle;
'star': pSeriesXY.Pointer.Style := psStar;
'diamond': pSeriesXY.Pointer.Style := psDiamond;
'lefttriangle': pSeriesXY.Pointer.Style := psLeftTriangle;
'righttriangle': pSeriesXY.Pointer.Style := psRightTriangle;
'vertbar': pSeriesXY.Pointer.Style := psVertBar;
'horbar': pSeriesXY.Pointer.Style := psHorBar;
'point': pSeriesXY.Pointer.Style := psPoint;
'hexagon': pSeriesXY.Pointer.Style := psHexagon;
'fullstar': pSeriesXY.Pointer.Style := psFullStar;
'lowbracket': pSeriesXY.Pointer.Style := psLowBracket;
'highbracket': pSeriesXY.Pointer.Style := psHighBracket;
'leftbracket': pSeriesXY.Pointer.Style := psLeftBracket;
'rightbracket': pSeriesXY.Pointer.Style := psRightBracket;
'none': pSeriesXY.Pointer.Style := psNone;
else
pSeriesXY.Pointer.Style:= psCircle;
end;
pSeriesXY.Transparency:=30;
If lowercase(pData[4]) = 'data_labels=true' then
begin
pSeriesXY.Marks.Format:= pProperty + ',' + pData[0] + ',' + pData[1];
pSeriesXY.Marks.Visible := true;
end;
pSeriesXY.Name := pProperty;
pSeriesXY.Title := pProperty;
pSeriesXY.ShowInLegend:=true;
end;
end;
end;
procedure Tform1.SetAxisAttributes();
begin
// 'xaxis': pAxis := 1;
// 'yaxis': pAxis := 0;
chtPlot.Axislist[0].Range.Min := pMinY;
chtPlot.AxisList[0].Range.Max := pMaxY;
chtPlot.Axislist[0].Marks.Range.Min := pMinY;
chtPlot.Axislist[0].Marks.Range.Max := pMaxY;
chtPlot.Axislist[0].Intervals.Count := pStopsY;
chtPlot.Axislist[1].Range.Min := pMinX;
chtPlot.AxisList[1].Range.Max := pMaxX;
chtPlot.Axislist[1].Marks.Range.Min := pMinX;
chtPlot.Axislist[1].Marks.Range.Max := pMaxX;
chtPlot.Axislist[1].Intervals.Count := pStopsX;
chtPlot.Extent.YMax := pMaxY;
chtPlot.Extent.YMin := pMinY;
chtPlot.Extent.XMax := pMaxX;
chtPlot.Extent.XMin := pMinX;
chtPlot.Extent.UseXMax := true;
chtPlot.Extent.UseXMin := true;
chtPlot.Extent.UseYMax := true;
chtPlot.Extent.UseYMin := true;
chtPlot.ExtentSizeLimit.YMax := pMaxY;
chtPlot.ExtentSizeLimit.YMin := pMinY;
chtPlot.ExtentSizeLimit.XMax := pMaxX;
chtPlot.ExtentSizeLimit.XMin := pMinX;
chtPlot.ExtentSizeLimit.UseXMax := true;
chtPlot.ExtentSizeLimit.UseXMin := true;
chtPlot.ExtentSizeLimit.UseYMax := true;
chtPlot.ExtentSizeLimit.UseYMin := true;
end;
function TForm1.RGB(r, g, b: Byte): TColor;
begin
Result := (Integer(r) or (Integer(g) shl 8) or (Integer(b) shl 16));
end;
function TForm1.ColorStrToColor(WebColor: string): TColor;
begin
Result :=
RGB(
StrToInt('$' + Copy(WebColor, 2, 2)),
StrToInt('$' + Copy(WebColor, 4, 2)),
StrToInt('$' + Copy(WebColor, 6, 2)));
end;
end.