Page 1 of 1

TChart zoom function override

Posted: Wed Oct 07, 2015 3:04 am
by 15671379
I have a program that basically plot many points (with x, y coordinates) on the chart. I can zoom in / out, pan with TChart. That's great.

However an old version of this program has a function that allow the user to box-select individual or a group of points on the chart. Those point got boxed will be highlighted as a different color. Here is the problem: now in the TChart world, the default box-select event is for zooming. I tried to write some code in the Tchart.Zoomed event. See below. The idea is to detect if the user press the Ctrl key, if no, then do the normal zooming, if yes, then undo the zoom and do the box-selection.

It doesn't work very well. Can anybody point out the problem, or give me some alternative solutions?

Thanks

Ji

Code: Select all

Private Sub TChart2_Zoomed(sender As Object, e As EventArgs) Handles TChart2.Zoomed

        If My.Computer.Keyboard.CtrlKeyDown Then
           
            Dim rec As New Rectangle(New System.Drawing.Point(TChart2.Zoom.x0, TChart2.Zoom.y0),
                                     New System.Drawing.Size(TChart2.Zoom.x1 - TChart2.Zoom.x0, TChart2.Zoom.y1 - TChart2.Zoom.y0))

            'If TChart2.Zoom.HistorySteps.Count > 0 Then TChart2.Zoom.HistorySteps.RemoveAt(TChart2.Zoom.HistorySteps.Count - 1)
            TChart2.Zoom.Undo()

            For i = 0 To TChart2.Series.Count - 1
                If rec.Contains(TChart2.Series(i).ValuePointToScreenPoint(TChart2.Series(i).XValues(0), TChart2.Series(i).YValues(0))) Then
                    'Do the highlighting here
                    TChart2.Series(i).Color = Color.Red
                End If
            Next

        End If
    End Sub


Re: TChart zoom function override

Posted: Wed Oct 07, 2015 10:49 am
by Christopher
Hello,
uqji wrote:It doesn't work very well. Can anybody point out the problem, or give me some alternative solutions?
A similar question was asked recently, and I posted a code example which you can read here. Is this what you are looking for?

Re: TChart zoom function override

Posted: Fri Oct 09, 2015 1:37 am
by 15671379
Thanks for the link. However, that only solves half of my problem.

In your code the chart's zoom function is disabled and replaced by the box-selection. I am hoping that I can keep the zoom function, which is actually a more important function in my program. I might have to create a zoom button for this to happen. However I thought there would be some easy way such as roll back the zoom action, when Ctrl key is pressed in the zoomed event, or use the zoom history feature.

Any other suggestions?
Christopher wrote:Hello,
uqji wrote:It doesn't work very well. Can anybody point out the problem, or give me some alternative solutions?
A similar question was asked recently, and I posted a code example which you can read here. Is this what you are looking for?

Re: TChart zoom function override

Posted: Fri Oct 09, 2015 12:42 pm
by Christopher
uqji wrote:Any other suggestions?
To use the rubber band painted by TChart's zoom functionality but to cancel its effect on the actual chart when the control button is pressed, you could derive your own TChart class thus:

Code: Select all

  public class MyTChart : TChart
  {
    protected override void OnMouseUp(MouseEventArgs e)
    {
      if(ModifierKeys == Keys.Control)
      {
        Zoom.Active = false;
      }
      base.OnMouseUp(e);
    }
  }
And then try it in code such as this:

Code: Select all

    private void InitializeChart()
    {
      tChart1.Aspect.View3D = false;
      Line series = new Line(tChart1.Chart);

      series.FillSampleValues(40);

      tChart1.MouseMove += TChart1_MouseMove;

    }

    private void TChart1_MouseMove(object sender, MouseEventArgs e)
    {
      if (!tChart1.Zoom.Active)
        tChart1.Invalidate();
    }