Stacked bar chart problems

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Errol
Newbie
Newbie
Posts: 75
Joined: Thu Jul 11, 2013 12:00 am

Re: Stacked bar chart problems

Post by Errol » Wed Feb 24, 2016 4:40 am

Thanks, your suggestion worked. However, now that I move from the test code to the actual code, I have more problems. My code is as follows, where InsertGeologicalSeries is similar to the test case code. I am attempting to calculate XPos and store the values in an array to use them in the ChartAfterDraw event. However, when I call CalcXPos I always get an access violation error. Can you suggest what may be wrong here.

Thanks and regards

Errol

Code: Select all

procedure TPBQuickGraph.AddMultipleGeologicalSeries(AFldCode, AFld: string);
var
  h: integer;

begin
    SetLength(aXPos,MultiGraphList.Count);

  with IntervalQueryCollection do
  begin
// do all the once-only settings outside mutligraph loop - EBA 20160117
      SetupBarChart(aFldCode,aFld,MultiGraphList.Count);

      for h:=0 to MultiGraphList.Count-1 do
      begin
        InsertGeologicalSeries(aFldCode,aFld,h,TDataSet(MultiGraphList.Objects[h]));
        BarXPos :=  TUnitBarSeries(IntervalQueryCollection.SeriesListB.Objects[h]).CalcXPos(0);

        Chart.Tools.Add(TAnnotationTool.Create(self));
        with (Chart.Tools[h + ToolCount] as TAnnotationTool) do
        begin
          Shape.CustomPosition := True;
          Shape.Pen.Color := clWhite;
          Shape.Transparent := True;
          Shape.Font.Size := 10;
          Shape.Font.Style := Shape.Font.Style + [fsBold];
          Text := WellName;
          PositionUnits := muPixels;
          Left := BarXPos;
        end;
        aXPos[h] := BarXPos;
      end;

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

Re: Stacked bar chart problems

Post by Yeray » Wed Feb 24, 2016 8:53 am

Hello Errol,

It's difficult to say without debugging the code.
I'd debug it to see if AddMultipleGeologicalSeries is called before populating the series. In that case, you could be incorrectly calling XValue(0).
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: 75
Joined: Thu Jul 11, 2013 12:00 am

Re: Stacked bar chart problems

Post by Errol » Sun Feb 28, 2016 11:27 pm

I have extensively debugged my code, and the annotation texts now draw in the correct place, but only after the graph is manually refreshed, which I do not understand. My code is shown below. I have checked that the bar series are populated, but CalcXPos consistently calculates incorrect values. ChartAfterDraw is called 3 times and on the third call, the CalcXPos values are correct but the text is not drawn in the correct place. If I refresh the graph (which calls AddMultipleGeologicalSeries again, the texts move to the correct place. Any suggestions?

I have one further question. I wish to remove the annotation tools on exiting the mutiple graph. I have other graph tools. Can I find out which tools are annotation tools and just remove these?

Thanks and regards

Errol

Code: Select all

procedure TPBQuickGraph.AddMultipleGeologicalSeries(AFldCode, AFld: string);
var
  h: integer;

begin
    for h := 0 to MultiGraphList.Count-1 do
    begin
      IntervalQueryCollection.InsertGeologicalSeries(aFldCode,aFld,h,TDataSet(MultiGraphList.Objects[h]));
      aWellName[h] := WellName;
      Chart.Tools.Add(TAnnotationTool.Create(self));
    end;
  Chart.RefreshData;
  Chart.Draw;
end;

procedure TPBQuickGraph.ChartAfterDraw(Sender: TObject);
var
  h: integer;

begin
    for h := 0 to MultiGraphList.Count - 1 do
    begin
      BarXPos :=  TUnitBarSeries(IntervalQueryCollection.SeriesListB.Objects[h]).CalcXPos(0);
      aBarXPos[h] := BarXPos;
      with (Chart.Tools[h + ToolCount] as TAnnotationTool) do
      begin
        Shape.CustomPosition := True;
        PositionUnits := muPixels;
        Text := aWellName[h];
        Left := aBarXPos[h];
      end;
    end;
    Chart.Repaint;
end;


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

Re: Stacked bar chart problems

Post by Yeray » Mon Feb 29, 2016 9:32 am

Hello,
Errol wrote:I have extensively debugged my code, and the annotation texts now draw in the correct place, but only after the graph is manually refreshed, which I do not understand. My code is shown below. I have checked that the bar series are populated, but CalcXPos consistently calculates incorrect values. ChartAfterDraw is called 3 times and on the third call, the CalcXPos values are correct but the text is not drawn in the correct place. If I refresh the graph (which calls AddMultipleGeologicalSeries again, the texts move to the correct place. Any suggestions?
CalcXPos needs some internal values to be calculated to work correctly; values that are calculated during the drawing process.
That's why we use to recommend forcing a chart repaint before calling such Calc* functions.

However, I see you are calling these functions at OnAfterDraw, and forcing chart repaints in this event can lead into endless loops.
I'd suggest you to try with other chart events like OnScroll, OnZoom and OnUndoZoom. These are more appropriate to force chart repaints.

Also, note repositioning some elements (like the annotation tools) at OnAfterDraw event may be too late for the change to take effect that paint cycle. You could try some OnBeforeDraw* event (OnBeforeDrawAxes, OnBeforeDrawChart or OnBeforeDrawSeries) instead.
Errol wrote:I wish to remove the annotation tools on exiting the mutiple graph. I have other graph tools. Can I find out which tools are annotation tools and just remove these?
You can do this:

Code: Select all

  for i:=Chart1.Tools.Count-1 downto 0 do
    if Chart1.Tools[i] is TAnnotationTool then
      Chart1.Tools[i].Free;
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

Post Reply