Page 1 of 1

Point Series and marks

Posted: Sun Jan 06, 2013 10:07 pm
by 10047857
Hi

I am using a point series to display annual temperature anomalies of X and Y values. I would also like to use display against each plotted point the year which it refers to. I thought I could do this by using the GetMarkText event and by means of the ValueIndex redefine the MarkText value:

Code: Select all

MarkText:=Format('%d',[ValueIndex+1772]);
I now realise that the component does not store the values in a list as they were added to the series but by where they occur on the bottom axis in ascending order. This means when I display a mark its out of "synch" with the year. Is there any way I can save an additional value along with the X and Y values that I can use in the GetMarkText event to display the correct year?

Bruce.

Re: Point Series and marks

Posted: Mon Jan 07, 2013 11:41 am
by yeray
Hi Bruce,

There are different alternatives depending on the exact requirements.
- You could add your values passing the year as label:

Code: Select all

uses Series, DateUtils;

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
    tmpDate: TDateTime;
begin
  Chart1.View3D:=false;
  Chart1.Legend.Visible:=false;

  with Chart1.AddSeries(TPointSeries) do
  begin
    XValues.DateTime:=true;
    Marks.Visible:=true;

    tmpDate:=Today;
    for i:=0 to 10 do
    begin
      AddXY(tmpDate, random, FloatToStr(YearOf(tmpDate)));
      tmpDate:=IncYear(tmpDate);
    end;
  end;

  Chart1.Axes.Bottom.LabelStyle:=talValue;
end;
- If you are using the label for another purpose, if the XValues contain the year you want to show in the marks, you could use that on the GetMarkText event:

Code: Select all

uses Series, DateUtils;

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
    tmpDate: TDateTime;
begin
  Chart1.View3D:=false;
  Chart1.Legend.Visible:=false;

  with Chart1.AddSeries(TPointSeries) do
  begin
    XValues.DateTime:=true;
    Marks.Visible:=true;

    tmpDate:=Today;
    for i:=0 to 10 do
    begin
      AddXY(tmpDate, random);
      tmpDate:=IncYear(tmpDate);
    end;

    OnGetMarkText:=PointGetMarkText;
  end;
end;

Procedure TForm1.PointGetMarkText(Sender:TChartSeries; ValueIndex:Integer; var MarkText:String);
begin
  MarkText:=FloatToStr(YearOf(Sender.XValue[ValueIndex]));
end;
- If the XValues don't store the TDateTime, you can always have an array of strings to store your labels. Something like the Labels list I'm using in the first alternative above:

Code: Select all

uses Series, DateUtils;
var MyMarks: array of string;

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
    tmpDate: TDateTime;
begin
  Chart1.View3D:=false;
  Chart1.Legend.Visible:=false;

  with Chart1.AddSeries(TPointSeries) do
  begin
    XValues.DateTime:=true;
    Marks.Visible:=true;

    SetLength(MyMarks, 10);
    tmpDate:=Today;
    for i:=0 to 9 do
    begin
      AddXY(tmpDate, random);
      MyMarks[i]:=FloatToStr(YearOf(tmpDate));
      tmpDate:=IncYear(tmpDate);
    end;

    OnGetMarkText:=PointGetMarkText;
  end;
end;

Procedure TForm1.PointGetMarkText(Sender:TChartSeries; ValueIndex:Integer; var MarkText:String);
begin
  MarkText:=MyMarks[ValueIndex];
end;

Re: Point Series and marks

Posted: Mon Jan 07, 2013 4:48 pm
by 10047857
Hi Yeray

Thanks for the examples of the various techniques you can use - I went with the first one after all that!

I changed the label.style from 'auto' to 'value' and that stopped the axis labels screwing up.

Thanks again.

Bruce.