Recent

Author Topic: [SOLVED] TChart without gui and instant pascal  (Read 1474 times)

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
[SOLVED] TChart without gui and instant pascal
« on: September 23, 2017, 01:25:35 am »
Hi,

I am trying to write a simple instant pascal template for TChart. I would like to use fpvectorial back-end and no gui. Is it possible? How?? Right now I have this incomplete code (I am receiving a linking error):
Quote
#!/usr/bin/env instantfpc
{$mode objfpc}{$H+}
uses
  Classes, SysUtils, math, csvdocument, TATools, TAGraph, TASeries;

const
  filename = 'x';

var
  Document : TCSVDocument;
  StringList : TStringList;
  Graphic : TChart;
  LineSerie : TLineSeries;

  data : array of Double;
  i : integer;
  error:  integer = 0;

begin
  Graphic := TChart.Create(nil);
  LineSerie := TLineSeries.Create(Graphic);

  StringList := TStringList.Create;
  Document := TCSVDocument.Create;
  Document.LoadFromFile(Filename);
  try
    StringList.BeginUpdate;
    Document.Delimiter := #9;
    for i := 0 to Document.RowCount -1 do
      if Document.Cells[4, i] = 'R' then
        StringList.Append(Document.Cells[0, i]);
    StringList.EndUpdate;

    SetLength(data, StringList.Count);
    for i := 0 to StringList.Count -1 do
      Val(StringList, data, error);

    LineSerie.AddArray(data);
    Graphic.AddSeries(LineSerie);
  finally
    StringList.Free;
    Document.Free;
    Graphic.Free;
  end;
end.


Ideally, I would like to use it without any package dependency. TAChart packages were included in the lazarus project.

Thanks in advance,

Best.
« Last Edit: September 25, 2017, 02:43:47 pm by cpicanco »
Be mindful and excellent with each other.
https://github.com/cpicanco/

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: TChart without gui and instant pascal
« Reply #1 on: September 23, 2017, 01:27:33 am »
PS.: Some sort of library like matplotlib or R plots would be great.
« Last Edit: September 23, 2017, 04:05:32 am by cpicanco »
Be mindful and excellent with each other.
https://github.com/cpicanco/

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: TChart without gui and instant pascal
« Reply #2 on: September 23, 2017, 06:43:47 am »
working example  :D
Code: Pascal  [Select][+][-]
  1. #!/usr/bin/env instantfpc
  2. {$mode objfpc}{$H+}
  3. uses
  4.   Interfaces, Classes, TAChartLazarusPkg,
  5.   FPCanvas, FPImage, FPImgCanv,
  6.   TAGraph,
  7.   TASeries,
  8.   TADrawerFPCanvas in './tadrawerfpcanvas.pas',
  9.   TADrawerCanvas,
  10.   TADrawUtils,
  11.   math,
  12.   TSVDocument;
  13.  
  14. const
  15.   filename = '/home/rafael/recordings/2016_10_26/000/stimulus_control/000.timestamps';
  16.  
  17. var
  18.   Document : TTSVDocument;
  19.   StringList : TStringList;
  20.   Chart : TChart;
  21.   LineSerie : TLineSeries;
  22.  
  23.   CanvasImage: TFPMemoryImage;
  24.   Canvas: TFPImageCanvas;
  25.   DrawerInterface: IChartDrawer;
  26.  
  27.   data : array of Double;
  28.   i : integer;
  29.   error:  integer = 0;
  30.  
  31. begin
  32.   Chart := TChart.Create(nil);
  33.   Chart.Font.Name:='/usr/share/fonts/truetype/freefont/FreeMono';
  34.   Chart.LeftAxis.Marks.LabelFont.Name := '/usr/share/fonts/truetype/freefont/FreeMono';
  35.   Chart.LeftAxis.Marks.LabelFont.Size := 12;
  36.   Chart.LeftAxis.Marks.LabelFont.Orientation := 0;
  37.   Chart.LeftAxis.Marks.Visible:=True;
  38.   Chart.LeftAxis.Grid.Style:=psClear;
  39.  
  40.   Chart.LeftAxis.Title.Visible:=True;
  41.   Chart.LeftAxis.Title.Caption := 'Respostas';
  42.   Chart.LeftAxis.Title.LabelFont.Name := '/usr/share/fonts/truetype/freefont/FreeMono';
  43.   Chart.LeftAxis.Title.LabelFont.Size := 12;
  44.   Chart.LeftAxis.Title.LabelFont.Orientation := 900;
  45.  
  46.   Chart.BottomAxis.Marks.Visible := false;
  47.   Chart.BottomAxis.Grid.Style := psClear;
  48.   Chart.BottomAxis.Title.Caption := 'Tempo';
  49.   Chart.BottomAxis.Title.Font.Name := '/usr/share/fonts/truetype/freefont/FreeMono';
  50.   Chart.BottomAxis.Title.Visible:=True;
  51.  
  52.  
  53.   Chart.Color := $FFFFFF;
  54.   Chart.BackColor := $FFFFFF;
  55.  
  56.   Chart.Title.Visible := True;
  57.   Chart.Title.Text.Text := 'test';
  58.   Chart.Title.Font.Name := '/usr/share/fonts/truetype/freefont/FreeMono';
  59.   Chart.Title.Font.Size := 12;
  60.   Chart.Title.Font.Orientation := 0;
  61.   Chart.Width := 800;
  62.   Chart.Height:= 800;
  63.  
  64.   Document := TTSVDocument.Create;
  65.   Document.LoadFromFile(Filename);
  66.   Document.SkipHeader := 5;
  67.  
  68.   StringList := Document.Filter('Event', 'R', 'Time');
  69.   try
  70.     SetLength(data, StringList.Count);
  71.     for i := 0 to StringList.Count -1 do
  72.       Val(StringList[i], data[i], error);
  73.  
  74.  
  75.     LineSerie := TLineSeries.Create(Chart);
  76.     LineSerie.AddArray(data);
  77.     Chart.AddSeries(LineSerie);
  78.  
  79.     CanvasImage := TFPMemoryImage.Create(Chart.Width, Chart.Height);
  80.     Canvas := TFPImageCanvas.Create(CanvasImage);
  81.     DrawerInterface := TFPCanvasDrawer.Create(Canvas);
  82.     DrawerInterface.DoGetFontOrientation := @CanvasGetFontOrientationFunc;
  83.     Chart.Draw(DrawerInterface, Rect(0, 0, Chart.Width, Chart.Height));
  84.     CanvasImage.SaveToFile('test.png');
  85.   finally
  86.     Canvas.Free;
  87.     CanvasImage.Free;
  88.     StringList.Free;
  89.     Document.Free;
  90.     Chart.Free;
  91.   end;
  92. end.
  93.  
Be mindful and excellent with each other.
https://github.com/cpicanco/

 

TinyPortal © 2005-2018