Recent

Author Topic: How to use TChart for simple Box-Whiskers plot?  (Read 516 times)

stephanweber

  • New Member
  • *
  • Posts: 37
How to use TChart for simple Box-Whiskers plot?
« on: June 17, 2019, 03:23:48 pm »
Hi,

I have a chart and at the x-position x=1 I want to place a box-whiskers element, e.g. with the 5 y-entries 0.50 ; 1.0; 1.5 ; 1.9; and 2.70.
How can I do this, e.g. using Addxy and an array yval[1..5] of double?
I need a simple code example, because currently I see only an empty chart.

Bye Stephan

wp

  • Hero Member
  • *****
  • Posts: 6235
Re: How to use TChart for simple Box-Whiskers plot?
« Reply #1 on: June 17, 2019, 04:12:07 pm »
Just call the AddXY of the BoxAndWhiskerSeries which accepts all five y values in the order lower whisker, lower box, center, upper box, upper whisker:

Code: Pascal  [Select]
  1.     function AddXY(
  2.       AX, AYLoWhisker, AYLoBox, AY, AYHiBox, AYHiWhisker: Double;
  3.       AXLabel: String = ''; AColor: TColor = clTAColor): Integer; overload;

In your case:
Code: Pascal  [Select]
  1. Chart1BoxAndWhiskerSeries1.AddXY(1, 0.5, 1.0, 1.5, 1.9. 2.7);
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

stephanweber

  • New Member
  • *
  • Posts: 37
Re: How to use TChart for simple Box-Whiskers plot?
« Reply #2 on: June 18, 2019, 09:04:23 am »
Thanks a lot!! Yesterday I experimented a lot and found this works:


    Chart1BoxAndWhiskerSeriesCpk.Clear;
    Chart1BoxAndWhiskerSeriesCpk.ListSource.YCount := 5;

    ylist[1]:=0.75;
    ylist[2]:=1;
    ylist[3]:=1.25;
    ylist[4]:=0.5;
    y0:=1.5;

    Chart1BoxAndWhiskerSeriesCpk.AddXY(1, y0, ylist);

So the order is really confusing, maybe a bug in my older Lazarus.

Bye Stephan
                                                       

wp

  • Hero Member
  • *****
  • Posts: 6235
Re: How to use TChart for simple Box-Whiskers plot?
« Reply #3 on: June 18, 2019, 10:35:41 am »
Read the documentation https://wiki.lazarus.freepascal.org/TAChart_documentation#Multi-valued_sources.

For TBoxAndWhiskerSeries, we need five y values. This means, in addition to the ordinary y value, four y values will go into the YList. BUT: YList is a zero-based array. This leads to some unusual indexing:

*  "normal" y value --> y index 0 --> stored in Series.ListSource[i ]^.Y
*  first extra y value --> y index 1 --> stored in Series.ListSource[i ]^.YList[0]
*  second extra y value --> y index 2 --> stored in Series.ListSource[i ]^.YList[1]
etc.

For 5 values in total, YList is allocated to get 4 values because the other one is outside the array. This means, you have Y, YList[0], YList[1], YList[2], YList[3]. There is not YList[4]! And you did not assign anything to YList[0]...

I don't know whether this produces the effect that you mention.

But note: TBoxAndWhiskerSeries has an overloaded AddXY method where you simply specify the y values in the order from lowest to highest - this frees you from all these details.

The following code snippet compares both methods to add data to a Box-and-whisker series:
Code: Pascal  [Select]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3. //  Chart1BoxAndWhiskerSeries1.ListSource.YCount := 5;
  4.   Chart1BoxAndWhiskerSeries1.AddXY(0, 0.5,  1.0, 1.5, 1.9, 2.7);   // specialized for TBoxAndWhiskerSeries
  5.   Chart1BoxAndWhiskerSeries1.AddXY(1, 0.5, [1.0, 1.5, 1.9, 2.7]);  // valid for all series types
  6. end;

Another note: If your Lazarus version is very old then you must specify the YCount of the chart source before adding data: BoxAndWhiskerSeries.ListSource.YCount := 5. In current versions, this is no longer required.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10