Page 1 of 1
Selecting a range of values
Posted: Wed Apr 23, 2014 3:08 pm
by 10546565
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
Re: Selecting a range of values
Posted: Thu Apr 24, 2014 11:37 am
by yeray
Hi Ed,
See the second part of the reply
here where I suggest a customer to use TNearestTool.GetNearestPoint function for a similar purpose.
Re: Selecting a range of values
Posted: Thu Apr 24, 2014 3:29 pm
by 10546565
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?
Re: Selecting a range of values
Posted: Fri Apr 25, 2014 7:42 am
by yeray
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.