Selecting a range of values

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
TestAlways
Advanced
Posts: 228
Joined: Tue Aug 28, 2007 12:00 am
Location: Oregon, USA

Selecting a range of values

Post by TestAlways » Wed Apr 23, 2014 3:08 pm

I want to use a color band to select a range of bars (values) in a stacked (vertical) bar grid.

If there is an example of this, that would be nice. (I thought implemented this last fall in a demo app but can't find the app nor the questions I asked about it).

So my first question: when the mouse is clicked (MouseDown) I need to find the closest X position between the two stacked bars to start the color band. How do I find that position?

Thank you,

Ed Dressel

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

Re: Selecting a range of values

Post by Yeray » Thu Apr 24, 2014 11:37 am

Hi Ed,

See the second part of the reply here where I suggest a customer to use TNearestTool.GetNearestPoint function for a similar purpose.
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

TestAlways
Advanced
Posts: 228
Joined: Tue Aug 28, 2007 12:00 am
Location: Oregon, USA

Re: Selecting a range of values

Post by TestAlways » Thu Apr 24, 2014 3:29 pm

If the mouse click is not on a series, the nearest point tool seems to find the nearest point via radius. I need the nearest X-index point with a given mouse click.

How can I do that?

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

Re: Selecting a range of values

Post by Yeray » Fri Apr 25, 2014 7:42 am

Hello Ed,

In this case you should do it on your own.
It's quite easy, you just have to loop the series values, use CalcXPos to get each value X position in pixels and store the ValueIndex of the point with a smaller distance to the given mouse X pos:

Code: Select all

uses Series;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.View3D:=false;
  Chart1.AddSeries(TBarSeries).FillSampleValues;
end;

procedure TForm1.Chart1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var s, v, tmpXPos, tmpDist, tmpSeriesIndex, tmpValueIndex: Integer;

  procedure AssignMinDist(ASeriesIndex: Integer; AValueIndex: Integer);
  begin
    tmpDist:=Abs(X-tmpXPos);
    tmpSeriesIndex:=s;
    tmpValueIndex:=v;
  end;

begin
  tmpSeriesIndex:=-1;
  tmpValueIndex:=-1;
  tmpDist:=-1;

  for s:=0 to Chart1.SeriesCount-1 do
    for v:=0 to Chart1[s].Count-1 do
    begin
      tmpXPos:=Chart1[s].CalcXPos(v);
      if Chart1[s] is TBarSeries then
        tmpXPos:=tmpXPos+((Chart1[s] as TBarSeries).BarWidth div 2);

      if tmpDist=-1 then
        AssignMinDist(s, v)
      else
        if (Abs(X-tmpXPos)<tmpDist) then
          AssignMinDist(s, v);
    end;

  Caption:='SeriesIndex: ' + IntToStr(tmpSeriesIndex) + ', ValueIndex: ' + IntToStr(tmpValueIndex);
end;
Note CalcXPos returns the left position of the bar bounds, when called with a TBarSeries. That's why I added (BarWidth div 2) to the tmpXPos.
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