Articles in this section
Category / Section

How to drag and drop chart series points at runtime in WinForms Chart?

1 min read

 

To enable dragging and repositioning of chart series points in WinForms Charts, you can calculate new X and Y values during ChartRegionMouse Events such as MouseUp, MouseDown, and MouseMove. These new X and Y values are derived from the mouse coordinates using the GetValueByPoint method, which returns the chart's data values corresponding to the mouse position.

C#

private int seriesIndex = -1;
private int selectedIndex = -1;
private bool isSelected = false;

this.chartControl1.ChartRegionMouseDown += ChartControl1_ChartRegionMouseDown;
this.chartControl1.ChartRegionMouseMove += ChartControl1_ChartRegionMouseMove;
this.chartControl1.ChartRegionMouseUp += ChartControl1_ChartRegionMouseUp;

private void ChartControl1_ChartRegionMouseDown(object sender, ChartRegionMouseEventArgs e)
{
    if (e.Region != null && e.Region.IsChartPoint)
    {
        seriesIndex = e.Region.SeriesIndex;
        selectedIndex = e.Region.PointIndex;
        isSelected = true;
    }
}

private void ChartControl1_ChartRegionMouseMove(object sender, ChartRegionMouseEventArgs e)
{
    if (isSelected && seriesIndex != -1 && selectedIndex != -1)
    {
        double newY = this.chartControl1.ChartArea.GetValueByPoint(e.Point).YValues[0];
        this.chartControl1.Series[seriesIndex].Points[selectedIndex].YValues[0] = newY;
        this.chartControl1.Refresh();
    }
}

private void ChartControl1_ChartRegionMouseUp(object sender, ChartRegionMouseEventArgs e)
{
    if (isSelected)
    {
        isSelected = false;
        this.chartControl1.Redraw(true);
        selectedIndex = -1;
        seriesIndex = -1;
    }
}

VB

Private seriesIndex As Integer = -1
Private selectedIndex As Integer = -1
Private isSelected As Boolean = False

AddHandler lineChart.ChartRegionMouseDown, AddressOf ChartControl1_ChartRegionMouseDown
AddHandler lineChart.ChartRegionMouseMove, AddressOf ChartControl1_ChartRegionMouseMove
AddHandler lineChart.ChartRegionMouseUp, AddressOf ChartControl1_ChartRegionMouseUp

Private Sub ChartControl1_ChartRegionMouseDown(sender As Object, e As ChartRegionMouseEventArgs)
    If e.Region IsNot Nothing AndAlso e.Region.IsChartPoint Then
        seriesIndex = e.Region.SeriesIndex
        selectedIndex = e.Region.PointIndex
        isSelected = True
    End If
End Sub

Private Sub ChartControl1_ChartRegionMouseMove(sender As Object, e As ChartRegionMouseEventArgs)
    If isSelected AndAlso seriesIndex <> -1 AndAlso selectedIndex <> -1 Then
        Dim newY As Double = lineChart.ChartArea.GetValueByPoint(e.Point).YValues(0)
        lineChart.Series(seriesIndex).Points(selectedIndex).YValues(0) = newY
        lineChart.Refresh()
    End If
End Sub

Private Sub ChartControl1_ChartRegionMouseUp(sender As Object, e As ChartRegionMouseEventArgs)
    If isSelected Then
        isSelected = False
        lineChart.Redraw(True)
        selectedIndex = -1
        seriesIndex = -1
    End If
End Sub

Output:

series drag drop


Conclusion

I hope you enjoyed learning about how to drag and drop chart series points at runtime in WinForms Chart.

You can refer to our WinForms Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our WinForms Chart examples to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you! 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied