用Tchart作图为何老出异常?(100分)

  • 主题发起人 主题发起人 zjmg
  • 开始时间 开始时间
Z

zjmg

Unregistered / Unconfirmed
GUEST, unregistred user!
我用文本文件中的数据作图
文本文件中的数据格式为:每行40个字符,为数字。
memo1.lines.loadfromfile(opendialog1.filename);
用循环语句依次读入每行数据,并用Tchart作图:
x:=strtoint(copy(memo1.lines,1,4);
y:=strtofloat(copy(memo1.lines,27,2))/10;
series1.clear;
series1.addxy(x,y,''clteecolor);

程序运行后,发现虽能运行,但多次作图后会产生异常,请问高手,这是什么原因?
 
我也想知道。
 
出现什么异常?
请把你的代码全贴出来
 
我的代码是:

procedure TMainform.N4Click(Sender: TObject); //引入数据文件
begin
chart1.visible:=false;
if opendialog1.Execute then
begin
memo1.clear;
memo1.lines.loadfromfile(opendialog1.Filename); //Memo中装入文件
end;
end;

procedure TMainform.N8Click(Sender: TObject); // 作 图
var
Sk,riqi0,riqi1:Tdatetime;
i,Nian,Yue,Ri,Shi,Fen,Miao:Integer;
y:Double;
begin
if MEmo1.lines.count=0 then application.messagebox('请先引入文件!','提示',mb_ok)
else
begin
chart1.undozoom;
chart1.Visible :=true;
chart1.BackWall.Pen.Visible:=false;
chart1.Legend.Visible :=false;
chart1.width:=screen.Width ;
chart1.height:=screen.height div 2;
chart1.left:=(screen.width-chart1.width)div 2;

With Chart1.LeftAxis do
begin
Automatic := False ;
Minimum:= 0 ;
Maximum:= 0 ;
Minimum:= 0.0 ;
Maximum:=9.0;
AxisValuesFormat := '###0.0###';
title.caption:='M';
end;

With Chart1.BottomAxis do
begin
Automatic := False ;
Series1.XValues.DateTime := True;
Increment := DateTimeStep[dtOnehour];
DateSeparator := '/';
DateTimeFormat:='d/m/yyyy';
Minimum:= 0 ;
Maximum:= 0 ;
riqi0:=EncodeDate(strtoint(copy(memo1.lines[0],1,4)),strtoint(copy(memo1.lines[0],5,2)),strtoint(copy(memo1.lines[0],7,2)))
+EncodeTime(strtoint(copy(memo1.lines[0],9,2)),0,0,0);
riqi1:=EncodeDate(strtoint(copy(memo1.lines[memo1.lines.count-1],1,4)),strtoint(copy(memo1.lines[memo1.lines.count-1],5,2)),strtoint(copy(memo1.lines[memo1.lines.count-1],7,2)))
+EncodeTime(strtoint(copy(memo1.lines[memo1.lines.count-1],9,2)),strtoint(copy(memo1.lines[memo1.lines.count-1],11,2))+5,50,50);
Maximum:=riqi1;
Minimum:=riqi0;
end;
chart1.leftAxis.Grid.Visible :=false;
chart1.bottomAxis.Grid.Visible :=false;
series1.clear;
series2.clear;
series3.clear;
series4.clear;
series5.clear;
series6.clear;
series7.clear;
series8.clear;
series9.clear;
series10.clear;
for i:=0 to memo1.lines.count-1 do
begin
Nian:=strtoint(copy(memo1.lines,1,4));
Yue:=strtoint(copy(Memo1.lines,5,2));
Ri:=strtoint(copy(Memo1.lines,7,2));
Shi:=strtoint(copy(Memo1.lines,9,2));
Fen:=strtoint(copy(Memo1.lines,11,2));
Miao:=strtoint(copy(Memo1.lines,13,2));
sk:=Encodedate(nian,yue,ri)+EncodeTime(shi,fen,miao,00);
y:=strtofloat(copy(memo1.lines,27,2))/10;
series8.addxy(sk,y,'',clteecolor); //绘图
end;
end;
end;

数据文件以下为例(一个文件):
199911012120142395711353044210002000000
199911012124590395711353045510002000000
199911012141500395711353043210002000000
199911012142304395711353043210002000000
199911012156493395711353042810002000000
199911012157116395711353043010002000000
199911012203213395711353043210002000000
199911012217164395711353043010002000000
199911012218227395711353043510002000000
199911012219443395711353043610002000000
199911012221368395711353043210002000000
199911012222012395711353043410002000000
199911012225488395711353043010002000000
199911012226340395711353043010002000000

当程序(Foxseis)运行,多次装入长度不同的数据文件后,就会出现异常,并提示如下:

Project Foxseis.exe raised exception class EaccessViolation with message 'Access
violation at address 0040230E in Module 'Foxseis.exe'. Read of ffffffff'.
process stoped. use step or Run to continue.
 
作图那段没有问题,我也是用同样的方法画线图和柱状图。
并且也是不停的Clear再重画,不同的是我是从数组中调数据。
所有还是查查你调用数据那段程序。


另:如果你的Chart不需要很多的改动,只接双击控件进行设置。

比如这一段:

With Chart1.LeftAxis do
begin
Automatic := False ;
Minimum:= 0 ;
Maximum:= 0 ;
Minimum:= 0.0 ;
Maximum:=9.0;
AxisValuesFormat := '###0.0###';
title.caption:='M';
end;


 
是我没有说清楚——
我用一个Tchart绘几种不同类型的图,需要分别设置坐标,然后绘。运行后,多次读入文件并绘图后,就会出现前述异常。
问题可能出在对坐标的分别设置上,但我不知如何解决此问题,是否必须用多个Tchart?
全部代码如下:

procedure TMainform.N4Click(Sender: TObject); //引入数据文件

begin
chart1.visible:=false;
label1.caption:='';
if opendialog1.Execute then
begin
memo1.clear;
memo1.lines.loadfromfile(opendialog1.Filename);
end;
end;


procedure TMainform.N8Click(Sender: TObject); //绘图1
var
Sk,riqi0,riqi1:Tdatetime;
i,Nian,Yue,Ri,Shi,Fen,Miao:Integer;
y:Double;
begin
if MEmo1.lines.count=0 then application.messagebox('请先引入文件!','提示',mb_ok)
else
begin
chart1.undozoom;
chart1.Visible :=true;
chart1.BackWall.Pen.Visible:=false;
chart1.Legend.Visible :=false;
chart1.width:=screen.Width ;
chart1.height:=screen.height div 2;
chart1.left:=(screen.width-chart1.width)div 2;

With Chart1.LeftAxis do
begin
Automatic := False ;
Minimum:= 0 ;
Maximum:= 0 ;
Minimum:= 0.0 ;
Maximum:=9.0;
AxisValuesFormat := '###0.0###';
title.caption:='M';
end;

With Chart1.BottomAxis do
begin
Automatic := False ;
Series1.XValues.DateTime := True;
Increment := DateTimeStep[dtOnehour];
DateSeparator := '/';
DateTimeFormat:='d/m/yyyy';
Minimum:= 0 ;
Maximum:= 0 ;
riqi0:=EncodeDate(strtoint(copy(memo1.lines[0],1,4)),strtoint(copy(memo1.lines[0],5,2)),strtoint(copy(memo1.lines[0],7,2)))
+EncodeTime(strtoint(copy(memo1.lines[0],9,2)),0,0,0);
riqi1:=EncodeDate(strtoint(copy(memo1.lines[memo1.lines.count-1],1,4)),strtoint(copy(memo1.lines[memo1.lines.count-1],5,2)),strtoint(copy(memo1.lines[memo1.lines.count-1],7,2)))
+EncodeTime(strtoint(copy(memo1.lines[memo1.lines.count-1],9,2)),strtoint(copy(memo1.lines[memo1.lines.count-1],11,2))+5,50,50);
Maximum:=riqi1;
Minimum:=riqi0;
end;
chart1.leftAxis.Grid.Visible :=false;
chart1.bottomAxis.Grid.Visible :=false;
series1.clear;
series2.clear;
series3.clear;
series4.clear;
series5.clear;
series6.clear;
series7.clear;
series8.clear;
series9.clear;
series10.clear;
for i:=0 to memo1.lines.count-1 do
begin
Nian:=strtoint(copy(memo1.lines,1,4));
Yue:=strtoint(copy(Memo1.lines,5,2));
Ri:=strtoint(copy(Memo1.lines,7,2));
Shi:=strtoint(copy(Memo1.lines,9,2));
Fen:=strtoint(copy(Memo1.lines,11,2));
Miao:=strtoint(copy(Memo1.lines,13,2));
sk:=Encodedate(nian,yue,ri)+EncodeTime(shi,fen,miao,00);
y:=strtofloat(copy(memo1.lines,27,2))/10;
series8.addxy(sk,y,'',clteecolor);
end;
end;
end;
procedure TMainform.Et1Click(Sender: TObject); //绘图2
var
i,Nian,Yue,Ri,Shi,Fen,Miao:integer;
sk,riqi0,riqi1:tdatetime;
y:double;
MS:single;
begin
if MEmo1.lines.count=0 then application.messagebox('请先引入文件!','提示',mb_ok)
else
begin
chart1.undozoom;
chart1.Visible :=true;
chart1.BackWall.Pen.Visible:=false;
chart1.Legend.Visible :=false;
chart1.width:=screen.width;
chart1.height:=screen.height div 2;
chart1.left:=(screen.width-chart1.width)div 2;


With Chart1.LeftAxis do
begin
Automatic := False ;
Minimum:= 0 ;
Maximum:= 0 ;
Maximum:= 10000000;
Minimum:=-600000;
AxisValuesFormat := '###0.###';
title.caption:='E';
end;
With Chart1.BottomAxis do
begin
Automatic := False ;
Minimum:= 0 ;
Maximum:= 0 ;
Series1.XValues.DateTime := True;
Increment := DateTimeStep[dtOneHour];
DateSeparator := '/';
DateTimeFormat:='d/m/yyyy';
riqi0:=EncodeDate(strtoint(copy(memo1.lines[0],1,4)),strtoint(copy(memo1.lines[0],5,2)),strtoint(copy(memo1.lines[0],7,2)))
+EncodeTime(strtoint(copy(memo1.lines[0],9,2)),strtoint(copy(memo1.lines[0],11,2)),strtoint(copy(memo1.lines[0],13,2)),0);
riqi1:=EncodeDate(strtoint(copy(memo1.lines[memo1.lines.count-1],1,4)),strtoint(copy(memo1.lines[memo1.lines.count-1],5,2)),strtoint(copy(memo1.lines[memo1.lines.count-1],7,2)))
+EncodeTime(strtoint(copy(memo1.lines[memo1.lines.count-1],9,2)),strtoint(copy(memo1.lines[memo1.lines.count-1],11,2)),strtoint(copy(memo1.lines[memo1.lines.count-1],13,2)),50);
Maximum:=riqi1;
Minimum:=riqi0;
end;
chart1.leftAxis.Grid.Visible :=false;
chart1.bottomAxis.Grid.Visible :=false;
series1.clear;
series2.clear;
series3.clear;
series4.clear;
series5.clear;
series6.clear;
series7.clear;
series8.clear;
series9.clear;
series10.clear;
for i:=0 to memo1.lines.count-1 do
begin
Nian:=strtoint(copy(memo1.lines,1,4));
Yue:=strtoint(copy(Memo1.lines,5,2));
Ri:=strtoint(copy(Memo1.lines,7,2));
Shi:=strtoint(copy(Memo1.lines,9,2));
Fen:=strtoint(copy(Memo1.lines,11,2));
Miao:=strtoint(copy(Memo1.lines,13,2));
sk:=Encodedate(nian,yue,ri)+EncodeTime(shi,fen,miao,00);
Ms:=0.113*(strtofloat(copy(memo1.lines,27,2)))-1.08;
y:=sqrt(exp((4.8+1.5*Ms)*ln(10)));
series9.addxy(sk,y,'',clteecolor);
end;
end;
end;

procedure TMainform.N9Click(Sender: TObject); //绘图3
var
i:integer;
begin
if Memo1.lines.count=0 then application.messagebox('请先引入文件!','提示',mb_ok)
else
begin
chart1.undozoom;
chart1.Visible:=true;
chart1.BackWall.Pen.Visible:=true;
chart1.width:=(screen.width)*2 div 3;
chart1.height:=(screen.height)*2 div 3;
chart1.left:=(screen.width-chart1.width)div 2;
chart1.Legend.Visible :=true;
Series1.XValues.DateTime := false;

With Chart1.LeftAxis do
begin
Automatic := False ;
Minimum:= 0;
Maximum:= 0;
Maximum:=2+strtofloat(COPY(memo1.lines[0],16,4))/100;
Minimum:= (strtofloat(COPY(memo1.lines[0],16,4))/100)-2;
title.caption:='';
end;
Chart1.BottomAxis.Increment :=1;
chart1.leftAxis.Grid.Visible :=true;
chart1.bottomAxis.Grid.Visible :=true;
With Chart1.bottomAxis do
begin
Automatic := False ;
Minimum:= 0;
Maximum:= 0;
Maximum:= 2+strtofloat(COPY(memo1.lines[1],20,5))/100;
Minimum:= (strtofloat(COPY(memo1.lines[1],20,5))/100)-2;
end;
series1.clear;
series2.clear;
series3.clear;
series4.clear;
series5.clear;
series6.clear;
series7.clear;
series8.clear;
series9.clear;
series10.clear;
for i:=0 to memo1.lines.count-1 do
begin
case strtoint(copy(memo1.lines,27,2)) of
1..19:series10.addxy(strtofloat(COPY(memo1.lines,20,3))+strtofloat(COPY(memo1.lines,23,2))/60,strtofloat(COPY(memo1.lines,16,2))+strtofloat(COPY(memo1.lines,18,2))/60,'',clteecolor);
20..29:series7.AddXY(strtofloat(COPY(memo1.lines,20,3))+strtofloat(COPY(memo1.lines,23,2))/60,strtofloat(COPY(memo1.lines,16,2))+strtofloat(COPY(memo1.lines,18,2))/60,'',clteecolor);
30..39:series6.addxy(strtofloat(COPY(memo1.lines,20,3))+strtofloat(COPY(memo1.lines,23,2))/60,strtofloat(COPY(memo1.lines,16,2))+strtofloat(COPY(memo1.lines,18,2))/60,'',clteecolor);
40..49:series5.addxy(strtofloat(COPY(memo1.lines,20,3))+strtofloat(COPY(memo1.lines,23,2))/60,strtofloat(COPY(memo1.lines,16,2))+strtofloat(COPY(memo1.lines,18,2))/60,'',clteecolor);
50..59:series4.addxy(strtofloat(COPY(memo1.lines,20,3))+strtofloat(COPY(memo1.lines,23,2))/60,strtofloat(COPY(memo1.lines,16,2))+strtofloat(COPY(memo1.lines,18,2))/60,'',clteecolor);
60..69:series3.addxy(strtofloat(COPY(memo1.lines,20,3))+strtofloat(COPY(memo1.lines,23,2))/60,strtofloat(COPY(memo1.lines,16,2))+strtofloat(COPY(memo1.lines,18,2))/60,'',clteecolor);
70..79:series2.addxy(strtofloat(COPY(memo1.lines,20,3))+strtofloat(COPY(memo1.lines,23,2))/60,strtofloat(COPY(memo1.lines,16,2))+strtofloat(COPY(memo1.lines,18,2))/60,'',clteecolor);
80..90:series1.addxy(strtofloat(COPY(memo1.lines,20,3))+strtofloat(COPY(memo1.lines,23,2))/60,strtofloat(COPY(memo1.lines,16,2))+strtofloat(COPY(memo1.lines,18,2))/60,'',clteecolor);
else
end;
end;
end;
end;
 
这我就不知道了解因为我一个CHART只画一种形状的图形。

真的还不如多用几个CHART,分别设置好坐标,用Visible来控制,不是方便的多吗?
 
可以动态生成series,chart1.addseries()添加,摘用series.free销毁无庸的系列。
 
多人接受答案了。
 
后退
顶部