delphiteechart

Adding values to TChart after LoadChartFromFile gives Access Violation Error


I Made a test vcl app for testing purposes, as I was trying to save a tchart series (so that when I close the app and open it again, the values that I had added would stay there instead of resetting to nothing.)

LoadChartFromFile and SaveChartToFile did the trick, but as one problem was solved, another appeared (as usual!)

After LoadChartFromFile happens, all of the values I had added before I closed and reopened the app are still there, but now I can't add any more values to it. Every time I try to, it gives and access violation error.

Here's basically all the code in this form. There's really not much in here.

unit Unit7;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, VclTee.TeeGDIPlus, VCLTee.TeEngine,
  VCLTee.Series, Vcl.ExtCtrls, VCLTee.TeeProcs, VCLTee.Chart, VCLTee.TeeStore, VCLTee.TeeEdiSeri,
  Vcl.StdCtrls;

type
  TForm7 = class(TForm)
    Chart1: TChart;
    Series1: TBarSeries;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form7: TForm7;

implementation

{$R *.dfm}

procedure TForm7.Button1Click(Sender: TObject);
begin
  With Series1 do
  begin
    Add (40);

  end;
end;

procedure TForm7.Button2Click(Sender: TObject);
begin
  With Series1 do
  begin
    Add (90);

  end;
end;

procedure TForm7.Button3Click(Sender: TObject);
begin
With Series1 do
  begin
    Add (10);

  end;
end;

procedure TForm7.Button4Click(Sender: TObject);
begin
With Series1 do
  begin
    Add (50);

  end;
end;

procedure TForm7.Button5Click(Sender: TObject);
begin
With Series1 do
  begin
    Add (140);

  end;
end;

procedure TForm7.FormCreate(Sender: TObject);
begin
LoadChartFromFile(TCustomChart(Chart1), 'File1');
end;

procedure TForm7.FormDestroy(Sender: TObject);
begin
SaveChartToFile(Chart1,'File1',True);
end;

end.

I Had created an IniFile on this form, so I got rid of it to see if it would do something, but there wasn't any differences. Chart saves and loads normally, and after it loads I cant add any more values to it. As you probably also saw in the code, I was using buttons to add values to the chart, so each button adds a different value. these buttons work, but after closing (which saves the chart values) and reopening the app (which loads the chart values), they give access violation error.

What I was expecting: to be able to keep adding values to the chart, and save them so that they are still there once you close and reopen the form.

Thanks for any advice or help!


Solution

  • The Series1 reference in the Form7 object is stale once a new chart or series is loaded/created. Refer to the series through the chart object instead of the form object.

    TBarSeries(Chart1.Series[0]).Add(125);;