TChart用法(網絡收集二)

TChart用法(網絡收集二)

Posted on 2009-03-29 09:40 - - . -(yi,jian,dao) 閱讀(2525) 評論(0)  編輯 收藏 所屬分類: 未整理

 

TChart使用經驗小結
 
1、問題:通過Addxy方法給TChart添加標記(Mark)時,發現在TChart的橫座標會隨着Mark而變化,後來發現通過以下方法可避免這種情況:雙擊TChart,點擊Axis-> top or bottom ->labels,在styles中將labels的形式改爲Value即可!
2、幾個有用的屬性:
        圖表上的每個點都是有索引的,就象一個數組一樣,在OnClickSeries事件中有個ValueIndex屬性,該屬性可以得到鼠標所點擊的點的索引值(必須將Series的Point設置爲可見,鼠標點擊到那個點時纔可以觸發該事件)。
        xValue[index]、yValue[index]分別表示圖表的索引爲index的橫縱座標值,用這兩個屬性可以讀取和設置索引爲index的點的值,注意:不要用xValues和yValues,這兩個屬性也可以達到同樣的目的,但是速度非常的慢。因爲後兩個在進行操作的時候可能要遍歷整個圖表上的值(個人觀點)
      在MouseDown,MouseMove,Mouseup中,可以利用xScreentoValue(x),yScreentoValue(y)得到鼠標當時所在點對應在圖表上的橫縱座標值。
e.g......
private
  Nowindex:Integer;
  Cantuo:boolean;
