Intercepting zero series values with logarithmic axes

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
Errol
Newbie
Newbie
Posts: 14
Joined: Fri Jul 08, 2016 12:00 am

Intercepting zero series values with logarithmic axes

Post by Errol » Mon Jul 25, 2016 5:15 am

I have y-axis data that starts at zero. To plot this on a log axis, I want to change the zero value to a small positive value (e.g. 0.1) before plotting. I have created an override version of AddXY (this also changes measurement units e.g. from metres to feet), which is shown below:

Code: Select all

// ------ TUnitLineSeries.AddXY ------------------------------------------------
{** add SI values aXValue, aYValue}
function TUnitLineSeries.AddXY(Const AXValue,AYValue:Double;
  Const ALabel:String {$IFDEF D4}=''{$ENDIF}; AColor:TColor {$IFDEF D4}=
    clTeeColor{$ENDIF}):Integer;

var
  sSite,sVarXID,sVarYID :string;
  LXValue,LYValue: double;

begin
  LXValue := AXValue;
  LYValue := AYValue;

// ** check for Depth = 0 change to 0.01 as test
  if SameText(YUnitType,'Depth') and (AYValue = 0) and then
    LYValue := 0.1;

    result:=inherited AddXY(fxUnitObject.ConvertFromSI(LXValue,''),
      fyUnitObject.ConvertFromSI(LYValue,''),ALabel,AColor);
  end else
    result:=0;
end;


At present, I am not testing for a log axis but applying this code to all graphs. It works fine for a linear axis, plotting the first y value at 0.1. However, with a log axis, the first point is not plotted. However, when I first start my program with the data already chosen and the log axis selected, the first point is correctly plotted at 0.1, but then disappears if I move away from that data set and back to it.

What I would like to know is how TeeChart handles zero numbers on a log axis. Is a zero number ignored? If so, when does this happen?

I look forward to any comments you may have.

Best regards

Errol

Yeray
Site Admin
Site Admin
Posts: 9533
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Intercepting zero series values with logarithmic axes

Post by Yeray » Tue Jul 26, 2016 12:19 pm

Hello Errol,

Take a look at this simple example:

Code: Select all

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

  Chart1.Axes.Left.Logarithmic:=true;

  with Chart1.AddSeries(TPointSeries) as TPointSeries do
  begin
    for i:=0 to 10 do
      AddXY(i, i*i);
  end;
end;
The first YValue in the series is 0. To avoid errors, TeeChart considers values <=0 in a logarithmic axis to be at the top/bottom of the axis:

Code: Select all

Function TChartAxis.LogXPosValue(Const Value:TChartValue):Integer;
begin
  if IRangeLog=0 then result:=ICenterPos
  else
  begin
    if Value<=0 then
       if FInverted then result:=IEndPos
                    else result:=IStartPos
    else
    begin
      if FInverted then result:=Round((ILogMax-ln(Value))*IAxisLogSizeRange)
                   else result:=Round((ln(Value)-ILogMin)*IAxisLogSizeRange);
      result:=IStartPos+result;
    end;
  end;
end;

Function TChartAxis.LogYPosValue(Const Value:TChartValue):Integer;
begin
  if IRangeLog=0 then result:=ICenterPos
  else
  begin
    if Value<=0 then
       if not FInverted then result:=IEndPos
                        else result:=IStartPos
    else
    begin
      if FInverted then result:=Round((ILogMax-ln(Value))*IAxisLogSizeRange)
                   else result:=Round((ln(Value)-ILogMin)*IAxisLogSizeRange);

      result:=IEndPos-result;
    end;
  end;
end;
So the code above gives this:
logzero.png
logzero.png (7.49 KiB) Viewed 5834 times
If you want to avoid this behaviour you could hide it using null points, or you could give it a different color.

If you still find problems with it, please arrange a simple example project we can run as-is to reproduce the problem here.
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Errol
Newbie
Newbie
Posts: 14
Joined: Fri Jul 08, 2016 12:00 am

Re: Intercepting zero series values with logarithmic axes

Post by Errol » Wed Jul 27, 2016 3:13 am

Hello Yeray

Thanks for your reply, confirming how TeeChart handles zero or negative numbers on a log scale. As a result, I searched for, and eventually found and removed, the obscure code (by former programmers) that unnecessarily trapped and deleted such numbers. I am grateful for your assistance.

Post Reply