Page 1 of 1

How to the Mouse to Change Horizontal Stacked Bar Series?

Posted: Fri Feb 12, 2016 3:42 pm
by 16556683
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

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

Posted: Mon Feb 15, 2016 3:42 pm
by yeray
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;

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

Posted: Mon Feb 15, 2016 3:46 pm
by 16556683
Thanks Yeray - I'll take a look,

Steve