打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Delphi 中 Chart的 應用示例
[delphi] view plain copy
  1. unit UnitChart;  
  2.   
  3. interface  
  4.   
  5. uses  
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
  7.   Dialogs, ComCtrls, TeEngine, Series, ExtCtrls, TeeProcs, Chart, StdCtrls;  
  8.   
  9. type  
  10.   TFormChart = class(TForm)  
  11.     StatusBar1: TStatusBar;  
  12.     cht1: TChart;  
  13.     LineSeiresOne: TLineSeries;  
  14.     LineSeriesTwo: TLineSeries;  
  15.     btn1: TButton;  
  16.     tmr1: TTimer;  
  17.     procedure FormCreate(Sender: TObject);  
  18.     procedure cht1GetAxisLabel(Sender: TChartAxis; Series: TChartSeries;  
  19.       ValueIndex: Integer; var LabelText: String);  
  20.     procedure btn1Click(Sender: TObject);  
  21.     procedure tmr1Timer(Sender: TObject);  
  22.   private  
  23.     { Private declarations }  
  24.   public  
  25.     { Public declarations }  
  26.   end;  
  27.   
  28. var  
  29.   FormChart: TFormChart;  
  30.   
  31. implementation  
  32.   
  33. {$R *.dfm}  
  34. var  
  35.   XTemp : Integer;  
  36.   YTemp1, YTemp2 : Double;  
  37.   ICount : Integer;  
  38.   
  39.   StrTime : string;  
  40. const  
  41.   MAX_X_VALUE =270;  
  42.   MAX_Y_VALUE =3000;  
  43.   ONE_INITAL_VALUE = 3000;  
  44.   TWO_INITAL_VALUE = 2000;  
  45.   // 時間初始賦值  
  46.   StartHour = 9;  
  47.   // 值振幅  
  48.   VALUE_AMPLITUDE = 0.005;  
  49. procedure TFormChart.FormCreate(Sender: TObject);  
  50. var  
  51.   i : Integer;  
  52.     
  53. begin  
  54.   tmr1.Enabled := False;  
  55.   BorderIcons := BorderIcons - [biMaximize]; // 去掉最大化按鈕  
  56.   BorderStyle := bsSingle;   // 邊框格式  
  57.   WindowState := wsMaximized;  // 開始最大化  
  58.   
  59.   cht1.BottomAxis.Maximum := MAX_X_VALUE;  
  60.   cht1.LeftAxis.Maximum := MAX_Y_VALUE;  
  61.   
  62.   cht1.Title.Text.Clear;  
  63.   cht1.Title.Text.Add('模擬股市K線圖');  
  64.   
  65.   Randomize;  
  66.   YTemp1 := ONE_INITAL_VALUE;  
  67.   for i := 0 to MAX_X_VALUE - 1 do  
  68.   begin  
  69.     YTemp1 :=  YTemp1 * (1 - VALUE_AMPLITUDE + 2 * VALUE_AMPLITUDE * Random(10)/10);  
  70.     LineSeiresOne.Add( YTemp1, '', clGreen);  
  71.   end;  
  72.   
  73.   YTemp2 := TWO_INITAL_VALUE;  
  74.   for i := 0 to MAX_X_VALUE - 1 do  
  75.   begin  
  76.     YTemp2 :=  YTemp2 * (1 - VALUE_AMPLITUDE + 2 * VALUE_AMPLITUDE * Random(10)/10);  
  77.     LineSeriesTwo.Add( YTemp2, '', clRed);  
  78.   end;  
  79. end;  
  80.   
  81. procedure TFormChart.cht1GetAxisLabel(Sender: TChartAxis;  
  82.   Series: TChartSeries; ValueIndex: Integer; var LabelText: String);  
  83. var  
  84.   Hour , Minute: Integer;   
  85.   IntTime : Integer;  
  86. begin  
  87.   if (Sender = Sender.ParentChart.BottomAxis) then  
  88.   begin  
  89.     // 時間處理  
  90.     IntTime := StrToInt(LabelText) ;  
  91.     Hour := StartHour + IntTime div 60;  
  92.     Minute := IntTime mod 60;  
  93.     StrTime := FormatFloat('00', Hour) + FormatFloat(':00', Minute);  
  94.       
  95.     // 研究 GetAxisLabel 的觸發時機和參數含義  
  96.     // 觸發時機: 標識出X軸刻度時 , 當是 BottomAxis 時 , ValueIndex=-1;  
  97.     // 觸發次數:  
  98.     // mmo1.Lines.add( 'LabelText ='+ LabelText  + ', ValueIndex =' + IntToStr(ValueIndex));  
  99.     case (IntTime mod 30) of  
  100.       0: LabelText := StrTime;  
  101.     else  
  102.       LabelText := '';     
  103.     end;  
  104.   end;  
  105. end;  
  106.   
  107. procedure TFormChart.btn1Click(Sender: TObject);  
  108. begin  
  109.   XTemp := 0;  
  110.   YTemp1 := 2000;  
  111.   YTemp2 := 2000;  
  112.   LineSeiresOne.Clear;  
  113.   LineSeriesTwo.Clear;  
  114.   
  115.   tmr1.Interval := 100;  
  116.   tmr1.Enabled := True;  
  117. end;  
  118.   
  119. procedure TFormChart.tmr1Timer(Sender: TObject);  
  120. var  
  121.   i : Integer;  
  122. begin  
  123.   Randomize;  
  124.   LineSeiresOne.AddXY(XTemp, YTemp1, '', clGreen);  
  125.   LineSeriesTwo.AddXY(XTemp, YTemp2, '', clRed);  
  126.   Inc(XTemp);  
  127.   YTemp1 :=  YTemp1 * (1 - VALUE_AMPLITUDE + 2 * VALUE_AMPLITUDE * Random(10)/10);  
  128.   YTemp2 :=  YTemp2 * (1 - VALUE_AMPLITUDE + 2 * VALUE_AMPLITUDE * Random(10)/10);  
  129.   
  130.   if XTemp > MAX_X_VALUE then tmr1.Enabled := False; // 畫完後則停止 Timer  
  131. end;  
  132.   
  133. end.  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
WPF 使用定时器
delphi7编程技巧与实例精解之图形图像(修正重绘变形)
vb.net入门——ProgressBar 控件的使用
调用自定义的 el-tree-select (elementUI)
java之关系运算符
TDBChart 用法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服