2017-07-16 273 views

回答

1

除了TLineSeries爲線數據中,添加一個TFastLineSeriesStairs屬性設置爲true創建標記指定數據點的虛線。 (結果圖的圖像附在答案的末尾)。

unit Unit1; 

interface 

uses 
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
    Dialogs, ExtCtrls, TeeProcs, TeEngine, Chart, Series; 

type 
    TForm1 = class(TForm) 
    Chart1: TChart; 
    LineSeries: TLineSeries; 
    StairSeries: TFastLineSeries; 
    procedure FormCreate(Sender: TObject); 
    procedure StairSeriesGetMarkText(Sender: TChartSeries; ValueIndex: Integer; var MarkText: string); 

    private 
    BottomMinimum : double; 
    LeftMinimum : double; 
    DataPoint : double; 

    public 
    { Public declarations } 
    end; 

var 
    Form1: TForm1; 

implementation 

{$R *.dfm} 


    function Fx(x : double) : double; 

    begin 
     Result := ((x - 1.5) * 20) + 17; 
    end; 

procedure TForm1.FormCreate(Sender: TObject); 

begin 
    LineSeries.Clear; 
    StairSeries.Clear; 

    LeftMinimum := 12; 
    BottomMinimum := 1.25; 

    DataPoint := 1.68; 

    LineSeries.AddXY(1.5, Fx(1.5)); 
    LineSeries.AddXY(1.6, Fx(1.6)); 
    LineSeries.AddXY(1.7, Fx(1.7)); 

    StairSeries.AddXY(BottomMinimum, Fx(DataPoint)); 
    StairSeries.AddXY(DataPoint,  Fx(DataPoint)); 
    StairSeries.AddXY(DataPoint,  LeftMinimum ); 


    Chart1.LeftAxis.SetMinMax(LeftMinimum, LeftMinimum + 12 ); 
    Chart1.BottomAxis.SetMinMax(BottomMinimum, BottomMinimum + 0.75); 
end; 

procedure TForm1.StairSeriesGetMarkText(Sender: TChartSeries; ValueIndex: Integer; var MarkText: string); 

begin 
    if valueIndex = 0 then 
     MarkText := '   '+Format('%5.2n',[ Fx(DataPoint) ]) 
    else if valueIndex = 1 then 
     MarkText := '' 
    else 
     MarkText := '   '+Format('%5.2n',[ DataPoint ]) 
end; 

end. 

的DFM:

object Form1: TForm1 
    Left = 234 
    Top = 127 
    Width = 602 
    Height = 533 
    Caption = 'Form1' 
    Color = clBtnFace 
    Font.Charset = DEFAULT_CHARSET 
    Font.Color = clWindowText 
    Font.Height = -11 
    Font.Name = 'Tahoma' 
    Font.Style = [] 
    OldCreateOrder = False 
    OnCreate = FormCreate 
    PixelsPerInch = 96 
    TextHeight = 13 
    object Chart1: TChart 
    Left = 56 
    Top = 24 
    Width = 465 
    Height = 409 
    BackWall.Brush.Color = clWhite 
    BackWall.Brush.Style = bsClear 
    Title.Text.Strings = (
     'TChart') 
    Legend.Visible = False 
    View3D = False 
    TabOrder = 0 
    object LineSeries: TLineSeries 
     Marks.ArrowLength = 8 
     Marks.Visible = False 
     SeriesColor = clBlue 
     LinePen.Color = clBlue 
     LinePen.Width = 4 
     Pointer.InflateMargins = True 
     Pointer.Style = psCircle 
     Pointer.Visible = True 
     XValues.DateTime = False 
     XValues.Name = 'X' 
     XValues.Multiplier = 1.000000000000000000 
     XValues.Order = loAscending 
     YValues.DateTime = False 
     YValues.Name = 'Y' 
     YValues.Multiplier = 1.000000000000000000 
     YValues.Order = loNone 
    end 
    object StairSeries: TFastLineSeries 
     Marks.ArrowLength = 8 
     Marks.Transparent = True 
     Marks.Frame.Visible = False 
     Marks.Visible = True 
     SeriesColor = clRed 
     OnGetMarkText = StairSeriesGetMarkText 
     LinePen.Color = clRed 
     LinePen.Style = psDash 
     LinePen.Width = 2 
     XValues.DateTime = False 
     XValues.Name = 'X' 
     XValues.Multiplier = 1.000000000000000000 
     XValues.Order = loAscending 
     YValues.DateTime = False 
     YValues.Name = 'Y' 
     YValues.Multiplier = 1.000000000000000000 
     YValues.Order = loNone 
    end 
    end 
end 

注:這是我用Delphi 10.2始建與2007年德爾福測試它,我降級的代碼德爾福7

一個Delphi 7例

要在2007年德爾福使用或更高更改設置軸MINMAX這個代碼:

Chart1.Axes.Left.SetMinMax(LeftMinimum, LeftMinimum+12); 
    Chart1.Axes.Bottom.SetMinMax(BottomMinimum, BottomMinimum+0.75); 

生成的圖表應該是這樣的:

enter image description here

+0

謝謝!謝謝!謝謝! – fromstog

+0

我的榮幸,請將我的回答標記爲已接受。 –