【问题标题】:Mouseover hint for TChart series valueTChart 系列值的鼠标悬停提示
【发布时间】:2009-06-25 11:47:46
【问题描述】:

我在 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 完成吗?以及如何...?

【问题讨论】:

    标签: delphi teechart


    【解决方案1】:

    这可以通过使用图表的 OnMouseMove 事件在 TChart 中完成。这样的事情应该让你开始:

    procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    var
      SeriesIndex: Integer;
    begin
      SeriesIndex := Series1.Clicked(X, Y);
    
      Chart1.ShowHint := SeriesIndex &lt&gt -1;
    
      if Chart1.ShowHint then
      begin
        query1.RecNo := SeriesIndex; { this may need to be SeriesIndex + 1 }
        Chart1.Hint := query1.FieldByName('YourFieldNameHere').AsString;
      end;
    end;
    

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

    【讨论】:

      【解决方案2】:

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

      procedure TForm1.ChartTool1GetText(Sender: TMarksTipTool;
        var Text: String);
      var Index : Integer;
      begin
        Index:=Series1.Clicked(Chart1.GetCursorPos);
      
        Text:='Hello point '+IntToStr(Index);
      end;
      

      【讨论】:

      • 标准版无法访问工具。需要专业版。
      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        您可以将面板放入图表中,并将其用作提示。使用 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 工具,但我们需要它来轻松找到对应点。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-05-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-06
          • 2014-12-16
          • 1970-01-01
          相关资源
          最近更新 更多