Page 1 of 1

Labeling Points in the Chart with Marks

Posted: Fri Jun 27, 2008 7:35 am
by 10546441
Hello everyone,

I've got a little problem with labelling point in a chart.

The goal is to label each point with variable text, if the user likes or to hide the labels if the user dislikes them.

So far everything works using:
series.Marks.Visible = true/false
But:
===========
The marks don't look like in design time. I designed little grey boxes with bold font in a designed size.
Each time I compile and run the program, the marks show up as if they where Tooltips - light yellow background and tooltip font.

The second thing is, I want the marks to be displayed above to the right from each point. For some reason they are always centered above the point.

Any idea how to fix this ??

this is the code I currently use to display the variable text for the marks:

Code: Select all

{ Punkte Namen aktualisieren }
  seriesP.Marks.Color := clSilver;
  seriesP.Marks.Font.Name := 'Tahoma';
  seriesP.Marks.Font.Color := clBlack;
  seriesP.Marks.Font.Size := 7;
  seriesP.Marks.Font.Style := [fsBold];
  seriesP.Marks.BackColor := clSilver;
  seriesP.Marks.Transparency := 0;


  for Index := 0 to PointStack.Count -1 do
  with seriesP.Marks.Item[ Index ].Text do begin
    seriesP.Marks.Item[ Index ].Text.Clear();
    seriesP.Marks.Item[ Index ].ShapeBounds.Left := seriesP.Marks.Item[ Index ].ShapeBounds.Left + 20;

    Font.Style := [fsBold];
    Font.Name := 'Tahoma';
    Font.Size := 7;
    Color := clGray;

    if PointStack.Items[Index].ZValid then
      Add( FloatToStr(PointStack.NormItems[Index].Z / 1000) )
    else
      Add('N/V');
  end;

Posted: Fri Jun 27, 2008 8:47 am
by narcis
Hi ChZiegelt,

You need to set custom marks as shown on this thread, for example:

Code: Select all

procedure TForm1.Chart1AfterDraw(Sender: TObject);
var i: Integer;
begin
  for i:=0 to Series1.Count-1 do
  begin
    With Series1.Marks.Positions.Position[i] do
    Begin
      Custom:=True;
      LeftTop.X := LeftTop.X + Width;
      Series1.Marks.Item[i].Color:=clRed;
    end;
  end;

  Series1.Marks.Arrow.Visible:=false;
  Series1.Repaint;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.Draw;
end;

Posted: Fri Jun 27, 2008 2:59 pm
by 10546441
Thanks NarcĂ­s for the help !

But I still have a problem now - the custom fonts now work.

But each time I try to change the position of the marks, I get a runtime error Access violation. I found no way to trace what exactly it comes from.

Code: Select all

procedure TformMain.chartAfterDraw(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to seriesP.Count -1 do
  begin
    with seriesP.Marks.Positions.Position[i] do
    begin
      Custom := True;
      LeftTop.X := LeftTop.X + Width;
    end;
  end;

  seriesP.Repaint;
end;

Posted: Fri Jun 27, 2008 3:13 pm
by narcis
Hi ChZiegelt,

Have you tried adding the Chart1.Draw call in the OnFormCreate event as in the code snippet I posted?

Thanks in advance.

Posted: Fri Jun 27, 2008 3:16 pm
by 10546441
Yes, it was a stupid arror from my side :-)

As I said, one can turn on/off the marks - if marks are visible=false
then there is an exception. Just had to question whether marks are visible.

But there is still a problem. If I use custom = true, the labels will stay at the given point.
If user zooms / pans, the labels remein where they are. any idea how to fix this ?

Posted: Fri Jun 27, 2008 3:25 pm
by narcis
Hi ChZiegelt,

Yes, you'll have to call Draw method at OnZoom, OnUndoZoom and OnScroll events so that the chart is internally repainted and OnAfterDraw is executed with proper values.

Posted: Fri Jun 27, 2008 3:35 pm
by 10546441
I am sorry, but that does not realy work.

Once the marks are displayed, there position is not changed if I set them on custom.

Code: Select all

procedure TformMain.chartAfterDraw(Sender: TObject);
var
  i: Integer;
begin

  if not seriesP.Marks.Visible then exit;

  // if Position of Marks is allready changed, do not
  // change again, as the marks would "slide" out of the window with each 
  // repaint
  //if MarksMoved then exit;

  // indicates whether the marks position is allready changed
  MarksMoved := True;

  for i := 0 to seriesP.Count -1 do
  begin
    with seriesP.Marks.Positions.Position[i] do
    begin
      Custom := True;
      LeftTop.X := LeftTop.X + 20;
      //Custom := false;
    end;
  end;

  seriesP.Repaint;
end;


Posted: Fri Jun 27, 2008 3:41 pm
by 10546441
So here is exactly what I do:

1.) add data to series, and display it

2.) user enables the marks

3.) marks are displayed and position is manually changed to somewhat left

4.) Marks Position is set to custom=true

5.) user pans / zooms and the marks stay where they have been when setting custom=true

OnZoom, OundoZoom and onScroll ar all on
"chart.Draw();"

If this line is commented out

Code: Select all

if MarksMoved then exit;
then my marks will move out of the window to the right (because lefttop.x + 20).

I reset MarksMoved when re-adding the mark Texts to the points in the series.
After this, the position is reset to the "not custom" values.

Posted: Fri Jun 27, 2008 3:43 pm
by narcis
Hi ChZiegelt,

Could you please send us a simple example project we can run "as-is" to reproduce the problem here?

You can either post your files at news://www.steema.net/steema.public.attachments newsgroup or at the upload page.

Thanks in advance.