2

Does anyone know how to copy the XValues from one TChartSeries to another in Delphi7 (and TeeChart 4.04)? TChartSeries.ReplaceList(CopySeries.XValues, OriginalSeries.XValues) does not work, since it seem to replace the reference, so when OriginalSeries is changed, so is CopySeries. The length of CopySeries is equal or greater than OriginalSeries. I want to preserve the CopySeries.YValues.

My workaround has been to create a temporary list

Dummy := TChartSeries.Create(nil);
Dummy.AssignValues(OriginalSeries);
CopySeries.ReplaceList(CopySeries.XValues, Dummy.XValues);
Dummy.YValues.Destroy;

but I get a memory leakage since I cannot Destroy the Dummy since that also removes the Dummy.XValues referenced by CopySeries.XValues.

Any help is greatly appreciated.

Truls
  • 307
  • 3
  • 12

1 Answers1

6

I can think of two options:

  1. Assigning ValueList arrays directly to the series as in the Real-time Charting article, for example:

    uses Series;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      Chart1.AddSeries(TLineSeries.Create(Self)).FillSampleValues;
      Chart1.AddSeries(TLineSeries.Create(Self));
    
      { set our X array }
      Chart1[1].XValues.Value:=Chart1[0].XValues.Value;  { <-- the array }
      Chart1[1].XValues.Count:=Chart1[0].Count;          { <-- number of points }
      Chart1[1].XValues.Modified:=True;                  { <-- recalculate min and max }
    
      { set our Y array }
      Chart1[1].YValues.Value:=Chart1[0].YValues.Value;
      Chart1[1].YValues.Count:=Chart1[0].Count;
      Chart1[1].YValues.Modified:=True;
    
      { Show data }
      Chart1.Series[1].Repaint;
    end;
    
  2. Clone series:

    uses Series;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
       Chart1.AddSeries(TLineSeries.Create(Self)).FillSampleValues;
       Chart1.AddSeries(CloneChartSeries(Chart1[0]));
    end;
    
  3. If you are using TeeChart 4.04 you'll probably have to address series like Chart1.Series[0] instead of Chart1[0] as in the Repaint call in the first example. Alternatively you could try something like this:

    uses Series, Math;
    
    procedure TForm1.FormCreate(Sender: TObject);
    var i, MinNumValues, MaxNumValues: Integer;
    begin
      Chart1.AddSeries(TLineSeries.Create(Self)).FillSampleValues(15);
      Chart1.AddSeries(TLineSeries.Create(Self)).FillSampleValues(25);
    
      MinNumValues:=Min(Chart1.Series[0].Count, Chart1.Series[1].Count);
      MaxNumValues:=Max(Chart1.Series[0].Count, Chart1.Series[1].Count);
    
      for i:=0 to MinNumValues -1 do
        Chart1.Series[1].XValue[i]:=Chart1.Series[0].XValue[i];
    
      for i:=MinNumValues to MaxNumValues-1 do
        Chart1.Series[1].ValueColor[i] := clNone;
    end;
    
Truls
  • 307
  • 3
  • 12
Narcís Calvet
  • 7,304
  • 5
  • 27
  • 47
  • Thanks for quick reply. I forgot to mention that I am using Delphi7 and TeeChart 4.04, so it seems like I am unable to use the syntax: `Value := Chart1.Series[0].XValues.Value.` I also cannot use the CloneChartSeries, since I want to preserve the YValues. – Truls Sep 23 '11 at 11:36
  • @DavidHeffernan I just did that as well. It seems code formatting in a list works a little bit different than usual... – Narcís Calvet Sep 23 '11 at 11:38
  • I get `[Error] '[' expected but := found` on the syntax `Value := ...` – Truls Sep 23 '11 at 11:40
  • @Truls Ok, I added a third solution option for TeeChart 4.04. What about that? – Narcís Calvet Sep 23 '11 at 11:48
  • Solution 3: Works perfectly as long as the Series.XValues have the same length, but they (generally) differ. So if I am using that approach I'll have to delete the rest of the XValues-array. – Truls Sep 23 '11 at 11:54
  • @Truls what about solution 3 now? I have modified the code a little bit to change XValues for the number of values of the "smallest" series. If necessary I could try to find a "clean" Delphi 7 install. – Narcís Calvet Sep 23 '11 at 12:01
  • @NarcísCalvet Almost. After the copy there should be the same amount of Values in the new XValues-array as the old, since otherwise the new one will print out too many points. – Truls Sep 23 '11 at 12:31
  • @Truls Ok, I extended solution #3 to remove remaining points. – Narcís Calvet Sep 23 '11 at 12:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3724/discussion-between-truls-and-narcis-calvet) – Truls Sep 23 '11 at 12:51