TChart 系列值的鼠标悬停提示

发布于 2024-07-25 09:16:08 字数 611 浏览 5 评论 0原文

我在 Delphi 7 中使用 TChart,我想显示一些条形图。 我使用以下代码从数据库查询中设置系列值:

  chart1.FreeAllSeries;

  chart1.SeriesList.Clear;

  chart1.AddSeries(TBarSeries.Create(Self));
  TBarSeries(chart1.Series[0]).BarStyle:=bsRectGradient;

  with query1 do
    begin
      Close;
      Execute;

      while not EoF do
        begin
          chart1.Series[0].Add(FieldAsFloat('sum_actual_days'), FieldAsString('contract_no'));
          Next;
        end;

    end;

每个条形(值)现在都在条形下方和条形上方的黄色矩形中显示标签。

我不想重复标签值两次,而是从查询中获得了一些附加信息,我希望将其显示在栏上方而不是标签上(或者最好作为鼠标悬停提示)。 这可以用 TChart 完成吗? 如何... ?

I am using a TChart in Delphi 7, and I want to display some bar charts. I am using the following code to set up the series values from a database query:

  chart1.FreeAllSeries;

  chart1.SeriesList.Clear;

  chart1.AddSeries(TBarSeries.Create(Self));
  TBarSeries(chart1.Series[0]).BarStyle:=bsRectGradient;

  with query1 do
    begin
      Close;
      Execute;

      while not EoF do
        begin
          chart1.Series[0].Add(FieldAsFloat('sum_actual_days'), FieldAsString('contract_no'));
          Next;
        end;

    end;

Each bar (value) is now showing the label both below the bar, and in a yellow rectangle above the bar.

Instead of repeating the label value twice, I have some additional information from the query that I would like to show above the bar instead of the label (or, preferably, as a mouseover hint). Can this be done with the TChart? And how... ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

恏ㄋ傷疤忘ㄋ疼 2024-08-01 09:16:08

这可以通过使用 TChart 图表的 OnMouseMove 事件来完成。 类似这样的事情应该可以帮助您开始:

procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
  SeriesIndex: Integer;
begin
  SeriesIndex := Series1.Clicked(X, Y);

  Chart1.ShowHint := SeriesIndex <> -1;

  if Chart1.ShowHint then
  begin
    query1.RecNo := SeriesIndex; { this may need to be SeriesIndex + 1 }
    Chart1.Hint := query1.FieldByName('YourFieldNameHere').AsString;
  end;
end;

当然,用于填充图表的查询必须仍然打开,此代码才能工作。

This can be done with a TChart by using the chart's OnMouseMove event. Something like this should get you started:

procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
  SeriesIndex: Integer;
begin
  SeriesIndex := Series1.Clicked(X, Y);

  Chart1.ShowHint := SeriesIndex <> -1;

  if Chart1.ShowHint then
  begin
    query1.RecNo := SeriesIndex; { this may need to be SeriesIndex + 1 }
    Chart1.Hint := query1.FieldByName('YourFieldNameHere').AsString;
  end;
end;

Of course, the query that you used to populate the chart must still be open for this code to work.

ㄟ。诗瑗 2024-08-01 09:16:08

“标记提示”工具提供了一个事件来提供自定义文本(OnGetText 事件):

procedure TForm1.ChartTool1GetText(Sender: TMarksTipTool;
  var Text: String);
var Index : Integer;
begin
  Index:=Series1.Clicked(Chart1.GetCursorPos);

  Text:='Hello point '+IntToStr(Index);
end;

The "Mark Tips" tool provides an event to supply custom text (OnGetText event):

procedure TForm1.ChartTool1GetText(Sender: TMarksTipTool;
  var Text: String);
var Index : Integer;
begin
  Index:=Series1.Clicked(Chart1.GetCursorPos);

  Text:='Hello point '+IntToStr(Index);
end;
红玫瑰 2024-08-01 09:16:08

有“标记提示”工具,可让您在移动到栏上时显示提示。 但我不确定您是否可以修改提示以显示自定义数据而不是预定义的样式。

There is the "Mark Tips" tool that allows you to show a hint when you move over a bar. But I'm not sure if you can modify the tip to show custom data instead of the predifined styles.

在你怀里撒娇 2024-08-01 09:16:08

您可以将面板放入图表中,并将其用作提示。 使用 NearestPoint 工具效果非常好。

输入图片这里的描述

首先将NearestPoint工具添加到图表中(双击图表,选择工具/添加)。

然后将面板添加到图表(图表组件中)并根据您的需要设置样式。

然后使用OnMouseMove事件:

procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var i:integer;
    px,py:integer;

begin   
  if PtInRect(Chart1.ChartRect,Point(X,Y)) then
    begin
      i:=ChartTool1.GetNearestPoint(Series1,X,Y);
      if (Series1.XValues.Count>0) and (i<Series1.Count) and (i>=0) then
        begin
          px:=Series1.CalcXPos(i);
          py:=Series1.CalcYPos(i);
          //You can add any data here to your panel, even you can put Images to it or anything else
          Panel_Hint.Caption:=TimeToStr(Series1.XValue[i])+' • '+FloatToStrF(Series1.YValue[i],ffNumber,20,2);
          Panel_Hint.Visible:=true;
          Form1.Canvas.Font.Assign(Panel_Hint.Font);
          Panel_Hint.Width:=Form1.Canvas.TextWidth(Panel_Hint.Caption)+8;
          Panel_Hint.Left:=px-Panel_Hint.Width div 2;
          Panel_Hint.Top:=py-Panel_Hint.Height-2;
        end
      else
        Panel_Hint.Visible:=false;
    end;
end;

如果你愿意,你可以禁用NearestPoint工具,但我们需要它来轻松找到对应的点。

You can put a Panel into the Chart, and use it as a hint. With the NearestPoint tool it works very well.

enter image description here

First add the NearestPoint tool to the chart (Double click on the chart, select Tools / Add).

Then add a Panel to the Chart (into the Chart component) and style it according to your needs.

Then use the OnMouseMove event:

procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var i:integer;
    px,py:integer;

begin   
  if PtInRect(Chart1.ChartRect,Point(X,Y)) then
    begin
      i:=ChartTool1.GetNearestPoint(Series1,X,Y);
      if (Series1.XValues.Count>0) and (i<Series1.Count) and (i>=0) then
        begin
          px:=Series1.CalcXPos(i);
          py:=Series1.CalcYPos(i);
          //You can add any data here to your panel, even you can put Images to it or anything else
          Panel_Hint.Caption:=TimeToStr(Series1.XValue[i])+' • '+FloatToStrF(Series1.YValue[i],ffNumber,20,2);
          Panel_Hint.Visible:=true;
          Form1.Canvas.Font.Assign(Panel_Hint.Font);
          Panel_Hint.Width:=Form1.Canvas.TextWidth(Panel_Hint.Caption)+8;
          Panel_Hint.Left:=px-Panel_Hint.Width div 2;
          Panel_Hint.Top:=py-Panel_Hint.Height-2;
        end
      else
        Panel_Hint.Visible:=false;
    end;
end;

If you want, you can disable the NearestPoint tool, but we need it to easily find the corresponding point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文