Page 1 of 1

Modifying TAxisItem has no effect on the rendered chart

Posted: Mon Feb 17, 2014 5:49 pm
by 16567885
Using the chart editor on the form designer, you can freely alter value and text of TAxisItems. See attached picture. That's pretty cool.

I am however unable to do this at runtime:

As an example, I'd like to shift every TAxisItem of the bottom axis by 0.5 to the right. Please see the following code snippet (in Delphi): It has no effect on the chart. However, All the TAxisItems already report my altered value. Is there something I'm doing wrong? I have a feeling I'm missing some kind of TChartAxis::Invalidate().

Code: Select all

procedure TMyForm.Button1Click(Sender: TObject);
const
	someOffset: Single = 0.5;
var
	axis: TChartAxis;
	axisItem: TAxisItem;
begin
	axis := Chart.Axes.Bottom;

	for axisItem in axis.Items do
		axisItem.Value := axisItem.Value + someOffset;

	// This even outputs the new and correct values
	for axisItem in axis.Items do
		self.Caption := self.Caption + ' ' + axisItem.Value.ToString();
		
end;

Re: Modifying TAxisItem has no effect on the rendered chart

Posted: Wed Feb 19, 2014 2:44 pm
by narcis
Hi Jens,

What you need to do is set Axis.Items to manual. If, they are automatic, their values won't be used. If you have a look at the editor screen-shot you posted, to be able to set Axis.Items values manually, you need to uncheck the "Automatic" checkbox. At run-time you can do this:

Code: Select all

  TheAxis:=Chart1.Axes.Bottom;
  TheAxis.Items.Automatic:=False;

  for i:=0 to TheAxis.Items.Count - 1 do
  begin
    TheAxis.Items[i].Value:=TheAxis.Items[i].Value + 0.5;
    //TheAxis.Items[i].Text:=FloatToStr(TheAxis.Items[i].Value + 0.5);
  end;

Re: Modifying TAxisItem has no effect on the rendered chart

Posted: Wed Feb 19, 2014 2:52 pm
by 16567885
Thank you!

It should however be noted that Setting an axis to Manual removes all the (previously generated) automatic Axis items and I will have to create and add them myself. However, that works like a charm.

Thanks a ton!

Best regards.

Re: Modifying TAxisItem has no effect on the rendered chart

Posted: Wed Feb 19, 2014 3:15 pm
by narcis
Hi Jens,

Calling Items.Clear does the job:

Code: Select all

  Chart1.Axes.Bottom.Items.Clear;