Creating a Trend Line series at run-time

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
Markus46
Newbie
Newbie
Posts: 10
Joined: Thu Sep 20, 2018 12:00 am

Creating a Trend Line series at run-time

Post by Markus46 » Wed Nov 07, 2018 10:48 pm

Hello,

I have an existing TChart object "LChart". It's a component on a form.
I have an existing TChartSeries "LSeries". It's created at runtime based on a series type string "LSeriesType" like so:

Code: Select all

var
  C : TRttiContext;
  T : TRttiInstanceType;
  V : TValue;
  LSeries: TChartSeries;
begin
    C := TRttiContext.Create;
    try
      T := (C.FindType('VCLTee.Series.'+LSeriesType) as TRttiInstanceType);
      if assigned(T) then begin
        V := T.GetMethod('Create').Invoke(T.metaClassType,[Visualisation]);
        LSeries := V.AsObject as TChartSeries;
      end
      else
        raise Exception.Create(format('Could not create %s graph type', ['VCLTee.Series.'+LSeriesType]));
    finally
      C.Free;
    end;
    LChart.AddSeries(LSeries);
end;

initialization
  // Reference every series type so that they get linked into the exe and can be found at runtime by TRttiContext.FindType
  TBarSeries.ClassName;
  TAreaSeries.ClassName;
  TBarSeries.ClassName;
  TFastLineSeries.ClassName;
  THorizAreaSeries.ClassName;
  THorizBarSeries.ClassName;
  THorizLineSeries.ClassName;
  TLineSeries.ClassName;
  TPieSeries.ClassName;
  TPointSeries.ClassName;
This code works and the chart displays OK.

Now I want to add a Trend Line series to the chart. I've added this additional code:

Code: Select all

var
  LTrendFunction: TTrendFunction;
  LLineSeries: TLineSeries;
begin
    LTrendFunction := TTrendFunction.Create(LChart);
    LTrendFunction.Period := 3;
    LLineSeries := LChart.AddSeries(TLineSeries) as TLineSeries;
    LLineSeries.SetFunction(LTrendFunction);
    LLineSeries.Color := TColor(fdqVisualisationSeries.FieldByName('TrendLineColor').AsInteger);
    LLineSeries.Title := 'Trend';
    LLineSeries.DataSource := LSeries;    // LSeries has no data at this point
    LLineSeries.XValues.Name := LSeries.XValues.Name;
    LLineSeries.XValues.Order := LSeries.XValues.Order;
    LLineSeries.YValues.Name := LSeries.YValues.Name;
    LLineSeries.YValues.Order := LSeries.YValues.Order;
end;
LSeries data points are added after the above code.

I am not seeing the Trend Line series displayed at all.
Can you please let me know what I might be doing wrong here?

Many thanks
Mark

Yeray
Site Admin
Site Admin
Posts: 8567
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Creating a Trend Line series at run-time

Post by Yeray » Thu Nov 08, 2018 11:09 am

Hello Mark,

Try calling CheckDataSource on the LLineSeries to force it to retrieve the new values:

Code: Select all

  LLineSeries.CheckDataSource;
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Image Please read our Bug Fixing Policy

Markus46
Newbie
Newbie
Posts: 10
Joined: Thu Sep 20, 2018 12:00 am

Re: Creating a Trend Line series at run-time

Post by Markus46 » Thu Nov 08, 2018 8:22 pm

Thanks Yeray!
That line fixes it.

Regards
Mark

Post Reply