Search code examples
c#teechart

TeeChart: Expand the "clickable" width of a Line/FastLine


I have a WinForms application where a number of lines are drawn in a TeeChart component. It is requested that it shall be possible to delete a line by right-clicking it.

Everything works fine, the clickseries event is captured and so on, but the user finds it difficult to hit the line on right click. The question is, is it possible to increase the region where the Line/FastLine object is sensible for clicking? That is, make the line wider without drawing the line any wider on the screen.

Tnx in advance


Solution

  • Yes, this is possible. The key to achieve that is PointInLineTolerance method. To achieve what you request you can combine it with NearestPoint's tool GetNearestPoint method as shown in this example:

    public Form1()
    {
      InitializeComponent();
      InitializeChart();
    }
    
    private void InitializeChart()
    {
      tChart1.Aspect.View3D = false;
    
      tChart1.Series.Add(new Steema.TeeChart.Styles.Line()).FillSampleValues();
      tChart1.MouseMove += TChart1_MouseMove;
    }
    
    private void TChart1_MouseMove(object sender, MouseEventArgs e)
    {
      var nearestPoint = new Steema.TeeChart.Tools.NearestPoint(tChart1[0]);
      nearestPoint.Active = false;
      var p = new Point(e.X, e.Y);
      var index = nearestPoint.GetNearestPoint(p);
    
      if (index != -1)
      {
        const int tolerance = 10;
        var px = tChart1[0].CalcXPos(index);
        var py = tChart1[0].CalcYPos(index);
    
        var index2 = (index == tChart1[0].Count - 1) ? index - 1 : index + 1;
        var qx = tChart1[0].CalcXPos(index2);
        var qy = tChart1[0].CalcYPos(index2);
    
        if (Steema.TeeChart.Drawing.Graphics3D.PointInLineTolerance(p, px, py, qx, qy, tolerance))
        {
          tChart1.Header.Text = "point " + index.ToString() + " clicked";
        }
        else
        {
          tChart1.Header.Text = "No point";
        } 
      }
    

    An alternative could be using an invisible fake series with same data as the original series.