How to the Mouse to Change Horizontal Stacked Bar Series?

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
Steve Maughan
Newbie
Newbie
Posts: 48
Joined: Tue Aug 03, 2010 12:00 am

How to the Mouse to Change Horizontal Stacked Bar Series?

Post by Steve Maughan » Fri Feb 12, 2016 3:42 pm

Hi,

Here's the situation. I have data (e.g. sales) which ranges in value across zip codes. I'd like to split this range up into separate "buckets". Each bucket will eventually be a different color plotted on a map.

I think a great way to define the buckets would be using a Horizontal Stacked bar chart. Ideally the user would use the mouse to hover over the dividing line, the cursor would change to crHSize, and when the user clicked and dragged left or right, the data series would change. See image:

Image
http://content.screencast.com/users/Ste ... -10.32.png

What's the best way to accomplish this mouse functionality? How do I know when the mouse is over a dividing line?

Note: I am not wanting to use TChart to plot the map. I'd like to only use TChart to define the dividing lines between each bucket.

Thanks,

Steve

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

Re: How to the Mouse to Change Horizontal Stacked Bar Series?

Post by Yeray » Mon Feb 15, 2016 3:42 pm

Hi Steve,

You could use the mouse events to do it manually. Ie:

Code: Select all

type BarClicked=record
    seriesIndex: Integer;
    valueIndex: Integer;
end;
//...
  private
    { Private declarations }
    function ClickedBar(X: Integer; Y: Integer): BarClicked;
//...
uses Series;

var draggingBar: boolean;
    myBar: BarClicked;

function TForm1.ClickedBar(X: Integer; Y: Integer): BarClicked;
var i,j, XPos, YPos: Integer;
begin
  Result.seriesIndex:=-1;
  Result.valueIndex:=-1;

  for i:=0 to Chart1.SeriesCount-1 do
  begin
    if Chart1[i] is THorizBarSeries then
    with Chart1[i] as THorizBarSeries do
    begin
      for j:=0 to Count-1 do
      begin
        YPos:=CalcYPos(j);
        if ((Y>=YPos) and (Y<=YPos+BarHeight)) then
        begin
          XPos:=CalcXPos(j);
          if (Abs(X-XPos) < 5) then
          begin
            Result.seriesIndex:=i;
            Result.valueIndex:=j;
            Chart1.Cursor:=crHSplit;
            Chart1.CancelMouse:=true;
            Exit;
          end;
        end;
      end;
    end;
  end;
end;

procedure TForm1.Chart1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if (myBar.seriesIndex<>-1) and (myBar.valueIndex<>-1) then
    draggingBar:=true;
end;

procedure TForm1.Chart1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  draggingBar:=false;
end;

procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var oldXPos, xDiff: Integer;
    xValueDiff: Double;
begin
  if draggingBar then
  begin
    oldXPos:=Chart1[myBar.seriesIndex].CalcXPos(myBar.valueIndex);
    xDiff:=Chart1.Axes.Bottom.IStartPos+oldXPos-X;
    xValueDiff:=Chart1.Axes.Bottom.CalcPosPoint(xDiff);
    Chart1[myBar.seriesIndex].XValue[myBar.valueIndex]:=Chart1[myBar.seriesIndex].XValue[myBar.valueIndex]-xValueDiff;
  end
  else
  begin
    myBar:=ClickedBar(X, Y);
    if myBar.seriesIndex<>-1 then
      Chart1.Zoom.Allow:=false
    else
      Chart1.Zoom.Allow:=true;
  end;
end;

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

  for i:=0 to 2 do
    with Chart1.AddSeries(THorizBarSeries) as THorizBarSeries do
    begin
      FillSampleValues(3);
      MultiBar:=mbStacked;
      Marks.Visible:=false;
    end;

  draggingBar:=false;
end;
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

Steve Maughan
Newbie
Newbie
Posts: 48
Joined: Tue Aug 03, 2010 12:00 am

Re: How to the Mouse to Change Horizontal Stacked Bar Series?

Post by Steve Maughan » Mon Feb 15, 2016 3:46 pm

Thanks Yeray - I'll take a look,

Steve

Post Reply