how the cursors can be displayed in same vertical line

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
Jilonger.Xusen
Newbie
Newbie
Posts: 11
Joined: Mon Mar 08, 2004 5:00 am
Contact:

how the cursors can be displayed in same vertical line

Post by Jilonger.Xusen » Tue Nov 16, 2004 5:30 am

how the cursors can be displayed in same vertical line when mouse move?
{please compile and run following code in dephi7 teechart7}

Code: Select all

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, TeEngine, TeeTools, StdCtrls, Buttons, Series, OHLChart,
  CandleCh, TeeProcs, Chart, ExtCtrls, StatChar;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Chart1: TChart;
    Chart2: TChart;
    Series1: TCandleSeries;
    Series2: TBarSeries;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    ChartTool1: TCursorTool;
    ChartTool2: TCursorTool;
    Chart3: TChart;
    Series3: TLineSeries;
    TeeFunction1: TExpMovAveFunction;
    ChartTool3: TCursorTool;
    procedure BitBtn1Click(Sender: TObject);
    procedure ChartTool1Change(Sender: TCursorTool; x, y: Integer;
      const XValue, YValue: Double; Series: TChartSeries;
      ValueIndex: Integer);
    procedure ChartTool2Change(Sender: TCursorTool; x, y: Integer;
      const XValue, YValue: Double; Series: TChartSeries;
      ValueIndex: Integer);
    procedure ChartTool3Change(Sender: TCursorTool; x, y: Integer;
      const XValue, YValue: Double; Series: TChartSeries;
      ValueIndex: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
procedure CursorSynchronize(Source, Dest1,Dest2: TCursorTool);
var
  i                 : integer;
begin
  Dest1.ParentChart.AutoRepaint := False; // stop repainting
  Dest1.RedrawCursor;                   // hide cursor
  Dest2.ParentChart.AutoRepaint := False; // stop repainting
  Dest2.RedrawCursor;
  Dest1.XValue := Source.XValue;
  Dest2.XValue := Source.XValue;
  Dest2.RedrawCursor;                   // draw cursor again
  Dest2.ParentChart.AutoRepaint := True; // enable repainting
  Dest1.RedrawCursor;                   // draw cursor again
  Dest1.ParentChart.AutoRepaint := True; // enable repainting
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  Series1.FillSampleValues(100);
  Series2.FillSampleValues(100);
  Chart1.LeftAxis.LabelsSize:=30;
  Chart2.LeftAxis.LabelsSize:=30;
  Chart3.LeftAxis.LabelsSize:=30;
  Series2.CustomBarWidth:=Series1.CandleWidth;
end;

procedure TForm1.ChartTool1Change(Sender: TCursorTool; x, y: Integer;
  const XValue, YValue: Double; Series: TChartSeries; ValueIndex: Integer);
begin
  CursorSynchronize(Charttool1, Charttool2,ChartTool3);
  Chart2.Repaint;
  Chart3.Repaint;
end;

procedure TForm1.ChartTool2Change(Sender: TCursorTool; x, y: Integer;
  const XValue, YValue: Double; Series: TChartSeries; ValueIndex: Integer);
begin
CursorSynchronize(Charttool2, Charttool1,ChartTool3);
Chart1.Repaint;
Chart3.Repaint;
end;

procedure TForm1.ChartTool3Change(Sender: TCursorTool; x, y: Integer;
  const XValue, YValue: Double; Series: TChartSeries; ValueIndex: Integer);
begin
CursorSynchronize(Charttool3, Charttool1,ChartTool2);
Chart1.Repaint;
Chart2.Repaint;
end;

end.

Code: Select all

object Form1: TForm1
  Left = 194
  Top = 106
  Width = 711
  Height = 559
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 703
    Height = 481
    Align = alTop
    Caption = 'Panel1'
    TabOrder = 0
    object Chart1: TChart
      Left = 1
      Top = 1
      Width = 701
      Height = 247
      MarginBottom = 0
      Title.Text.Strings = (
        'TChart')
      BottomAxis.Inverted = True
      BottomAxis.Visible = False
      View3D = False
      Align = alTop
      TabOrder = 0
      object Series1: TCandleSeries
        CloseValues.Name = 'Close'
        CloseValues.Order = loNone
        DateValues.DateTime = False
        DateValues.Name = 'Date'
        DateValues.Order = loAscending
        HighValues.Name = 'High'
        HighValues.Order = loNone
        LowValues.Name = 'Low'
        LowValues.Order = loNone
        OpenValues.Name = 'Open'
        OpenValues.Order = loNone
        Marks.Callout.Brush.Color = clBlack
        Marks.Visible = False
        ShowInLegend = False
        HighLowPen.Color = clDefault
      end
      object ChartTool1: TCursorTool
        FollowMouse = True
        Series = Series1
        Snap = True
        OnChange = ChartTool1Change
      end
    end
    object Chart2: TChart
      Left = 1
      Top = 349
      Width = 701
      Height = 131
      MarginTop = 0
      Title.Text.Strings = (
        'TChart')
      Title.Visible = False
      BottomAxis.Inverted = True
      TopAxis.Visible = False
      View3D = False
      Align = alBottom
      TabOrder = 1
      object Series2: TBarSeries
        Marks.Callout.Brush.Color = clBlack
        Marks.Visible = False
        ShowInLegend = False
        Gradient.Direction = gdTopBottom
        XValues.Name = 'X'
        XValues.Order = loAscending
        YValues.Name = 'Bar'
        YValues.Order = loNone
      end
      object ChartTool2: TCursorTool
        FollowMouse = True
        Series = Series2
        Style = cssVertical
        OnChange = ChartTool2Change
      end
    end
    object Chart3: TChart
      Left = 1
      Top = 248
      Width = 701
      Height = 101
      Legend.Visible = False
      MarginBottom = 0
      MarginTop = 0
      Title.Text.Strings = (
        'TChart')
      Title.Visible = False
      BottomAxis.Inverted = True
      BottomAxis.Visible = False
      TopAxis.Visible = False
      View3D = False
      Align = alClient
      TabOrder = 2
      object Series3: TLineSeries
        Marks.Callout.Brush.Color = clBlack
        Marks.Visible = False
        DataSource = Series1
        Pointer.InflateMargins = True
        Pointer.Style = psRectangle
        Pointer.Visible = False
        XValues.Name = 'X'
        XValues.Order = loAscending
        YValues.Name = 'Y'
        YValues.Order = loNone
        YValues.ValueSource = 'Close'
        object TeeFunction1: TExpMovAveFunction
          Period = 10.000000000000000000
        end
      end
      object ChartTool3: TCursorTool
        FollowMouse = True
        Series = Series3
        Style = cssVertical
        OnChange = ChartTool3Change
      end
    end
  end
  object BitBtn1: TBitBtn
    Left = 182
    Top = 496
    Width = 75
    Height = 25
    Caption = 'Data'
    TabOrder = 1
    OnClick = BitBtn1Click
  end
  object BitBtn2: TBitBtn
    Left = 284
    Top = 496
    Width = 75
    Height = 25
    Caption = 'BitBtn2'
    TabOrder = 2
  end
end

Pep
Site Admin
Site Admin
Posts: 3273
Joined: Fri Nov 14, 2003 5:00 am
Contact:

Post by Pep » Sun Nov 21, 2004 11:58 am

Hi,

I see what you refer. I think the best way to accomplsh this (as you're using Series types with different pointer width) is to use the Canvas techniques (draw a line over the Charts and get the Value using the mouse events). You could use similar code to the following :

Code: Select all

unit Unit1;

interface 

uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, TeEngine, TeeTools, StdCtrls, Buttons, Series, OHLChart, 
  CandleCh, TeeProcs, Chart, ExtCtrls, StatChar; 

type 
  TForm1 = class(TForm) 
    Panel1: TPanel; 
    Chart1: TChart; 
    Chart2: TChart; 
    Series1: TCandleSeries; 
    Series2: TBarSeries;
    BitBtn1: TBitBtn; 
    BitBtn2: TBitBtn; 
    ChartTool1: TCursorTool; 
    ChartTool2: TCursorTool; 
    Chart3: TChart; 
    ChartTool3: TCursorTool;
    Series3: TFastLineSeries;

    procedure BitBtn1Click(Sender: TObject);
    procedure Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var 
  Form1: TForm1; 

implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  Series1.FillSampleValues(100);
  Series2.FillSampleValues(100);
  Series3.FillSampleValues(100);
  Chart1.LeftAxis.LabelsSize:=30;
  Chart2.LeftAxis.LabelsSize:=30;
  Chart3.LeftAxis.LabelsSize:=30;
  Series2.CustomBarWidth:=Series1.CandleWidth;

end;

procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var i, s, posi : integer;
begin;
  Chart1.Repaint;
  Chart2.Repaint;
  Chart3.Repaint;

  Chart1.Canvas.Brush.Style := bsClear;

  If Chart1.SeriesCount > 0 Then
  begin
   for s := 0 to Chart1.SeriesCount-1 do
   begin
    If Chart1.Series[s].Count > 0 Then begin
      For i := Chart1.Axes.Top.PosAxis To Chart1.Canvas.Bounds.Bottom do begin
        If Chart1.Series[s].Clicked(X, i) <> -1 Then posi := i;
      end;
      Chart1.Canvas.TextOut( 10, 10 + (10 * s), 'Series ' + inttostr(s) + ' Value: ' + floattostr(Chart1.Series[s].YScreenToValue(posi)));
    end;
   end;
  end;
  Chart1.Canvas.Pen.Color := clBlue;
  Chart1.Canvas.MoveTo (X, round(Chart1.Axes.top.PosAxis));
  Chart1.Canvas.LineTo (X, round(Chart1.Canvas.Bounds.Bottom));
  Chart3.Canvas.Pen.Color := clBlue;
  Chart3.Canvas.MoveTo (X, round (Chart3.Canvas.Bounds.Top));
  Chart3.Canvas.LineTo( X, round (Chart3.Canvas.Bounds.Bottom));
  Chart2.Canvas.Pen.Color := clBlue;
  Chart2.Canvas.MoveTo (X, round (Chart2.Canvas.Bounds.Top));
  Chart2.Canvas.LineTo( X, round (Chart2.axes.Bottom.PosAxis));
end;
end.

Code: Select all

object Form1: TForm1
  Left = 194
  Top = 106
  Width = 711
  Height = 561
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 703
    Height = 481
    Align = alTop
    Caption = 'Panel1'
    TabOrder = 0
    object Chart1: TChart
      Left = 1
      Top = 1
      Width = 701
      Height = 247
      MarginBottom = 0
      Title.Text.Strings = (
        'TChart')
      BottomAxis.Inverted = True
      BottomAxis.Visible = False
      View3D = False
      Align = alTop
      TabOrder = 0
      OnMouseMove = Chart1MouseMove
      object Series1: TCandleSeries
        CloseValues.Name = 'Close'
        CloseValues.Order = loNone
        DateValues.DateTime = False
        DateValues.Name = 'Date'
        DateValues.Order = loAscending
        HighValues.Name = 'High'
        HighValues.Order = loNone
        LowValues.Name = 'Low'
        LowValues.Order = loNone
        OpenValues.Name = 'Open'
        OpenValues.Order = loNone
        HorizAxis = aTopAxis
        Marks.Callout.Brush.Color = clBlack
        Marks.Visible = False
        ShowInLegend = False
        HighLowPen.Color = clDefault
      end
      object ChartTool1: TCursorTool
        Active = False
        FollowMouse = True
        Series = Series1
      end
    end
    object Chart2: TChart
      Left = 1
      Top = 349
      Width = 701
      Height = 131
      MarginTop = 0
      Title.Text.Strings = (
        'TChart')
      Title.Visible = False
      BottomAxis.Inverted = True
      TopAxis.Visible = False
      View3D = False
      Align = alBottom
      TabOrder = 1
      object Series2: TBarSeries
        Marks.Callout.Brush.Color = clBlack
        Marks.Visible = False
        ShowInLegend = False
        Gradient.Direction = gdTopBottom
        XValues.Name = 'X'
        XValues.Order = loAscending
        YValues.Name = 'Bar'
        YValues.Order = loNone
      end
      object ChartTool2: TCursorTool
        Active = False
        FollowMouse = True
        Series = Series2
        Style = cssVertical
      end
    end
    object Chart3: TChart
      Left = 1
      Top = 248
      Width = 701
      Height = 101
      Legend.Visible = False
      MarginBottom = 0
      MarginTop = 0
      Title.Text.Strings = (
        'TChart')
      Title.Visible = False
      BottomAxis.Inverted = True
      BottomAxis.Visible = False
      TopAxis.Visible = False
      View3D = False
      Align = alClient
      TabOrder = 2
      object Series3: TFastLineSeries
        Marks.Callout.Brush.Color = clBlack
        Marks.Visible = False
        LinePen.Color = clRed
        XValues.Name = 'X'
        XValues.Order = loAscending
        YValues.Name = 'Y'
        YValues.Order = loNone
      end
      object ChartTool3: TCursorTool
        Active = False
        FollowMouse = True
        Style = cssVertical
      end
    end
  end
  object BitBtn1: TBitBtn
    Left = 182
    Top = 496
    Width = 75
    Height = 25
    Caption = 'Data'
    TabOrder = 1
    OnClick = BitBtn1Click
  end
  object BitBtn2: TBitBtn
    Left = 284
    Top = 496
    Width = 75
    Height = 25
    Caption = 'BitBtn2'
    TabOrder = 2
  end
end
Another way is to do this using the Custom Axes feature, you can see some example which show you how this can be done in the TeeChart Pro Demo features project.

Post Reply