........
procedure TfrmMain.Chart1ClickSeries(Sender: TCustomChart;
  Series: TChartSeries; ValueIndex: Integer; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  NowIndex:=ValueIndex;
end;
procedure TfrmMain.Chart1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
Cantuo:=true;
end;
procedure TfrmMain.Chart1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
Cantuo:=false;
end;
procedure TfrmMain.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
if Cantuo then
begin
  Series1.yValue[NowIndex]:=  Series1.yScreenToValue(y) ;
end;
end;
這裏即實現了可以在圖表中拖動某一個點使其在縱軸上變化位置
Tchart分析報告
1     Tchart分析報告
1.1      [概述]
   TChart是delphi裏面一個標準的圖形顯示控件。它可以靜態設計(at design time)也可以動態生成。
1.2      [繼承關係]   

TObject
 

TPersistent
 

TComponent
 

TControl
 

TCustomControl
 

TWedgetControl
 

TChart
 

TCustomPanel
1.3      [tips]
1.3.1            Pro Version支持Bezier , Contour , Radar   point3D 曲線
1.3.2            支持jpeg文件的導出
1.3.3            Chart中的Series 可以連接到Table , Query , RemoteDataset(其他數據集)
1.3.4            TChart裏的seriesactive屬性可以實現對已繪製圖形的顯示或者隱藏
1.3.5            TChart, tchartSeries是所有具體series的父類,沒有畫出什麼來的,用一個具體的series類來創建就可以了,比如用TLineSeriesTPieSeries TPointSeries TPointSeries等等都行
1.3.6            TTeeFunction Component可以實現在同一個TChart裏面,一個Serries對另一個Serries的統計
 
1.4      [問題極其使用技巧]
1.4.1            TChart中如何實現只有Y軸的放大與縮小功能?
設置BottomAxis或者LeftAxis的Automatic:=false並同時設置Minimum,Maximum屬性
1.4.2            如何固定TChart中的座標,不使TChart中的座標跟隨Series的變化而變化?
//設置底座標
  with myChart.BottomAxis do
  begin
    Automatic:=false;
    Minimum:=0;
    LabelStyle := talText;
  end;
  //設置左座標
  with myChart.LeftAxis do
  begin
    Automatic:=false;
    Minimum:=0;
    Title.Angle:=270;
    Title.Font:=Self.Font;
    Title.Font.Charset:=ANSI_CHARSET;
    Title.Font.Name:='@宋體';
    Grid.Visible := False;
  end;
  //設置右座標
  with myChart.RightAxis do
  begin
    Automatic:=false;
    Title.Font:=Self.Font;
    Title.Font.Charset:=ANSI_CHARSET;
    Title.Font.Name:='@宋體';
    Title.Caption:='累計百分比(%)';
    Maximum:=100;
    Minimum:=0;
  end;
1.4.3            如何刪除一個圖形中的一個點?
使用Series的delete 方法
1.4.4            如何修改一個點的X或者Y 值?
LineSeries1.YValue[3] := 27.1 ;
{In Bubble Series}
BubbleSeries1.RadiusValues.Value[ 8 ] := 8.1 ;
{In Pie Series}
PieSeries1.PieValues.Value[ 3 ] := 111 ;
1.4.5            如果橫座標是時間(日期),如何進行設置?
{First, you need to set the DateTime property to True in the desired X and/or Y values list.}
LineSeries1.XValues.DateTime := True ;
{Second, use the same above described methods, but give the values as Date, Time or DateTime values}
LineSeries1.AddXY( EncodeDate( 1996 , 1 , 23 ) , 25.4 , 'Barcelona' , clGreen );
1.4.6            如何在chart中畫出的曲線某個點上標記出該點的值?
Series.Marks.Visible:=true;
Series.Marks.Style:=smsValue;
1.4.7            如何設置橫軸或者縱軸的增長率?
Chart.BottomAxis.Increment := DataTimeStep[ dtOneHour ] ;
Chart.RightAxis.Increment := 1000;
1.4.8            如何對圖象進行縮放?
TChart的ZoomRect或者ZoomPercent方法 (Pie圖可能不支持縮放) 
1.5      [TChart可以繪製的圖形]
1.5.1            Line ( TLineSeries)
1.5.2    FastLine (TFastLineSeries) 相對Line來說,它損耗了某些屬性從而來實現快速繪製
1.5.3            Bar (TBarSeries)
1.5.4            Horizontal bar (THorizBarSeries)
1.5.5            Area (TAreaSeries)
1.5.6            Point (TPointSeries)
1.5.7            Pie (TPieSeries)
1.5.8            Arrow (TArrowSeries)
1.5.9            Bubble (TBubbleSeries)
1.5.10         Gantt (TGanttSeries)
1.5.11         Sharp (TChartShape)
1.6      [TChart的實時繪製] 
實時繪製對機器性能要求比較高,因此我們在編程的時候要注意下面幾個方面:
ü         使用2D圖形
ü         是Chart儘可能包含少的點
ü         如果需要,可以移除(remove)chart的legend(?????)和Title
ü         使用默認的字體和字體大小
ü         使用FastLineSeries
ü         使用實體(solid)畫筆和畫刷格式
ü         儘量避免使用圓形和環行bar樣式
ü         不要使用背景圖片和漸變效果樣式
ü         把Chart的BevelInner和BevelOUter屬性設置爲bcNone
ü         如果需要,把TChart的AxisVisible屬性設置爲False
ü         把BufferedDisplay設置爲false可以加速chart的重繪 
1.7      [Scrolling]
   TChart有4中scroll選擇(AllowPanning屬性),分別是 不允許Scroll ( pmNone) ; 水平Scroll (pmHorizontal) ; 垂直Scroll (pmVertical)  ;  水平和垂直Scroll (pmBoth)
Procedure Scroll(Const Offset:Double; CheckLimits:Boolean);
例子如下:
 Chart1.BottomAxis.Scroll(  1000, True );這段代碼也等同於
With Chart1.BottomAxis do
Begin
 Automatic:=false;
 SetMinMax( Minimum+1000, Maximum+1000 );
    End;
1.8             [TChart中的全局變量]
1.9             ü         TeeScrollMouseButton := mbRight;設置鼠標右鍵爲TChart滾動鍵(默認)
ü         TeeScrollKeyShift    := [ ssCtrl ]; 要按住Control鍵纔可以使Scroll滾動
 
1.9      [TChartSerries使用技巧]
1.9.1            運行時候創建一個Serries, 三種方法:
1.Var MySeries : TBarSeries ;
MySeries := TBarSeries.Create( Self );
MySeries.ParentChart := Chart1 ;
            2.Chart1.AddSeries( TBarSeries.Create( Self ) );
                3.Var MyClass : TChartSeriesClass;
MyClass := TBarSeries ;
Chart1.AddSeries( MyClass.Create( Self ) );
1.9.2            獲得TChart中的Serries數組,也有三種方法
1.MySeries := Chart1.SeriesList [ 0 ]
2.MySeries := Chart1.Series [ 0 ]
3.MySeries := Chart1 [ 0 ]
1.9.3            SerriesCount屬性獲得SeriesListSeries的個數
1.9.4            隱藏TChart中的Series有三種方法,但是效果不等價
1.  Series1.Active:=False; 僅僅隱藏,當設置爲true的時候還可以顯示出來
2.  Series1.ParentChart:=nil ; 隱藏,重新設置ParentChart爲TChart時候可以顯示
3.  Series1.Free; 刪除了Series. 不可以恢復
1.9.5            TChart中的數據排序
    With Series1 do
begin
    YValues.Order:=loAscending;
    YValues.Sort;
    Repaint;
end;
Ø         定位一個點(Loacate a point)
Series1.XValues.Locate(123);
Ø         XValue和YValue都擁有的屬性Total , TotalABS , MaxValue , MinValue
 
 
 
teechart筆記(Delphi)
來自:bobo 日期:2006-12-4 23:09:07 全文閱讀:loading... 分類:學習札記
1.series的方法
1.1 ColorRange設定一定範圍的點和線的顏色。
procedure ColorRange(AValueList: TChartValueList; Const FromValue, ToValue: Double; AColor: TColor);
其中:The TChartValueList component is the container of point values.
      Each Series component has one or more values lists.
 The XValues and YValues properties are TChartValueList components.
     可以是XValues 或YValues;FromValue, ToValue是範圍。AColor是點的顏色。
 UnitTeEngine
 DescriptionThis method will change the Color of a specified range of points.
 The FromValue and ToValue parameters are the beginning and end of the specified AValueList range.
 
AValueList can be any Series ValueList such as: XValues, YValues, etc.
 對某一特定的點:可以用series.valuecolor[index]屬性
property ValueColor[Index: Integer]: TColor;
Use the ValueColor property to get or set the Color of a point at a particular position. Index gives the position of the point, where 0 is the first, 1 is the second, and so on.
 不過這兩個方法都會導致線的顏色也會改變:改變的模式是若下一點出界,則從此點到下一點的連線的顏色變爲指定點的顏色。
 用這種方法可以改變各個點的形狀,result即是具體點的形狀。
function TForm1.Series1GetPointerStyle(Sender: TChartSeries;
  ValueIndex: Integer): TSeriesPointerStyle;
begin
  if ValueIndex mod 2=0 then result:=psRectangle
                        else result:=psTriangle;
 end;
 1.2 CalcXPosValue 把某一軸X/Y軸的值,轉化爲窗體的像素值(整型)。
CalcXPosValue(Const Value: Double): Integer;
 UnitTeEngine
 DescriptionReturns the pixel Screen Horizontal coordinate of the specified Value.
for example:
procedure TForm1.Chart1AfterDraw(Sender: TObject);
var tmp : Integer;
begin
  tmp:=Series1.CalcPosValue( 123 );
  with Chart1.Canvas do
       Rectangle(tmp-5,tmp-5,tmp 5,tmp 5);
end;
 1.3 畫水平直線Chart1AfterDraw的時間中
Procedure TForm1.DBChart1AfterDraw(Sender: TObject);
begin
drawLimits(ChrtNum, width: integer; Value: Double; color: TColor);
end;
 drawLimits(ChrtNum, width: integer; Value: Double; color: TColor);
var
  PixValue:integer;
begin
with dbcht1.Canvas do
    begin
      PixValue := seriesx.CalcYPosValue(Value);
      if (PixValue<dbcht1.ChartRect.Top) or (PixValue>dbcht1.ChartRect.Bottom) then//判斷是否已經超出dbcht1.ChartRect的範圍。
        Pen.Color:=dbcht1.Color
      else
        Pen.Color := color;
      Pen.Width := width;
      MoveTo(dbcht1.ChartRect.Left, PixValue);
      LineTo(dbcht1.ChartRect.Right, PixValue);
    end;
end; 
1.4 通過函數function來畫直線/各種自定義曲線;
series|dataSource|function. 在object treeView對象觀察器中 series1|TeeFunction1 的OnCalculate事件中添加公式
procedure TForm1.TeeFunction1Calculate(Sender: TCustomTeeFunction;
  const x: Double; var y: Double);
begin
   y:=50;
   //y:=Sin(x/10);
end;
 1.5  取消chart的網格
在chart|axis|Ticks|grid…中操作,此操作針對某一軸而言,如leftaxis,對於bottomaxis要選擇它再操作。
 1.6 mark tips顯示數據標籤的chart工具。
設定自定義的標籤的方法:
設定一個單元局部變量 MarkText:string
在chart的onMouseMove中添加事件。
procedure TfrmVChart.dbcht1MouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
 var tmp: Integer;
 begin
  tmp := SeriesX.Clicked(x, y);//返回該點的所在的series上序號,從0開始。
  if tmp = -1 then  //沒有點中曲線上的點。
     labelText := ''
   else
   begin
  MarkText := '序號:' FloatToStr(SeriesX.XValue[tmp]) #13 '數值:';
    MarkText := labelText FormatFloat('#,##0.####',SeriesX.YValue[tmp]) #13 'XUCL:' FormatFloat('#,##0.####',XUCL);
     MarkText := labelText #13 'XCL :' FormatFloat('#,##0.####',XCL) #13 'XLCL:' FormatFloat('#,##0.####',XLCL);
  end;
 end;
 再在marktipstools的GetText事件中添加代碼,修改要顯示的值。
 procedure TfrmVChart.ChartTool1GetText(Sender: TMarksTipTool;
   var Text: string);
begin
  Text := MarkText;
end;
 1.7  自定義軸的標籤(Label)
procedure TAxisItemsDemo.FormCreate(Sender: TObject);
begin
  inherited;
   Series1.AddArray([200,0,123,300,260,-100,650,400]);//添加數據
   AddCustomLabels;
end;
 Procedure TAxisItemsDemo.AddCustomLabels;
begin
  with Chart1.Axes.Left do
  begin
    Items.Clear;  // remove all custom labels//刪除原有的標籤。
     // add custom labels
     Items.Add(123,'Hello').Font.Size:=16;
     Items.Add(466,'Good'#13'Bye').Transparent:=False;
     Items.Add(300);
     with Items.Add(-100) do   //標籤的一些屬性。
    begin
      Transparent:=False;
      Transparency:=50;
      Color:=clBlue;
    end;
  end;
end;
 1.8 teefunction,自定義函數的功能。
屬性:munPoints:計算次數
      Period:兩次計算之間的x值的增量
      starX:X的開始值。
      periodStyle:period的方式,當是datetime類型時,很有用。如:Period:DateTimeStep[ dtOneMonth ],或DateTimeStep[ dtOneDay ]
     
1.9 teefunction的periodStyle
So, for example you can now plot the, for example,   monthly average of sales  function just using a normal  Average  function on a date-time source series and setting the function period to  one month :
 { Place a Series1 and fill it with datetime data values at runtime (or from a database) }
 Series2.SetFunction( TAverageTeeFunction.Create ) ;
Series2.FunctionType.PeriodStyle:=psRange;
Series2.FunctionType.Period:=DateTimeStep[ dtOneMonth ];
Series2.DataSource:=Series1 ;
 This will result in several points, each one showing the  average  of each month of data in Series1.
It's mandatory that points in the source Series1 should be sorted by date when calculating functions on datetime periods.
 The range can also be used for non-datetime series:
 Series2.SetFunction( TAverageTeeFunction.Create ) ;
Series2.FunctionType.PeriodStyle:=psRange;
Series2.FunctionType.Period:=100;
Series2.DataSource:=Series1 ;
 This will calculate an average for each group of points inside every  100  interval.
 (Points with X >=0, X<100 will be used to calculate the first average, points with X >=100, X<200 will be used to calculate the second average and so on... )
Notice this is different than calculating an average for every 100 points. 
在表單上增加一個Chart1,在FormCreate事件中添加
   Chart1->AddSeries(new TPieSeries(Chart1)); //  添加一個餅形圖
   Chart1->Series[0]->Clear();
   Chart1->View3D = true;    // 是否以3D形式顯示
     Chart1->TopAxis->Title->Caption = "X TEST TEST";    Chart1->BottomAxis->Title->Caption = "Y TESTTES ";
    Chart1->Title->Text->Strings[0] = "hi";
    for (int i = 1; i <= 12; i )
    {
        Chart1->Series[0]->AddXY(i , i);
    } 
在各種圖形之間切換
首先 delete Chart1->Series[0] 刪除原來的圖形對象,再重新生成,如:
   delete Chart1->Series[0];
   Chart1->AddSeries(new TBarSeries(Chart1));
   Chart1->View3D = true;
     Chart1->TopAxis->Title->Caption = "X TEST TEST";
    Chart1->BottomAxis->Title->Caption = "Y TESTTES ";
     for (int i = 1; i <= 12; i )
    {
        Chart1->Series[0]->AddXY(i , i);
    }【全文結束】
delphiTeeChart的各種屬性
 
TeeChart使用指南
TeeChart控件介紹
TeeChart   Pro   ActiveX是西班牙Steema   SL公司開發的圖表類控件,主要用來生成各種複雜的圖表。熟悉Delphi和C++   Builder的編程人員對它不會陌生,因爲在Delphi和C++   Builder裏包括了TeeChart的VCL版本。
TeeChart使用目的
如果你需要在程序中製作曲線圖、條狀圖、餅狀圖等等,使用這個控件都將是你的明智選擇。它因爲是已經被封裝好的產品,所以使用方便,可控性強,不過有的時候會有點小BUG。最好能找到源碼,並自己打幾個補丁。
TeeChart名詞解釋
Series
Axis
Scales
Line
Bar
Pie
TeeChart配置說明
ChartSeries(序列)   :   在一個圖表中可以有一個或多個序列,每個序列可以有不同的顯示類型,如Line、Bar、Pie等等。
Add…   添加新的序列
Fast   Line(TFastLineSeries簡單曲線圖)、
Line(TLineSeries   3D曲線圖)、
Bar(TBarSeries豎條狀圖)、
Horiz.   Bar(THorizBarSeries橫條狀圖)
Area(TAreaSeries   區域圖)、
Point(TPointSeries   點狀圖)、
Pie(TPieSeries   餅狀圖)、
Shape(TChartShape   實體圖)、
Gantt(TGanttSeries   甘特圖)、
Arrow(TArrowSeries   箭頭圖)、
Bubble(TBubbleSeries   泡泡圖)
SeriesFormat:修改序列的格式
SeriesPoint:修改序列中點的樣子
SeriesGeneral:對序列的配置,包括Axis,Legend,Formats,Cursor。
SeriesMarks:是否顯示序列中每個點的值。
SeriesData   Source:數據源。可以採用No   Data,Random   Values,Function。
Title…   修改序列的名稱。
Change…   修改序列的類型,可以從Line改變成Bar或者Pie。
ChartGeneral:一些基本的參數設置。
Print   Priview…:打印及打印預覽
Export…:輸出
Margins:頁邊空白
Allow   Zoom:允許縮放
Animated   Zoom:縮放過程是否是動態的,還是一次成功。(如果圖的點太多時,可以打開這個功能)
Allow   Scroll:滾動條
ChartAxis   :   控制圖表座標軸(上、下、左、右、深)的屬性
Show   Axis:是否顯示座標軸
ChartAxisScales:調整座標軸的比例
Automatic:可以自動處理最大與最小值,也可以手工設置。
Change…:可以自動處理增量,也可以手工設置。
Logarithmic:對數的
Inverted:反向的
ChartAxisTitle:設置座標軸的標題
Title:標題
Angle:標題的角度
Size:標題的寬度
Font…:標題的字體
ChartAxisLabels:設置座標軸的標籤
Titles   :   
ChartLegend(圖例):圖表中的一個長方形的用來顯示圖例標註的區域。可以標註Series的名稱或者Series中的項目和數值。
Visible
Back   Color
Font
Frame
Position
Margin
Legend   Style
Text   Style
Resize   Chart
Inverted
%Top   Pos
%Color   Width   
Dividing   Lines…
Shadow
ChartPanel   (面板):Panel可以設置圖表的背景。可以使用漸變的顏色或者圖像文件作爲整個圖表的背景
Bevel   Inner   (Bevel   Innner   )   Width
Bevel   Outer   (Bevel   Outer)   Width
Back   Image:圖表的背景圖
Style:(Stretch伸展,   Tile瓦片,   Center居中)
Inside:只顯示在背後壁上
Panel   Color:Panel的Inner的顏色
Border:給控件加邊界
Gradient(梯度):梯度顯示顏色
Visible、Start   Color…、End   Color…、
Direction(方向):上下、左右、從中間
  
ChartPaging   :圖表有幾頁組成
Points   Per   Page(每頁顯示幾個點):0爲所有的點顯示在一頁,其他按數字處理。
Scale   Last   Page:最後一頁按比例顯示,使之充滿整個圖表。
  
ChartWalls(壁)
Left   Walls:Y軸的平面
Bottom   Walls:X軸的平面
Back   Walls:背後的平面
Pattern…(模式):=(Solid實心,None無,Horizontal豎條紋,Vertical橫條紋,
Diagonal對角線,Back.Diagonal反向對角線,Cross十字線,DiagonalCross對角十字線);
Border…(邊線):=(Solid實線,   Dash劃線,   Dot點,   
Dash   Dot線點,   Dash   Dot   Dot線點點,   Small   Dots小點)
Transparent   (透明)
  
Chart3D
3Dimensions(維):是否3維顯示
Orthogonal(直角的):3維顯示爲直角顯示,則Elevation,Rotaion,Perspective被屏蔽
ZoomText:座標數字與圖形一起縮放
Zoom:圖形的縮放
Rotaion(旋轉):關閉Orthogonal後,可以在Y軸上旋轉
Elevation(正視圖)   :關閉Orthogonal後,可以在X軸上旋
Horiz.   Offset:在X軸移動圖形
Vert.   Offset:在Y軸移動圖形
Perspective(透視)   :關閉Orthogonal後,將焦點沿Z軸移動。
  
TeeChart使用實例
//   AddPages
NewTabSheet   :=   TTabSheet.Create(pgMain);
          with   NewTabSheet   do
          begin
              Parent   :=   pgMain;
              PageControl   :=   pgMain;
              Tag   :=   Ord(CountTypeIndex);
              Caption   :=   arrCountType[CountTypeIndex];
          end;
//   AddCharts
          NewChart   :=   TChart.Create(NewTabSheet);
          with   NewChart   do
          begin
              Parent   :=   NewTabSheet;
              Title.Text.Add('網間結算'   +   arrCountType[CountTypeIndex]   +   '/天分佈圖');
              LeftAxis.Title.Caption   :=   arrCountType[CountTypeIndex];
              BottomAxis.Title.Caption   :=   '話單日期';
              Legend.Visible   :=   sbLegend.Down;
              Legend.Alignment   :=   laBottom;
              Legend.LegendStyle   :=   lsSeries;
              View3D   :=   sb3D.Down;
              Width   :=   NewTabSheet.Width;
              Height   :=   NewTabSheet.Height;
          end;
//   ClearSeries
              AChart.Series[SeriesIndex].Free;
//   AddSeries
              NewSeries   :=   TLineSeries.Create(AChart);
              NewSeries.Title   :=   ANameList.Strings[SeriesIndex];
  
              NewSeries.Marks.Visible   :=True;
              AChart.AddSeries(NewSeries);
//   AddNameForSeries
AChart.SeriesList[SeriesIndex].Title:=   NewName;
//   ShowSeries
AChart.Series[SeriesIndex].Active   :=   True;
//   EmptySeries
AChart.Series[SeriesIndex].Clear;
//   FillSeries
AChart.Series[SeriesIndex].AddXY(); 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章