Autor Beitrag
DorJo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 139



BeitragVerfasst: Di 29.07.08 22:58 
Hy @ all,
ich hab mal wieder ne Frage :D
Ich habe ein TChart mit einer Series. Nun wollte ich, wenn der Benutzer mit der Maus über die Series wandert, das die (x und) y Werte angezeigt werden, welche zur Mausposition gehören!

Ist dies irgendwie möglich?

Danke schonmal im Vorraus!

Mit freundlich Grüßen

Dor Jo
huuuuuh
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 665
Erhaltene Danke: 19

win xp, (win vista), win 7
VS 2008 Express Edition, VS 2010 Express Edition, VS 2010 Professionell
BeitragVerfasst: Di 29.07.08 23:04 
hat tchart ein onmousedown event? wenn ja, da werden x und y mitgeliefert. (ohne gewähr)
DorJo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 139



BeitragVerfasst: Di 29.07.08 23:45 
JA! Es hat auch ein MouseMove event. Aber da werden ja bloß die x,y werte des charts ausgegeben und nicht die variablen der Series!
wp_xxyyzz
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 40



BeitragVerfasst: Mi 30.07.08 16:38 
Hallo,

folgender Code sollte das tun, was du möchtest. Schlüssel ist die "Clicked"-Methode von TChartSeries, die den Index des Datenpunktes zurückgibt, über dem sich die Maus (Ort x,y) befindet.

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
procedure TForm1.ChartMouseMove(Sender: TObject; Shift: TShiftState;
  X,Y: Integer);
var
  Series : TChartSeries;
  i,j : integer;
  fmtx, fmty : string;
  txtx, txty : string;
  ax : TChartAxis;
  ds : char;
begin
  fmtx := Chart.BottomAxis.AxisValuesFormat;
  txtx := Chart.BottomAxis.Title.Caption;
  for i:=0 to Chart.SeriesCount-1 do begin
    Series := Chart.Series[i];
    j := Series.Clicked(x,y);
    if j <> -1 then begin
      if Series.VertAxis = aLeftAxis
        then ax := Chart.LeftAxis
        else ax := Chart.RightAxis;
      fmty := ax.AxisValuesFormat;
      txty := ax.Title.Caption;
      Statusbar1.SimpleText := Format('Plot "%s": %s = %s, %s = %s', [
        Series.Title,
        txtx, FormatFloat(fmtx, Series.XValues[j]),
        txty, FormatFloat(fmty, Series.YVAlues[j])
      ]);
      exit;
    end;
  end;
  Statusbar1.SimpleText := '';
end;
DorJo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 139



BeitragVerfasst: Mi 30.07.08 21:20 
Krass =O Also es funktioniert fast :)
ich hab folgende Werte:
x=27.07.2008;y=0
x=28.07.2008;y=-39,98
x=28.07.2008;y=-19,98
x=29.07.2008;y=-14,98

bis zu x=28.07. zeigt er mir als y=0 an... und danach funktuniert es einwandfrei!
Wisst ihr woran das liegen kann?

Aber danke erstmal für die unglaublich schnelle Antwort!

MFG

DorJO
wp_xxyyzz
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 40



BeitragVerfasst: Do 31.07.08 12:46 
Ohne weitere Angaben kann ich nicht genau nachvollziehen, worin dein Problem liegt: Welcher Serientyp, wieviele Serien, welche Daten?

Mein Code gibt nur dann eine Meldung in der Statuszeile aus, wenn sich die Maus innerhalb eines gewissen Bereichs über einem Datenpunkt befindet. Bei einem Liniendiagramm erfolgt also auf den Linienstücken zwischen den Datenpunkten keine Ausgabe. Wenn du das auch benötigst, musst du die Interpolation selbst schreiben, etwa so (wobei hier nur die x-Koordinate abgefragt wird):

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState;
  X,Y: Integer);
var
  i : integer;
  xm, ym : double;
  x1, x2, y1, y2 : double;
begin
  if Chart1.SeriesCount=1 then begin
    xm := Chart1.BottomAxis.CalcPosPoint(X);
    // Mauspunkt in Diagramm-Koordinaten
    for i:=0 to Chart1.Series[0].Count-2 do begin
      if (xm >= Chart1.Series[0].XValues[i])
        and (xm < Chart1.Series[0].XValues[i+1])
      then begin
        // Maus zwischen Datenpunkten i und i+1
        x1 := Chart1.Series[0].XValues[i];
        y1 := Chart1.Series[0].YValues[i];
        x2 := Chart1.Series[0].XValues[i+1];
        y2 := Chart1.Series[0].YValues[i+1];
        if x1 <> x2 then
          ym := (y2 - y1)/(x2 - x1)*(xm - x1) + y1
        else
          ym := Chart1.Series[0].GetVertAxis.CalcPosPoint(Y);
        StatusBar1.SimpleText := Format('x = %f, y = %f', [xm, ym]);
        exit;
      end;
    end;
  end;
  Statusbar1.SimpleText := 'außerhalb des Datenbereichs';
end;