* * *

Author Topic: [SOLVED] How to insert vertical marks in a chart?  (Read 773 times)

valford

  • New member
  • *
  • Posts: 7
[SOLVED] How to insert vertical marks in a chart?
« on: February 13, 2018, 05:46:31 am »
Hello,
I am using Lazarus 1.6.4 in windows. I have an app which collects data from some sensors and generate graphs with the data (TChart); after some measurements, I must change the conditions and follow the pattern of change in the graphs. To mark those changes on my file I insert a new line which marks that point (a comment which is not a data point); however, I want to know how to create a mark of this event in the graphs (a vertical line parallel to the y-ax). Sometimes I need several marks on the same graph so I can not just clear the graph and assume my mark in x=0.

I have been trying inserting a new series constant-line to the graph (but I cannot create new lines  :'( ), also changing the grid properties (I could not do it), but I think those are not the best options to do it (and are nor working very well). Does someone know or have an idea of how to do it?

Because it is a specific line in a graph I do not have much code to share but I have been trying something like this:

Code: Pascal  [Select]
  1. CommentCh.position:=SecondsBetween(now,Temp);

But I cannot insert new lines only changes in the position... anyway I think this is not a good solution or need to be improved...
« Last Edit: February 13, 2018, 11:26:31 pm by valford »

wp

  • Hero Member
  • *****
  • Posts: 4836
Re: How to insert vertical marks in a chart?
« Reply #1 on: February 13, 2018, 10:18:31 am »
If I understand correctly you want to add markers to the chart to identify events which happened during data aquisition. As markers you want to add vertical constant lines.

A TConstantLine series is added like any other series at runtime: Create it, set its properties, then add it to the chart by calling AddSeries  (http://wiki.lazarus.freepascal.org/TAChart_Runtime_FAQ#How_to_add_a_series_at_runtime.3F):

Code: Pascal  [Select]
  1. uses
  2.   TASeries;
  3.  
  4. procedure TForm1.AddLineMarker(x: Double);
  5. var
  6.   line: TConstantLine;
  7. begin
  8.   line := TConstantLine.Create(self);
  9.   line.Position := x;
  10.   line.LineStyle := lsVertical;
  11.   line.Pen.Color := clRed;
  12.   Chart1.AddSeries(line);
  13. end;
  14.  

Alternatively you can assign some message text to the datapoint at which the event happened. The message text can be displayed as a label at the corresponding data point:

Code: Pascal  [Select]
  1. uses
  2.   TACustomSource, TACustomSeries, TAChartUtils;
  3.  
  4. procedure TForm1.AddMarkerText(ASeries: TChartSeries;
  5.   AIndex: Integer; AText: String);
  6. // Index is the data point index which will get the label
  7. var
  8.   item: PChartDataItem;
  9. begin
  10.   item := ASeries.Source[AIndex];
  11.   item^.Text := AText;
  12.   ASeries.Marks.Style := smsLabel;
  13. end;

In the attachment you can find a working demo which illustrates the principle.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

valford

  • New member
  • *
  • Posts: 7
Re: How to insert vertical marks in a chart?
« Reply #2 on: February 13, 2018, 11:25:32 pm »
If I understand correctly you want to add markers to the chart to identify events which happened during data aquisition. As markers you want to add vertical constant lines.

A TConstantLine series is added like any other series at runtime: Create it, set its properties, then add it to the chart by calling AddSeries  (http://wiki.lazarus.freepascal.org/TAChart_Runtime_FAQ#How_to_add_a_series_at_runtime.3F):

Code: Pascal  [Select]
  1. uses
  2.   TASeries;
  3.  
  4. procedure TForm1.AddLineMarker(x: Double);
  5. var
  6.   line: TConstantLine;
  7. begin
  8.   line := TConstantLine.Create(self);
  9.   line.Position := x;
  10.   line.LineStyle := lsVertical;
  11.   line.Pen.Color := clRed;
  12.   Chart1.AddSeries(line);
  13. end;
  14.  

Alternatively you can assign some message text to the datapoint at which the event happened. The message text can be displayed as a label at the corresponding data point:

Code: Pascal  [Select]
  1. uses
  2.   TACustomSource, TACustomSeries, TAChartUtils;
  3.  
  4. procedure TForm1.AddMarkerText(ASeries: TChartSeries;
  5.   AIndex: Integer; AText: String);
  6. // Index is the data point index which will get the label
  7. var
  8.   item: PChartDataItem;
  9. begin
  10.   item := ASeries.Source[AIndex];
  11.   item^.Text := AText;
  12.   ASeries.Marks.Style := smsLabel;
  13. end;

In the attachment you can find a working demo which illustrates the principle.

Thanks a lot. It works perfectly.

I was trying to do the same thing with AddXY  %) ... of course it did not work  :-[ .

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus