1. 程式人生 > >TChart使用經驗小結

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;

這裡即實現了可以在圖表中拖動某一個點使其在縱軸上變化位置

 [tips]
1.3.1 Pro Version支援Bezier , Contour , Radar 和  point3D 曲線
1.3.2 支援jpeg檔案的匯出
1.3.3 TChart中的Series 可以連線到Table , Query , RemoteDataset(其他資料集)
1.3.4 TChart裡的series的active屬性可以實現對已繪製圖形的顯示或者隱藏
1.3.5 在TChart中, TChartSeries是所有具體series的父類,沒有畫出什麼來的,用一個具體的series類來建立就可以了,比如用TLineSeries、  

     TPieSeries、 TPointSeries、 TPointSeries等等都行
1.3.6 TeeFunctionComponent可以實現在同一個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的Beve

lInner和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中的全域性變數]
    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屬性獲得SeriesList中Series的個數
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