Teechart,由于标题更改,自动重新计算自定义标记区域

时间:2018-10-14 17:15:55

标签: delphi teechart

Delphi 10.1 VCL,具有嵌入式Teechart。

我有一个区域系列,其标记通过代码移动到自定义位置。 标记标题内容更改后,黄色背景不会调整(自动调整大小)为新的标记内容。 我已经解决了这个问题,但是它有闪烁现象,而且并不优雅。 我正在寻找一种方法来做得更好。

详细信息: 我在图表上放置了三个按钮,一个用于移动标记位置,第二个按钮向标记标题添加第二个内容行。第三个按钮是我的工作,以获取合适的尺寸。 系列创作:

procedure TForm2.FormCreate(Sender: TObject);
begin
  Chart1.View3D := false;
  Chart1.Axes.Bottom.SetMinMax(0,5);

  with Chart1.AddSeries(tAreaSeries) as tAreaSeries do
    begin
      AddXY(1, 10);                  // Two points AreaSeries
      AddXY(4, 10);
      Marks[1].Visible     := false; // Hide the other Mark, the default is true
      Marks.Visible        := true;  // Global Visibility for all Markers
      Chart1[0].Marks[0].Text.Text := 'First-line';
    end;
end;

按“移动标记”按钮代码:

procedure TForm2.btnMoveMarkClick(Sender: TObject);
begin
  Chart1[0].Marks.Positions[0].Custom := true;
  Chart1[0].Marks.Positions[0].Offset(point(50,70));
//  Chart1[0].Marks.Positions[0].LeftTop := point(150,200);  // It is moving the Mark but not drawing the line to Series point
  Chart1.Repaint; // It doesn't work without this Repaint
end;

将生成以下屏幕: enter image description here 现在,按第二个按钮将“标记标题”内容更改为:

procedure TForm2.btnChangeMarkContentClick(Sender: TObject);
begin
  Chart1[0].Marks[0].Text.Text := 'First-line'+#13+'Second-line';
end;

如您所见,黄色背景大小未更改: enter image description here

我的蛮力解决方法是删除自定义位置,这将调整标记的大小,然后按照以下步骤重新放置标记:

procedure TForm2.btnResizeMarkClick(Sender: TObject);
var
  LastPoint: tpoint;
begin
  LastPoint := Chart1[0].Marks.Positions[0].LeftTop;
  Chart1[0].Marks.Positions.Automatic(0);
  Chart1.Repaint;

  Chart1[0].Marks.Positions[0].Custom := true;
  Chart1.Repaint;
//  Chart1[0].Marks[0].MoveTo(LastPoint); // It doesn't work - Why?
  Chart1[0].Marks.Positions[0].LeftTop := LastPoint; // Better to use Offset
  Chart1.Repaint;
end;

它正在完成工作,但是由于Mark的移动而闪烁,如下所示: enter image description here

感谢任何提示,如何在不删除标记自定义位置的情况下调整标记的大小,而导致闪烁。 Reron

2 个答案:

答案 0 :(得分:1)

您可以重新计算标记范围,并将WidthHeight分配给相应的位置:

  TCustomTextShapeAccess(Chart1[0].Marks[0]).CalcBounds(Chart1);
  Chart1[0].Marks.Positions[0].Height:=Chart1[0].Marks[0].Height;
  Chart1[0].Marks.Positions[0].Width:=Chart1[0].Marks[0].Width;
  Chart1.Repaint;

请注意,您必须声明TCustomTextShapeAccess类才能访问受保护的CalcBounds方法:

type TCustomTextShapeAccess=class(TCustomTextShape);

答案 1 :(得分:0)

Yeray解决了主要问题。此外,箭头的调整也应如下:

type
  tCustomTextShapeAccess = class(tCustomTextShape); // Yeray: tCustomTextShapeAccess class to get access to the protected CalcBounds method

const
  tcaTopLeft = 0;
  tcaArrowTo = 1;

procedure TeeChart_ResizeCustomMark(aChart: tChart; aSeriesInx, aMarkInx, aAnchor: integer);
// Resize Custom Mark area shape. It is required after Title text modification
// aAnchor: tcaTopLeft(0), tcaArrowTo(1); Choose which point to keep
var
  aSeries: tChartSeries;
  aMark  : tMarksItem;
  aMarkPosision: tSeriesMarkPosition;
begin
  // Assignments for more readable code
  aSeries       := aChart[aSeriesInx];
  aMark         := aChart[aSeriesInx].Marks[aMarkInx];
  aMarkPosision := aSeries.Marks.Positions[aMarkInx];

  // Bounds Calculation of the new Mark. Yeray solution.
  tCustomTextShapeAccess(aMark).CalcBounds(aChart); // Yeray: tCustomTextShapeAccess class to get access to the protected CalcBounds method
  aMarkPosision.Height := aMark.Height;
  aMarkPosision.Width  := aMark.Width;

  // Set Mark position based on aAnchor
  case aAnchor of
    tcaTopLeft: // Keep LeftTop point. Set new ArrowTo point.
      begin
        aMarkPosision.ArrowTo.X := aMarkPosision.LeftTop.X + (aMarkPosision.Width div 2);
        if aSeries.CalcYPos(aMarkInx) > aMarkPosision.ArrowTo.Y then // Mark above Series point
          aMarkPosision.ArrowTo.Y := aMarkPosision.LeftTop.Y + aMarkPosision.Height
        else
          aMarkPosision.ArrowTo.Y := aMarkPosision.LeftTop.Y;
      end;
    else        // Set ArrowTo point. Set a New LeftTop point.
      begin
        aMarkPosision.LeftTop.X := aMarkPosision.ArrowTo.X - (aMarkPosision.Width div 2);
        if aSeries.CalcYPos(aMarkInx) > aMarkPosision.ArrowTo.Y then // Mark above Series point
          aMarkPosision.LeftTop.Y := aMarkPosision.ArrowTo.Y - (aMarkPosision.Height -1)
        else                                                         // Mark below Series point
          aMarkPosision.ArrowTo.Y := aMarkPosision.LeftTop.Y;
      end;
  end; // case

  aChart.Repaint;
end;