How to drag a line?


https://www.nevron.com/Forum/Topic5855.aspx
Print Topic | Close Window

By Enjoyear Guo - 13 Years Ago

In the nevron examples, I find the NDataPointDragTool to drag a data point, but I want to enable the user to drag some lines. For example, the user could click on the chart, which will generate a vertical line. And then I hope the user could drag this vertical line left and right to make some selection. How can I do it??

To make it clear, the way I add a vertical line is through _chart.Axis(StandardAxis.PrimaryX).ConstLines[0] . Is it possible to drag the const line?

Many thanks!

 

By Nevron Support - 13 Years Ago

Hi Enjoyear,

Yes you can easily achieve const line drag behaviour by intercepting the mouse down, move and up events - for example:

  private void Form1_Load(object sender, EventArgs e)
  {
   NCartesianChart chart = (NCartesianChart)nChartControl1.Charts[0];

   NBarSeries bar = new NBarSeries();
   bar.Values.Add(10);
   bar.Values.Add(20);
   bar.Values.Add(30);

   chart.Series.Add(bar);

   NAxisConstLine constLine = new NAxisConstLine();
   constLine.Value = 1;
   chart.Axis(StandardAxis.PrimaryX).ConstLines.Add(constLine);

   nChartControl1.MouseDown += new MouseEventHandler(nChartControl1_MouseDown);
   nChartControl1.MouseMove += new MouseEventHandler(nChartControl1_MouseMove);
   nChartControl1.MouseUp += new MouseEventHandler(nChartControl1_MouseUp);
  }

  NAxisConstLine m_DragLine;

  void nChartControl1_MouseUp(object sender, MouseEventArgs e)
  {
   if (m_DragLine != null)
   {
    m_DragLine = null;
    nChartControl1.Capture = false;
    nChartControl1.Cursor = Cursors.Default;
   }
  }

  void nChartControl1_MouseMove(object sender, MouseEventArgs e)
  {
   if (m_DragLine != null)
   {
    NViewToScale1DTransformation viewToScale = new NViewToScale1DTransformation(nChartControl1.View.Context, nChartControl1.Charts[0], (int)StandardAxis.PrimaryX, (int)StandardAxis.PrimaryY);

    double value = 0;
    if (viewToScale.Transform(new NPointF(e.X, e.Y), ref value))
    {
     m_DragLine.Value = value;
     nChartControl1.Refresh();
    }
   }
   
  }

  void nChartControl1_MouseDown(object sender, MouseEventArgs e)
  {
   NHitTestResult result = nChartControl1.HitTest(new Point(e.X, e.Y));
   if (result.ChartElement == ChartElement.AxisConstLine)
   {
    m_DragLine = nChartControl1.Charts[0].Axis(StandardAxis.PrimaryX).ConstLines[0];
    nChartControl1.Capture = true;
    nChartControl1.Cursor = Cursors.SizeWE;
   }
  }

The idea is to check on mouse down whether the currently hit object is the const line you added and then to move it around by transforming the mouse coordinate to scale coordinates.

Hope this helps - let us know if you meet any problems...

By Enjoyear Guo - 13 Years Ago
It works great!! Thanks!
By Kasper Juul Larsen - 4 Years Ago
I am trying to do the same, only with a horizontal constant line. How can I do this?
By Nevron Support - 4 Years Ago
Hi Casper,
You can take a look at the WinForms \ All Examples \ Interactivity \ Tools \ Custom Drag Tool example - it shows how to implement a custom dragging tool - the example uses const lines as dragging targets. Let us know if you meet any problems or have any questions.